Skip to content

Commit a8622c4

Browse files
authored
HardwareSerial::end(bool) review + Baud Rate detection review and example (espressif#8762)
* removes pin detaching from end(false) * adds UART0 baud rate detection example * fixes baud rate messages
1 parent 093680e commit a8622c4

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

cores/esp32/HardwareSerial.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ _eventTask(NULL)
183183

184184
HardwareSerial::~HardwareSerial()
185185
{
186-
end();
186+
end(true); // explicit Full UART termination
187187
#if !CONFIG_DISABLE_HAL_LOCKS
188188
if(_lock != NULL){
189189
vSemaphoreDelete(_lock);
@@ -398,7 +398,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
398398
if(_uart) {
399399
// in this case it is a begin() over a previous begin() - maybe to change baud rate
400400
// thus do not disable debug output
401-
end(false);
401+
end(false); // disables IDF UART driver and UART event Task + sets _uart to NULL
402402
}
403403

404404
// 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
413413
yield();
414414
}
415415

416-
end(false);
416+
end(false); // disables IDF UART driver and UART event Task + sets _uart to NULL
417417

418418
if(detectedBaudRate) {
419419
delay(100); // Give some time...
@@ -470,10 +470,8 @@ void HardwareSerial::end(bool fullyTerminate)
470470
// do not invalidate callbacks, detach pins, invalidate DBG output
471471
uart_driver_delete(_uart_nr);
472472
}
473-
474-
uartEnd(_uart_nr);
475-
_uart = 0;
476-
_destroyEventTask();
473+
_destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too
474+
_uart = NULL;
477475
}
478476

479477
void HardwareSerial::setDebugOutput(bool en)

cores/esp32/esp32-hal-uart.c

+10-17
Original file line numberDiff line numberDiff line change
@@ -884,32 +884,29 @@ void uartStartDetectBaudrate(uart_t *uart) {
884884
return;
885885
}
886886

887-
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
887+
// Baud rate detection only works for ESP32 and ESP32S2
888+
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
889+
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
890+
hw->auto_baud.glitch_filt = 0x08;
891+
hw->auto_baud.en = 0;
892+
hw->auto_baud.en = 1;
893+
#else
888894

889895
// ESP32-C3 requires further testing
890896
// Baud rate detection returns wrong values
891897

892-
log_e("ESP32-C3 baud rate detection is not supported.");
898+
log_e("baud rate detection for this SoC is not supported.");
893899
return;
894900

895901
// Code bellow for C3 kept for future recall
896902
//hw->rx_filt.glitch_filt = 0x08;
897903
//hw->rx_filt.glitch_filt_en = 1;
898904
//hw->conf0.autobaud_en = 0;
899905
//hw->conf0.autobaud_en = 1;
900-
#elif CONFIG_IDF_TARGET_ESP32S3
901-
log_e("ESP32-S3 baud rate detection is not supported.");
902-
return;
903-
#else
904-
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
905-
hw->auto_baud.glitch_filt = 0x08;
906-
hw->auto_baud.en = 0;
907-
hw->auto_baud.en = 1;
908906
#endif
909907
}
910908

911-
unsigned long
912-
uartDetectBaudrate(uart_t *uart)
909+
unsigned long uartDetectBaudrate(uart_t *uart)
913910
{
914911
if(uart == NULL) {
915912
return 0;
@@ -955,11 +952,7 @@ uartDetectBaudrate(uart_t *uart)
955952

956953
return default_rates[i];
957954
#else
958-
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
959-
log_e("ESP32-C3 baud rate detection is not supported.");
960-
#else
961-
log_e("ESP32-S3 baud rate detection is not supported.");
962-
#endif
955+
log_e("baud rate detection this SoC is not supported.");
963956
return 0;
964957
#endif
965958
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This Sketch demonstrates how to detect and set the baud rate when the UART0 is connected to
3+
some port that is sending data. It can be used with the Arduino IDE Serial Monitor to send the data.
4+
5+
Serial.begin(0) will start the baud rate detection. Valid range is 300 to 230400 baud.
6+
It will try to detect for 20 seconds, by default, while reading RX.
7+
This timeout of 20 seconds can be changed in the begin() function through <<timeout_ms>> parameter:
8+
9+
void HardwareSerial::begin(baud, config, rxPin, txPin, invert, <<timeout_ms>>, rxfifo_full_thrhd)
10+
11+
It is necessary that the other end sends some data within <<timeout_ms>>, otherwise the detection won't work.
12+
13+
IMPORTANT NOTE: baud rate detection seem to only work with ESP32 and ESP32-S2.
14+
In other other SoCs, it doesn't work.
15+
16+
*/
17+
18+
// Open the Serial Monitor with testing baud start typing and sending caracters
19+
void setup() {
20+
Serial.begin(0); // it will try to detect the baud rate for 20 seconds
21+
22+
Serial.print("\n==>The baud rate is ");
23+
Serial.println(Serial.baudRate());
24+
25+
//after 20 seconds timeout, when not detected, it will return zero - in this case, we set it back to 115200.
26+
if (Serial.baudRate() == 0) {
27+
// Trying to set Serial to a safe state at 115200
28+
Serial.end();
29+
Serial.begin(115200);
30+
Serial.setDebugOutput(true);
31+
delay(1000);
32+
log_e("Baud rate detection failed.");
33+
}
34+
}
35+
36+
void loop() {
37+
}

0 commit comments

Comments
 (0)