diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 495e8687379207bc9b5afa99e21668a822c99be2..545c9b05562cb24be502bc2f27510fa686f6b582 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -55,6 +55,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert); if(!baud) { + uartStartDetectBaudrate(_uart); time_t startMillis = millis(); unsigned long detectedBaudRate = 0; while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 0028b1d744f83c265e9f78a247e5562ea2b5ac1c..2dee63dc325e64f032c376a01d955585296834ec 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -401,7 +401,12 @@ uint32_t uartGetBaudRate(uart_t* uart) if(uart == NULL) { return 0; } + uint32_t clk_div = (uart->dev->clk_div.div_int << 4) | (uart->dev->clk_div.div_frag & 0x0F); + if(!clk_div) { + return 0; + } + return ((getApbFrequency()<<4)/clk_div); } @@ -522,6 +527,14 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg) * detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is * rounded to the closed real baudrate. */ +void uartStartDetectBaudrate(uart_t *uart) { + if(!uart) return; + + uart->dev->auto_baud.glitch_filt = 0x08; + uart->dev->auto_baud.en = 0; + uart->dev->auto_baud.en = 1; +} + unsigned long uartDetectBaudrate(uart_t *uart) { diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 9874866bcf728144a36ff08daceb6b2154f8956a..821ca9c6ce0e3f58680a1d05904e9633f9e81f2e 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -72,6 +72,7 @@ size_t uartResizeRxBuffer(uart_t* uart, size_t new_size); void uartSetDebug(uart_t* uart); int uartGetDebug(); +void uartStartDetectBaudrate(uart_t *uart); unsigned long uartDetectBaudrate(uart_t *uart); bool uartRxActive(uart_t* uart);