Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HardwareSerial::end(bool) review + Baud Rate detection review and example #8762

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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...
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 10 additions & 17 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,32 +884,29 @@ 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
//hw->rx_filt.glitch_filt = 0x08;
//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;
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <<timeout_ms>> parameter:

void HardwareSerial::begin(baud, config, rxPin, txPin, invert, <<timeout_ms>>, rxfifo_full_thrhd)

It is necessary that the other end sends some data within <<timeout_ms>>, 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() {
}