Skip to content

uart_baud_rate detect() in uart.c does not work correctly #4953

Closed
@Jeroen88

Description

@Jeroen88

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP8266
  • Core Version: latest git
  • Development Env: Arduino IDE
  • Operating System: Ubuntu

Settings in IDE

  • Module: Generic ESP8266 Module (Nodemcu running c++)
  • Flash Mode: QIO
  • Flash Size: 4MB/1MB
  • lwip Variant: v2 Higher Bandwidth
  • Reset Method: ck
  • Flash Frequency: 40Mhz
  • CPU Frequency: 160MHz
  • Upload Using: SERIAL
  • Upload Speed: 23400 (serial upload only)

Problem Description

When I flash a sketch to my ESP8266 (using the serial port), uart_detect_baudrate(UART0) returns 0. When I push the reset button, it gives the correct answer (9600 in my case, my device sends data every 10s). If I call uart_detect_baudrate(UART0) again it returns 0, while I would expect 9600 again.
I need a robust automatic baudrate detection, and for this I need to call uart_detect_baudrate several times.
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. I also tried to set PULSE_HIGH and PULSE_LOW to 0xFFFFFUL, again without success.

MCVE Sketch

#include <Arduino.h>
#include <uart.h>

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];
}


void setup() {
  Serial.begin(115200, SERIAL_8N1);
  unsigned long detectedBaudrate;
  time_t start = millis();
  while(millis() - start < 11000) {
    if(detectedBaudrate = uart_detect_baudrate(UART0)) break;
    yield();
    delay(100);
  }
  Serial.println(detectedBaudrate);
  start = millis();
  while(millis() - start < 11000) {
    if(detectedBaudrate = uart_detect_baudrate(UART0)) break;
    yield();
    delay(100);
  }
  Serial.println(detectedBaudrate);
}

void loop() {

}

Debug Messages

After flashing:
0
0

After hard reset:
9600
0

Expected:
9600
9600

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions