Skip to content

Commit f71a4bd

Browse files
Jeroen88me-no-dev
Jeroen88
authored andcommitted
Bugfix/detect baudrate (#3188)
* Expose uartStartDetectBaudrate(uart_t *) in esp32-hal-uart.h and call it from HardwareSerial::begin() if baudrate detection is requested (by passing a baudrate of 0) to solve baudrate detection problems * Avoid a division by zero error in uartGetBaudRate()
1 parent 5f77b01 commit f71a4bd

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

cores/esp32/HardwareSerial.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
5555
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
5656

5757
if(!baud) {
58+
uartStartDetectBaudrate(_uart);
5859
time_t startMillis = millis();
5960
unsigned long detectedBaudRate = 0;
6061
while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {

cores/esp32/esp32-hal-uart.c

+13
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,12 @@ uint32_t uartGetBaudRate(uart_t* uart)
401401
if(uart == NULL) {
402402
return 0;
403403
}
404+
404405
uint32_t clk_div = (uart->dev->clk_div.div_int << 4) | (uart->dev->clk_div.div_frag & 0x0F);
406+
if(!clk_div) {
407+
return 0;
408+
}
409+
405410
return ((getApbFrequency()<<4)/clk_div);
406411
}
407412

@@ -522,6 +527,14 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
522527
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
523528
* rounded to the closed real baudrate.
524529
*/
530+
void uartStartDetectBaudrate(uart_t *uart) {
531+
if(!uart) return;
532+
533+
uart->dev->auto_baud.glitch_filt = 0x08;
534+
uart->dev->auto_baud.en = 0;
535+
uart->dev->auto_baud.en = 1;
536+
}
537+
525538
unsigned long
526539
uartDetectBaudrate(uart_t *uart)
527540
{

cores/esp32/esp32-hal-uart.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ size_t uartResizeRxBuffer(uart_t* uart, size_t new_size);
7272
void uartSetDebug(uart_t* uart);
7373
int uartGetDebug();
7474

75+
void uartStartDetectBaudrate(uart_t *uart);
7576
unsigned long uartDetectBaudrate(uart_t *uart);
7677

7778
bool uartRxActive(uart_t* uart);

0 commit comments

Comments
 (0)