Skip to content

Commit b1b6228

Browse files
fix(uart): Sets XTAL as clock source for uart (#10087)
* fix(uart): Sets XTAL as clock source for uart C6 and H2 have problems after returning from light sleep. The baud rate seems to be off when APB is used as clock source. This fix solves the issue using a steady clock source. * fix(typo): Typo and commentaries Adds C2 in the XTAL list. * fix(uart): adjust get/set baudrate Fixes the functions for reading/writing UART baudrate by using IDF functions instead of HAL/LL. * fix(uart): uses REF_TICK for ESP32/S2 * fix(uart): esp32/s2 baudrate > 1MHz Fixes the baudrate for ESP32 and ESP32-S2 when the baud rate is higher than 1MHz. REF_TICK is just 2MHZ and can handle up to 1MHZ baudrate. * fix(uart): rxTimeout minimum Default RxTimeout changed to the minimum = 1. When TICK_REF is used as clock source, this is mandatory, * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 393681c commit b1b6228

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void serialEventRun(void) {
9696
#endif
9797

9898
HardwareSerial::HardwareSerial(uint8_t uart_nr)
99-
: _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256), _txBufferSize(0), _onReceiveCB(NULL), _onReceiveErrorCB(NULL), _onReceiveTimeout(false), _rxTimeout(2),
99+
: _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256), _txBufferSize(0), _onReceiveCB(NULL), _onReceiveErrorCB(NULL), _onReceiveTimeout(false), _rxTimeout(1),
100100
_rxFIFOFull(0), _eventTask(NULL)
101101
#if !CONFIG_DISABLE_HAL_LOCKS
102102
,

Diff for: cores/esp32/esp32-hal-uart.c

+21-9
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,20 @@ uart_t *uartBegin(
503503
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
504504
uart_config.rx_flow_ctrl_thresh = rxfifo_full_thrhd;
505505
uart_config.baud_rate = baudrate;
506-
// CLK_APB for ESP32|S2|S3|C3 -- CLK_PLL_F40M for C2 -- CLK_PLL_F48M for H2 -- CLK_PLL_F80M for C6
507-
uart_config.source_clk = UART_SCLK_DEFAULT;
506+
// there is an issue when returning from light sleep with the C6 and H2: the uart baud rate is not restored
507+
// therefore, uart clock source will set to XTAL for all SoC that support it. This fix solves the C6|H2 issue.
508+
#if SOC_UART_SUPPORT_XTAL_CLK
509+
uart_config.source_clk = UART_SCLK_XTAL; // valid for C2, S3, C3, C6, H2 and P4
510+
#elif SOC_UART_SUPPORT_REF_TICK
511+
if (baudrate <= 1000000) {
512+
uart_config.source_clk = UART_SCLK_REF_TICK; // valid for ESP32, S2 - MAX supported baud rate is 1MHz
513+
} else {
514+
uart_config.source_clk = UART_SCLK_APB; // baudrate may change with the APB Frequency!
515+
}
516+
#else
517+
// Default CLK Source: CLK_APB for ESP32|S2|S3|C3 -- CLK_PLL_F40M for C2 -- CLK_PLL_F48M for H2 -- CLK_PLL_F80M for C6
518+
uart_config.source_clk = UART_SCLK_DEFAULT; // baudrate may change with the APB Frequency!
519+
#endif
508520

509521
UART_MUTEX_LOCK();
510522
bool retCode = ESP_OK == uart_driver_install(uart_nr, rx_buffer_size, tx_buffer_size, 20, &(uart->uart_event_queue), 0);
@@ -778,25 +790,25 @@ void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
778790
return;
779791
}
780792
UART_MUTEX_LOCK();
781-
uint32_t sclk_freq;
782-
if (uart_get_sclk_freq(UART_SCLK_DEFAULT, &sclk_freq) == ESP_OK) {
783-
uart_ll_set_baudrate(UART_LL_GET_HW(uart->num), baud_rate, sclk_freq);
793+
if (uart_set_baudrate(uart->num, baud_rate) == ESP_OK) {
794+
uart->_baudrate = baud_rate;
795+
} else {
796+
log_e("Setting UART%d baud rate to %d has failed.", uart->num, baud_rate);
784797
}
785-
uart->_baudrate = baud_rate;
786798
UART_MUTEX_UNLOCK();
787799
}
788800

789801
uint32_t uartGetBaudRate(uart_t *uart) {
790802
uint32_t baud_rate = 0;
791-
uint32_t sclk_freq;
792803

793804
if (uart == NULL) {
794805
return 0;
795806
}
796807

797808
UART_MUTEX_LOCK();
798-
if (uart_get_sclk_freq(UART_SCLK_DEFAULT, &sclk_freq) == ESP_OK) {
799-
baud_rate = uart_ll_get_baudrate(UART_LL_GET_HW(uart->num), sclk_freq);
809+
if (uart_get_baudrate(uart->num, &baud_rate) != ESP_OK) {
810+
log_e("Getting UART%d baud rate has failed.", uart->num);
811+
baud_rate = (uint32_t)-1; // return value when failed
800812
}
801813
UART_MUTEX_UNLOCK();
802814
return baud_rate;

0 commit comments

Comments
 (0)