Skip to content

Commit 56da126

Browse files
committed
fixes setPins and begin to keep rx/tx unmodified
1 parent c4954dd commit 56da126

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,37 +114,46 @@ void serialEventRun(void)
114114
}
115115
#endif
116116

117-
118117
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}
119118

119+
enum { UART_NUM_0, UART_NUM_1, UART_NUM_2 };
120+
120121
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)
121122
{
122123
if(0 > _uart_nr || _uart_nr >= SOC_UART_NUM) {
123124
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
124125
return;
125126
}
127+
128+
// First Time or after end() --> set default Pins
129+
if (!uartIsDriverInstalled(_uart)) {
130+
switch (_uart_nr) {
131+
case UART_NUM_0:
132+
rxPin = rxPin < 0 ? SOC_RX0 : rxPin;
133+
txPin = txPin < 0 ? SOC_TX0 : txPin;
134+
break;
135+
#if SOC_UART_NUM > 1 // may save some flash bytes...
136+
case UART_NUM_1:
137+
rxPin = rxPin < 0 ? RX1 : rxPin;
138+
txPin = txPin < 0 ? TX1 : txPin;
139+
break;
140+
#endif
141+
#if SOC_UART_NUM > 2 // may save some flash bytes...
142+
case UART_NUM_2:
143+
rxPin = rxPin < 0 ? RX2 : rxPin;
144+
txPin = txPin < 0 ? TX2 : txPin;
145+
break;
146+
#endif
147+
}
148+
}
149+
126150
if(_uart) {
127151
// in this case it is a begin() over a previous begin() - maybe to change baud rate
128152
// thus do not disable debug output
129153
end(false);
130154
}
131-
if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
132-
rxPin = SOC_RX0;
133-
txPin = SOC_TX0;
134-
}
135-
#if SOC_UART_NUM > 1
136-
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
137-
rxPin = RX1;
138-
txPin = TX1;
139-
}
140-
#endif
141-
#if SOC_UART_NUM > 2
142-
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
143-
rxPin = RX2;
144-
txPin = TX2;
145-
}
146-
#endif
147155

156+
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
148157
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
149158
if (!baud) {
150159
// using baud rate as zero, forces it to try to detect the current baud rate in place
@@ -159,7 +168,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
159168

160169
if(detectedBaudRate) {
161170
delay(100); // Give some time...
162-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
171+
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
163172
} else {
164173
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
165174
_uart = NULL;
@@ -280,9 +289,16 @@ void HardwareSerial::setRxInvert(bool invert)
280289
uartSetRxInvert(_uart, invert);
281290
}
282291

283-
void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
292+
// negative Pin value will keep it unmodified
293+
void HardwareSerial::setPins(int8_t rxPin, int8_t txPin)
294+
{
295+
uartSetPins(_uart, rxPin, txPin, -1, -1);
296+
}
297+
298+
// negative Pin value will keep it unmodified
299+
void HardwareSerial::setAllPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
284300
{
285-
uartSetPins(_uart, rxPin, txPin);
301+
uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin);
286302
}
287303

288304
size_t HardwareSerial::setRxBufferSize(size_t new_size) {

cores/esp32/HardwareSerial.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ class HardwareSerial: public Stream
107107
void setDebugOutput(bool);
108108

109109
void setRxInvert(bool);
110-
void setPins(uint8_t rxPin, uint8_t txPin);
110+
111+
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
112+
// SetPins shall be called after Serial begin()
113+
void setPins(int8_t rxPin, int8_t txPin);
114+
void setAllPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
115+
111116
size_t setRxBufferSize(size_t new_size);
112117

113118
protected:

cores/esp32/esp32-hal-uart.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,17 @@ bool uartIsDriverInstalled(uart_t* uart)
155155
return false;
156156
}
157157

158-
void uartSetPins(uart_t* uart, uint8_t rxPin, uint8_t txPin)
158+
// Valid pin UART_PIN_NO_CHANGE is defined to (-1)
159+
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
160+
void uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
159161
{
160-
if(uart == NULL || rxPin >= SOC_GPIO_PIN_COUNT || txPin >= SOC_GPIO_PIN_COUNT) {
162+
if(uart == NULL) {
161163
return;
162164
}
163165
UART_MUTEX_LOCK();
164-
ESP_ERROR_CHECK(uart_set_pin(uart->num, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
165-
UART_MUTEX_UNLOCK();
166-
166+
// IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
167+
uart_set_pin(uart->num, txPin, rxPin, ctsPin, rtsPin);
168+
UART_MUTEX_UNLOCK();
167169
}
168170

169171

@@ -173,10 +175,6 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
173175
return NULL;
174176
}
175177

176-
if(rxPin == -1 && txPin == -1) {
177-
return NULL;
178-
}
179-
180178
uart_t* uart = &_uart_bus_array[uart_nr];
181179

182180
if (uart_is_driver_installed(uart_nr)) {

cores/esp32/esp32-hal-uart.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ void uartSetDebug(uart_t* uart);
7676
int uartGetDebug();
7777

7878
bool uartIsDriverInstalled(uart_t* uart);
79-
void uartSetPins(uart_t* uart, uint8_t rxPin, uint8_t txPin);
79+
80+
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
81+
void uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
8082

8183
void uartStartDetectBaudrate(uart_t *uart);
8284
unsigned long uartDetectBaudrate(uart_t *uart);

0 commit comments

Comments
 (0)