Skip to content

Commit 68a9899

Browse files
committed
UART SerialHArdware Refactoring based on IDF
1 parent 2af8cc3 commit 68a9899

File tree

4 files changed

+180
-502
lines changed

4 files changed

+180
-502
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "HardwareSerial.h"
88

99
#if CONFIG_IDF_TARGET_ESP32
10+
// ESP32 UART1 and UART2 suggested default pins - software configurable
1011

1112
#ifndef RX1
1213
#define RX1 9
@@ -24,8 +25,16 @@
2425
#define TX2 17
2526
#endif
2627

27-
#else
28+
void serialEvent(void) __attribute__((weak));
29+
void serialEvent1(void) __attribute__((weak));
30+
void serialEvent2(void) __attribute__((weak));
31+
void serialEvent(void) {}
32+
void serialEvent1(void) {}
33+
void serialEvent2(void) {}
34+
35+
#elif CONFIG_IDF_TARGET_ESP32S2
2836

37+
// ESP32-S2 UART1 suggested default pins - software configurable
2938
#ifndef RX1
3039
#define RX1 18
3140
#endif
@@ -34,8 +43,29 @@
3443
#define TX1 17
3544
#endif
3645

46+
void serialEvent(void) __attribute__((weak));
47+
void serialEvent1(void) __attribute__((weak));
48+
void serialEvent(void) {}
49+
void serialEvent1(void) {}
50+
51+
#elif CONFIG_IDF_TARGET_ESP32C3
52+
// ESP32-C3 UART1 suggested default pins - software configurable
53+
54+
#ifndef RX1
55+
#define RX1 18
56+
#endif
57+
58+
#ifndef TX1
59+
#define TX1 19
60+
61+
void serialEvent(void) __attribute__((weak));
62+
void serialEvent1(void) __attribute__((weak));
63+
void serialEvent(void) {}
64+
void serialEvent1(void) {}
65+
3766
#endif
3867

68+
#endif
3969
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
4070
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
4171
HardwareSerial Serial0(0);
@@ -48,6 +78,15 @@ HardwareSerial Serial2(2);
4878
#endif
4979
#endif
5080

81+
void serialEventRun(void)
82+
{
83+
if(Serial.available()) serialEvent();
84+
if(Serial1.available()) serialEvent1();
85+
#if CONFIG_IDF_TARGET_ESP32
86+
if(Serial2.available()) serialEvent2();
87+
#endif
88+
}
89+
5190
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
5291

5392
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
@@ -57,7 +96,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
5796
return;
5897
}
5998
if(_uart) {
60-
end();
99+
// in this case it is a begin() over a previous begin() - maybe to change baud rate
100+
// thus do not disable debug output
101+
end(false);
61102
}
62103
if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
63104
#if CONFIG_IDF_TARGET_ESP32
@@ -82,51 +123,23 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
82123
}
83124
#endif
84125
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
85-
_tx_pin = txPin;
86-
_rx_pin = rxPin;
87-
88-
if(!baud) {
89-
uartStartDetectBaudrate(_uart);
90-
time_t startMillis = millis();
91-
unsigned long detectedBaudRate = 0;
92-
while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
93-
yield();
94-
}
95-
96-
end();
97-
98-
if(detectedBaudRate) {
99-
delay(100); // Give some time...
100-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
101-
} else {
102-
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
103-
_uart = NULL;
104-
_tx_pin = 255;
105-
_rx_pin = 255;
106-
}
107-
}
108126
}
109127

110128
void HardwareSerial::updateBaudRate(unsigned long baud)
111129
{
112130
uartSetBaudRate(_uart, baud);
113131
}
114132

115-
void HardwareSerial::end()
133+
void HardwareSerial::end(bool turnOffDebug)
116134
{
117-
if(uartGetDebug() == _uart_nr) {
135+
if(turnOffDebug && uartGetDebug() == _uart_nr) {
118136
uartSetDebug(0);
119137
}
120138
delay(10);
121-
log_v("pins %d %d",_tx_pin, _rx_pin);
122-
uartEnd(_uart, _tx_pin, _rx_pin);
139+
uartEnd(_uart);
123140
_uart = 0;
124141
}
125142

126-
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
127-
return uartResizeRxBuffer(_uart, new_size);
128-
}
129-
130143
void HardwareSerial::setDebugOutput(bool en)
131144
{
132145
if(_uart == 0) {
@@ -212,7 +225,7 @@ uint32_t HardwareSerial::baudRate()
212225
}
213226
HardwareSerial::operator bool() const
214227
{
215-
return true;
228+
return uartIsDriverInstalled(_uart);
216229
}
217230

218231
void HardwareSerial::setRxInvert(bool invert)

cores/esp32/HardwareSerial.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class HardwareSerial: public Stream
5656
HardwareSerial(int uart_nr);
5757

5858
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
59-
void end();
59+
void end(bool turnOffDebug = true);
6060
void updateBaudRate(unsigned long baud);
6161
int available(void);
6262
int availableForWrite(void);
@@ -98,16 +98,13 @@ class HardwareSerial: public Stream
9898
uint32_t baudRate();
9999
operator bool() const;
100100

101-
size_t setRxBufferSize(size_t);
102101
void setDebugOutput(bool);
103102

104103
void setRxInvert(bool);
105104

106105
protected:
107106
int _uart_nr;
108107
uart_t* _uart;
109-
uint8_t _tx_pin;
110-
uint8_t _rx_pin;
111108
};
112109

113110
extern void serialEventRun(void) __attribute__((weak));

0 commit comments

Comments
 (0)