5
5
6
6
#include " pins_arduino.h"
7
7
#include " HardwareSerial.h"
8
+ #include " soc/soc_caps.h"
8
9
10
+ #ifndef SOC_RX0
9
11
#if CONFIG_IDF_TARGET_ESP32
12
+ #define SOC_RX0 3
13
+ #elif CONFIG_IDF_TARGET_ESP32S2
14
+ #define SOC_RX0 44
15
+ #elif CONFIG_IDF_TARGET_ESP32C3
16
+ #define SOC_RX0 20
17
+ #endif
18
+ #endif
19
+
20
+ #ifndef SOC_TX0
21
+ #if CONFIG_IDF_TARGET_ESP32
22
+ #define SOC_TX0 1
23
+ #elif CONFIG_IDF_TARGET_ESP32S2
24
+ #define SOC_TX0 43
25
+ #elif CONFIG_IDF_TARGET_ESP32C3
26
+ #define SOC_TX0 21
27
+ #endif
28
+ #endif
29
+
30
+ void serialEvent (void ) __attribute__((weak));
31
+ void serialEvent (void ) {}
32
+
33
+ #if SOC_UART_NUM > 1
10
34
11
35
#ifndef RX1
36
+ #if CONFIG_IDF_TARGET_ESP32
12
37
#define RX1 9
38
+ #elif CONFIG_IDF_TARGET_ESP32S2
39
+ #define RX1 18
40
+ #elif CONFIG_IDF_TARGET_ESP32C3
41
+ #define RX1 18
42
+ #endif
13
43
#endif
14
44
15
45
#ifndef TX1
46
+ #if CONFIG_IDF_TARGET_ESP32
16
47
#define TX1 10
48
+ #elif CONFIG_IDF_TARGET_ESP32S2
49
+ #define TX1 17
50
+ #elif CONFIG_IDF_TARGET_ESP32C3
51
+ #define TX1 19
17
52
#endif
53
+ #endif
54
+
55
+ void serialEvent1 (void ) __attribute__((weak));
56
+ void serialEvent1 (void ) {}
57
+ #endif /* SOC_UART_NUM > 1 */
18
58
59
+ #if SOC_UART_NUM > 2
19
60
#ifndef RX2
61
+ #if CONFIG_IDF_TARGET_ESP32
20
62
#define RX2 16
21
63
#endif
64
+ #endif
22
65
23
66
#ifndef TX2
67
+ #if CONFIG_IDF_TARGET_ESP32
24
68
#define TX2 17
25
69
#endif
26
-
27
- #else
28
-
29
- #ifndef RX1
30
- #define RX1 18
31
- #endif
32
-
33
- #ifndef TX1
34
- #define TX1 17
35
70
#endif
36
71
37
- #endif
72
+ void serialEvent2 (void ) __attribute__((weak));
73
+ void serialEvent2 (void ) {}
74
+ #endif /* SOC_UART_NUM > 2 */
38
75
39
76
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
40
77
#if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
41
78
HardwareSerial Serial0 (0 );
42
79
#else
43
80
HardwareSerial Serial (0 );
44
81
#endif
82
+ #if SOC_UART_NUM > 1
45
83
HardwareSerial Serial1 (1 );
46
- #if CONFIG_IDF_TARGET_ESP32
84
+ #endif
85
+ #if SOC_UART_NUM > 2
47
86
HardwareSerial Serial2 (2 );
48
87
#endif
49
88
#endif
50
89
90
+ void serialEventRun (void )
91
+ {
92
+ #if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
93
+ if (Serial0.available ()) serialEvent ();
94
+ #else
95
+ if (Serial.available ()) serialEvent ();
96
+ #endif
97
+ #if SOC_UART_NUM > 1
98
+ if (Serial1.available ()) serialEvent1 ();
99
+ #endif
100
+ #if SOC_UART_NUM > 2
101
+ if (Serial2.available ()) serialEvent2 ();
102
+ #endif
103
+ }
104
+
105
+
51
106
HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ) {}
52
107
53
108
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)
54
109
{
55
- if (0 > _uart_nr || _uart_nr > 2 ) {
56
- log_e (" Serial number is invalid, please use 0, 1 or 2 " );
110
+ if (0 > _uart_nr || _uart_nr >= SOC_UART_NUM ) {
111
+ log_e (" Serial number is invalid, please use numers from 0 to %u " , SOC_UART_NUM - 1 );
57
112
return ;
58
113
}
59
114
if (_uart) {
60
- end ();
115
+ // in this case it is a begin() over a previous begin() - maybe to change baud rate
116
+ // thus do not disable debug output
117
+ end (false );
61
118
}
62
119
if (_uart_nr == 0 && rxPin < 0 && txPin < 0 ) {
63
- #if CONFIG_IDF_TARGET_ESP32
64
- rxPin = 3 ;
65
- txPin = 1 ;
66
- #elif CONFIG_IDF_TARGET_ESP32S2
67
- rxPin = 44 ;
68
- txPin = 43 ;
69
- #elif CONFIG_IDF_TARGET_ESP32C3
70
- rxPin = 20 ;
71
- txPin = 21 ;
72
- #endif
120
+ rxPin = SOC_RX0;
121
+ txPin = SOC_TX0;
73
122
}
123
+ #if SOC_UART_NUM > 1
74
124
if (_uart_nr == 1 && rxPin < 0 && txPin < 0 ) {
75
125
rxPin = RX1;
76
126
txPin = TX1;
77
127
}
78
- #if CONFIG_IDF_TARGET_ESP32
128
+ #endif
129
+ #if SOC_UART_NUM > 2
79
130
if (_uart_nr == 2 && rxPin < 0 && txPin < 0 ) {
80
131
rxPin = RX2;
81
132
txPin = TX2;
82
133
}
83
134
#endif
84
- _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, 256 , invert, rxfifo_full_thrhd);
85
- _tx_pin = txPin;
86
- _rx_pin = rxPin;
87
135
88
- if (!baud) {
136
+ _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, 256 , invert, rxfifo_full_thrhd);
137
+ if (!baud) {
138
+ // using baud rate as zero, forces it to try to detect the current baud rate in place
89
139
uartStartDetectBaudrate (_uart);
90
140
time_t startMillis = millis ();
91
141
unsigned long detectedBaudRate = 0 ;
92
142
while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
93
143
yield ();
94
144
}
95
145
96
- end ();
146
+ end (false );
97
147
98
148
if (detectedBaudRate) {
99
149
delay (100 ); // Give some time...
100
150
_uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, 256 , invert, rxfifo_full_thrhd);
101
151
} else {
102
152
log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
103
153
_uart = NULL ;
104
- _tx_pin = 255 ;
105
- _rx_pin = 255 ;
106
154
}
107
155
}
108
156
}
@@ -112,21 +160,16 @@ void HardwareSerial::updateBaudRate(unsigned long baud)
112
160
uartSetBaudRate (_uart, baud);
113
161
}
114
162
115
- void HardwareSerial::end ()
163
+ void HardwareSerial::end (bool turnOffDebug )
116
164
{
117
- if (uartGetDebug () == _uart_nr) {
165
+ if (turnOffDebug && uartGetDebug () == _uart_nr) {
118
166
uartSetDebug (0 );
119
167
}
120
168
delay (10 );
121
- log_v (" pins %d %d" ,_tx_pin, _rx_pin);
122
- uartEnd (_uart, _tx_pin, _rx_pin);
169
+ uartEnd (_uart);
123
170
_uart = 0 ;
124
171
}
125
172
126
- size_t HardwareSerial::setRxBufferSize (size_t new_size) {
127
- return uartResizeRxBuffer (_uart, new_size);
128
- }
129
-
130
173
void HardwareSerial::setDebugOutput (bool en)
131
174
{
132
175
if (_uart == 0 ) {
@@ -212,10 +255,16 @@ uint32_t HardwareSerial::baudRate()
212
255
}
213
256
HardwareSerial::operator bool () const
214
257
{
215
- return true ;
258
+ return uartIsDriverInstalled (_uart) ;
216
259
}
217
260
218
261
void HardwareSerial::setRxInvert (bool invert)
219
262
{
220
263
uartSetRxInvert (_uart, invert);
221
264
}
265
+
266
+ void HardwareSerial::setPins (uint8_t rxPin, uint8_t txPin)
267
+ {
268
+ uartSetPins (_uart, rxPin, txPin);
269
+ }
270
+
0 commit comments