Unverified Commit d048e211 authored by Rodrigo Garcia's avatar Rodrigo Garcia Committed by GitHub

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
parent fe70e9b3
...@@ -133,7 +133,7 @@ void serialEventRun(void) ...@@ -133,7 +133,7 @@ void serialEventRun(void)
#define HSERIAL_MUTEX_UNLOCK() #define HSERIAL_MUTEX_UNLOCK()
#endif #endif
HardwareSerial::HardwareSerial(int uart_nr) : HardwareSerial::HardwareSerial(uint8_t uart_nr) :
_uart_nr(uart_nr), _uart_nr(uart_nr),
_uart(NULL), _uart(NULL),
_rxBufferSize(256), _rxBufferSize(256),
...@@ -147,8 +147,6 @@ _eventTask(NULL) ...@@ -147,8 +147,6 @@ _eventTask(NULL)
#if !CONFIG_DISABLE_HAL_LOCKS #if !CONFIG_DISABLE_HAL_LOCKS
,_lock(NULL) ,_lock(NULL)
#endif #endif
,_rxPin(-1)
,_txPin(-1)
,_ctsPin(-1) ,_ctsPin(-1)
,_rtsPin(-1) ,_rtsPin(-1)
{ {
...@@ -161,6 +159,14 @@ _eventTask(NULL) ...@@ -161,6 +159,14 @@ _eventTask(NULL)
} }
} }
#endif #endif
// sets UART0 (default console) RX/TX pins as already configured in boot
if (uart_nr == 0) {
_rxPin = SOC_RX0;
_txPin = SOC_TX0;
} else {
_rxPin = -1;
_txPin = -1;
}
} }
HardwareSerial::~HardwareSerial() HardwareSerial::~HardwareSerial()
...@@ -330,7 +336,7 @@ void HardwareSerial::_uartEventTask(void *args) ...@@ -330,7 +336,7 @@ void HardwareSerial::_uartEventTask(void *args)
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) 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)
{ {
if(0 > _uart_nr || _uart_nr >= SOC_UART_NUM) { if(_uart_nr >= SOC_UART_NUM) {
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1); log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
return; return;
} }
...@@ -348,23 +354,26 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in ...@@ -348,23 +354,26 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
switch (_uart_nr) { switch (_uart_nr) {
case UART_NUM_0: case UART_NUM_0:
if (rxPin < 0 && txPin < 0) { if (rxPin < 0 && txPin < 0) {
rxPin = SOC_RX0; // do not change RX0/TX0 if it has already been set before
txPin = SOC_TX0; rxPin = _rxPin < 0 ? SOC_RX0 : _rxPin;
txPin = _txPin < 0 ? SOC_TX0 : _txPin;
} }
break; break;
#if SOC_UART_NUM > 1 // may save some flash bytes... #if SOC_UART_NUM > 1 // may save some flash bytes...
case UART_NUM_1: case UART_NUM_1:
if (rxPin < 0 && txPin < 0) { if (rxPin < 0 && txPin < 0) {
rxPin = RX1; // do not change RX1/TX1 if it has already been set before
txPin = TX1; rxPin = _rxPin < 0 ? RX1 : _rxPin;
txPin = _txPin < 0 ? TX1 : _txPin;
} }
break; break;
#endif #endif
#if SOC_UART_NUM > 2 // may save some flash bytes... #if SOC_UART_NUM > 2 // may save some flash bytes...
case UART_NUM_2: case UART_NUM_2:
if (rxPin < 0 && txPin < 0) { if (rxPin < 0 && txPin < 0) {
rxPin = RX2; // do not change RX2/TX2 if it has already been set before
txPin = TX2; rxPin = _rxPin < 0 ? RX2 : _rxPin;
txPin = _txPin < 0 ? TX2 : _txPin;
} }
break; break;
#endif #endif
...@@ -424,9 +433,17 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in ...@@ -424,9 +433,17 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
uartSetRxFIFOFull(_uart, fifoFull); uartSetRxFIFOFull(_uart, fifoFull);
_rxFIFOFull = fifoFull; _rxFIFOFull = fifoFull;
} }
// detach previous attached RX/TX pins when it has changed
_rxPin = rxPin; if (_uart != NULL) {
_txPin = txPin; if (rxPin >= 0 && rxPin != _rxPin) {
uartDetachPins(_uart_nr, _rxPin, -1, -1, -1);
_rxPin = rxPin;
}
if (txPin >= 0 && txPin != _txPin) {
uartDetachPins(_uart_nr, -1, _txPin, -1, -1);
_txPin = txPin;
}
}
HSERIAL_MUTEX_UNLOCK(); HSERIAL_MUTEX_UNLOCK();
} }
...@@ -449,7 +466,7 @@ void HardwareSerial::end(bool fullyTerminate) ...@@ -449,7 +466,7 @@ void HardwareSerial::end(bool fullyTerminate)
_rxFIFOFull = 0; _rxFIFOFull = 0;
uartDetachPins(_uart, _rxPin, _txPin, _ctsPin, _rtsPin); uartDetachPins(_uart_nr, _rxPin, _txPin, _ctsPin, _rtsPin);
_rxPin = _txPin = _ctsPin = _rtsPin = -1; _rxPin = _txPin = _ctsPin = _rtsPin = -1;
} }
...@@ -554,11 +571,6 @@ void HardwareSerial::setRxInvert(bool invert) ...@@ -554,11 +571,6 @@ void HardwareSerial::setRxInvert(bool invert)
// negative Pin value will keep it unmodified // negative Pin value will keep it unmodified
bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
{ {
if(_uart == NULL) {
log_e("setPins() shall be called after begin() - nothing done\n");
return false;
}
// map logical pins to GPIO numbers // map logical pins to GPIO numbers
rxPin = digitalPinToGPIONumber(rxPin); rxPin = digitalPinToGPIONumber(rxPin);
txPin = digitalPinToGPIONumber(txPin); txPin = digitalPinToGPIONumber(txPin);
...@@ -566,12 +578,18 @@ bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r ...@@ -566,12 +578,18 @@ bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r
rtsPin = digitalPinToGPIONumber(rtsPin); rtsPin = digitalPinToGPIONumber(rtsPin);
// uartSetPins() checks if pins are valid for each function and for the SoC // uartSetPins() checks if pins are valid for each function and for the SoC
bool retCode = uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin); bool retCode = uartSetPins(_uart_nr, rxPin, txPin, ctsPin, rtsPin);
if (retCode) { if (retCode) {
_txPin = _txPin >= 0 ? txPin : _txPin; // detach previous attached UART pins if not set as same as before
_rxPin = _rxPin >= 0 ? rxPin : _rxPin; if (_rxPin >= 0 && rxPin >= 0 &&_rxPin != rxPin) uartDetachPins(_uart_nr, _rxPin, -1, -1, -1);
_rtsPin = _rtsPin >= 0 ? rtsPin : _rtsPin; if (_txPin >= 0 && txPin >= 0 && _txPin != txPin) uartDetachPins(_uart_nr, -1, _txPin, -1, -1);
_ctsPin = _ctsPin >= 0 ? ctsPin : _ctsPin; if (_ctsPin >= 0 && ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(_uart_nr, -1, -1, _ctsPin, -1);
if (_rtsPin >= 0 && rtsPin >= 0 &&_rtsPin != rtsPin) uartDetachPins(_uart_nr, -1, -1, -1, _rtsPin);
// set new pins for a future end() or a setPins()
_txPin = txPin >= 0 ? txPin : _txPin;
_rxPin = rxPin >= 0 ? rxPin : _rxPin;
_rtsPin = rtsPin >= 0 ? rtsPin : _rtsPin;
_ctsPin = ctsPin >= 0 ? ctsPin : _ctsPin;
} else { } else {
log_e("Error when setting Serial port Pins. Invalid Pin.\n"); log_e("Error when setting Serial port Pins. Invalid Pin.\n");
} }
......
...@@ -71,7 +71,7 @@ typedef std::function<void(hardwareSerial_error_t)> OnReceiveErrorCb; ...@@ -71,7 +71,7 @@ typedef std::function<void(hardwareSerial_error_t)> OnReceiveErrorCb;
class HardwareSerial: public Stream class HardwareSerial: public Stream
{ {
public: public:
HardwareSerial(int uart_nr); HardwareSerial(uint8_t uart_nr);
~HardwareSerial(); ~HardwareSerial();
// 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) // 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 @@ public: ...@@ -170,7 +170,7 @@ public:
size_t setTxBufferSize(size_t new_size); size_t setTxBufferSize(size_t new_size);
protected: protected:
int _uart_nr; uint8_t _uart_nr;
uart_t* _uart; uart_t* _uart;
size_t _rxBufferSize; size_t _rxBufferSize;
size_t _txBufferSize; size_t _txBufferSize;
......
...@@ -79,13 +79,13 @@ static uart_t _uart_bus_array[] = { ...@@ -79,13 +79,13 @@ static uart_t _uart_bus_array[] = {
// be seen in the previous pins and new pins as well. // be seen in the previous pins and new pins as well.
// Valid pin UART_PIN_NO_CHANGE is defined to (-1) // Valid pin UART_PIN_NO_CHANGE is defined to (-1)
// Negative Pin Number will keep it unmodified, thus this function can detach individual pins // Negative Pin Number will keep it unmodified, thus this function can detach individual pins
void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) void uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
{ {
if(uart == NULL) { if(uart_num >= SOC_UART_NUM) {
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
return; return;
} }
UART_MUTEX_LOCK();
if (txPin >= 0) { if (txPin >= 0) {
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO); gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_out_signal(txPin, SIG_GPIO_OUT_IDX, false, false); esp_rom_gpio_connect_out_signal(txPin, SIG_GPIO_OUT_IDX, false, false);
...@@ -93,7 +93,7 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int ...@@ -93,7 +93,7 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int
if (rxPin >= 0) { if (rxPin >= 0) {
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rxPin], PIN_FUNC_GPIO); gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rxPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart->num, SOC_UART_RX_PIN_IDX), false); esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), false);
} }
if (rtsPin >= 0) { if (rtsPin >= 0) {
...@@ -103,9 +103,8 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int ...@@ -103,9 +103,8 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int
if (ctsPin >= 0) { if (ctsPin >= 0) {
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctsPin], PIN_FUNC_GPIO); gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctsPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart->num, SOC_UART_CTS_PIN_IDX), false); esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), false);
} }
UART_MUTEX_UNLOCK();
} }
// solves issue https://github.com/espressif/arduino-esp32/issues/6032 // solves issue https://github.com/espressif/arduino-esp32/issues/6032
...@@ -147,15 +146,15 @@ bool uartIsDriverInstalled(uart_t* uart) ...@@ -147,15 +146,15 @@ bool uartIsDriverInstalled(uart_t* uart)
// Valid pin UART_PIN_NO_CHANGE is defined to (-1) // Valid pin UART_PIN_NO_CHANGE is defined to (-1)
// Negative Pin Number will keep it unmodified, thus this function can set individual pins // Negative Pin Number will keep it unmodified, thus this function can set individual pins
bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
{ {
if(uart == NULL) { if(uart_num >= SOC_UART_NUM) {
return false; log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
return;
} }
UART_MUTEX_LOCK();
// IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation. // IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
bool retCode = uart_set_pin(uart->num, txPin, rxPin, rtsPin, ctsPin) == ESP_OK; bool retCode = uart_set_pin(uart_num, txPin, rxPin, rtsPin, ctsPin) == ESP_OK;
UART_MUTEX_UNLOCK();
return retCode; return retCode;
} }
......
...@@ -131,8 +131,8 @@ int uartGetDebug(); ...@@ -131,8 +131,8 @@ int uartGetDebug();
bool uartIsDriverInstalled(uart_t* uart); bool uartIsDriverInstalled(uart_t* uart);
// Negative Pin Number will keep it unmodified, thus this function can set/reset individual pins // Negative Pin Number will keep it unmodified, thus this function can set/reset individual pins
bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); void uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
// Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins // Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins
bool uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold); bool uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment