From e3db6904a158079b5e7fc1f727a18ac38d38cb3f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 13 Oct 2023 18:15:19 -0300 Subject: [PATCH 1/3] removes pin detaching from end(false) --- cores/esp32/HardwareSerial.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 1780da41213..086b57d8367 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -183,7 +183,7 @@ _eventTask(NULL) HardwareSerial::~HardwareSerial() { - end(); + end(true); // explicit Full UART termination #if !CONFIG_DISABLE_HAL_LOCKS if(_lock != NULL){ vSemaphoreDelete(_lock); @@ -398,7 +398,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in if(_uart) { // in this case it is a begin() over a previous begin() - maybe to change baud rate // thus do not disable debug output - end(false); + end(false); // disables IDF UART driver and UART event Task + sets _uart to NULL } // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. @@ -413,7 +413,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in yield(); } - end(false); + end(false); // disables IDF UART driver and UART event Task + sets _uart to NULL if(detectedBaudRate) { delay(100); // Give some time... @@ -470,10 +470,8 @@ void HardwareSerial::end(bool fullyTerminate) // do not invalidate callbacks, detach pins, invalidate DBG output uart_driver_delete(_uart_nr); } - - uartEnd(_uart_nr); - _uart = 0; - _destroyEventTask(); + _destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too + _uart = NULL; } void HardwareSerial::setDebugOutput(bool en) From 5dad572d04d43f857e6b23dccf9078be73eceac2 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 13 Oct 2023 23:51:12 -0300 Subject: [PATCH 2/3] adds UART0 baud rate detection example --- .../BaudRateDetect_Demo.ino | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 libraries/ESP32/examples/Serial/BaudRateDetect_Demo/BaudRateDetect_Demo.ino diff --git a/libraries/ESP32/examples/Serial/BaudRateDetect_Demo/BaudRateDetect_Demo.ino b/libraries/ESP32/examples/Serial/BaudRateDetect_Demo/BaudRateDetect_Demo.ino new file mode 100644 index 00000000000..5c63960487e --- /dev/null +++ b/libraries/ESP32/examples/Serial/BaudRateDetect_Demo/BaudRateDetect_Demo.ino @@ -0,0 +1,37 @@ +/* + This Sketch demonstrates how to detect and set the baud rate when the UART0 is connected to + some port that is sending data. It can be used with the Arduino IDE Serial Monitor to send the data. + + Serial.begin(0) will start the baud rate detection. Valid range is 300 to 230400 baud. + It will try to detect for 20 seconds, by default, while reading RX. + This timeout of 20 seconds can be changed in the begin() function through <> parameter: + + void HardwareSerial::begin(baud, config, rxPin, txPin, invert, <>, rxfifo_full_thrhd) + + It is necessary that the other end sends some data within <>, otherwise the detection won't work. + + IMPORTANT NOTE: baud rate detection seem to only work with ESP32 and ESP32-S2. + In other other SoCs, it doesn't work. + +*/ + +// Open the Serial Monitor with testing baud start typing and sending caracters +void setup() { + Serial.begin(0); // it will try to detect the baud rate for 20 seconds + + Serial.print("\n==>The baud rate is "); + Serial.println(Serial.baudRate()); + + //after 20 seconds timeout, when not detected, it will return zero - in this case, we set it back to 115200. + if (Serial.baudRate() == 0) { + // Trying to set Serial to a safe state at 115200 + Serial.end(); + Serial.begin(115200); + Serial.setDebugOutput(true); + delay(1000); + log_e("Baud rate detection failed."); + } + } + +void loop() { +} From 3f372a13c11160143f1f1bfc335618e62446a3e9 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 13 Oct 2023 23:52:09 -0300 Subject: [PATCH 3/3] fixes baud rate messages --- cores/esp32/esp32-hal-uart.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 243eb16a454..0fc3a430612 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -884,12 +884,18 @@ void uartStartDetectBaudrate(uart_t *uart) { return; } -#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 +// Baud rate detection only works for ESP32 and ESP32S2 +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 + uart_dev_t *hw = UART_LL_GET_HW(uart->num); + hw->auto_baud.glitch_filt = 0x08; + hw->auto_baud.en = 0; + hw->auto_baud.en = 1; +#else // ESP32-C3 requires further testing // Baud rate detection returns wrong values - log_e("ESP32-C3 baud rate detection is not supported."); + log_e("baud rate detection for this SoC is not supported."); return; // Code bellow for C3 kept for future recall @@ -897,19 +903,10 @@ void uartStartDetectBaudrate(uart_t *uart) { //hw->rx_filt.glitch_filt_en = 1; //hw->conf0.autobaud_en = 0; //hw->conf0.autobaud_en = 1; -#elif CONFIG_IDF_TARGET_ESP32S3 - log_e("ESP32-S3 baud rate detection is not supported."); - return; -#else - uart_dev_t *hw = UART_LL_GET_HW(uart->num); - hw->auto_baud.glitch_filt = 0x08; - hw->auto_baud.en = 0; - hw->auto_baud.en = 1; #endif } -unsigned long -uartDetectBaudrate(uart_t *uart) +unsigned long uartDetectBaudrate(uart_t *uart) { if(uart == NULL) { return 0; @@ -955,11 +952,7 @@ uartDetectBaudrate(uart_t *uart) return default_rates[i]; #else -#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 - log_e("ESP32-C3 baud rate detection is not supported."); -#else - log_e("ESP32-S3 baud rate detection is not supported."); -#endif + log_e("baud rate detection this SoC is not supported."); return 0; #endif }