Bug in uart_baudrate_detect

JeroenD
Posts: 1
Joined: Tue Jul 24, 2018 1:40 pm

Bug in uart_baudrate_detect

Postby JeroenD » Tue Jul 24, 2018 1:52 pm

When I flash a sketch to my ESP8266, uart_detect_baudrate(UART0) returns 0. When I push the reset button, it gives the correct answer (9600 in my case). If I call uart_detect_baudrate(UART0) again it returns zero. I need a robust automatic baudrate detection. I think it might have something to do with the initialization of the UART. However, I tried to set PULSE_NUM, PULSE_HIGH and PULSE_LOW to zero, prior to calling the function, without success. Can you please help?

Code: Select all

  unsigned long detectedBaudrate;
  while(millis() - start < 11000) {
    if(detectedBaudrate = uart_detect_baudrate(UART0)) break;
    yield();
    delay(100);
  }
  Serial.println(detectedBaudrate);
  unsigned long detectedBaudrate;
  while(millis() - start < 11000) {
    if(detectedBaudrate = uart_detect_baudrate(UART0)) break;
    yield();
    delay(100);
  }
  Serial.println(detectedBaudrate);

And

Code: Select all

uint32_t uart_detect_baudrate(int uart_nr) {
  uint32_t divisor = uart_baudrate_detect(uart_nr, 1);
  if(!divisor) return 0;

  uint32_t baudrate = UART_CLK_FREQ / divisor;

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

  size_t i;
  for(i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1;
i++) {
    if(baudrate <= default_rates[i]) {
      if(baudrate - default_rates[i - 1] < default_rates[i] - baudrate)
i--;
      break;
    }
  }

  return  default_rates[i];
}


Prints zero's after falshing, prints 9600 and 0 after hard reset.

Who is online

Users browsing this forum: No registered users and 2 guests