Skip to content

Commit c93eb26

Browse files
committed
MR suggestions
1 parent e5edcf7 commit c93eb26

File tree

4 files changed

+229
-86
lines changed

4 files changed

+229
-86
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,110 @@
55

66
#include "pins_arduino.h"
77
#include "HardwareSerial.h"
8+
#include "soc/soc_caps.h"
89

10+
#ifndef SOC_RX0
911
#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
1417
#endif
15-
16-
#ifndef TX1
17-
#define TX1 10
1818
#endif
1919

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
2227
#endif
23-
24-
#ifndef TX2
25-
#define TX2 17
2628
#endif
2729

2830
void serialEvent(void) __attribute__((weak));
29-
void serialEvent1(void) __attribute__((weak));
30-
void serialEvent2(void) __attribute__((weak));
3131
void serialEvent(void) {}
32-
void serialEvent1(void) {}
33-
void serialEvent2(void) {}
3432

35-
#elif CONFIG_IDF_TARGET_ESP32S2
33+
#if SOC_UART_NUM > 1
3634

37-
// ESP32-S2 UART1 suggested default pins - software configurable
3835
#ifndef RX1
36+
#if CONFIG_IDF_TARGET_ESP32
37+
#define RX1 9
38+
#elif CONFIG_IDF_TARGET_ESP32S2
3939
#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-
5140
#elif CONFIG_IDF_TARGET_ESP32C3
52-
// ESP32-C3 UART1 suggested default pins - software configurable
53-
54-
#ifndef RX1
5541
#define RX1 18
5642
#endif
43+
#endif
5744

5845
#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
5951
#define TX1 19
52+
#endif
53+
#endif
6054

61-
void serialEvent(void) __attribute__((weak));
6255
void serialEvent1(void) __attribute__((weak));
63-
void serialEvent(void) {}
6456
void serialEvent1(void) {}
57+
#endif /* SOC_UART_NUM > 1 */
6558

59+
#if SOC_UART_NUM > 2
60+
#ifndef RX2
61+
#if CONFIG_IDF_TARGET_ESP32
62+
#define RX2 16
63+
#endif
6664
#endif
6765

66+
#ifndef TX2
67+
#if CONFIG_IDF_TARGET_ESP32
68+
#define TX2 17
69+
#endif
6870
#endif
71+
72+
void serialEvent2(void) __attribute__((weak));
73+
void serialEvent2(void) {}
74+
#endif /* SOC_UART_NUM > 2 */
75+
6976
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
7077
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
7178
HardwareSerial Serial0(0);
7279
#else
7380
HardwareSerial Serial(0);
7481
#endif
82+
#if SOC_UART_NUM > 1
7583
HardwareSerial Serial1(1);
76-
#if CONFIG_IDF_TARGET_ESP32
84+
#endif
85+
#if SOC_UART_NUM > 2
7786
HardwareSerial Serial2(2);
7887
#endif
7988
#endif
8089

8190
void serialEventRun(void)
8291
{
92+
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
93+
if(Serial0.available()) serialEvent();
94+
#else
8395
if(Serial.available()) serialEvent();
96+
#endif
97+
#if SOC_UART_NUM > 1
8498
if(Serial1.available()) serialEvent1();
85-
#if CONFIG_IDF_TARGET_ESP32
99+
#endif
100+
#if SOC_UART_NUM > 2
86101
if(Serial2.available()) serialEvent2();
87102
#endif
88103
}
89104

105+
90106
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
91107

92108
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)
93109
{
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);
96112
return;
97113
}
98114
if(_uart) {
@@ -101,28 +117,42 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
101117
end(false);
102118
}
103119
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;
114122
}
123+
#if SOC_UART_NUM > 1
115124
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
116125
rxPin = RX1;
117126
txPin = TX1;
118127
}
119-
#if CONFIG_IDF_TARGET_ESP32
128+
#endif
129+
#if SOC_UART_NUM > 2
120130
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
121131
rxPin = RX2;
122132
txPin = TX2;
123133
}
124134
#endif
135+
125136
_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+
}
126156
}
127157

128158
void HardwareSerial::updateBaudRate(unsigned long baud)
@@ -232,3 +262,9 @@ void HardwareSerial::setRxInvert(bool invert)
232262
{
233263
uartSetRxInvert(_uart, invert);
234264
}
265+
266+
void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
267+
{
268+
uartSetPins(_uart, rxPin, txPin);
269+
}
270+

cores/esp32/HardwareSerial.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
#include "Stream.h"
5151
#include "esp32-hal.h"
52+
#include "soc/soc_caps.h"
5253

5354
class HardwareSerial: public Stream
5455
{
@@ -101,6 +102,7 @@ class HardwareSerial: public Stream
101102
void setDebugOutput(bool);
102103

103104
void setRxInvert(bool);
105+
void setPins(uint8_t rxPin, uint8_t txPin);
104106

105107
protected:
106108
int _uart_nr;
@@ -120,8 +122,10 @@ extern HardwareSerial Serial0;
120122
#else
121123
extern HardwareSerial Serial;
122124
#endif
125+
#if SOC_UART_NUM > 1
123126
extern HardwareSerial Serial1;
124-
#if CONFIG_IDF_TARGET_ESP32
127+
#endif
128+
#if SOC_UART_NUM > 2
125129
extern HardwareSerial Serial2;
126130
#endif
127131
#endif

0 commit comments

Comments
 (0)