From d64bc28677878353b92c6079bb75459de12c8731 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 18 Mar 2025 17:53:20 -0300 Subject: [PATCH 1/4] feat(uart): adds a function to calculate maximum valid rx timeout --- cores/esp32/esp32-hal-uart.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 9edbc4bfd22..4d686fdd23d 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -116,6 +116,10 @@ void uart_send_break(uint8_t uartNum); // Sends a buffer and at the end of the stream, it generates BREAK in the line int uart_send_msg_with_break(uint8_t uartNum, uint8_t *msg, size_t msgSize); +// UART RX Timeout (in UART Symbols) depends on the UART Clock Source and the SoC that is used +// This is a helper function that calculates what is the maximum RX Timeout that a running UART IDF driver allows. +uint16_t uart_get_max_rx_timeout(uint8_t uartNum); + #ifdef __cplusplus } #endif From 5748fe074cd999decbf33badc6ba75722bd9ac1f Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 18 Mar 2025 18:12:54 -0300 Subject: [PATCH 2/4] fix(uart): check uart rx timeout value and log an error msg --- cores/esp32/esp32-hal-uart.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index d76578480f5..263051367f3 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -757,7 +757,11 @@ bool uartSetRxTimeout(uart_t *uart, uint8_t numSymbTimeout) { if (uart == NULL) { return false; } - + uint16_t maxRXTimeout = uart_get_max_rx_timeout(uart->num); + if (numSymbTimeout > maxRXTimeout) { + log_e("Invalid RX Timeout value, its limit is %d", maxRXTimeout); + return false; + } UART_MUTEX_LOCK(); bool retCode = (ESP_OK == uart_set_rx_timeout(uart->num, numSymbTimeout)); UART_MUTEX_UNLOCK(); @@ -1382,4 +1386,24 @@ int uart_send_msg_with_break(uint8_t uartNum, uint8_t *msg, size_t msgSize) { return uart_write_bytes_with_break(uartNum, (const void *)msg, msgSize, 12); } +// returns the maximum valid uart RX Timeout based on the UART Source Clock and Baudrate +uint16_t uart_get_max_rx_timeout(uint8_t uartNum) { + if (uartNum >= SOC_UART_NUM) { + log_e("UART%d does not exist. This device has %d UARTs, from 0 to %d.", uartNum, SOC_UART_NUM, SOC_UART_NUM - 1); + return (uint16_t) -1; + } + uint16_t tout_max_thresh = uart_ll_max_tout_thrd(UART_LL_GET_HW(uartNum)); + uint8_t symbol_len = 1; // number of bits per symbol including start + uart_parity_t parity_mode; + uart_stop_bits_t stop_bit; + uart_word_length_t data_bit; + uart_ll_get_data_bit_num(UART_LL_GET_HW(uartNum), &data_bit); + uart_ll_get_stop_bits(UART_LL_GET_HW(uartNum), &stop_bit); + uart_ll_get_parity(UART_LL_GET_HW(uartNum), &parity_mode); + symbol_len += (data_bit < UART_DATA_BITS_MAX) ? (uint8_t)data_bit + 5 : 8; + symbol_len += (stop_bit > UART_STOP_BITS_1) ? 2 : 1; + symbol_len += (parity_mode > UART_PARITY_DISABLE) ? 1 : 0; + return (uint16_t) (tout_max_thresh / symbol_len); +} + #endif /* SOC_UART_SUPPORTED */ From 57fb057057f9a970538589831666f343ca576d42 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 18 Mar 2025 18:25:56 -0300 Subject: [PATCH 3/4] fix(uart): changes log message to a more clear one --- cores/esp32/esp32-hal-uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 263051367f3..fd51401aa24 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1389,7 +1389,7 @@ int uart_send_msg_with_break(uint8_t uartNum, uint8_t *msg, size_t msgSize) { // returns the maximum valid uart RX Timeout based on the UART Source Clock and Baudrate uint16_t uart_get_max_rx_timeout(uint8_t uartNum) { if (uartNum >= SOC_UART_NUM) { - log_e("UART%d does not exist. This device has %d UARTs, from 0 to %d.", uartNum, SOC_UART_NUM, SOC_UART_NUM - 1); + log_e("UART%d is invalid. This device has %d UARTs, from 0 to %d.", uartNum, SOC_UART_NUM, SOC_UART_NUM - 1); return (uint16_t) -1; } uint16_t tout_max_thresh = uart_ll_max_tout_thrd(UART_LL_GET_HW(uartNum)); From 038af27dea64c017e0a58561a80978d723fb9185 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 12:13:46 +0000 Subject: [PATCH 4/4] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-uart.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index fd51401aa24..75e2da013ea 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1390,10 +1390,10 @@ int uart_send_msg_with_break(uint8_t uartNum, uint8_t *msg, size_t msgSize) { uint16_t uart_get_max_rx_timeout(uint8_t uartNum) { if (uartNum >= SOC_UART_NUM) { log_e("UART%d is invalid. This device has %d UARTs, from 0 to %d.", uartNum, SOC_UART_NUM, SOC_UART_NUM - 1); - return (uint16_t) -1; + return (uint16_t)-1; } uint16_t tout_max_thresh = uart_ll_max_tout_thrd(UART_LL_GET_HW(uartNum)); - uint8_t symbol_len = 1; // number of bits per symbol including start + uint8_t symbol_len = 1; // number of bits per symbol including start uart_parity_t parity_mode; uart_stop_bits_t stop_bit; uart_word_length_t data_bit; @@ -1403,7 +1403,7 @@ uint16_t uart_get_max_rx_timeout(uint8_t uartNum) { symbol_len += (data_bit < UART_DATA_BITS_MAX) ? (uint8_t)data_bit + 5 : 8; symbol_len += (stop_bit > UART_STOP_BITS_1) ? 2 : 1; symbol_len += (parity_mode > UART_PARITY_DISABLE) ? 1 : 0; - return (uint16_t) (tout_max_thresh / symbol_len); + return (uint16_t)(tout_max_thresh / symbol_len); } #endif /* SOC_UART_SUPPORTED */