From 921e6064260f7afa80f3d10424bd49526241a3ee Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 13 Jun 2021 17:36:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=B0=86defined(=5F=5FCC=5FARM)=20||=20def?= =?UTF-8?q?ined(=5F=5FCLANG=5FARM)=20=E6=94=B9=E4=B8=BA=20ifdef=20=5F=5FAR?= =?UTF-8?q?MCC=5FVERSION?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/finsh/shell.c | 2 +- include/libc/libc_signal.h | 2 +- include/rtlibc.h | 2 +- include/rtthread.h | 2 +- src/components.c | 4 ++-- src/kservice.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index a749660629..c89139c0ed 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -817,7 +817,7 @@ int finsh_system_init(void) rt_thread_t tid; #ifdef FINSH_USING_SYMTAB -#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */ +#ifdef __ARMCC_VERSION /* ARM C Compiler */ extern const int FSymTab$$Base; extern const int FSymTab$$Limit; extern const int VSymTab$$Base; diff --git a/include/libc/libc_signal.h b/include/libc/libc_signal.h index 8a4cc0d738..1fb8194643 100644 --- a/include/libc/libc_signal.h +++ b/include/libc/libc_signal.h @@ -71,7 +71,7 @@ typedef unsigned long sigset_t; #include -#if defined(__CC_ARM) || defined(__CLANG_ARM) +#ifdef __ARMCC_VERSION #define SIGHUP 1 /* #define SIGINT 2 */ diff --git a/include/rtlibc.h b/include/rtlibc.h index a62051e157..4e7caa81fc 100644 --- a/include/rtlibc.h +++ b/include/rtlibc.h @@ -22,7 +22,7 @@ #include "libc/libc_stdio.h" #ifndef RT_USING_LIBC -#if defined(__CC_ARM) || defined(__CLANG_ARM) || defined(__IAR_SYSTEMS_ICC__) +#if defined(__ARMCC_VERSION) || defined(__IAR_SYSTEMS_ICC__) typedef signed long off_t; typedef int mode_t; #endif diff --git a/include/rtthread.h b/include/rtthread.h index ccd2d147a8..cf897b0e5e 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -567,7 +567,7 @@ rt_size_t rt_strlen(const char *src); #endif /*RT_KSERVICE_USING_STDLIB*/ char *rt_strdup(const char *s); -#if defined(__CC_ARM) || defined(__CLANG_ARM) +#ifdef __ARMCC_VERSION /* lack strdup interface */ char* strdup(const char* str); #endif diff --git a/src/components.c b/src/components.c index d684d5e6fd..e156b783e4 100644 --- a/src/components.c +++ b/src/components.c @@ -134,7 +134,7 @@ void rt_application_init(void); void rt_hw_board_init(void); int rtthread_startup(void); -#if defined(__CC_ARM) || defined(__CLANG_ARM) +#ifdef __ARMCC_VERSION extern int $Super$$main(void); /* re-define main function */ int $Sub$$main(void) @@ -183,7 +183,7 @@ void main_thread_entry(void *parameter) rt_hw_secondary_cpu_up(); #endif /* RT_USING_SMP */ /* invoke system main function */ -#if defined(__CC_ARM) || defined(__CLANG_ARM) +#ifdef __ARMCC_VERSION { extern int $Super$$main(void); $Super$$main(); /* for ARMCC. */ diff --git a/src/kservice.c b/src/kservice.c index 448b2d717d..49e1486078 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -532,7 +532,7 @@ char *rt_strdup(const char *s) return tmp; } RTM_EXPORT(rt_strdup); -#if defined(__CC_ARM) || defined(__CLANG_ARM) +#ifdef __ARMCC_VERSION char *strdup(const char *s) __attribute__((alias("rt_strdup"))); #endif #endif /* RT_USING_HEAP */ From 5d4260ab6df4faa1d829e164ffb95aeabd9aa4e5 Mon Sep 17 00:00:00 2001 From: wanghaijing Date: Tue, 22 Jun 2021 23:54:54 +0800 Subject: [PATCH 2/4] [pm] index should be less than PM_MODLUE_MAX_ID --- components/drivers/pm/pm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index 80ed101498..cf488a9ce1 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -836,12 +836,10 @@ rt_uint32_t rt_pm_module_get_status(void) rt_uint32_t req_status = 0x00; pm = &_pm; - for (index = 0; index < 32; index ++) + for (index = 0; index < PM_MODULE_MAX_ID; index ++) { if (pm->module_status[index].req_status == 0x01) req_status |= 1<= PM_MODULE_MAX_ID) - break; } return req_status; From 312753686b7c64dfabf02d38ecca28c59ebc2810 Mon Sep 17 00:00:00 2001 From: chenjh Date: Thu, 17 Jun 2021 15:56:04 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[testcase]=20=E5=A2=9E=E5=8A=A0serial=5Fv2?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/utest/testcases/Kconfig | 1 + examples/utest/testcases/drivers/SConscript | 15 + .../utest/testcases/drivers/serial_v2/Kconfig | 7 + .../testcases/drivers/serial_v2/README.md | 120 +++++++ .../testcases/drivers/serial_v2/SConscript | 16 + .../drivers/serial_v2/uart_rxb_txb.c | 230 ++++++++++++++ .../drivers/serial_v2/uart_rxb_txnb.c | 263 ++++++++++++++++ .../drivers/serial_v2/uart_rxnb_txb.c | 266 ++++++++++++++++ .../drivers/serial_v2/uart_rxnb_txnb.c | 294 ++++++++++++++++++ 9 files changed, 1212 insertions(+) create mode 100644 examples/utest/testcases/drivers/SConscript create mode 100644 examples/utest/testcases/drivers/serial_v2/Kconfig create mode 100644 examples/utest/testcases/drivers/serial_v2/README.md create mode 100644 examples/utest/testcases/drivers/serial_v2/SConscript create mode 100644 examples/utest/testcases/drivers/serial_v2/uart_rxb_txb.c create mode 100644 examples/utest/testcases/drivers/serial_v2/uart_rxb_txnb.c create mode 100644 examples/utest/testcases/drivers/serial_v2/uart_rxnb_txb.c create mode 100644 examples/utest/testcases/drivers/serial_v2/uart_rxnb_txnb.c diff --git a/examples/utest/testcases/Kconfig b/examples/utest/testcases/Kconfig index a9b556f1e3..5dc1c6c420 100644 --- a/examples/utest/testcases/Kconfig +++ b/examples/utest/testcases/Kconfig @@ -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 diff --git a/examples/utest/testcases/drivers/SConscript b/examples/utest/testcases/drivers/SConscript new file mode 100644 index 0000000000..4c815c49b8 --- /dev/null +++ b/examples/utest/testcases/drivers/SConscript @@ -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') diff --git a/examples/utest/testcases/drivers/serial_v2/Kconfig b/examples/utest/testcases/drivers/serial_v2/Kconfig new file mode 100644 index 0000000000..3c8d480b3a --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/Kconfig @@ -0,0 +1,7 @@ +menu "Utest Serial Testcase" + +config UTEST_SERIAL_TC + bool "Serial testcase" + default n + +endmenu diff --git a/examples/utest/testcases/drivers/serial_v2/README.md b/examples/utest/testcases/drivers/serial_v2/README.md new file mode 100644 index 0000000000..38d71ce8e8 --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/README.md @@ -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和B,A为接收线程,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` 中对串口相应的宏进行配置,来选择不同的工作模式去进行测试。 + diff --git a/examples/utest/testcases/drivers/serial_v2/SConscript b/examples/utest/testcases/drivers/serial_v2/SConscript new file mode 100644 index 0000000000..a7a46e656a --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/SConscript @@ -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') diff --git a/examples/utest/testcases/drivers/serial_v2/uart_rxb_txb.c b/examples/utest/testcases/drivers/serial_v2/uart_rxb_txb.c new file mode 100644 index 0000000000..8b2205ee1a --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/uart_rxb_txb.c @@ -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 +#include "utest.h" +#include +#include + +#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 */ diff --git a/examples/utest/testcases/drivers/serial_v2/uart_rxb_txnb.c b/examples/utest/testcases/drivers/serial_v2/uart_rxb_txnb.c new file mode 100644 index 0000000000..f03a24fe13 --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/uart_rxb_txnb.c @@ -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 +#include "utest.h" +#include +#include + +#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 diff --git a/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txb.c b/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txb.c new file mode 100644 index 0000000000..c1c88ddc9f --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txb.c @@ -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 +#include "utest.h" +#include +#include + +#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 */ diff --git a/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txnb.c b/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txnb.c new file mode 100644 index 0000000000..ad8d20b8cf --- /dev/null +++ b/examples/utest/testcases/drivers/serial_v2/uart_rxnb_txnb.c @@ -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 +#include "utest.h" +#include +#include + +#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 From ec3c1d4d0085c57d996a5c6acc21e7dbde85b30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=BE=84?= <36236188+Eureka1024@users.noreply.github.com> Date: Wed, 23 Jun 2021 21:23:07 +0800 Subject: [PATCH 4/4] [kernel] Fix comment error for rt_mutex_detach(). --- src/ipc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ipc.c b/src/ipc.c index c981aabe43..959f0fb522 100755 --- a/src/ipc.c +++ b/src/ipc.c @@ -588,7 +588,7 @@ rt_err_t rt_mutex_detach(rt_mutex_t mutex) /* wakeup all suspended threads */ rt_ipc_list_resume_all(&(mutex->parent.suspend_thread)); - /* detach semaphore object */ + /* detach mutex object */ rt_object_detach(&(mutex->parent.parent)); return RT_EOK;