fix(uart): Avoid division by zero error when calling uart_get_baudrate

If uart_driver_install is not called before uart_get_baudrate,
the UART port may not be ready. In such cases, the register values
cannot be read out, leading to a division by zero error in uart_ll_get_baudrate.
This commit adds a check to ensure the UART port is enabled before
attempting to read its registers.

Closes https://github.com/espressif/esp-idf/issues/17513
This commit is contained in:
Song Ruo Jing
2026-02-25 11:30:25 +08:00
parent f773c59e89
commit be2bb37f0e
2 changed files with 2 additions and 1 deletions
+1
View File
@@ -390,6 +390,7 @@ esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baud_rate)
esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t *baudrate)
{
ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
ESP_RETURN_ON_FALSE(uart_ll_is_enabled(uart_num), ESP_FAIL, UART_TAG, "uart port not enabled, unable to get register values");
soc_module_clk_t src_clk;
uint32_t sclk_freq;