Skip to content

Commit

Permalink
feat: adds the baud rate detection feature to ESP32-S3 and ESP32-C3
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider authored Feb 17, 2024
1 parent 668910a commit c77ae60
Showing 1 changed file with 20 additions and 34 deletions.
54 changes: 20 additions & 34 deletions cores/esp32/esp32-hal-uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,32 +630,32 @@ void log_print_buf(const uint8_t *b, size_t len){

/*
* if enough pulses are detected return the minimum high pulse duration + minimum low pulse duration divided by two.
* In the case of S3 and C3 using XTAL as UART CLK SOURCE, one bit period = Negative Pulse Count + 1
* This equals one bit period. If flag is true the function return inmediately, otherwise it waits for enough pulses.
*/
unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
{
// Baud rate detection only works for ESP32 and ESP32S2
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
if(uart == NULL) {
return 0;
}

uart_dev_t *hw = UART_LL_GET_HW(uart->num);

while(hw->rxd_cnt.edge_cnt < 30) { // UART_PULSE_NUM(uart_num)
while(uart_ll_get_rxd_edge_cnt(hw) < 30) { // UART_PULSE_NUM(uart_num)
if(flg) return 0;
ets_delay_us(1000);
}

UART_MUTEX_LOCK();
//log_i("lowpulse_min_cnt = %d hightpulse_min_cnt = %d", hw->lowpulse.min_cnt, hw->highpulse.min_cnt);
unsigned long ret = ((hw->lowpulse.min_cnt + hw->highpulse.min_cnt) >> 1);
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
unsigned long ret = ((uart_ll_get_low_pulse_cnt(hw) + uart_ll_get_high_pulse_cnt(hw)) >> 1);
#else
unsigned long ret = uart_ll_get_neg_pulse_cnt(hw) + 1;
#endif
UART_MUTEX_UNLOCK();

return ret;
#else
return 0;
#endif
}


Expand All @@ -675,16 +675,12 @@ void uartStartDetectBaudrate(uart_t *uart) {
uart_ll_set_autobaud_en(hw, true);
}

unsigned long
uartDetectBaudrate(uart_t *uart)
unsigned long uartDetectBaudrate(uart_t *uart)
{
if(uart == NULL) {
return 0;
}

// Baud rate detection only works for ESP32 and ESP32S2
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2

static bool uartStateDetectingBaudrate = false;

if(!uartStateDetectingBaudrate) {
Expand All @@ -698,37 +694,27 @@ uartDetectBaudrate(uart_t *uart)
}

uart_dev_t *hw = UART_LL_GET_HW(uart->num);
hw->auto_baud.en = 0;
uart_ll_set_autobaud_en(hw, false);

uartStateDetectingBaudrate = false; // Initialize for the next round

unsigned long baudrate = getApbFrequency() / divisor;

//log_i("APB_FREQ = %d\nraw baudrate detected = %d", getApbFrequency(), baudrate);

static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};

#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
unsigned long baudrate = getApbFrequency() / divisor; // ESP32 and S2 APB Freq
#else
unsigned long baudrate = (getXtalFrequencyMhz() * 1000000) / divisor; // S3 and C3 XTAL Frequency
#endif

static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000,
256000, 460800, 500000, 921600, 1000000, 1843200, 2000000, 3686400};
size_t i;
for (i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate
{
if (baudrate <= default_rates[i])
{
if (baudrate - default_rates[i - 1] < default_rates[i] - baudrate) {
i--;
}
for (i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) { // find the nearest real baudrate
if (baudrate <= default_rates[i]) {
if (baudrate - default_rates[i - 1] < default_rates[i] - baudrate) i--;
break;
}
}

return default_rates[i];
#else
#ifdef CONFIG_IDF_TARGET_ESP32C3
log_e("ESP32-C3 baud rate detection is not supported.");
#else
log_e("ESP32-S3 baud rate detection is not supported.");
#endif
return 0;
#endif
}

/*
Expand Down

0 comments on commit c77ae60

Please sign in to comment.