Skip to content

Commit 8873adb

Browse files
authored
Sets correct code for UART baud rate detection (espressif#7838)
1 parent 9676ef5 commit 8873adb

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ void log_print_buf(const uint8_t *b, size_t len){
615615
*/
616616
unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
617617
{
618-
#ifndef CONFIG_IDF_TARGET_ESP32S3
618+
// Baud rate detection only works for ESP32 and ESP32S2
619+
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
619620
if(uart == NULL) {
620621
return 0;
621622
}
@@ -679,6 +680,8 @@ void uartStartDetectBaudrate(uart_t *uart) {
679680
//hw->conf0.autobaud_en = 0;
680681
//hw->conf0.autobaud_en = 1;
681682
#elif CONFIG_IDF_TARGET_ESP32S3
683+
log_e("ESP32-S3 baud rate detection is not supported.");
684+
return;
682685
#else
683686
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
684687
hw->auto_baud.glitch_filt = 0x08;
@@ -694,7 +697,8 @@ uartDetectBaudrate(uart_t *uart)
694697
return 0;
695698
}
696699

697-
#ifndef CONFIG_IDF_TARGET_ESP32C3 // ESP32-C3 requires further testing - Baud rate detection returns wrong values
700+
// Baud rate detection only works for ESP32 and ESP32S2
701+
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
698702

699703
static bool uartStateDetectingBaudrate = false;
700704

@@ -707,22 +711,14 @@ uartDetectBaudrate(uart_t *uart)
707711
if (!divisor) {
708712
return 0;
709713
}
710-
// log_i(...) below has been used to check C3 baud rate detection results
711-
//log_i("Divisor = %d\n", divisor);
712-
//log_i("BAUD RATE based on Positive Pulse %d\n", getApbFrequency()/((hw->pospulse.min_cnt + 1)/2));
713-
//log_i("BAUD RATE based on Negative Pulse %d\n", getApbFrequency()/((hw->negpulse.min_cnt + 1)/2));
714-
715714

716-
#ifdef CONFIG_IDF_TARGET_ESP32C3
717-
//hw->conf0.autobaud_en = 0;
718-
#elif CONFIG_IDF_TARGET_ESP32S3
719-
#else
720715
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
721716
hw->auto_baud.en = 0;
722-
#endif
717+
723718
uartStateDetectingBaudrate = false; // Initialize for the next round
724719

725720
unsigned long baudrate = getApbFrequency() / divisor;
721+
726722
//log_i("APB_FREQ = %d\nraw baudrate detected = %d", getApbFrequency(), baudrate);
727723

728724
static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};
@@ -741,7 +737,11 @@ uartDetectBaudrate(uart_t *uart)
741737

742738
return default_rates[i];
743739
#else
740+
#ifdef CONFIG_IDF_TARGET_ESP32C3
744741
log_e("ESP32-C3 baud rate detection is not supported.");
742+
#else
743+
log_e("ESP32-S3 baud rate detection is not supported.");
744+
#endif
745745
return 0;
746746
#endif
747747
}
@@ -795,4 +795,4 @@ int uart_send_msg_with_break(uint8_t uartNum, uint8_t *msg, size_t msgSize)
795795
{
796796
// 12 bits long BREAK for 8N1
797797
return uart_write_bytes_with_break(uartNum, (const void *)msg, msgSize, 12);
798-
}
798+
}

0 commit comments

Comments
 (0)