7
7
#include " HardwareSerial.h"
8
8
9
9
#if CONFIG_IDF_TARGET_ESP32
10
+ // ESP32 UART1 and UART2 suggested default pins - software configurable
10
11
11
12
#ifndef RX1
12
13
#define RX1 9
24
25
#define TX2 17
25
26
#endif
26
27
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
28
36
37
+ // ESP32-S2 UART1 suggested default pins - software configurable
29
38
#ifndef RX1
30
39
#define RX1 18
31
40
#endif
34
43
#define TX1 17
35
44
#endif
36
45
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
+
37
66
#endif
38
67
68
+ #endif
39
69
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
40
70
#if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
41
71
HardwareSerial Serial0 (0 );
@@ -48,6 +78,15 @@ HardwareSerial Serial2(2);
48
78
#endif
49
79
#endif
50
80
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
+
51
90
HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ) {}
52
91
53
92
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
57
96
return ;
58
97
}
59
98
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 );
61
102
}
62
103
if (_uart_nr == 0 && rxPin < 0 && txPin < 0 ) {
63
104
#if CONFIG_IDF_TARGET_ESP32
@@ -82,51 +123,23 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
82
123
}
83
124
#endif
84
125
_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
- }
108
126
}
109
127
110
128
void HardwareSerial::updateBaudRate (unsigned long baud)
111
129
{
112
130
uartSetBaudRate (_uart, baud);
113
131
}
114
132
115
- void HardwareSerial::end ()
133
+ void HardwareSerial::end (bool turnOffDebug )
116
134
{
117
- if (uartGetDebug () == _uart_nr) {
135
+ if (turnOffDebug && uartGetDebug () == _uart_nr) {
118
136
uartSetDebug (0 );
119
137
}
120
138
delay (10 );
121
- log_v (" pins %d %d" ,_tx_pin, _rx_pin);
122
- uartEnd (_uart, _tx_pin, _rx_pin);
139
+ uartEnd (_uart);
123
140
_uart = 0 ;
124
141
}
125
142
126
- size_t HardwareSerial::setRxBufferSize (size_t new_size) {
127
- return uartResizeRxBuffer (_uart, new_size);
128
- }
129
-
130
143
void HardwareSerial::setDebugOutput (bool en)
131
144
{
132
145
if (_uart == 0 ) {
@@ -212,7 +225,7 @@ uint32_t HardwareSerial::baudRate()
212
225
}
213
226
HardwareSerial::operator bool () const
214
227
{
215
- return true ;
228
+ return uartIsDriverInstalled (_uart) ;
216
229
}
217
230
218
231
void HardwareSerial::setRxInvert (bool invert)
0 commit comments