uart_set_baudrate sets incorrect baud rate

user91829
Posts: 11
Joined: Thu Mar 03, 2022 11:20 am

uart_set_baudrate sets incorrect baud rate

Postby user91829 » Mon Mar 21, 2022 9:27 pm

  1.     static QueueHandle_t uart0_queue;
  2.    uart_config_t uart_config = {
  3.         .baud_rate = 115200,
  4.         .data_bits = UART_DATA_8_BITS,
  5.         .parity = UART_PARITY_DISABLE,
  6.         .stop_bits = UART_STOP_BITS_1,
  7.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  8.     };
  9.     // Install UART driver, and get the queue.
  10.     uart_driver_install(UART_NUM_1, 1024, 1024, 20, &uart0_queue, 0);
  11.     uart_param_config(UART_NUM_1, &uart_config);
  12.     vTaskDelay(100 / portTICK_RATE_MS);
  13.     uint32_t baudrate;
  14.     ESP_LOGI("MAIN", "BAUD RATE 0: %d", baudrate);
  15.     uart_get_baudrate(UART_NUM_1, &baudrate);
  16.     ESP_LOGI("MAIN", "BAUD RATE 1: %d", baudrate);
  17.     vTaskDelay(100 / portTICK_RATE_MS);
  18.     uart_set_baudrate(UART_NUM_1, 230400);
  19.     vTaskDelay(100 / portTICK_RATE_MS);
  20.     uart_get_baudrate(UART_NUM_1, &baudrate);
  21.     ESP_LOGI("MAIN", "BAUD RATE 2: %d", baudrate);
  22.     vTaskDelay(100 / portTICK_RATE_MS);
  23.     uart_set_baudrate(UART_NUM_1, 460800);
  24.     vTaskDelay(100 / portTICK_RATE_MS);
  25.     uart_get_baudrate(UART_NUM_1, &baudrate);
  26.     ESP_LOGI("MAIN", "BAUD RATE 3: %d", baudrate);
  27.     vTaskDelay(100 / portTICK_RATE_MS);
  28.     uart_set_baudrate(UART_NUM_1, 921600);
  29.     vTaskDelay(100 / portTICK_RATE_MS);
  30.     uart_get_baudrate(UART_NUM_1, &baudrate);
  31.     ESP_LOGI("MAIN", "BAUD RATE 4: %d", baudrate);
The above code gives the following output

I (857) MAIN: BAUD RATE 0: 0
I (857) MAIN: BAUD RATE 1: 115201
I (1057) MAIN: BAUD RATE 2: 230423
I (1257) MAIN: BAUD RATE 3: 460929
I (1457) MAIN: BAUD RATE 4: 922190

Am I doing something wrong here ?

I am using latest docker image `espressif/idf:release-v4.4` and this is the only code in `app_main()`

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: uart_set_baudrate sets incorrect baud rate

Postby WiFive » Mon Mar 21, 2022 10:46 pm

approximation is to be expected due to divider and clock frequency limitations

Who is online

Users browsing this forum: Gaston1980 and 117 guests