Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(UART): sets the correct uart clock source when using begin(baudrate) #11122

Merged
merged 6 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,17 +585,11 @@ uart_t *uartBegin(
uartEnd(uart_nr);
} else {
bool retCode = true;
UART_MUTEX_LOCK();
//User may just want to change some parameters, such as baudrate, data length, parity, stop bits or pins
if (uart->_baudrate != baudrate) {
if (ESP_OK != uart_set_baudrate(uart_nr, baudrate)) {
log_e("UART%d changing baudrate failed.", uart_nr);
retCode = false;
} else {
log_v("UART%d changed baudrate to %d", uart_nr, baudrate);
uart->_baudrate = baudrate;
}
retCode = uartSetBaudRate(uart, baudrate);
}
UART_MUTEX_LOCK();
uart_word_length_t data_bits = (config & 0xc) >> 2;
uart_parity_t parity = config & 0x3;
uart_stop_bits_t stop_bits = (config & 0x30) >> 4;
Expand Down Expand Up @@ -972,10 +966,11 @@ void uartFlushTxOnly(uart_t *uart, bool txOnly) {
UART_MUTEX_UNLOCK();
}

void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
bool uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
if (uart == NULL) {
return;
return false;
}
bool retCode = true;
UART_MUTEX_LOCK();
#if SOC_UART_SUPPORT_XTAL_CLK // ESP32-S3, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2 and ESP32-P4
soc_module_clk_t newClkSrc = UART_SCLK_XTAL;
Expand All @@ -993,12 +988,14 @@ void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
uart_ll_set_sclk(UART_LL_GET_HW(uart->num), newClkSrc);
#endif
if (uart_set_baudrate(uart->num, baud_rate) == ESP_OK) {
log_v("Setting UART%d baud rate to %d.", uart->num, baud_rate);
log_v("Setting UART%d baud rate to %ld.", uart->num, baud_rate);
uart->_baudrate = baud_rate;
} else {
log_e("Setting UART%d baud rate to %d has failed.", uart->num, baud_rate);
retCode = false;
log_e("Setting UART%d baud rate to %ld has failed.", uart->num, baud_rate);
}
UART_MUTEX_UNLOCK();
return retCode;
}

uint32_t uartGetBaudRate(uart_t *uart) {
Expand Down
2 changes: 1 addition & 1 deletion cores/esp32/esp32-hal-uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void uartWriteBuf(uart_t *uart, const uint8_t *data, size_t len);
void uartFlush(uart_t *uart);
void uartFlushTxOnly(uart_t *uart, bool txOnly);

void uartSetBaudRate(uart_t *uart, uint32_t baud_rate);
bool uartSetBaudRate(uart_t *uart, uint32_t baud_rate);
uint32_t uartGetBaudRate(uart_t *uart);

void uartSetRxInvert(uart_t *uart, bool invert);
Expand Down
Loading