From b66aa19edaa7ef9c23629e221b6a7f78b375655b Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 18 Apr 2023 16:27:27 -0300 Subject: [PATCH] Makes UART work at any APB Frequency (#8097) Fixes HardwareSerial to work with IDF 5.1 on any CPU/APB Frequency (240MHz to 10MHZ), including user created low power modes. --- cores/esp32/esp32-hal-uart.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index e8be34860be..b0eb2ce7ac4 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -110,19 +110,6 @@ void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int UART_MUTEX_UNLOCK(); } -// solves issue https://github.com/espressif/arduino-esp32/issues/6032 -// baudrate must be multiplied when CPU Frequency is lower than APB 80MHz -uint32_t _get_effective_baudrate(uint32_t baudrate) -{ - uint32_t Freq = getApbFrequency()/1000000; - if (Freq < 80) { - return 80 / Freq * baudrate; - } - else { - return baudrate; - } -} - // Routines that take care of UART events will be in the HardwareSerial Class code void uartGetEventQueue(uart_t* uart, QueueHandle_t *q) { @@ -204,14 +191,8 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx uart_config.stop_bits = (config & 0x30) >> 4; uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; uart_config.rx_flow_ctrl_thresh = rxfifo_full_thrhd; -#if SOC_UART_SUPPORT_XTAL_CLK - // works independently of APB frequency - uart_config.source_clk = UART_SCLK_XTAL; // ESP32C3, ESP32S3 uart_config.baud_rate = baudrate; -#else - uart_config.source_clk = UART_SCLK_APB; // ESP32, ESP32S2 - uart_config.baud_rate = _get_effective_baudrate(baudrate); -#endif + uart_config.source_clk = UART_SCLK_APB; ESP_ERROR_CHECK(uart_driver_install(uart_nr, rx_buffer_size, tx_buffer_size, 20, &(uart->uart_event_queue), 0)); ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config)); ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); @@ -467,7 +448,7 @@ void uartSetBaudRate(uart_t* uart, uint32_t baud_rate) UART_MUTEX_LOCK(); uint32_t sclk_freq; if(uart_get_sclk_freq(UART_SCLK_DEFAULT, &sclk_freq) == ESP_OK){ - uart_ll_set_baudrate(UART_LL_GET_HW(uart->num), _get_effective_baudrate(baud_rate), sclk_freq); + uart_ll_set_baudrate(UART_LL_GET_HW(uart->num), baud_rate, sclk_freq); } UART_MUTEX_UNLOCK(); }