mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'bugfix/uart_fix_get_baudrate_error_bug' into 'master'
driver(uart): Fix uart_get_baudrate incorrect return value when using ref_tick See merge request idf/esp-idf!2295
This commit is contained in:
commit
a2556229aa
48
components/driver/test/test_uart.c
Normal file
48
components/driver/test/test_uart.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "driver/uart.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define UART_TAG "Uart"
|
||||
#define UART_NUM1 (UART_NUM_1)
|
||||
#define BUF_SIZE (100)
|
||||
#define UART1_RX_PIN (22)
|
||||
#define UART1_TX_PIN (23)
|
||||
#define UART_BAUD_11520 (11520)
|
||||
#define UART_BAUD_115200 (115200)
|
||||
#define TOLERANCE (0.02) //baud rate error tolerance 2%.
|
||||
|
||||
#define UART_TOLERANCE_CHECK(val, uper_limit, lower_limit) ( (val) <= (uper_limit) && (val) >= (lower_limit) )
|
||||
|
||||
static void uart_config(uint32_t baud_rate, bool use_ref_tick)
|
||||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = baud_rate,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.use_ref_tick = use_ref_tick,
|
||||
};
|
||||
uart_param_config(UART_NUM1, &uart_config);
|
||||
uart_set_pin(UART_NUM1, UART1_TX_PIN, UART1_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
||||
uart_driver_install(UART_NUM1, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("test uart get baud-rate","[uart]")
|
||||
{
|
||||
uint32_t baud_rate1 = 0;
|
||||
uint32_t baud_rate2 = 0;
|
||||
printf("init uart%d, use reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_11520);
|
||||
uart_config(UART_BAUD_11520, true);
|
||||
uart_get_baudrate(UART_NUM1, &baud_rate1);
|
||||
printf("init uart%d, unuse reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_115200);
|
||||
uart_config(UART_BAUD_115200, false);
|
||||
uart_get_baudrate(UART_NUM1, &baud_rate2);
|
||||
printf("get baud rate when use reftick: %d\n", (int)baud_rate1);
|
||||
printf("get baud rate when don't use reftick: %d\n", (int)baud_rate2);
|
||||
uart_driver_delete(UART_NUM1);
|
||||
TEST_ASSERT(UART_TOLERANCE_CHECK(baud_rate1, (1.0 + TOLERANCE)*UART_BAUD_11520, (1.0 - TOLERANCE)*UART_BAUD_11520))
|
||||
TEST_ASSERT(UART_TOLERANCE_CHECK(baud_rate2, (1.0 + TOLERANCE)*UART_BAUD_115200, (1.0 - TOLERANCE)*UART_BAUD_115200))
|
||||
ESP_LOGI(UART_TAG, "get baud-rate test passed ....\n");
|
||||
}
|
@ -210,7 +210,11 @@ esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate)
|
||||
UART_ENTER_CRITICAL(&uart_spinlock[uart_num]);
|
||||
uint32_t clk_div = (UART[uart_num]->clk_div.div_int << 4) | UART[uart_num]->clk_div.div_frag;
|
||||
UART_EXIT_CRITICAL(&uart_spinlock[uart_num]);
|
||||
(*baudrate) = ((UART_CLK_FREQ) << 4) / clk_div;
|
||||
uint32_t uart_clk_freq = esp_clk_apb_freq();
|
||||
if(UART[uart_num]->conf0.tick_ref_always_on == 0) {
|
||||
uart_clk_freq = REF_CLK_FREQ;
|
||||
}
|
||||
(*baudrate) = ((uart_clk_freq) << 4) / clk_div;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user