提交 f71a4bd4 编写于 作者: J Jeroen88 提交者: Me No Dev

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()
上级 5f77b010
...@@ -55,6 +55,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in ...@@ -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); _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
if(!baud) { if(!baud) {
uartStartDetectBaudrate(_uart);
time_t startMillis = millis(); time_t startMillis = millis();
unsigned long detectedBaudRate = 0; unsigned long detectedBaudRate = 0;
while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) { while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
......
...@@ -401,7 +401,12 @@ uint32_t uartGetBaudRate(uart_t* uart) ...@@ -401,7 +401,12 @@ uint32_t uartGetBaudRate(uart_t* uart)
if(uart == NULL) { if(uart == NULL) {
return 0; return 0;
} }
uint32_t clk_div = (uart->dev->clk_div.div_int << 4) | (uart->dev->clk_div.div_frag & 0x0F); 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); return ((getApbFrequency()<<4)/clk_div);
} }
...@@ -522,6 +527,14 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg) ...@@ -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 * detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
* rounded to the closed real baudrate. * 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 unsigned long
uartDetectBaudrate(uart_t *uart) uartDetectBaudrate(uart_t *uart)
{ {
......
...@@ -72,6 +72,7 @@ size_t uartResizeRxBuffer(uart_t* uart, size_t new_size); ...@@ -72,6 +72,7 @@ size_t uartResizeRxBuffer(uart_t* uart, size_t new_size);
void uartSetDebug(uart_t* uart); void uartSetDebug(uart_t* uart);
int uartGetDebug(); int uartGetDebug();
void uartStartDetectBaudrate(uart_t *uart);
unsigned long uartDetectBaudrate(uart_t *uart); unsigned long uartDetectBaudrate(uart_t *uart);
bool uartRxActive(uart_t* uart); bool uartRxActive(uart_t* uart);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册