[testcase] 增加serial_v2的测试用例

This commit is contained in:
chenjh 2021-06-17 15:56:04 +08:00
parent 0f65821335
commit 312753686b
9 changed files with 1212 additions and 0 deletions

View File

@ -9,6 +9,7 @@ if RT_USING_UTESTCASES
source "$RTT_DIR/examples/utest/testcases/utest/Kconfig"
source "$RTT_DIR/examples/utest/testcases/kernel/Kconfig"
source "$RTT_DIR/examples/utest/testcases/drivers/serial_v2/Kconfig"
endif

View File

@ -0,0 +1,15 @@
# RT-Thread building script for bridge
import os
from building import *
cwd = GetCurrentDir()
objs = []
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(d, 'SConscript'))
Return('objs')

View File

@ -0,0 +1,7 @@
menu "Utest Serial Testcase"
config UTEST_SERIAL_TC
bool "Serial testcase"
default n
endmenu

View File

@ -0,0 +1,120 @@
## 1、介绍
该目录下 c 文件是新版本串口的测试用例,在 `examples/utest/testcases/drivers/serial_v2` 目录结构里,该测试用例用来测试串口的各个操作模式是否正常工作。
## 2、 文件说明
| 文件 | 描述 |
| ---------------- | ----------------------------------------- |
| uart_rxb_txb.c | 串口接收阻塞和发送阻塞模式 的测试用例 |
| uart_rxb_txnb.c | 串口接收阻塞和发送非阻塞模式 的测试用例 |
| uart_rxnb_txb.c | 串口接收非阻塞和发送阻塞模式 的测试用例 |
| uart_rxnb_txnb.c | 串口接收非阻塞和发送非阻塞模式 的测试用例 |
## 3、软硬件环境
硬件上需要支持 RT-Thread 的完整版操作系统版本为4.0.4及以上,且硬件有串口硬件外设,软件上需要支持 内核接口、IPC 、Device 框架。
## 4、测试项
### 4.1 测试说明
上文所提及的模式是指串口使用时的操作模式不涉及硬件的工作模式的配置情况硬件工作模式一般有轮询POLL、中断INT、DMA因此使用时需要结合具体的硬件工作模式去配置使用。例如 发送阻塞和接收非阻塞模式 这个测试有很多种硬件配置配置情况例如DMA发送阻塞和DMA接收非阻塞INT发送阻塞和DMA接收非阻塞POLL发送阻塞和DMA接收非阻塞等等。因此通过排列组合后的测试场景有4*9=36种有意义的组合方式为20种。如下表
| 接收非阻塞 | 发送阻塞 | 组合 | 有意义的组合方式 |
| ---------- | -------- | ----------------- | ---------------- |
| POLL | POLL | RX_POLL + TX_POLL | |
| | INT | RX_POLL + TX_INT | |
| | DMA | RX_POLL + TX_DMA | |
| INT | POLL | RX_INT + TX_POLL | ✔ |
| | INT | RX_INT + TX_INT | ✔ |
| | DMA | RX_INT + TX_DMA | ✔ |
| DMA | POLL | RX_DMA + TX_POLL | ✔ |
| | INT | RX_DMA + TX_INT | ✔ |
| | DMA | RX_DMA + TX_DMA | ✔ |
| 接收非阻塞 | 发送非阻塞 | 组合 | 有意义的组合方式 |
| ---------- | ---------- | ----------------- | ---------------- |
| POLL | POLL | RX_POLL + TX_POLL | |
| | INT | RX_POLL + TX_INT | |
| | DMA | RX_POLL + TX_DMA | |
| INT | POLL | RX_INT + TX_POLL | |
| | INT | RX_INT + TX_INT | ✔ |
| | DMA | RX_INT + TX_DMA | ✔ |
| DMA | POLL | RX_DMA + TX_POLL | |
| | INT | RX_DMA + TX_INT | ✔ |
| | DMA | RX_DMA + TX_DMA | ✔ |
| 接收阻塞 | 发送阻塞 | 组合 | 有意义的组合方式 |
| -------- | -------- | ----------------- | ---------------- |
| POLL | POLL | RX_POLL + TX_POLL | |
| | INT | RX_POLL + TX_INT | |
| | DMA | RX_POLL + TX_DMA | |
| INT | POLL | RX_INT + TX_POLL | ✔ |
| | INT | RX_INT + TX_INT | ✔ |
| | DMA | RX_INT + TX_DMA | ✔ |
| DMA | POLL | RX_DMA + TX_POLL | ✔ |
| | INT | RX_DMA + TX_INT | ✔ |
| | DMA | RX_DMA + TX_DMA | ✔ |
| 接收阻塞 | 发送非阻塞 | 组合 | 有意义的组合方式 |
| -------- | ---------- | ----------------- | ---------------- |
| POLL | POLL | RX_POLL + TX_POLL | |
| | INT | RX_POLL + TX_INT | |
| | DMA | RX_POLL + TX_DMA | |
| INT | POLL | RX_INT + TX_POLL | |
| | INT | RX_INT + TX_INT | ✔ |
| | DMA | RX_INT + TX_DMA | ✔ |
| DMA | POLL | RX_DMA + TX_POLL | |
| | INT | RX_DMA + TX_INT | ✔ |
| | DMA | RX_DMA + TX_DMA | ✔ |
需要解释的是为什么会存在无意义的组合模式举个例子非阻塞模式下肯定是不会出现POLL轮询方式的因为POLL方式已经表明是阻塞方式了。
该测试用例在测试多种组合时,需要通过更改`rtconfig.h`文件对硬件模式进行静态配置。
### 4.2 测试思路
这四个测试用例的测试思路基本一致。
硬件上:**短接串口的发送TX引脚和接收RX引脚完成自发自收的回路**。
软件上创建两个线程A和BA为接收线程B为发送线程设置A线程优先级比B线程优先级高。发送线程发送随机长度长度范围是 0 到 1000的数据接收线程接收到数据进行校验数据正确则测试通过默认测试100次。
## 5、配置
使用该测试用例需要在 `env` 工具的 `menuconfig` 中做相关配置,配置如下所示(使用 RT-Thread-Studio 的配置路径一致
```
RT-Thread Utestcases --->
[*] RT-Thread Utestcases --->
Utest Serial Testcase --->
[*] Serial testcase
```
## 6、使用
\- 编译下载。
\- 在 MSH 中输入 `utest_run testcases.drivers.uart_rxb_txb` 运行串口接收阻塞和发送阻塞测试用例。
\- 在 MSH 中输入 `utest_run testcases.drivers.uart_rxb_txb` 运行串口接收阻塞和发送阻塞测试用例。
\- 在 MSH 中输入 `utest_run testcases.drivers.uart_rxb_txb` 运行串口接收阻塞和发送阻塞测试用例。
\- 在 MSH 中输入 `utest_run testcases.drivers.uart_rxb_txb` 运行串口接收阻塞和发送阻塞测试用例。
如果仅仅配置了 `Serial testcase` 相关的测试用例,则直接输入 `utest_run` 运行即可将上述测试用例按序测试。
## 7、注意事项
\- 需配置正确的测试用例。
\- 如有需要,可开启 ULOG 查看测试用例日志信息。
\- 需在 MSH 中输入正确的命令行。
\- 测试用例默认的测试数据长度范围最大为1000字节如果接收端的缓冲区大小配置为小于1000字节时那么在测试接收阻塞模式时将会由于获取不了1000字节长度导致线程持续阻塞因为测试用例是按 `recv_len` 长度去接收的,而不是按照单字节去接收的),因此建议接收端的缓冲区大小 (对应宏例如为 `BSP_UART2_RX_BUFSIZE`设置为1024即可当然也可按需减小测试的最大数据长度。
\- 该测试用例需要结合硬件具体的工作模式POLL 、INT、DMA进行测试而硬件工作模式只能选择一种因此需要在 `rtconfig.h` 中对串口相应的宏进行配置,来选择不同的工作模式去进行测试。

View File

@ -0,0 +1,16 @@
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
src = Split('''
uart_rxb_txnb.c
uart_rxb_txb.c
uart_rxnb_txb.c
uart_rxnb_txnb.c
''')
CPPPATH = [cwd]
group = DefineGroup('utestcases', src, depend = ['UTEST_SERIAL_TC'], CPPPATH = CPPPATH)
Return('group')

View File

@ -0,0 +1,230 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-16 KyleChan the first version
*/
#include <rtthread.h>
#include "utest.h"
#include <rtdevice.h>
#include <stdlib.h>
#define TC_UART_DEVICE_NAME "uart2"
#define TC_UART_SEND_TIMES 100
#ifdef UTEST_SERIAL_TC
#define TEST_UART_NAME TC_UART_DEVICE_NAME
static struct rt_serial_device *serial;
static rt_uint8_t uart_over_flag;
static rt_bool_t uart_result = RT_TRUE;
static rt_err_t uart_find(void)
{
serial = (struct rt_serial_device *)rt_device_find(TEST_UART_NAME);
if (serial == RT_NULL)
{
LOG_E("find %s device failed!\n", TEST_UART_NAME);
return -RT_ERROR;
}
return RT_EOK;
}
static void uart_send_entry(void *parameter)
{
rt_uint8_t *uart_write_buffer;
rt_uint16_t send_len;
rt_uint32_t i = 0;
send_len = *(rt_uint16_t *)parameter;
/* assign send buffer */
uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
if (uart_write_buffer == RT_NULL)
{
LOG_E("Without spare memory for uart dma!");
uart_result = RT_FALSE;
return;
}
rt_memset(uart_write_buffer, 0, send_len);
for (i = 0; i < send_len; i++)
{
uart_write_buffer[i] = (rt_uint8_t)i;
}
/* send buffer */
if (rt_device_write(&serial->parent, 0, uart_write_buffer, send_len) != send_len)
{
LOG_E("device write failed\r\n");
}
rt_free(uart_write_buffer);
}
static void uart_rec_entry(void *parameter)
{
rt_uint16_t rev_len;
rev_len = *(rt_uint16_t *)parameter;
rt_uint8_t *ch;
ch = (rt_uint8_t *)rt_calloc(1, sizeof(rt_uint8_t) * (rev_len + 1));
rt_int32_t cnt, i;
rt_uint8_t last_old_data;
rt_bool_t fisrt_flag = RT_TRUE;
rt_uint32_t all_receive_length = 0;
while (1)
{
cnt = rt_device_read(&serial->parent, 0, (void *)ch, rev_len);
if (cnt == 0)
{
continue;
}
if (fisrt_flag != RT_TRUE)
{
if ((rt_uint8_t)(last_old_data + 1) != ch[0])
{
LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, ch[0]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
else
{
fisrt_flag = RT_FALSE;
}
for (i = 0; i < cnt - 1; i++)
{
if ((rt_uint8_t)(ch[i] + 1) != ch[i + 1])
{
LOG_E("Read Different data -> former data: %x, current data: %x.", ch[i], ch[i + 1]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
all_receive_length += cnt;
if (all_receive_length >= rev_len)
break;
else
last_old_data = ch[cnt - 1];
}
rt_free(ch);
uart_over_flag = RT_TRUE;
}
static rt_err_t uart_api(rt_uint16_t length)
{
rt_thread_t thread_send = RT_NULL;
rt_thread_t thread_recv = RT_NULL;
rt_err_t result = RT_EOK;
uart_over_flag = RT_FALSE;
result = uart_find();
if (result != RT_EOK)
{
return -RT_ERROR;
}
/* Reinitialize */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_115200;
config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
if (result != RT_EOK)
{
LOG_E("Open uart device failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
thread_send = rt_thread_create("uart_send", uart_send_entry, &length, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &length, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
if ((thread_send != RT_NULL) && (thread_recv != RT_NULL))
{
rt_thread_startup(thread_send);
rt_thread_startup(thread_recv);
}
else
{
result = -RT_ERROR;
goto __exit;
}
while (1)
{
if (uart_result != RT_TRUE)
{
LOG_E("The test for uart dma is failure.");
result = -RT_ERROR;
goto __exit;
}
if (uart_over_flag == RT_TRUE)
{
goto __exit;
}
/* waiting for test over */
rt_thread_mdelay(5);
}
__exit:
rt_device_close(&serial->parent);
return result;
}
static void tc_uart_api(void)
{
rt_uint32_t times = 0;
rt_uint16_t num = 0;
while (TC_UART_SEND_TIMES - times)
{
num = (rand() % 1000) + 1;
if(uart_api(num) == RT_EOK)
LOG_I("data_lens [%3d], it is correct to read and write data. [%d] times testing.", num, ++times);
else
{
LOG_E("uart test error");
break;
}
}
uassert_true(uart_over_flag == RT_TRUE);
}
static rt_err_t utest_tc_init(void)
{
LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
uart_result = RT_TRUE;
uart_over_flag = RT_FALSE;
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(tc_uart_api);
}
UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_rxb_txb", utest_tc_init, utest_tc_cleanup, 30);
#endif /* TC_UART_USING_TC */

View File

@ -0,0 +1,263 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-16 KyleChan the first version
*/
#include <rtthread.h>
#include "utest.h"
#include <rtdevice.h>
#include <stdlib.h>
#define TC_UART_DEVICE_NAME "uart2"
#define TC_UART_SEND_TIMES 100
#ifdef UTEST_SERIAL_TC
#define TEST_UART_NAME TC_UART_DEVICE_NAME
static struct rt_serial_device *serial;
static rt_sem_t tx_sem;
static rt_uint8_t uart_over_flag;
static rt_bool_t uart_result = RT_TRUE;
static rt_err_t uart_find(void)
{
serial = (struct rt_serial_device *)rt_device_find(TEST_UART_NAME);
if (serial == RT_NULL)
{
LOG_E("find %s device failed!\n", TEST_UART_NAME);
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t uart_tx_completion(rt_device_t device, void *buffer)
{
rt_sem_release(tx_sem);
return RT_EOK;
}
static void uart_send_entry(void *parameter)
{
rt_uint8_t *uart_write_buffer;
rt_uint16_t send_len, len = 0;
rt_err_t result;
rt_uint32_t i = 0;
send_len = *(rt_uint16_t *)parameter;
/* assign send buffer */
uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
if (uart_write_buffer == RT_NULL)
{
LOG_E("Without spare memory for uart dma!");
uart_result = RT_FALSE;
return;
}
rt_memset(uart_write_buffer, 0, send_len);
for (i = 0; i < send_len; i++)
{
uart_write_buffer[i] = (rt_uint8_t)i;
}
/* send buffer */
while (send_len - len)
{
len += rt_device_write(&serial->parent, 0, uart_write_buffer + len, send_len - len);
result = rt_sem_take(tx_sem, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
LOG_E("take sem err in send.");
}
}
rt_free(uart_write_buffer);
}
static void uart_rec_entry(void *parameter)
{
rt_uint16_t rev_len;
rev_len = *(rt_uint16_t *)parameter;
rt_uint8_t *ch;
ch = (rt_uint8_t *)rt_calloc(1, sizeof(rt_uint8_t) * (rev_len + 1));
rt_int32_t cnt, i;
rt_uint8_t last_old_data;
rt_bool_t fisrt_flag = RT_TRUE;
rt_uint32_t all_receive_length = 0;
while (1)
{
cnt = rt_device_read(&serial->parent, 0, (void *)ch, rev_len);
if (cnt != rev_len)
{
continue;
}
if (fisrt_flag != RT_TRUE)
{
if ((rt_uint8_t)(last_old_data + 1) != ch[0])
{
LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, ch[0]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
else
{
fisrt_flag = RT_FALSE;
}
for (i = 0; i < cnt - 1; i++)
{
if ((rt_uint8_t)(ch[i] + 1) != ch[i + 1])
{
LOG_E("Read Different data -> former data: %x, current data: %x.", ch[i], ch[i + 1]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
all_receive_length += cnt;
if (all_receive_length >= rev_len)
break;
else
last_old_data = ch[cnt - 1];
}
rt_free(ch);
uart_over_flag = RT_TRUE;
}
static rt_err_t uart_api(rt_uint16_t test_buf)
{
rt_thread_t thread_send = RT_NULL;
rt_thread_t thread_recv = RT_NULL;
rt_err_t result = RT_EOK;
uart_over_flag = RT_FALSE;
result = uart_find();
if (result != RT_EOK)
{
return -RT_ERROR;
}
tx_sem = rt_sem_create("tx_sem", 0, RT_IPC_FLAG_PRIO);
if (tx_sem == RT_NULL)
{
LOG_E("Init sem failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
/* Reinitialize */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_115200;
config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING);
if (result != RT_EOK)
{
LOG_E("Open uart device failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
/* set receive callback function */
result = rt_device_set_tx_complete(&serial->parent, uart_tx_completion);
if (result != RT_EOK)
{
goto __exit;
}
thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
thread_send = rt_thread_create("uart_send", uart_send_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
if (thread_send != RT_NULL && thread_recv != RT_NULL)
{
rt_thread_startup(thread_recv);
rt_thread_startup(thread_send);
}
else
{
result = -RT_ERROR;
goto __exit;
}
while (1)
{
if (uart_result != RT_TRUE)
{
LOG_E("The test for uart dma is failure.");
result = -RT_ERROR;
goto __exit;
}
if (uart_over_flag == RT_TRUE)
{
goto __exit;
}
/* waiting for test over */
rt_thread_mdelay(5);
}
__exit:
if (tx_sem)
rt_sem_delete(tx_sem);
rt_device_close(&serial->parent);
return result;
}
static void tc_uart_api(void)
{
rt_uint32_t times = 0;
rt_uint16_t num = 0;
while (TC_UART_SEND_TIMES - times)
{
num = (rand() % 1000) + 1;
if(uart_api(num) == RT_EOK)
LOG_I("data_lens [%3d], it is correct to read and write data. [%d] times testing.", num, ++times);
else
{
LOG_E("uart test error");
break;
}
}
uassert_true(uart_over_flag == RT_TRUE);
}
static rt_err_t utest_tc_init(void)
{
LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
tx_sem = RT_NULL;
uart_result = RT_TRUE;
uart_over_flag = RT_FALSE;
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(tc_uart_api);
}
UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_rxb_txnb", utest_tc_init, utest_tc_cleanup, 30);
#endif

View File

@ -0,0 +1,266 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-16 KyleChan the first version
*/
#include <rtthread.h>
#include "utest.h"
#include <rtdevice.h>
#include <stdlib.h>
#define TC_UART_DEVICE_NAME "uart2"
#define TC_UART_SEND_TIMES 100
#ifdef UTEST_SERIAL_TC
#define TEST_UART_NAME TC_UART_DEVICE_NAME
static struct rt_serial_device *serial;
static rt_sem_t rx_sem;
static rt_uint8_t uart_over_flag;
static rt_bool_t uart_result = RT_TRUE;
static rt_err_t uart_find(void)
{
serial = (struct rt_serial_device *)rt_device_find(TEST_UART_NAME);
if (serial == RT_NULL)
{
LOG_E("find %s device failed!\n", TEST_UART_NAME);
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t uart_rx_indicate(rt_device_t device, rt_size_t size)
{
rt_sem_release(rx_sem);
return RT_EOK;
}
static void uart_send_entry(void *parameter)
{
rt_uint8_t *uart_write_buffer;
rt_uint16_t send_len;
rt_uint32_t i = 0;
send_len = *(rt_uint16_t *)parameter;
/* assign send buffer */
uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
if (uart_write_buffer == RT_NULL)
{
LOG_E("Without spare memory for uart dma!");
uart_result = RT_FALSE;
return;
}
rt_memset(uart_write_buffer, 0, send_len);
for (i = 0; i < send_len; i++)
{
uart_write_buffer[i] = (rt_uint8_t)i;
}
/* send buffer */
if (rt_device_write(&serial->parent, 0, uart_write_buffer, send_len) != send_len)
{
LOG_E("device write failed\r\n");
}
rt_free(uart_write_buffer);
}
static void uart_rec_entry(void *parameter)
{
rt_uint16_t rev_len;
rev_len = *(rt_uint16_t *)parameter;
rt_uint8_t *ch;
ch = (rt_uint8_t *)rt_calloc(1, sizeof(rt_uint8_t) * (rev_len + 1));
rt_int32_t cnt, i;
rt_uint8_t last_old_data;
rt_bool_t fisrt_flag = RT_TRUE;
rt_uint32_t all_receive_length = 0;
while (1)
{
rt_err_t result;
result = rt_sem_take(rx_sem, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
LOG_E("take sem err in recv.");
}
cnt = rt_device_read(&serial->parent, 0, (void *)ch, rev_len);
if (cnt == 0)
{
continue;
}
if (fisrt_flag != RT_TRUE)
{
if ((rt_uint8_t)(last_old_data + 1) != ch[0])
{
LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, ch[0]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
else
{
fisrt_flag = RT_FALSE;
}
for (i = 0; i < cnt - 1; i++)
{
if ((rt_uint8_t)(ch[i] + 1) != ch[i + 1])
{
LOG_E("Read Different data -> former data: %x, current data: %x.", ch[i], ch[i + 1]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
all_receive_length += cnt;
if (all_receive_length >= rev_len)
break;
else
last_old_data = ch[cnt - 1];
}
rt_free(ch);
uart_over_flag = RT_TRUE;
}
static rt_err_t uart_api(rt_uint16_t test_buf)
{
rt_thread_t thread_send = RT_NULL;
rt_thread_t thread_recv = RT_NULL;
rt_err_t result = RT_EOK;
result = uart_find();
if (result != RT_EOK)
{
return -RT_ERROR;
}
rx_sem = rt_sem_create("rx_sem", 0, RT_IPC_FLAG_PRIO);
if (rx_sem == RT_NULL)
{
LOG_E("Init sem failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
/* reinitialize */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_115200;
config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
if (result != RT_EOK)
{
LOG_E("Open uart device failed.");
uart_result = RT_FALSE;
rt_sem_delete(rx_sem);
return -RT_ERROR;
}
/* set receive callback function */
result = rt_device_set_rx_indicate(&serial->parent, uart_rx_indicate);
if (result != RT_EOK)
{
goto __exit;
}
thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
thread_send = rt_thread_create("uart_send", uart_send_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
if (thread_send != RT_NULL && thread_recv != RT_NULL)
{
rt_thread_startup(thread_recv);
rt_thread_startup(thread_send);
}
else
{
result = -RT_ERROR;
goto __exit;
}
while (1)
{
if (uart_result != RT_TRUE)
{
LOG_E("The test for uart dma is failure.");
result = -RT_ERROR;
goto __exit;
}
if (uart_over_flag == RT_TRUE)
{
goto __exit;
}
/* waiting for test over */
rt_thread_mdelay(5);
}
__exit:
if (rx_sem)
rt_sem_delete(rx_sem);
rt_device_close(&serial->parent);
uart_over_flag = RT_FALSE;
return result;
}
static void tc_uart_api(void)
{
rt_uint32_t times = 0;
rt_uint16_t num = 0;
while (TC_UART_SEND_TIMES - times)
{
num = (rand() % 1000) + 1;
if(uart_api(num) == RT_EOK)
LOG_I("data_lens [%3d], it is correct to read and write data. [%d] times testing.", num, ++times);
else
{
LOG_E("uart test error");
break;
}
}
uassert_true(uart_result == RT_TRUE);
}
static rt_err_t utest_tc_init(void)
{
LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
rx_sem = RT_NULL;
uart_result = RT_TRUE;
uart_over_flag = RT_FALSE;
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(tc_uart_api);
}
UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_rxnb_txb", utest_tc_init, utest_tc_cleanup, 30);
#endif /* TC_UART_USING_TC */

View File

@ -0,0 +1,294 @@
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-16 KyleChan the first version
*/
#include <rtthread.h>
#include "utest.h"
#include <rtdevice.h>
#include <stdlib.h>
#define TC_UART_DEVICE_NAME "uart2"
#define TC_UART_SEND_TIMES 100
#ifdef UTEST_SERIAL_TC
#define TEST_UART_NAME TC_UART_DEVICE_NAME
static struct rt_serial_device *serial;
static rt_sem_t tx_sem;
static rt_sem_t rx_sem;
static rt_uint8_t uart_over_flag;
static rt_bool_t uart_result = RT_TRUE;
static rt_err_t uart_find(void)
{
serial = (struct rt_serial_device *)rt_device_find(TEST_UART_NAME);
if (serial == RT_NULL)
{
LOG_E("find %s device failed!\n", TEST_UART_NAME);
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t uart_tx_completion(rt_device_t device, void *buffer)
{
rt_sem_release(tx_sem);
return RT_EOK;
}
static rt_err_t uart_rx_indicate(rt_device_t device, rt_size_t size)
{
rt_sem_release(rx_sem);
return RT_EOK;
}
static void uart_send_entry(void *parameter)
{
rt_uint8_t *uart_write_buffer;
rt_uint16_t send_len, len = 0;
rt_err_t result;
rt_uint32_t i = 0;
send_len = *(rt_uint16_t *)parameter;
/* assign send buffer */
uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
if (uart_write_buffer == RT_NULL)
{
LOG_E("Without spare memory for uart dma!");
uart_result = RT_FALSE;
return;
}
rt_memset(uart_write_buffer, 0, send_len);
for (i = 0; i < send_len; i++)
{
uart_write_buffer[i] = (rt_uint8_t)i;
}
/* send buffer */
while (send_len - len)
{
len += rt_device_write(&serial->parent, 0, uart_write_buffer + len, send_len - len);
result = rt_sem_take(tx_sem, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
LOG_E("take sem err in send.");
}
}
rt_free(uart_write_buffer);
}
static void uart_rec_entry(void *parameter)
{
rt_uint16_t rev_len;
rev_len = *(rt_uint16_t *)parameter;
rt_uint8_t *ch;
ch = (rt_uint8_t *)rt_calloc(1, sizeof(rt_uint8_t) * (rev_len + 1));
rt_int32_t cnt, i;
rt_uint8_t last_old_data;
rt_bool_t fisrt_flag = RT_TRUE;
rt_uint32_t all_receive_length = 0;
while (1)
{
rt_err_t result;
result = rt_sem_take(rx_sem, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
LOG_E("take sem err in recv.");
}
cnt = rt_device_read(&serial->parent, 0, (void *)ch, rev_len);
if (cnt == 0)
{
continue;
}
if (fisrt_flag != RT_TRUE)
{
if ((rt_uint8_t)(last_old_data + 1) != ch[0])
{
LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, ch[0]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
else
{
fisrt_flag = RT_FALSE;
}
for (i = 0; i < cnt - 1; i++)
{
if ((rt_uint8_t)(ch[i] + 1) != ch[i + 1])
{
LOG_E("Read Different data -> former data: %x, current data: %x.", ch[i], ch[i + 1]);
uart_result = RT_FALSE;
rt_free(ch);
return;
}
}
all_receive_length += cnt;
if (all_receive_length >= rev_len)
break;
else
last_old_data = ch[cnt - 1];
}
rt_free(ch);
uart_over_flag = RT_TRUE;
}
static rt_err_t uart_api(rt_uint16_t test_buf)
{
rt_thread_t thread_send = RT_NULL;
rt_thread_t thread_recv = RT_NULL;
rt_err_t result = RT_EOK;
uart_over_flag = RT_FALSE;
result = uart_find();
if (result != RT_EOK)
{
return -RT_ERROR;
}
rx_sem = rt_sem_create("rx_sem", 0, RT_IPC_FLAG_PRIO);
if (rx_sem == RT_NULL)
{
LOG_E("Init rx_sem failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
tx_sem = rt_sem_create("tx_sem", 0, RT_IPC_FLAG_PRIO);
if (tx_sem == RT_NULL)
{
LOG_E("Init tx_sem failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
/* reinitialize */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_115200;
config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING);
if (result != RT_EOK)
{
LOG_E("Open uart device failed.");
uart_result = RT_FALSE;
return -RT_ERROR;
}
/* set receive callback function */
result = rt_device_set_tx_complete(&serial->parent, uart_tx_completion);
if (result != RT_EOK)
{
goto __exit;
}
result = rt_device_set_rx_indicate(&serial->parent, uart_rx_indicate);
if (result != RT_EOK)
{
goto __exit;
}
thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
thread_send = rt_thread_create("uart_send", uart_send_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
if (thread_send != RT_NULL && thread_recv != RT_NULL)
{
rt_thread_startup(thread_recv);
rt_thread_startup(thread_send);
}
else
{
result = -RT_ERROR;
goto __exit;
}
while (1)
{
if (uart_result != RT_TRUE)
{
LOG_E("The test for uart dma is failure.");
result = -RT_ERROR;
goto __exit;
}
if (uart_over_flag == RT_TRUE)
{
goto __exit;
}
/* waiting for test over */
rt_thread_mdelay(5);
}
__exit:
if (tx_sem)
rt_sem_delete(tx_sem);
if (rx_sem)
rt_sem_delete(rx_sem);
rt_device_close(&serial->parent);
return result;
}
static void tc_uart_api(void)
{
rt_uint32_t times = 0;
rt_uint16_t num = 0;
while (TC_UART_SEND_TIMES - times)
{
num = (rand() % 1000) + 1;
if(uart_api(num) == RT_EOK)
LOG_I("data_lens [%3d], it is correct to read and write data. [%d] times testing.", num, ++times);
else
{
LOG_E("uart test error");
break;
}
}
uassert_true(uart_over_flag == RT_TRUE);
}
static rt_err_t utest_tc_init(void)
{
LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
tx_sem = RT_NULL;
uart_result = RT_TRUE;
uart_over_flag = RT_FALSE;
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(tc_uart_api);
}
UTEST_TC_EXPORT(testcase, "testcases.drivers.uart_rxnb_txnb", utest_tc_init, utest_tc_cleanup, 30);
#endif