2021-05-23 19:06:17 -04:00
|
|
|
/*
|
2024-01-24 09:29:13 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
2021-05-23 19:06:17 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-04-24 04:11:02 -04:00
|
|
|
#include <string.h>
|
2018-07-17 07:10:33 -04:00
|
|
|
#include <sys/param.h>
|
2018-04-24 04:11:02 -04:00
|
|
|
#include "unity.h"
|
2024-01-24 09:29:13 -05:00
|
|
|
#include "test_utils.h"
|
|
|
|
#include "driver/uart.h"
|
2018-04-24 04:11:02 -04:00
|
|
|
#include "esp_log.h"
|
2020-11-19 04:03:10 -05:00
|
|
|
#include "esp_rom_gpio.h"
|
2024-01-24 09:29:13 -05:00
|
|
|
#include "driver/lp_io.h"
|
2020-11-19 04:03:10 -05:00
|
|
|
#include "soc/uart_periph.h"
|
2024-01-24 09:29:13 -05:00
|
|
|
#include "soc/uart_pins.h"
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
#include "soc/clk_tree_defs.h"
|
2020-08-20 00:22:36 -04:00
|
|
|
|
2018-04-24 04:11:02 -04:00
|
|
|
#define BUF_SIZE (100)
|
|
|
|
#define UART_BAUD_11520 (11520)
|
|
|
|
#define UART_BAUD_115200 (115200)
|
|
|
|
#define TOLERANCE (0.02) //baud rate error tolerance 2%.
|
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
typedef struct {
|
|
|
|
uart_port_t port_num;
|
|
|
|
soc_module_clk_t default_src_clk;
|
|
|
|
int tx_pin_num;
|
|
|
|
int rx_pin_num;
|
|
|
|
uint32_t rx_flow_ctrl_thresh;
|
|
|
|
} uart_port_param_t;
|
2018-04-11 02:56:00 -04:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
static bool port_select(uart_port_param_t *port_param)
|
|
|
|
{
|
|
|
|
char argv[10];
|
|
|
|
unity_wait_for_signal_param("select to test 'uart' or 'lp_uart' port", argv, sizeof(argv));
|
|
|
|
|
|
|
|
if (strcmp(argv, "uart") == 0) {
|
|
|
|
port_param->port_num = UART_NUM_1; // Test HP_UART with UART1 port
|
|
|
|
port_param->default_src_clk = UART_SCLK_DEFAULT;
|
|
|
|
port_param->tx_pin_num = 4;
|
|
|
|
port_param->rx_pin_num = 5;
|
|
|
|
port_param->rx_flow_ctrl_thresh = 120;
|
|
|
|
return true;
|
|
|
|
#if SOC_UART_LP_NUM > 0
|
|
|
|
} else if (strcmp(argv, "lp_uart") == 0) {
|
|
|
|
port_param->port_num = LP_UART_NUM_0;
|
|
|
|
port_param->default_src_clk = LP_UART_SCLK_DEFAULT;
|
|
|
|
port_param->tx_pin_num = LP_U0TXD_GPIO_NUM;
|
|
|
|
port_param->rx_pin_num = LP_U0RXD_GPIO_NUM;
|
|
|
|
port_param->rx_flow_ctrl_thresh = 12;
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_config(uart_port_t uart_num, uint32_t baud_rate, uart_sclk_t source_clk)
|
2018-04-24 04:11:02 -04:00
|
|
|
{
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = baud_rate,
|
2020-11-19 04:03:10 -05:00
|
|
|
.source_clk = source_clk,
|
2018-04-24 04:11:02 -04:00
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
|
|
};
|
2020-11-19 04:03:10 -05:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
TEST_ESP_OK(uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0));
|
|
|
|
TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
|
|
|
|
TEST_ESP_OK(uart_set_loop_back(uart_num, true));
|
2018-04-24 04:11:02 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 04:37:26 -05:00
|
|
|
static volatile bool exit_flag, case_end;
|
2019-06-04 00:17:55 -04:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
typedef struct {
|
|
|
|
uart_port_t port_num;
|
|
|
|
SemaphoreHandle_t exit_sem;
|
|
|
|
} uart_task1_param_t;
|
|
|
|
|
2022-11-10 04:37:26 -05:00
|
|
|
static void test_task1(void *pvParameters)
|
2019-06-04 00:17:55 -04:00
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_task1_param_t *param = (uart_task1_param_t *)pvParameters;
|
2019-06-04 00:17:55 -04:00
|
|
|
char* data = (char *) malloc(256);
|
|
|
|
|
|
|
|
while (exit_flag == false) {
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_tx_chars(param->port_num, data, 256);
|
2019-06-04 00:17:55 -04:00
|
|
|
// The uart_wait_tx_done() function does not block anything if ticks_to_wait = 0.
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_wait_tx_done(param->port_num, 0);
|
2019-06-04 00:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(data);
|
2024-01-24 09:29:13 -05:00
|
|
|
xSemaphoreGive(param->exit_sem);
|
2019-06-04 00:17:55 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_task2(void *pvParameters)
|
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_t uart_num = (uart_port_t)pvParameters;
|
2019-06-04 00:17:55 -04:00
|
|
|
while (exit_flag == false) {
|
|
|
|
// This task obstruct a setting tx_done_sem semaphore in the UART interrupt.
|
|
|
|
// It leads to waiting the ticks_to_wait time in uart_wait_tx_done() function.
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_disable_tx_intr(uart_num);
|
2019-06-04 00:17:55 -04:00
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2022-11-10 04:37:26 -05:00
|
|
|
static void test_task3(void *pvParameters)
|
2019-06-04 00:17:55 -04:00
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
|
|
|
uart_config(uart_num, UART_BAUD_11520, port_param.default_src_clk);
|
2019-06-04 00:17:55 -04:00
|
|
|
|
2022-02-08 04:39:38 -05:00
|
|
|
SemaphoreHandle_t exit_sema = xSemaphoreCreateBinary();
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_task1_param_t task1_param = {
|
|
|
|
.port_num = uart_num,
|
|
|
|
.exit_sem = exit_sema,
|
|
|
|
};
|
2019-06-04 00:17:55 -04:00
|
|
|
exit_flag = false;
|
2022-11-10 04:37:26 -05:00
|
|
|
case_end = false;
|
2019-06-04 00:17:55 -04:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
xTaskCreate(test_task1, "tsk1", 2048, (void *)&task1_param, 5, NULL);
|
|
|
|
xTaskCreate(test_task2, "tsk2", 2048, (void *)uart_num, 5, NULL);
|
2019-06-04 00:17:55 -04:00
|
|
|
|
|
|
|
printf("Waiting for 5 sec\n");
|
2022-11-10 04:37:26 -05:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
2019-06-04 00:17:55 -04:00
|
|
|
exit_flag = true;
|
|
|
|
|
2022-11-10 04:37:26 -05:00
|
|
|
if (xSemaphoreTake(exit_sema, pdMS_TO_TICKS(1000)) == pdTRUE) {
|
2019-06-04 00:17:55 -04:00
|
|
|
vSemaphoreDelete(exit_sema);
|
|
|
|
} else {
|
|
|
|
TEST_FAIL_MESSAGE("uart_wait_tx_done is blocked");
|
|
|
|
}
|
2024-01-24 09:29:13 -05:00
|
|
|
TEST_ESP_OK(uart_driver_delete(uart_num));
|
2022-11-10 04:37:26 -05:00
|
|
|
|
|
|
|
vTaskDelay(2); // wait for test_task1 to exit
|
|
|
|
|
|
|
|
case_end = true;
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("test uart_wait_tx_done is not blocked when ticks_to_wait=0", "[uart]")
|
|
|
|
{
|
|
|
|
xTaskCreate(test_task3, "tsk3", 4096, NULL, 5, NULL);
|
|
|
|
while(!case_end);
|
|
|
|
vTaskDelay(2); // wait for test_task3 to exit
|
2019-06-04 00:17:55 -04:00
|
|
|
}
|
|
|
|
|
2019-04-15 09:07:38 -04:00
|
|
|
TEST_CASE("test uart get baud-rate", "[uart]")
|
2018-04-24 04:11:02 -04:00
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
|
|
|
soc_module_clk_t uart_clk_srcs[] = SOC_UART_CLKS;
|
|
|
|
uint32_t uart_clk_srcs_num = sizeof(uart_clk_srcs) / sizeof(uart_clk_srcs[0]);
|
|
|
|
|
|
|
|
soc_module_clk_t *clk_srcs = uart_clk_srcs;
|
|
|
|
uint32_t clk_srcs_num = uart_clk_srcs_num;
|
|
|
|
|
|
|
|
#if SOC_UART_LP_NUM > 0
|
|
|
|
soc_module_clk_t lp_uart_clk_srcs[] = SOC_LP_UART_CLKS;
|
|
|
|
uint32_t lp_uart_clk_srcs_num = sizeof(lp_uart_clk_srcs) / sizeof(lp_uart_clk_srcs[0]);
|
|
|
|
|
|
|
|
if (uart_num >= SOC_UART_HP_NUM) {
|
|
|
|
clk_srcs = lp_uart_clk_srcs;
|
|
|
|
clk_srcs_num = lp_uart_clk_srcs_num;
|
|
|
|
}
|
2020-11-19 04:03:10 -05:00
|
|
|
#endif
|
2024-01-24 09:29:13 -05:00
|
|
|
|
|
|
|
uart_config(uart_num, UART_BAUD_115200, port_param.default_src_clk);
|
|
|
|
|
|
|
|
const uint32_t test_baudrate_vals[] = {UART_BAUD_11520, UART_BAUD_115200};
|
|
|
|
for (size_t i = 0; i < sizeof(test_baudrate_vals) / sizeof(test_baudrate_vals[0]); i++) {
|
|
|
|
for (size_t j = 0; j < clk_srcs_num; j++) {
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = test_baudrate_vals[i],
|
|
|
|
.source_clk = clk_srcs[j],
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
|
|
|
|
|
|
|
|
uint32_t actual_baudrate = 0;
|
|
|
|
uart_get_baudrate(uart_num, &actual_baudrate);
|
|
|
|
TEST_ASSERT_UINT32_WITHIN(test_baudrate_vals[i] * TOLERANCE, test_baudrate_vals[i], actual_baudrate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uart_driver_delete(uart_num);
|
2018-04-11 02:56:00 -04:00
|
|
|
}
|
|
|
|
|
2019-04-15 09:07:38 -04:00
|
|
|
TEST_CASE("test uart tx data with break", "[uart]")
|
2018-07-19 09:41:35 -04:00
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
2018-07-19 09:41:35 -04:00
|
|
|
const int buf_len = 200;
|
|
|
|
const int send_len = 128;
|
|
|
|
const int brk_len = 10;
|
|
|
|
char *psend = (char *)malloc(buf_len);
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_NOT_NULL(psend);
|
2018-07-19 09:41:35 -04:00
|
|
|
memset(psend, '0', buf_len);
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_config(uart_num, UART_BAUD_115200, port_param.default_src_clk);
|
|
|
|
printf("Uart%d send %d bytes with break\n", uart_num, send_len);
|
|
|
|
uart_write_bytes_with_break(uart_num, (const char *)psend, send_len, brk_len);
|
|
|
|
uart_wait_tx_done(uart_num, portMAX_DELAY);
|
2018-07-19 09:41:35 -04:00
|
|
|
//If the code is running here, it means the test passed, otherwise it will crash due to the interrupt wdt timeout.
|
|
|
|
printf("Send data with break test passed\n");
|
|
|
|
free(psend);
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_driver_delete(uart_num);
|
2018-07-19 09:41:35 -04:00
|
|
|
}
|
2019-11-27 08:32:52 -05:00
|
|
|
|
|
|
|
static void uart_word_len_set_get_test(int uart_num)
|
|
|
|
{
|
|
|
|
printf("uart word len set and get test\n");
|
|
|
|
uart_word_length_t word_length_set = 0;
|
|
|
|
uart_word_length_t word_length_get = 0;
|
|
|
|
for (int i = 0; i < UART_DATA_BITS_MAX; i++) {
|
|
|
|
word_length_set = UART_DATA_5_BITS + i;
|
|
|
|
TEST_ESP_OK(uart_set_word_length(uart_num, word_length_set));
|
|
|
|
TEST_ESP_OK(uart_get_word_length(uart_num, &word_length_get));
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_EQUAL(word_length_set, word_length_get);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_stop_bit_set_get_test(int uart_num)
|
|
|
|
{
|
|
|
|
printf("uart stop bit set and get test\n");
|
|
|
|
uart_stop_bits_t stop_bit_set = 0;
|
|
|
|
uart_stop_bits_t stop_bit_get = 0;
|
|
|
|
for (int i = UART_STOP_BITS_1; i < UART_STOP_BITS_MAX; i++) {
|
|
|
|
stop_bit_set = i;
|
|
|
|
TEST_ESP_OK(uart_set_stop_bits(uart_num, stop_bit_set));
|
|
|
|
TEST_ESP_OK(uart_get_stop_bits(uart_num, &stop_bit_get));
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_EQUAL(stop_bit_set, stop_bit_get);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_parity_set_get_test(int uart_num)
|
|
|
|
{
|
|
|
|
printf("uart parity set and get test\n");
|
|
|
|
uart_parity_t parity_set[3] = {
|
|
|
|
UART_PARITY_DISABLE,
|
|
|
|
UART_PARITY_EVEN,
|
|
|
|
UART_PARITY_ODD,
|
|
|
|
};
|
|
|
|
uart_parity_t parity_get = 0;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
TEST_ESP_OK(uart_set_parity(uart_num, parity_set[i]));
|
|
|
|
TEST_ESP_OK(uart_get_parity(uart_num, &parity_get));
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_EQUAL(parity_set[i], parity_get);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_hw_flow_set_get_test(int uart_num)
|
|
|
|
{
|
|
|
|
printf("uart hw flow control set and get test\n");
|
2019-12-11 22:32:38 -05:00
|
|
|
uart_hw_flowcontrol_t flowcontrol_set = 0;
|
|
|
|
uart_hw_flowcontrol_t flowcontrol_get = 0;
|
2019-11-27 08:32:52 -05:00
|
|
|
for (int i = 0; i < UART_HW_FLOWCTRL_DISABLE; i++) {
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ESP_OK(uart_set_hw_flow_ctrl(uart_num, flowcontrol_set, 20));
|
|
|
|
TEST_ESP_OK(uart_get_hw_flow_ctrl(uart_num, &flowcontrol_get));
|
|
|
|
TEST_ASSERT_EQUAL(flowcontrol_set, flowcontrol_get);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_wakeup_set_get_test(int uart_num)
|
|
|
|
{
|
|
|
|
printf("uart wake up set and get test\n");
|
|
|
|
int wake_up_set = 0;
|
|
|
|
int wake_up_get = 0;
|
|
|
|
for (int i = 3; i < 0x3ff; i++) {
|
|
|
|
wake_up_set = i;
|
|
|
|
TEST_ESP_OK(uart_set_wakeup_threshold(uart_num, wake_up_set));
|
|
|
|
TEST_ESP_OK(uart_get_wakeup_threshold(uart_num, &wake_up_get));
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_EQUAL(wake_up_set, wake_up_get);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("uart general API test", "[uart]")
|
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
2019-11-27 08:32:52 -05:00
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 115200,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
2024-01-24 09:29:13 -05:00
|
|
|
.source_clk = port_param.default_src_clk,
|
2019-11-27 08:32:52 -05:00
|
|
|
};
|
|
|
|
uart_param_config(uart_num, &uart_config);
|
|
|
|
uart_word_len_set_get_test(uart_num);
|
|
|
|
uart_stop_bit_set_get_test(uart_num);
|
|
|
|
uart_parity_set_get_test(uart_num);
|
|
|
|
uart_hw_flow_set_get_test(uart_num);
|
|
|
|
uart_wakeup_set_get_test(uart_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_write_task(void *param)
|
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_t uart_num = (uart_port_t)param;
|
2019-11-27 08:32:52 -05:00
|
|
|
uint8_t *tx_buf = (uint8_t *)malloc(1024);
|
|
|
|
if(tx_buf == NULL) {
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_FAIL_MESSAGE("tx buffer malloc fail");
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
for(int i = 1; i < 1023; i++) {
|
|
|
|
tx_buf[i] = (i & 0xff);
|
|
|
|
}
|
|
|
|
for(int i = 0; i < 1024; i++) {
|
|
|
|
//d[0] and d[1023] are header
|
|
|
|
tx_buf[0] = (i & 0xff);
|
|
|
|
tx_buf[1023] = ((~i) & 0xff);
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_write_bytes(uart_num, (const char *)tx_buf, 1024);
|
2022-11-10 04:37:26 -05:00
|
|
|
uart_wait_tx_done(uart_num, portMAX_DELAY);
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
free(tx_buf);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_CASE("uart read write test", "[uart]")
|
2019-11-27 08:32:52 -05:00
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
2019-11-27 08:32:52 -05:00
|
|
|
uint8_t *rd_data = (uint8_t *)malloc(1024);
|
|
|
|
if(rd_data == NULL) {
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_FAIL_MESSAGE("rx buffer malloc fail");
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 2000000,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
2020-09-25 06:34:27 -04:00
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
|
2024-01-24 09:29:13 -05:00
|
|
|
.source_clk = port_param.default_src_clk,
|
|
|
|
.rx_flow_ctrl_thresh = port_param.rx_flow_ctrl_thresh,
|
2019-11-27 08:32:52 -05:00
|
|
|
};
|
|
|
|
TEST_ESP_OK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 20, NULL, 0));
|
|
|
|
TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
|
2024-01-24 09:29:13 -05:00
|
|
|
// Use loop back feature to connect TX signal to RX signal, CTS signal to RTS signal internally. Then no need to configure uart pins.
|
2019-11-27 08:32:52 -05:00
|
|
|
TEST_ESP_OK(uart_set_loop_back(uart_num, true));
|
2019-12-12 01:20:39 -05:00
|
|
|
|
|
|
|
TEST_ESP_OK(uart_wait_tx_done(uart_num, portMAX_DELAY));
|
2022-11-10 04:37:26 -05:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(20)); // make sure last byte has flushed from TX FIFO
|
2019-12-12 01:20:39 -05:00
|
|
|
TEST_ESP_OK(uart_flush_input(uart_num));
|
2022-11-10 04:37:26 -05:00
|
|
|
xTaskCreate(uart_write_task, "uart_write_task", 8192, (void *)uart_num, 5, NULL);
|
2019-11-27 08:32:52 -05:00
|
|
|
for (int i = 0; i < 1024; i++) {
|
2020-09-25 06:34:27 -04:00
|
|
|
int bytes_remaining = 1024;
|
2019-11-27 08:32:52 -05:00
|
|
|
memset(rd_data, 0, 1024);
|
2020-09-25 06:34:27 -04:00
|
|
|
while (bytes_remaining) {
|
2022-11-10 04:37:26 -05:00
|
|
|
int bytes_received = uart_read_bytes(uart_num, rd_data + 1024 - bytes_remaining, bytes_remaining, pdMS_TO_TICKS(100));
|
2020-09-25 06:34:27 -04:00
|
|
|
if (bytes_received < 0) {
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_FAIL_MESSAGE("read timeout, uart read write test fail");
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
2020-09-25 06:34:27 -04:00
|
|
|
bytes_remaining -= bytes_received;
|
|
|
|
}
|
|
|
|
int check_fail_cnt = 0;
|
|
|
|
if (rd_data[0] != (i & 0xff)) {
|
|
|
|
printf("packet %d index check error at offset 0, expected 0x%02x\n", i, i);
|
|
|
|
++check_fail_cnt;
|
|
|
|
}
|
|
|
|
if (rd_data[1023] != ((~i) & 0xff)) {
|
|
|
|
printf("packet %d index check error at offset 1023, expected 0x%02x\n", i, ((~i) & 0xff));
|
|
|
|
++check_fail_cnt;
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
for (int j = 1; j < 1023; j++) {
|
2020-09-25 06:34:27 -04:00
|
|
|
if (rd_data[j] != (j & 0xff)) {
|
|
|
|
printf("data mismatch in packet %d offset %d, expected 0x%02x got 0x%02x\n", i, j, (j & 0xff), rd_data[j]);
|
|
|
|
++check_fail_cnt;
|
|
|
|
}
|
|
|
|
if (check_fail_cnt > 10) {
|
|
|
|
printf("(further checks skipped)\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (check_fail_cnt > 0) {
|
|
|
|
ESP_LOG_BUFFER_HEX("rd_data", rd_data, 1024);
|
|
|
|
TEST_FAIL();
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-10 04:37:26 -05:00
|
|
|
uart_wait_tx_done(uart_num, portMAX_DELAY);
|
2019-11-27 08:32:52 -05:00
|
|
|
uart_driver_delete(uart_num);
|
|
|
|
free(rd_data);
|
2022-11-10 04:37:26 -05:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
vTaskDelay(2); // wait for uart_write_task to exit
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("uart tx with ringbuffer test", "[uart]")
|
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
2019-11-27 08:32:52 -05:00
|
|
|
uint8_t *rd_data = (uint8_t *)malloc(1024);
|
|
|
|
uint8_t *wr_data = (uint8_t *)malloc(1024);
|
|
|
|
if(rd_data == NULL || wr_data == NULL) {
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_FAIL_MESSAGE("buffer malloc fail");
|
2019-11-27 08:32:52 -05:00
|
|
|
}
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 2000000,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
2020-06-11 23:31:34 -04:00
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
|
2024-01-24 09:29:13 -05:00
|
|
|
.rx_flow_ctrl_thresh = port_param.rx_flow_ctrl_thresh,
|
|
|
|
.source_clk = port_param.default_src_clk,
|
2019-11-27 08:32:52 -05:00
|
|
|
};
|
2020-06-04 08:57:50 -04:00
|
|
|
uart_wait_tx_idle_polling(uart_num);
|
2019-11-27 08:32:52 -05:00
|
|
|
TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
|
2024-01-24 09:29:13 -05:00
|
|
|
TEST_ESP_OK(uart_driver_install(uart_num, 1024 * 2, 1024 * 2, 20, NULL, 0));
|
|
|
|
// Use loop back feature to connect TX signal to RX signal, CTS signal to RTS signal internally. Then no need to configure uart pins.
|
2019-11-27 08:32:52 -05:00
|
|
|
TEST_ESP_OK(uart_set_loop_back(uart_num, true));
|
2020-11-19 04:03:10 -05:00
|
|
|
|
2019-11-27 08:32:52 -05:00
|
|
|
for (int i = 0; i < 1024; i++) {
|
|
|
|
wr_data[i] = i;
|
|
|
|
rd_data[i] = 0;
|
|
|
|
}
|
2022-06-29 05:56:38 -04:00
|
|
|
|
|
|
|
size_t tx_buffer_free_space;
|
|
|
|
uart_get_tx_buffer_free_size(uart_num, &tx_buffer_free_space);
|
|
|
|
TEST_ASSERT_EQUAL_INT(2048, tx_buffer_free_space); // full tx buffer space is free
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_write_bytes(uart_num, (const char *)wr_data, 1024);
|
2022-06-29 05:56:38 -04:00
|
|
|
uart_get_tx_buffer_free_size(uart_num, &tx_buffer_free_space);
|
|
|
|
TEST_ASSERT_LESS_THAN(2048, tx_buffer_free_space); // tx transmit in progress: tx buffer has content
|
|
|
|
TEST_ASSERT_GREATER_OR_EQUAL(1024, tx_buffer_free_space);
|
2022-11-10 04:37:26 -05:00
|
|
|
uart_wait_tx_done(uart_num, portMAX_DELAY);
|
2022-06-29 05:56:38 -04:00
|
|
|
uart_get_tx_buffer_free_size(uart_num, &tx_buffer_free_space);
|
|
|
|
TEST_ASSERT_EQUAL_INT(2048, tx_buffer_free_space); // tx done: tx buffer back to empty
|
2022-11-10 04:37:26 -05:00
|
|
|
uart_read_bytes(uart_num, rd_data, 1024, pdMS_TO_TICKS(1000));
|
2019-12-11 22:32:38 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(wr_data, rd_data, 1024);
|
2019-11-27 08:32:52 -05:00
|
|
|
TEST_ESP_OK(uart_driver_delete(uart_num));
|
|
|
|
free(rd_data);
|
|
|
|
free(wr_data);
|
|
|
|
}
|
2021-12-07 23:15:02 -05:00
|
|
|
|
|
|
|
TEST_CASE("uart int state restored after flush", "[uart]")
|
|
|
|
{
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_port_param_t port_param = {};
|
|
|
|
TEST_ASSERT(port_select(&port_param));
|
|
|
|
|
|
|
|
uart_port_t uart_num = port_param.port_num;
|
2021-12-07 23:15:02 -05:00
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 115200,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
2024-01-24 09:29:13 -05:00
|
|
|
.source_clk = port_param.default_src_clk,
|
2021-12-07 23:15:02 -05:00
|
|
|
};
|
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
const int uart_tx_signal = uart_periph_signal[uart_num].pins[SOC_UART_TX_PIN_IDX].signal;
|
|
|
|
const int uart_tx = port_param.tx_pin_num;
|
|
|
|
const int uart_rx = port_param.rx_pin_num;
|
2021-12-07 23:15:02 -05:00
|
|
|
const int buf_size = 256;
|
|
|
|
const int intr_alloc_flags = 0;
|
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
TEST_ESP_OK(uart_driver_install(uart_num, buf_size * 2, 0, 0, NULL, intr_alloc_flags));
|
|
|
|
TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
|
|
|
|
TEST_ESP_OK(uart_set_pin(uart_num, uart_tx, uart_rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
2021-12-07 23:15:02 -05:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
/* Make sure UART's TX signal is connected to RX pin
|
2021-12-07 23:15:02 -05:00
|
|
|
* This creates a loop that lets us receive anything we send on the UART */
|
2024-01-24 09:29:13 -05:00
|
|
|
if (uart_num < SOC_UART_HP_NUM) {
|
|
|
|
esp_rom_gpio_connect_out_signal(uart_rx, uart_tx_signal, false, false);
|
|
|
|
#if SOC_UART_LP_NUM > 0
|
|
|
|
} else {
|
|
|
|
// LP_UART
|
|
|
|
#if SOC_LP_GPIO_MATRIX_SUPPORTED
|
|
|
|
TEST_ESP_OK(lp_gpio_connect_out_signal(uart_rx, uart_tx_signal, false, false));
|
|
|
|
#else
|
|
|
|
// The only way is to use loop back feature
|
|
|
|
TEST_ESP_OK(uart_set_loop_back(uart_num, true));
|
|
|
|
#endif
|
|
|
|
#endif // SOC_UART_LP_NUM > 0
|
|
|
|
}
|
2021-12-07 23:15:02 -05:00
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
uint8_t *data = (uint8_t *)malloc(buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_NULL(data);
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
|
|
|
|
/* As we set up a loopback, we can read them back on RX */
|
2024-01-24 09:29:13 -05:00
|
|
|
int len = uart_read_bytes(uart_num, data, buf_size, pdMS_TO_TICKS(1000));
|
|
|
|
printf("len is %d\n", len);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_EQUAL(len, buf_size);
|
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
/**
|
|
|
|
* The first goal of this test is to make sure that when our RX FIFO is full,
|
|
|
|
* we can continue receiving back data after flushing
|
|
|
|
* For more details, check IDF-4374 */
|
|
|
|
|
2021-12-07 23:15:02 -05:00
|
|
|
/* Fill the RX buffer, this should disable the RX interrupts */
|
2024-01-24 09:29:13 -05:00
|
|
|
int written = uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(-1, written);
|
2024-01-24 09:29:13 -05:00
|
|
|
written = uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(-1, written);
|
2024-01-24 09:29:13 -05:00
|
|
|
written = uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(-1, written);
|
|
|
|
|
|
|
|
/* Flush the input buffer, RX interrupts should be re-enabled */
|
2024-01-24 09:29:13 -05:00
|
|
|
uart_flush_input(uart_num);
|
|
|
|
written = uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(-1, written);
|
2024-01-24 09:29:13 -05:00
|
|
|
len = uart_read_bytes(uart_num, data, buf_size, pdMS_TO_TICKS(1000));
|
2021-12-07 23:15:02 -05:00
|
|
|
/* len equals buf_size bytes if interrupts were indeed re-enabled */
|
|
|
|
TEST_ASSERT_EQUAL(len, buf_size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Second test, make sure that if we explicitly disable the RX interrupts,
|
|
|
|
* they are NOT re-enabled after flushing
|
|
|
|
* To do so, start by cleaning the RX FIFO, disable the RX interrupts,
|
|
|
|
* flush again, send data to the UART and check that we haven't received
|
|
|
|
* any of the bytes */
|
2024-01-24 09:29:13 -05:00
|
|
|
|
|
|
|
uart_flush_input(uart_num);
|
|
|
|
uart_disable_rx_intr(uart_num);
|
|
|
|
uart_flush_input(uart_num);
|
|
|
|
written = uart_write_bytes(uart_num, (const char *)data, buf_size);
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(-1, written);
|
2024-01-24 09:29:13 -05:00
|
|
|
len = uart_read_bytes(uart_num, data, buf_size, pdMS_TO_TICKS(250));
|
2021-12-07 23:15:02 -05:00
|
|
|
TEST_ASSERT_EQUAL(len, 0);
|
|
|
|
|
2024-01-24 09:29:13 -05:00
|
|
|
TEST_ESP_OK(uart_driver_delete(uart_num));
|
2021-12-07 23:15:02 -05:00
|
|
|
free(data);
|
|
|
|
}
|