Skip to content

Commit d048e21

Browse files
authored
Uart detach 2.0.13 (#8629)
* detaches previous pins in setPins() * detaches previous pins in begin() * allows setPins() or end() before begin() - detach pins * fixes code TAB * setPins() shouldn't detach pin = -1
1 parent fe70e9b commit d048e21

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+42-24
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void serialEventRun(void)
133133
#define HSERIAL_MUTEX_UNLOCK()
134134
#endif
135135

136-
HardwareSerial::HardwareSerial(int uart_nr) :
136+
HardwareSerial::HardwareSerial(uint8_t uart_nr) :
137137
_uart_nr(uart_nr),
138138
_uart(NULL),
139139
_rxBufferSize(256),
@@ -147,8 +147,6 @@ _eventTask(NULL)
147147
#if !CONFIG_DISABLE_HAL_LOCKS
148148
,_lock(NULL)
149149
#endif
150-
,_rxPin(-1)
151-
,_txPin(-1)
152150
,_ctsPin(-1)
153151
,_rtsPin(-1)
154152
{
@@ -161,6 +159,14 @@ _eventTask(NULL)
161159
}
162160
}
163161
#endif
162+
// sets UART0 (default console) RX/TX pins as already configured in boot
163+
if (uart_nr == 0) {
164+
_rxPin = SOC_RX0;
165+
_txPin = SOC_TX0;
166+
} else {
167+
_rxPin = -1;
168+
_txPin = -1;
169+
}
164170
}
165171

166172
HardwareSerial::~HardwareSerial()
@@ -330,7 +336,7 @@ void HardwareSerial::_uartEventTask(void *args)
330336

331337
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)
332338
{
333-
if(0 > _uart_nr || _uart_nr >= SOC_UART_NUM) {
339+
if(_uart_nr >= SOC_UART_NUM) {
334340
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
335341
return;
336342
}
@@ -348,23 +354,26 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
348354
switch (_uart_nr) {
349355
case UART_NUM_0:
350356
if (rxPin < 0 && txPin < 0) {
351-
rxPin = SOC_RX0;
352-
txPin = SOC_TX0;
357+
// do not change RX0/TX0 if it has already been set before
358+
rxPin = _rxPin < 0 ? SOC_RX0 : _rxPin;
359+
txPin = _txPin < 0 ? SOC_TX0 : _txPin;
353360
}
354361
break;
355362
#if SOC_UART_NUM > 1 // may save some flash bytes...
356363
case UART_NUM_1:
357364
if (rxPin < 0 && txPin < 0) {
358-
rxPin = RX1;
359-
txPin = TX1;
365+
// do not change RX1/TX1 if it has already been set before
366+
rxPin = _rxPin < 0 ? RX1 : _rxPin;
367+
txPin = _txPin < 0 ? TX1 : _txPin;
360368
}
361369
break;
362370
#endif
363371
#if SOC_UART_NUM > 2 // may save some flash bytes...
364372
case UART_NUM_2:
365373
if (rxPin < 0 && txPin < 0) {
366-
rxPin = RX2;
367-
txPin = TX2;
374+
// do not change RX2/TX2 if it has already been set before
375+
rxPin = _rxPin < 0 ? RX2 : _rxPin;
376+
txPin = _txPin < 0 ? TX2 : _txPin;
368377
}
369378
break;
370379
#endif
@@ -424,9 +433,17 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
424433
uartSetRxFIFOFull(_uart, fifoFull);
425434
_rxFIFOFull = fifoFull;
426435
}
427-
428-
_rxPin = rxPin;
429-
_txPin = txPin;
436+
// detach previous attached RX/TX pins when it has changed
437+
if (_uart != NULL) {
438+
if (rxPin >= 0 && rxPin != _rxPin) {
439+
uartDetachPins(_uart_nr, _rxPin, -1, -1, -1);
440+
_rxPin = rxPin;
441+
}
442+
if (txPin >= 0 && txPin != _txPin) {
443+
uartDetachPins(_uart_nr, -1, _txPin, -1, -1);
444+
_txPin = txPin;
445+
}
446+
}
430447

431448
HSERIAL_MUTEX_UNLOCK();
432449
}
@@ -449,7 +466,7 @@ void HardwareSerial::end(bool fullyTerminate)
449466

450467
_rxFIFOFull = 0;
451468

452-
uartDetachPins(_uart, _rxPin, _txPin, _ctsPin, _rtsPin);
469+
uartDetachPins(_uart_nr, _rxPin, _txPin, _ctsPin, _rtsPin);
453470
_rxPin = _txPin = _ctsPin = _rtsPin = -1;
454471

455472
}
@@ -554,24 +571,25 @@ void HardwareSerial::setRxInvert(bool invert)
554571
// negative Pin value will keep it unmodified
555572
bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
556573
{
557-
if(_uart == NULL) {
558-
log_e("setPins() shall be called after begin() - nothing done\n");
559-
return false;
560-
}
561-
562574
// map logical pins to GPIO numbers
563575
rxPin = digitalPinToGPIONumber(rxPin);
564576
txPin = digitalPinToGPIONumber(txPin);
565577
ctsPin = digitalPinToGPIONumber(ctsPin);
566578
rtsPin = digitalPinToGPIONumber(rtsPin);
567579

568580
// uartSetPins() checks if pins are valid for each function and for the SoC
569-
bool retCode = uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin);
581+
bool retCode = uartSetPins(_uart_nr, rxPin, txPin, ctsPin, rtsPin);
570582
if (retCode) {
571-
_txPin = _txPin >= 0 ? txPin : _txPin;
572-
_rxPin = _rxPin >= 0 ? rxPin : _rxPin;
573-
_rtsPin = _rtsPin >= 0 ? rtsPin : _rtsPin;
574-
_ctsPin = _ctsPin >= 0 ? ctsPin : _ctsPin;
583+
// detach previous attached UART pins if not set as same as before
584+
if (_rxPin >= 0 && rxPin >= 0 &&_rxPin != rxPin) uartDetachPins(_uart_nr, _rxPin, -1, -1, -1);
585+
if (_txPin >= 0 && txPin >= 0 && _txPin != txPin) uartDetachPins(_uart_nr, -1, _txPin, -1, -1);
586+
if (_ctsPin >= 0 && ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(_uart_nr, -1, -1, _ctsPin, -1);
587+
if (_rtsPin >= 0 && rtsPin >= 0 &&_rtsPin != rtsPin) uartDetachPins(_uart_nr, -1, -1, -1, _rtsPin);
588+
// set new pins for a future end() or a setPins()
589+
_txPin = txPin >= 0 ? txPin : _txPin;
590+
_rxPin = rxPin >= 0 ? rxPin : _rxPin;
591+
_rtsPin = rtsPin >= 0 ? rtsPin : _rtsPin;
592+
_ctsPin = ctsPin >= 0 ? ctsPin : _ctsPin;
575593
} else {
576594
log_e("Error when setting Serial port Pins. Invalid Pin.\n");
577595
}

Diff for: cores/esp32/HardwareSerial.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ typedef std::function<void(hardwareSerial_error_t)> OnReceiveErrorCb;
7171
class HardwareSerial: public Stream
7272
{
7373
public:
74-
HardwareSerial(int uart_nr);
74+
HardwareSerial(uint8_t uart_nr);
7575
~HardwareSerial();
7676

7777
// setRxTimeout sets the timeout after which onReceive callback will be called (after receiving data, it waits for this time of UART rx inactivity to call the callback fnc)
@@ -170,7 +170,7 @@ class HardwareSerial: public Stream
170170
size_t setTxBufferSize(size_t new_size);
171171

172172
protected:
173-
int _uart_nr;
173+
uint8_t _uart_nr;
174174
uart_t* _uart;
175175
size_t _rxBufferSize;
176176
size_t _txBufferSize;

Diff for: cores/esp32/esp32-hal-uart.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ static uart_t _uart_bus_array[] = {
7979
// be seen in the previous pins and new pins as well.
8080
// Valid pin UART_PIN_NO_CHANGE is defined to (-1)
8181
// Negative Pin Number will keep it unmodified, thus this function can detach individual pins
82-
void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
82+
void uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
8383
{
84-
if(uart == NULL) {
84+
if(uart_num >= SOC_UART_NUM) {
85+
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
8586
return;
8687
}
8788

88-
UART_MUTEX_LOCK();
8989
if (txPin >= 0) {
9090
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO);
9191
esp_rom_gpio_connect_out_signal(txPin, SIG_GPIO_OUT_IDX, false, false);
9292
}
9393

9494
if (rxPin >= 0) {
9595
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rxPin], PIN_FUNC_GPIO);
96-
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart->num, SOC_UART_RX_PIN_IDX), false);
96+
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), false);
9797
}
9898

9999
if (rtsPin >= 0) {
@@ -103,9 +103,8 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int
103103

104104
if (ctsPin >= 0) {
105105
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctsPin], PIN_FUNC_GPIO);
106-
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart->num, SOC_UART_CTS_PIN_IDX), false);
106+
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), false);
107107
}
108-
UART_MUTEX_UNLOCK();
109108
}
110109

111110
// solves issue https://github.com/espressif/arduino-esp32/issues/6032
@@ -147,15 +146,15 @@ bool uartIsDriverInstalled(uart_t* uart)
147146

148147
// Valid pin UART_PIN_NO_CHANGE is defined to (-1)
149148
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
150-
bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
149+
bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
151150
{
152-
if(uart == NULL) {
153-
return false;
151+
if(uart_num >= SOC_UART_NUM) {
152+
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
153+
return;
154154
}
155-
UART_MUTEX_LOCK();
155+
156156
// IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
157-
bool retCode = uart_set_pin(uart->num, txPin, rxPin, rtsPin, ctsPin) == ESP_OK;
158-
UART_MUTEX_UNLOCK();
157+
bool retCode = uart_set_pin(uart_num, txPin, rxPin, rtsPin, ctsPin) == ESP_OK;
159158
return retCode;
160159
}
161160

Diff for: cores/esp32/esp32-hal-uart.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ int uartGetDebug();
131131
bool uartIsDriverInstalled(uart_t* uart);
132132

133133
// Negative Pin Number will keep it unmodified, thus this function can set/reset individual pins
134-
bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
135-
void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
134+
bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
135+
void uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
136136

137137
// Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins
138138
bool uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold);

0 commit comments

Comments
 (0)