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
10
- // ESP32 UART1 and UART2 suggested default pins - software configurable
11
-
12
- #ifndef RX1
13
- #define RX1 9
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
14
17
#endif
15
-
16
- #ifndef TX1
17
- #define TX1 10
18
18
#endif
19
19
20
- #ifndef RX2
21
- #define RX2 16
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
22
27
#endif
23
-
24
- #ifndef TX2
25
- #define TX2 17
26
28
#endif
27
29
28
30
void serialEvent (void ) __attribute__((weak));
29
- void serialEvent1 (void ) __attribute__((weak));
30
- void serialEvent2 (void ) __attribute__((weak));
31
31
void serialEvent (void ) {}
32
- void serialEvent1 (void ) {}
33
- void serialEvent2 (void ) {}
34
32
35
- #elif CONFIG_IDF_TARGET_ESP32S2
33
+ #if SOC_UART_NUM > 1
36
34
37
- // ESP32-S2 UART1 suggested default pins - software configurable
38
35
#ifndef RX1
36
+ #if CONFIG_IDF_TARGET_ESP32
37
+ #define RX1 9
38
+ #elif CONFIG_IDF_TARGET_ESP32S2
39
39
#define RX1 18
40
- #endif
41
-
42
- #ifndef TX1
43
- #define TX1 17
44
- #endif
45
-
46
- void serialEvent (void ) __attribute__((weak));
47
- void serialEvent1 (void ) __attribute__((weak));
48
- void serialEvent (void ) {}
49
- void serialEvent1 (void ) {}
50
-
51
40
#elif CONFIG_IDF_TARGET_ESP32C3
52
- // ESP32-C3 UART1 suggested default pins - software configurable
53
-
54
- #ifndef RX1
55
41
#define RX1 18
56
42
#endif
43
+ #endif
57
44
58
45
#ifndef TX1
46
+ #if CONFIG_IDF_TARGET_ESP32
47
+ #define TX1 10
48
+ #elif CONFIG_IDF_TARGET_ESP32S2
49
+ #define TX1 17
50
+ #elif CONFIG_IDF_TARGET_ESP32C3
59
51
#define TX1 19
52
+ #endif
53
+ #endif
60
54
61
- void serialEvent (void ) __attribute__((weak));
62
55
void serialEvent1 (void ) __attribute__((weak));
63
- void serialEvent (void ) {}
64
56
void serialEvent1 (void ) {}
57
+ #endif /* SOC_UART_NUM > 1 */
65
58
59
+ #if SOC_UART_NUM > 2
60
+ #ifndef RX2
61
+ #if CONFIG_IDF_TARGET_ESP32
62
+ #define RX2 16
63
+ #endif
66
64
#endif
67
65
66
+ #ifndef TX2
67
+ #if CONFIG_IDF_TARGET_ESP32
68
+ #define TX2 17
69
+ #endif
68
70
#endif
71
+
72
+ void serialEvent2 (void ) __attribute__((weak));
73
+ void serialEvent2 (void ) {}
74
+ #endif /* SOC_UART_NUM > 2 */
75
+
69
76
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
70
77
#if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
71
78
HardwareSerial Serial0 (0 );
72
79
#else
73
80
HardwareSerial Serial (0 );
74
81
#endif
82
+ #if SOC_UART_NUM > 1
75
83
HardwareSerial Serial1 (1 );
76
- #if CONFIG_IDF_TARGET_ESP32
84
+ #endif
85
+ #if SOC_UART_NUM > 2
77
86
HardwareSerial Serial2 (2 );
78
87
#endif
79
88
#endif
80
89
81
90
void serialEventRun (void )
82
91
{
92
+ #if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
93
+ if (Serial0.available ()) serialEvent ();
94
+ #else
83
95
if (Serial.available ()) serialEvent ();
96
+ #endif
97
+ #if SOC_UART_NUM > 1
84
98
if (Serial1.available ()) serialEvent1 ();
85
- #if CONFIG_IDF_TARGET_ESP32
99
+ #endif
100
+ #if SOC_UART_NUM > 2
86
101
if (Serial2.available ()) serialEvent2 ();
87
102
#endif
88
103
}
89
104
105
+
90
106
HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ) {}
91
107
92
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)
93
109
{
94
- if (0 > _uart_nr || _uart_nr > 2 ) {
95
- 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 );
96
112
return ;
97
113
}
98
114
if (_uart) {
@@ -101,28 +117,42 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
101
117
end (false );
102
118
}
103
119
if (_uart_nr == 0 && rxPin < 0 && txPin < 0 ) {
104
- #if CONFIG_IDF_TARGET_ESP32
105
- rxPin = 3 ;
106
- txPin = 1 ;
107
- #elif CONFIG_IDF_TARGET_ESP32S2
108
- rxPin = 44 ;
109
- txPin = 43 ;
110
- #elif CONFIG_IDF_TARGET_ESP32C3
111
- rxPin = 20 ;
112
- txPin = 21 ;
113
- #endif
120
+ rxPin = SOC_RX0;
121
+ txPin = SOC_TX0;
114
122
}
123
+ #if SOC_UART_NUM > 1
115
124
if (_uart_nr == 1 && rxPin < 0 && txPin < 0 ) {
116
125
rxPin = RX1;
117
126
txPin = TX1;
118
127
}
119
- #if CONFIG_IDF_TARGET_ESP32
128
+ #endif
129
+ #if SOC_UART_NUM > 2
120
130
if (_uart_nr == 2 && rxPin < 0 && txPin < 0 ) {
121
131
rxPin = RX2;
122
132
txPin = TX2;
123
133
}
124
134
#endif
135
+
125
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
139
+ uartStartDetectBaudrate (_uart);
140
+ time_t startMillis = millis ();
141
+ unsigned long detectedBaudRate = 0 ;
142
+ while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
143
+ yield ();
144
+ }
145
+
146
+ end (false );
147
+
148
+ if (detectedBaudRate) {
149
+ delay (100 ); // Give some time...
150
+ _uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, 256 , invert, rxfifo_full_thrhd);
151
+ } else {
152
+ log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
153
+ _uart = NULL ;
154
+ }
155
+ }
126
156
}
127
157
128
158
void HardwareSerial::updateBaudRate (unsigned long baud)
@@ -232,3 +262,9 @@ void HardwareSerial::setRxInvert(bool invert)
232
262
{
233
263
uartSetRxInvert (_uart, invert);
234
264
}
265
+
266
+ void HardwareSerial::setPins (uint8_t rxPin, uint8_t txPin)
267
+ {
268
+ uartSetPins (_uart, rxPin, txPin);
269
+ }
270
+
0 commit comments