diff options
| author | Wayne Lin <[email protected]> | 2021-11-10 16:01:38 +0800 |
|---|---|---|
| committer | Wayne Lin <[email protected]> | 2021-11-10 16:01:38 +0800 |
| commit | 9b44e57a6022b8eb46eda7037b719ec918fdcc55 (patch) | |
| tree | 26be9facf0f37d1c38b9e4b6e4abc99d6f8b5a8d /examples | |
| parent | 37f5236dfa45b98d02e867536a10ac0b6622b80b (diff) | |
| parent | fdbaa2a808d12d522ad90702795c08ae8443aab9 (diff) | |
Merge branch 'master' of https://github.com/RT-Thread/rt-thread into nuvoton
Diffstat (limited to 'examples')
37 files changed, 4225 insertions, 76 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 06c746a642..9649347734 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -67,7 +67,6 @@ void list_dir(const char* path) #include <finsh.h> FINSH_FUNCTION_EXPORT(list_dir, list directory); -#ifdef FINSH_USING_MSH static void cmd_list_dir(int argc, char *argv[]) { char* filename; @@ -84,5 +83,4 @@ static void cmd_list_dir(int argc, char *argv[]) list_dir(filename); } MSH_CMD_EXPORT_ALIAS(cmd_list_dir, list_dir, list directory); -#endif /* FINSH_USING_MSH */ #endif /* RT_USING_FINSH */ diff --git a/examples/file/readspeed.c b/examples/file/readspeed.c index ffc19635cf..5f4cad3190 100644 --- a/examples/file/readspeed.c +++ b/examples/file/readspeed.c @@ -58,7 +58,6 @@ void readspeed(const char* filename, int block_size) #include <finsh.h> FINSH_FUNCTION_EXPORT(readspeed, perform file read test); -#ifdef FINSH_USING_MSH static void cmd_readspeed(int argc, char *argv[]) { char* filename; @@ -82,6 +81,5 @@ static void cmd_readspeed(int argc, char *argv[]) } readspeed(filename, block_size); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_readspeed, readspeed, test file system read speed); #endif /* RT_USING_FINSH */ diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 56f3b7e16c..d1dd5bc038 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -147,7 +147,6 @@ __exit: /* export to finsh */ FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); -#ifdef FINSH_USING_MSH static void cmd_readwrite(int argc, char *argv[]) { char* filename; @@ -163,6 +162,5 @@ static void cmd_readwrite(int argc, char *argv[]) } readwrite(filename); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test); #endif /* RT_USING_FINSH */ diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index 680761583c..f76027d28b 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -66,7 +66,6 @@ void writespeed(const char* filename, int total_length, int block_size) #include <finsh.h> FINSH_FUNCTION_EXPORT(writespeed, perform file write test); -#ifdef FINSH_USING_MSH static void cmd_writespeed(int argc, char *argv[]) { char* filename; @@ -93,6 +92,5 @@ static void cmd_writespeed(int argc, char *argv[]) } writespeed(filename, length, block_size); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_writespeed, __cmd_writespeed, test file system write speed); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_writespeed, writespeed, test file system write speed); #endif /* RT_USING_FINSH */ diff --git a/examples/kernel/event_simple.c b/examples/kernel/event_simple.c index d2a2948fc7..8b2f966d3d 100644 --- a/examples/kernel/event_simple.c +++ b/examples/kernel/event_simple.c @@ -83,7 +83,7 @@ static void thread3_entry(void *param) int event_simple_init() { /* 初始化事件对象 */ - rt_event_init(&event, "event", RT_IPC_FLAG_FIFO); + rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); /* 创建线程1 */ tid1 = rt_thread_create("t1", diff --git a/examples/kernel/mbox_send_wait.c b/examples/kernel/mbox_send_wait.c index 67f358ecea..919ceca31d 100644 --- a/examples/kernel/mbox_send_wait.c +++ b/examples/kernel/mbox_send_wait.c @@ -82,7 +82,7 @@ int mbox_send_wait_init() "mbt", /* 名称是mbt */ &mb_pool[0], /* 邮箱用到的内存池是mb_pool */ sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4,因为一封邮件的大小是4字节 */ - RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */ + RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */ /* 创建线程1 */ tid1 = rt_thread_create("t1", diff --git a/examples/kernel/mbox_simple.c b/examples/kernel/mbox_simple.c index 3f39bc0016..373c5f2817 100644 --- a/examples/kernel/mbox_simple.c +++ b/examples/kernel/mbox_simple.c @@ -80,7 +80,7 @@ int mbox_simple_init() "mbt", /* 名称是mbt */ &mb_pool[0], /* 邮箱用到的内存池是mb_pool */ sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4,因为一封邮件的大小是4字节 */ - RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */ + RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */ /* 创建线程1 */ tid1 = rt_thread_create("t1", diff --git a/examples/kernel/messageq_simple.c b/examples/kernel/messageq_simple.c index 62504bebb7..90bed7dd82 100644 --- a/examples/kernel/messageq_simple.c +++ b/examples/kernel/messageq_simple.c @@ -98,7 +98,7 @@ int messageq_simple_init() &msg_pool[0], /* 内存池指向msg_pool */ 128 - sizeof(void*), /* 每个消息的大小是 128 - void* */ sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */ - RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ + RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照优先级由高到低的方法分配消息 */ /* 创建线程1 */ tid1 = rt_thread_create("t1", diff --git a/examples/kernel/mutex_priority.c b/examples/kernel/mutex_priority.c index c1db934157..a7702eed6f 100755 --- a/examples/kernel/mutex_priority.c +++ b/examples/kernel/mutex_priority.c @@ -281,7 +281,7 @@ static void thread29_entry(void* parameter) static int mutex_simple_init() { /* 创建互斥锁 */ - mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); + mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); if (mutex == RT_NULL) { tc_stat(TC_STAT_END | TC_STAT_FAILED); diff --git a/examples/kernel/mutex_simple.c b/examples/kernel/mutex_simple.c index 4f6c5d7d28..ee875cca7d 100644 --- a/examples/kernel/mutex_simple.c +++ b/examples/kernel/mutex_simple.c @@ -87,7 +87,7 @@ static void thread3_entry(void* parameter) int mutex_simple_init() { /* 创建互斥锁 */ - mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); + mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); if (mutex == RT_NULL) { tc_stat(TC_STAT_END | TC_STAT_FAILED); diff --git a/examples/kernel/semaphore_buffer_worker.c b/examples/kernel/semaphore_buffer_worker.c index 5e4723b417..bd5a488460 100644 --- a/examples/kernel/semaphore_buffer_worker.c +++ b/examples/kernel/semaphore_buffer_worker.c @@ -209,7 +209,7 @@ int semaphore_buffer_worker_init() rb_init(&working_rb, working_buffer, BUFFER_SIZE); /* 创建信号量 */ - sem = rt_sem_create("sem", 1, RT_IPC_FLAG_FIFO); + sem = rt_sem_create("sem", 1, RT_IPC_FLAG_PRIO); if (sem == RT_NULL) { tc_stat(TC_STAT_END | TC_STAT_FAILED); diff --git a/examples/kernel/semaphore_dynamic.c b/examples/kernel/semaphore_dynamic.c index 564d73c496..dacdf10c37 100644 --- a/examples/kernel/semaphore_dynamic.c +++ b/examples/kernel/semaphore_dynamic.c @@ -78,7 +78,7 @@ static void thread_entry(void* parameter) int semaphore_dynamic_init() { /* 创建一个信号量,初始值是0 */ - sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO); + sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO); if (sem == RT_NULL) { tc_stat(TC_STAT_END | TC_STAT_FAILED); diff --git a/examples/kernel/semaphore_producer_consumer.c b/examples/kernel/semaphore_producer_consumer.c index b6f996ff57..4d9eb24e44 100644 --- a/examples/kernel/semaphore_producer_consumer.c +++ b/examples/kernel/semaphore_producer_consumer.c @@ -98,9 +98,9 @@ void consumer_thread_entry(void* parameter) int semaphore_producer_consumer_init() { /* 初始化3个信号量 */ - rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO); - rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO); - rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO); + rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_PRIO); + rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_PRIO); + rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_PRIO); /* 创建线程1 */ producer_tid = rt_thread_create("producer", diff --git a/examples/kernel/semaphore_static.c b/examples/kernel/semaphore_static.c index 641b0bc7c9..16b043db9c 100644 --- a/examples/kernel/semaphore_static.c +++ b/examples/kernel/semaphore_static.c @@ -81,7 +81,7 @@ int semaphore_static_init(void) rt_err_t result; /* 初始化信号量,初始值是0 */ - result = rt_sem_init(&sem, "sem", 0, RT_IPC_FLAG_FIFO); + result = rt_sem_init(&sem, "sem", 0, RT_IPC_FLAG_PRIO); if (result != RT_EOK) { tc_stat(TC_STAT_END | TC_STAT_FAILED); diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c index 7153412aa0..0a43ca3855 100644 --- a/examples/kernel/tc_comm.c +++ b/examples/kernel/tc_comm.c @@ -25,7 +25,6 @@ static const char* _tc_current; static void (*_tc_cleanup)(void) = RT_NULL; static rt_uint32_t _tc_scale = 1; -FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale) static rt_uint32_t _tc_loop; @@ -35,7 +34,7 @@ void tc_thread_entry(void* parameter) struct finsh_syscall* index; /* create tc semaphore */ - rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO); + rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_PRIO); do { for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index)) diff --git a/examples/kernel/timer_timeout.c b/examples/kernel/timer_timeout.c index 40bbbf2831..f72b45be3c 100644 --- a/examples/kernel/timer_timeout.c +++ b/examples/kernel/timer_timeout.c @@ -73,7 +73,7 @@ int timer_timeout_init() &msg_pool[0], /* 内存池指向msg_pool */ 128 - sizeof(void*), /* 每个消息的大小是 128 - void* */ sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */ - RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ + RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照优先级由高到低的方法分配消息 */ /* 创建线程 */ tid = rt_thread_create("t", diff --git a/examples/libc/termios_test.c b/examples/libc/termios_test.c index f65bff1a9f..855bc82b96 100644 --- a/examples/libc/termios_test.c +++ b/examples/libc/termios_test.c @@ -16,7 +16,7 @@ #if defined (RT_USING_POSIX) #include <dfs_posix.h> - #include <dfs_select.h> + #include <sys/select.h> #if defined (RT_USING_POSIX_TERMIOS) #include <termios.h> #else @@ -356,8 +356,8 @@ int termios_test(int argc, char **argv) #ifdef RT_USING_FINSH #include <finsh.h> - #ifdef FINSH_USING_MSH + #ifdef RT_USING_FINSH MSH_CMD_EXPORT_ALIAS(termios_test, termtest, e.g: termtest /dev/uart4 115200); - #endif /* FINSH_USING_MSH */ + #endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH */ diff --git a/examples/pm/wakeup_app.c b/examples/pm/wakeup_app.c index d287bf74ec..230d30ac4e 100644 --- a/examples/pm/wakeup_app.c +++ b/examples/pm/wakeup_app.c @@ -61,7 +61,7 @@ static int wakeup_app(void) { rt_thread_t tid; - wakeup_event = rt_event_create("wakup", RT_IPC_FLAG_FIFO); + wakeup_event = rt_event_create("wakup", RT_IPC_FLAG_PRIO); RT_ASSERT(wakeup_event != RT_NULL); tid = rt_thread_create("wakeup_app", wakeup_app_entry, RT_NULL, diff --git a/examples/rt-link/rtlink_dev_example.c b/examples/rt-link/rtlink_dev_example.c new file mode 100644 index 0000000000..523b67ba53 --- /dev/null +++ b/examples/rt-link/rtlink_dev_example.c @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-19 Sherman the first version + */ + +#include <string.h> +#include <stdlib.h> + +#define DBG_TAG "example" +#define DBG_LVL DBG_LOG +#include <rtdbg.h> +#include <rtlink_dev.h> + +enum +{ + NONE_TEST = 0, + SHORT_FRAME_TEST, + LONG_FRAME_TEST +}; +static rt_uint8_t speed_test_type = NONE_TEST; + +static struct rt_link_device rtlink_dev = {0}; +#define RTLINK01 "rtlink01" +#define TEST_CONTEXT "This message is sent by RT-Link" +rt_uint8_t test_buff[1024] = {0}; + +static rt_err_t rtlink_dev_rx_ind(rt_device_t dev, rt_size_t size) +{ + RT_ASSERT(dev != RT_NULL); + LOG_I("rx_ind: dev name %s, rx size %d", dev->parent.name, size); + return RT_EOK; +} + +static rt_err_t rtlink_dev_tx_done(rt_device_t dev, void *buffer) +{ + RT_ASSERT(dev != RT_NULL); + struct rt_link_device *rtlink_dev = (struct rt_link_device *)dev; + LOG_I("tx_done: dev name %s, buffer 0x%p errno %d", dev->parent.name, buffer, rtlink_dev->service.err); + rt_free(buffer); + return RT_EOK; +} + +#ifdef RT_USING_POSIX +#include <dfs_posix.h> +#include <poll.h> +#include <sys/select.h> + +#define RTLINK01_PATH "/dev/rtlink01" +int fd = -1; + +static void rtlink_fopen(int argc, char *argv[]) +{ + fd = open(RTLINK01_PATH, O_RDWR | O_NONBLOCK); + + if (fd < 0) + { + LOG_E("open rt_link failed!"); + } +} +MSH_CMD_EXPORT(rtlink_fopen, rtlink posix interface example); + +static void rtlink_fclose(int argc, char *argv[]) +{ + LOG_D("colse %d", fd); + close(fd); + fd = -1; +} +MSH_CMD_EXPORT(rtlink_fclose, rtlink posix interface example); + +static void rtlink_fread(int argc, char *argv[]) +{ + int read_len; + read_len = read(fd, test_buff, sizeof(test_buff)); + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); +} +MSH_CMD_EXPORT(rtlink_fread, rtlink posix interface example); + +static void rtlink_fwrite(int argc, char *argv[]) +{ + char *data = RT_NULL; + rt_size_t length = 0; + rt_uint16_t count = 0; + rt_size_t ret = 0; + + if (argc == 1) + { + data = rt_malloc(sizeof(TEST_CONTEXT)); + if (data) + { + length = sizeof(TEST_CONTEXT) - 1; + rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1); + ret = write(fd, data, length); + } + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else if (argc >= 3) + { + if (strcmp(argv[1], "-l") == 0) + { + data = rt_malloc(atoi((const char *)argv[2])); + if (data) + { + for (count = 0; count < atoi((const char *)argv[2]); count++) + { + data[count] = (count % 93 + 33); + } + length = atoi((const char *)argv[2]); + ret = write(fd, data, length); + } + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else + { + LOG_E("Invalid parameter."); + } + } +} +MSH_CMD_EXPORT(rtlink_fwrite, rtlink posix interface example); + +#define RTLINK02 "rtlink02" +#define RTLINK02_PATH "/dev/rtlink02" +static struct rt_link_device rtlink_fd = {0}; +rt_uint8_t fd_buff[1024] = {0}; + +static void listen_thread(void *param) +{ + int fd = open(RTLINK02_PATH, O_RDWR | O_NONBLOCK); + if (fd < 0) + { + LOG_E("open (%s) failed", RTLINK02); + return; + } + while (1) + { + rt_uint8_t *write_buf = RT_NULL; + int write_len = 0; + fd_set readfds, writefds; + FD_ZERO(&readfds); + FD_ZERO(&writefds); + FD_SET(fd, &readfds); + FD_SET(fd, &writefds); + + int ret = select(fd + 1, &readfds, &writefds, RT_NULL, RT_NULL); + LOG_D("select ret(%d), read (%d), write (%d)", ret, readfds, writefds); + if (FD_ISSET(fd, &readfds)) + { + LOG_I("POLLIN"); + int read_len = read(fd, fd_buff, sizeof(test_buff)); + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); + } + + if (FD_ISSET(fd, &writefds)) + { + LOG_I("POLLOUT"); + write_buf = rt_malloc(1024); + if (write_buf) + { + write_len = write(fd, write_buf, 1024); + LOG_D("write %d", write_len); + } + } + + rt_thread_delay(500); + } + LOG_I("fd (%s) listen thread exit", RTLINK02); +} + +static void rtlink_fselect() +{ + /* step1: register rtlink to to the device framework */ + rt_link_dev_register(&rtlink_fd, RTLINK02, + RT_DEVICE_FLAG_RDWR | + RT_DEVICE_FLAG_REMOVABLE | + RT_DEVICE_FLAG_STANDALONE, + RT_NULL); + + /* step2: Initialize the rlink device as the default configuration, */ + rt_device_t device = rt_device_find(RTLINK02); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_device_init(device); + + /* step3: config rtlink device rx/tx callback, channel, send timeout */ + rt_device_set_rx_indicate(device, rtlink_dev_rx_ind); + rt_device_set_tx_complete(device, rtlink_dev_tx_done); + struct rt_link_service service; + service.service = RT_LINK_SERVICE_MNGT; + service.timeout_tx = RT_WAITING_NO; + rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service); + + rt_thread_t tid = rt_thread_create(RTLINK02, listen_thread, RT_NULL, 1024, 21, 20); + if (tid) + { + rt_thread_startup(tid); + } +} +MSH_CMD_EXPORT(rtlink_fselect, rtlink posix interface example); +#endif /* RT_USING_POSIX */ + +static void rtlink_dread(void) +{ + rt_size_t read_len = 0; + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("dev %s not find ", RTLINK01); + return; + } + + read_len = rt_device_read(dev, 0, test_buff, sizeof(test_buff)); + + LOG_D("read len %d", read_len); + LOG_HEX("read", 8, test_buff, 32); +} +MSH_CMD_EXPORT(rtlink_dread, rtlink device interface example); + +void rt_link_speed_test(void *paremeter) +{ + int ret; + rt_uint8_t *send_buf, *data; + rt_size_t bufflen = 0; + rt_size_t sentlen = 0; + rt_size_t count = 0; + rt_tick_t tick1, tick2; + rt_size_t total = 0; + rt_uint32_t integer, decimal; + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("dev %s not find!", RTLINK01); + return ; + } + + if (speed_test_type == SHORT_FRAME_TEST) + { + bufflen = 988; + } + else + { + bufflen = 3036; + } + + send_buf = rt_malloc(bufflen); + if (send_buf != RT_NULL) + { + data = send_buf; + for (count = 0; count < bufflen; count++) + { + *data++ = (count % 93 + 33); + } + } + else + { + rt_kprintf("speed of send buffer malloc failed."); + return; + } + + tick1 = rt_tick_get(); + while (speed_test_type) + { + ret = rt_device_write(dev, 0, send_buf, bufflen); + + if (ret == RT_EOK) + { + sentlen += bufflen; + } + + tick2 = rt_tick_get(); + if (tick2 - tick1 >= RT_TICK_PER_SECOND) + { + total = sentlen * RT_TICK_PER_SECOND / 125 / (tick2 - tick1); + integer = total / 1000; + decimal = total % 1000; + LOG_I("%d.%03d0 Mbps!", integer, decimal); + sentlen = 0; + tick1 = tick2; + } + } + rt_free(send_buf); + LOG_W("speed test end, type %d", speed_test_type); +} + +void create_thead_to_test_speed(rt_uint8_t mutil_num) +{ + rt_uint8_t i = 0; + + LOG_D("Speed test type [%02d], mutil [%02d]", speed_test_type, mutil_num); + for (i = 0; i < mutil_num; i++) + { + rt_thread_t tid; + char tid_name[RT_NAME_MAX + 1] = {0}; + + rt_snprintf(tid_name, sizeof(tid_name), "lny_s%03d", i + 1); + tid = rt_thread_create(tid_name, rt_link_speed_test, RT_NULL, 1024, 20, 10); + rt_thread_startup(tid); + LOG_I("Speed test thread[%s] startup", tid_name); + } +} + +static void rtlink_dwrite(int argc, char *argv[]) +{ + char *data = RT_NULL; + rt_size_t length = 0; + rt_uint16_t count = 0; + rt_size_t ret = RT_ERROR; + + rt_device_t dev = rt_device_find(RTLINK01); + if (dev == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + + if (argc == 1) + { + data = rt_malloc(sizeof(TEST_CONTEXT)); + length = sizeof(TEST_CONTEXT) - 1; + rt_memcpy(data, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1); + + ret = rt_device_write(dev, 0, data, length); + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else if (argc >= 3) + { + if (strcmp(argv[1], "-l") == 0) + { + data = rt_malloc(atoi((const char *)argv[2])); + for (count = 0; count < atoi((const char *)argv[2]); count++) + { + data[count] = (count % 93 + 33); + } + length = atoi((const char *)argv[2]); + ret = rt_device_write(dev, 0, data, length); + LOG_I("write data(0x%p) result: %d.", data, ret); + } + else + { + LOG_E("Invalid parameter."); + } + } +} +MSH_CMD_EXPORT(rtlink_dwrite, rtlink device interface example); + +static void rtlink_dinit(void) +{ + /* step1: register rtlink to to the device framework */ + rt_link_dev_register(&rtlink_dev, RTLINK01, + RT_DEVICE_FLAG_RDWR | + RT_DEVICE_FLAG_REMOVABLE | + RT_DEVICE_FLAG_STANDALONE, + RT_NULL); + + /* step2: Initialize the rlink device as the default configuration, */ + rt_device_t device = rt_device_find(RTLINK01); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_device_init(device); + + /* step3: config rtlink device rx/tx callback, channel, send timeout */ + rt_device_set_rx_indicate(device, rtlink_dev_rx_ind); + rt_device_set_tx_complete(device, rtlink_dev_tx_done); + struct rt_link_service service; + service.service = RT_LINK_SERVICE_SOCKET; + service.timeout_tx = RT_WAITING_FOREVER; + service.flag = RT_LINK_FLAG_ACK | RT_LINK_FLAG_CRC; + rt_device_control(device, RT_DEVICE_CTRL_CONFIG, &service); +} +MSH_CMD_EXPORT(rtlink_dinit, rtlink device interface example); + +static void rtlink_dopen() +{ + /* step4: open rtlink device, attach the service channel */ + rt_device_t device = rt_device_find(RTLINK01); + if (device == RT_NULL) + { + LOG_E("device not find!"); + return ; + } + rt_err_t ret = rt_device_open(device, RT_DEVICE_OFLAG_RDWR); + LOG_I("dev open ret %d", ret); +} +MSH_CMD_EXPORT(rtlink_dopen, rtlink device interface example); diff --git a/examples/rt-link/rtlink_example.c b/examples/rt-link/rtlink_example.c index 841e56b17e..77d488b786 100644 --- a/examples/rt-link/rtlink_example.c +++ b/examples/rt-link/rtlink_example.c @@ -1,4 +1,3 @@ - /* * Copyright (c) 2006-2021, RT-Thread Development Team * @@ -7,6 +6,7 @@ * Change Logs: * Date Author Notes * 2021-05-15 Sherman the first version + * 2021-08-04 Sherman Adapted to new version of rt-link API */ #include <stdlib.h> @@ -30,33 +30,25 @@ enum void rt_link_speed_test(void *paremeter); static rt_uint8_t speed_test_type = NONE_TEST; +static struct rt_link_service serv_socket; +static struct rt_link_service serv_wifi; -rt_err_t rt_link_receive_example_callback(void *data, rt_size_t length) +static void send_cb(struct rt_link_service *service, void *buffer) { - LOG_I("recv data %d",length); - LOG_HEX("example",8,data,length); - rt_free(data); - return RT_EOK; + LOG_I("send_cb: service (%d) buffer (0x%p) err(%d)", service->service, buffer, service->err); } -void create_thead_to_test_speed(rt_uint8_t mutil_num) +static void recv_cb(struct rt_link_service *service, void *data, rt_size_t size) { - rt_uint8_t i = 0; + LOG_I("service (%d) size (%d) data(0x%p)", service->service, size, data); - LOG_D("Speed test type [%02d], mutil [%02d]", speed_test_type, mutil_num); - for(i = 0; i< mutil_num; i++) + if (size) { - rt_thread_t tid; - char tid_name[RT_NAME_MAX + 1] = {0}; - - rt_snprintf(tid_name, sizeof(tid_name), "lny_s%03d", i + 1); - tid = rt_thread_create(tid_name, rt_link_speed_test, RT_NULL, 1024, 20, 10); - rt_thread_startup(tid); - LOG_I("Speed test thread[%s] startup", tid_name); + rt_free(data); } } -void rt_link_speed_test(void *paremeter) +static void rt_link_speed_test(void *paremeter) { int ret; rt_uint8_t *send_buf, *data; @@ -67,20 +59,20 @@ void rt_link_speed_test(void *paremeter) rt_size_t total = 0; rt_uint32_t integer, decimal; - if(speed_test_type == SHORT_FRAME_TEST) + if (speed_test_type == SHORT_FRAME_TEST) { - bufflen = 2044; + bufflen = RT_LINK_MAX_DATA_LENGTH; } else { - bufflen = 6132; + bufflen = RT_LINK_MAX_DATA_LENGTH * RT_LINK_FRAMES_MAX; } send_buf = rt_malloc(bufflen); - if(send_buf != RT_NULL) + if (send_buf != RT_NULL) { data = send_buf; - for(count = 0;count < bufflen; count++) + for (count = 0; count < bufflen; count++) { *data++ = (count % 93 + 33); } @@ -94,70 +86,94 @@ void rt_link_speed_test(void *paremeter) tick1 = rt_tick_get(); while (speed_test_type) { - ret = rt_link_send(RT_LINK_SERVICE_RTLINK, send_buf, bufflen); - if(ret > 0) + ret = rt_link_send(&serv_socket, send_buf, bufflen); + if (ret > 0) { sentlen += ret; } + else + { + LOG_W("send err %d", ret); + } tick2 = rt_tick_get(); - if (tick2 - tick1 >= RT_TICK_PER_SECOND)//* 10 + if (tick2 - tick1 >= RT_TICK_PER_SECOND) { total = sentlen * RT_TICK_PER_SECOND / 125 / (tick2 - tick1); - integer = total/1000; - decimal = total%1000; + integer = total / 1000; + decimal = total % 1000; LOG_I("%d.%03d0 Mbps!", integer, decimal); sentlen = 0; tick1 = tick2; } } rt_free(send_buf); - LOG_W("speed test end, type %d",speed_test_type); + LOG_W("speed test end, type %d", speed_test_type); +} + +static void create_thead_to_test_speed(rt_uint8_t mutil_num) +{ + rt_uint8_t i = 0; + + LOG_D("Speed test type [%02d], mutil [%02d]", speed_test_type, mutil_num); + for (i = 0; i < mutil_num; i++) + { + rt_thread_t tid; + char tid_name[RT_NAME_MAX + 1] = {0}; + + rt_snprintf(tid_name, sizeof(tid_name), "lny_s%03d", i + 1); + tid = rt_thread_create(tid_name, rt_link_speed_test, RT_NULL, 1024, 20, 10); + if (tid) + { + rt_thread_startup(tid); + LOG_I("Speed test thread[%s] startup", tid_name); + } + } } -int rt_link_example_send(int argc, char **argv) +static int rtlink_exsend(int argc, char **argv) { char *receive = RT_NULL; rt_size_t length = 0; rt_uint16_t count = 0; - if(argc == 1) + if (argc == 1) { receive = rt_malloc(sizeof(TEST_CONTEXT)); rt_memcpy(receive, TEST_CONTEXT, sizeof(TEST_CONTEXT) - 1); - length = rt_link_send(RT_LINK_SERVICE_RTLINK, receive, sizeof(TEST_CONTEXT) - 1); + length = rt_link_send(&serv_socket, receive, sizeof(TEST_CONTEXT) - 1); LOG_I("send data length: %d.", length); rt_free(receive); } - else if(argc >= 3) + else if (argc >= 3) { - if(strcmp(argv[1], "-l") == 0) + if (strcmp(argv[1], "-l") == 0) { receive = rt_malloc(atoi((const char *)argv[2])); - for(count = 0;count < atoi((const char *)argv[2]); count++) + for (count = 0; count < atoi((const char *)argv[2]); count++) { *receive++ = (count % 93 + 33); } - length = rt_link_send(RT_LINK_SERVICE_RTLINK, receive - atoi((const char *)argv[2]), atoi((const char *)argv[2])); + length = rt_link_send(&serv_socket, receive - atoi((const char *)argv[2]), atoi((const char *)argv[2])); rt_free(receive - atoi((const char *)argv[2])); LOG_I("send data length: %d.", length); } - else if(strcmp(argv[1], "-s") == 0) + else if (strcmp(argv[1], "-s") == 0) { - if(speed_test_type == NONE_TEST) + if (speed_test_type == NONE_TEST) { rt_uint8_t mutil_num = 1; - if(argc > 3) + if (argc > 3) { mutil_num = atoi((const char *)argv[3]); } - if(strncmp(argv[2], "-s", rt_strlen(argv[2])) == 0) + if (strncmp(argv[2], "-s", rt_strlen(argv[2])) == 0) { speed_test_type = SHORT_FRAME_TEST; } - else if(strncmp(argv[2], "-l", rt_strlen(argv[2])) == 0) + else if (strncmp(argv[2], "-l", rt_strlen(argv[2])) == 0) { speed_test_type = LONG_FRAME_TEST; } @@ -176,12 +192,23 @@ int rt_link_example_send(int argc, char **argv) } return 0; } -MSH_CMD_EXPORT(rt_link_example_send, rt link layer send test); +MSH_CMD_EXPORT(rtlink_exsend, rt link layer send test); -int rt_link_example_init(void) +int rtlink_exinit(void) { + serv_socket.service = RT_LINK_SERVICE_SOCKET; + serv_socket.timeout_tx = RT_WAITING_FOREVER; + serv_socket.flag = RT_LINK_FLAG_ACK | RT_LINK_FLAG_CRC; + serv_socket.recv_cb = recv_cb; + serv_socket.send_cb = send_cb; + rt_link_service_attach(&serv_socket); - rt_link_service_attach(RT_LINK_SERVICE_RTLINK, rt_link_receive_example_callback); + serv_wifi.service = RT_LINK_SERVICE_WIFI; + serv_wifi.timeout_tx = RT_WAITING_FOREVER; + serv_wifi.flag = RT_NULL; + serv_wifi.recv_cb = recv_cb; + serv_wifi.send_cb = send_cb; + rt_link_service_attach(&serv_wifi); return RT_EOK; } -MSH_CMD_EXPORT(rt_link_example_init, rt link layer example init); +MSH_CMD_EXPORT(rtlink_exinit, rt link example init); diff --git a/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index 15d7aa830f..08b0c5acaf 100644 --- a/examples/test/hwtimer_test.c +++ b/examples/test/hwtimer_test.c @@ -105,7 +105,7 @@ EXIT: return err; } -#ifdef FINSH_USING_MSH +#ifdef RT_USING_FINSH MSH_CMD_EXPORT(hwtimer, "Test hardware timer"); #endif #endif /* RT_USING_HWTIMER */ diff --git a/examples/utest/configs/kernel/ipc.conf b/examples/utest/configs/kernel/ipc.conf new file mode 100644 index 0000000000..8af98d607a --- /dev/null +++ b/examples/utest/configs/kernel/ipc.conf @@ -0,0 +1,13 @@ +CONFIG_UTEST_SEMAPHORE_TC=y +CONFIG_UTEST_EVENT_TC=y +CONFIG_UTEST_MESSAGEQUEUE_TC=y +CONFIG_UTEST_SIGNAL_TC=y +CONFIG_UTEST_MUTEX_TC=y +CONFIG_UTEST_MAILBOX_TC=y +# dependencies +CONFIG_RT_USING_SEMAPHORE=y +CONFIG_RT_USING_EVENT=y +CONFIG_RT_USING_MESSAGEQUEUE=y +CONFIG_RT_USING_SIGNALS=y +CONFIG_RT_USING_MUTEX=y +CONFIG_RT_USING_MAILBOX=y diff --git a/examples/utest/configs/kernel/irq.conf b/examples/utest/configs/kernel/irq.conf new file mode 100644 index 0000000000..83fe485638 --- /dev/null +++ b/examples/utest/configs/kernel/irq.conf @@ -0,0 +1,2 @@ +CONFIG_UTEST_IRQ_TC=y +# dependencies diff --git a/examples/utest/configs/kernel/thread.conf b/examples/utest/configs/kernel/thread.conf new file mode 100644 index 0000000000..9b3234f0d7 --- /dev/null +++ b/examples/utest/configs/kernel/thread.conf @@ -0,0 +1,4 @@ +CONFIG_UTEST_THREAD_TC=y +# dependencies +CONFIG_RT_USING_TIMER_SOFT=y +CONFIG_RT_USING_THREAD=y diff --git a/examples/utest/configs/kernel/timer.conf b/examples/utest/configs/kernel/timer.conf new file mode 100644 index 0000000000..20696f065f --- /dev/null +++ b/examples/utest/configs/kernel/timer.conf @@ -0,0 +1,4 @@ +CONFIG_UTEST_TIMER_TC=y + +# dependencies +CONFIG_RT_USING_TIMER_SOFT=y diff --git a/examples/utest/testcases/kernel/Kconfig b/examples/utest/testcases/kernel/Kconfig index 14334af16f..08c3022b77 100644 --- a/examples/utest/testcases/kernel/Kconfig +++ b/examples/utest/testcases/kernel/Kconfig @@ -5,4 +5,44 @@ config UTEST_MEMHEAP_TC default y depends on RT_USING_MEMHEAP +config UTEST_IRQ_TC + bool "IRQ test" + default n + +config UTEST_SEMAPHORE_TC + bool "semaphore test" + default n + depends on RT_USING_SEMAPHORE + +config UTEST_EVENT_TC + bool "event test" + default n + depends on RT_USING_EVENT + +config UTEST_TIMER_TC + bool "timer test" + default n + +config UTEST_MESSAGEQUEUE_TC + bool "message queue test" + default n + +config UTEST_SIGNAL_TC + bool "signal test" + default n + +config UTEST_MUTEX_TC + bool "mutex test" + default n + +config UTEST_MAILBOX_TC + bool "mailbox test" + default n + +config UTEST_THREAD_TC + bool "thread test" + default n + select RT_USING_TIMER_SOFT + select RT_USING_THREAD + endmenu diff --git a/examples/utest/testcases/kernel/SConscript b/examples/utest/testcases/kernel/SConscript index 9411cdd9a0..0d2645427b 100644 --- a/examples/utest/testcases/kernel/SConscript +++ b/examples/utest/testcases/kernel/SConscript @@ -8,6 +8,33 @@ src = Split(''' if GetDepend(['UTEST_MEMHEAP_TC']): src += ['memheap_tc.c'] +if GetDepend(['UTEST_IRQ_TC']): + src += ['irq_tc.c'] + +if GetDepend(['UTEST_SEMAPHORE_TC']): + src += ['semaphore_tc.c'] + +if GetDepend(['UTEST_EVENT_TC']): + src += ['event_tc.c'] + +if GetDepend(['UTEST_TIMER_TC']): + src += ['timer_tc.c'] + +if GetDepend(['UTEST_MESSAGEQUEUE_TC']): + src += ['messagequeue_tc.c'] + +if GetDepend(['UTEST_SIGNAL_TC']): + src += ['signal_tc.c'] + +if GetDepend(['UTEST_MUTEX_TC']): + src += ['mutex_tc.c'] + +if GetDepend(['UTEST_MAILBOX_TC']): + src += ['mailbox_tc.c'] + +if GetDepend(['UTEST_THREAD_TC']): + src += ['thread_tc.c'] + CPPPATH = [cwd] group = DefineGroup('utestcases', src, depend = [], CPPPATH = CPPPATH) diff --git a/examples/utest/testcases/kernel/event_tc.c b/examples/utest/testcases/kernel/event_tc.c new file mode 100644 index 0000000000..9847529214 --- /dev/null +++ b/examples/utest/testcases/kernel/event_tc.c @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-15 liukang the first version + */ + +#include <rtthread.h> +#include "utest.h" +#include <stdlib.h> + +#define EVENT_FLAG3 (1 << 3) +#define EVENT_FLAG5 (1 << 5) + +static struct rt_event static_event = {0}; +#ifdef RT_USING_HEAP +static rt_event_t dynamic_event = RT_NULL; +static rt_uint32_t dynamic_event_recv_thread_finish = 0, dynamic_event_send_thread_finish = 0; + +ALIGN(RT_ALIGN_SIZE) +static char thread3_stack[1024]; +static struct rt_thread thread3; + +ALIGN(RT_ALIGN_SIZE) +static char thread4_stack[1024]; +static struct rt_thread thread4; +#endif /* RT_USING_HEAP */ + +static rt_uint32_t recv_event_times1 = 0, recv_event_times2 = 0; +static rt_uint32_t static_event_recv_thread_finish = 0, static_event_send_thread_finish = 0; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; + +#define THREAD_PRIORITY 9 +#define THREAD_TIMESLICE 5 + +static void test_event_init(void) +{ + rt_err_t result; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_event_detach(void) +{ + rt_err_t result = RT_EOK; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread1_recv_static_event(void *param) +{ + rt_uint32_t e; + + if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + + recv_event_times1 = e; + + rt_thread_mdelay(50); + + if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + recv_event_times2 = e; + + static_event_recv_thread_finish = 1; +} + +static void thread2_send_static_event(void *param) +{ + rt_event_send(&static_event, EVENT_FLAG3); + rt_thread_mdelay(10); + + rt_event_send(&static_event, EVENT_FLAG5); + rt_thread_mdelay(10); + + rt_event_send(&static_event, EVENT_FLAG3); + + static_event_send_thread_finish = 1; +} + + +static void test_static_event_send_recv(void) +{ + rt_err_t result = RT_EOK; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + + rt_thread_init(&thread1, + "thread1", + thread1_recv_static_event, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + thread2_send_static_event, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + while (static_event_recv_thread_finish != 1 || static_event_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5)) + { + if (rt_event_detach(&static_event) != RT_EOK) + { + uassert_false(1); + } + uassert_true(1); + } + else + { + if (rt_event_detach(&static_event) != RT_EOK) + { + uassert_false(1); + } + uassert_false(1); + } + + return; +} + +#ifdef RT_USING_HEAP +static void test_event_create(void) +{ + rt_err_t result = RT_EOK; + + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + result = rt_event_delete(dynamic_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_event_delete(void) +{ + rt_err_t result; + + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + result = rt_event_delete(dynamic_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread3_recv_dynamic_event(void *param) +{ + rt_uint32_t e; + + if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + + recv_event_times1 = e; + + rt_thread_mdelay(50); + + if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + recv_event_times2 = e; + + dynamic_event_recv_thread_finish = 1; +} + +static void thread4_send_dynamic_event(void *param) +{ + rt_event_send(dynamic_event, EVENT_FLAG3); + rt_thread_mdelay(10); + + rt_event_send(dynamic_event, EVENT_FLAG5); + rt_thread_mdelay(10); + + rt_event_send(dynamic_event, EVENT_FLAG3); + + dynamic_event_send_thread_finish = 1; +} + +static void test_dynamic_event_send_recv(void) +{ + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_PRIO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + rt_thread_init(&thread3, + "thread3", + thread3_recv_dynamic_event, + RT_NULL, + &thread3_stack[0], + sizeof(thread3_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread3); + + rt_thread_init(&thread4, + "thread4", + thread4_send_dynamic_event, + RT_NULL, + &thread4_stack[0], + sizeof(thread4_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread4); + + while (dynamic_event_recv_thread_finish != 1 || dynamic_event_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5)) + { + if (rt_event_delete(dynamic_event) != RT_EOK) + { + uassert_false(1); + } + uassert_true(1); + } + else + { + if (rt_event_delete(dynamic_event) != RT_EOK) + { + uassert_false(1); + } + uassert_false(1); + } + + return; +} +#endif + +static rt_err_t utest_tc_init(void) +{ + static_event_recv_thread_finish = 0; + static_event_send_thread_finish = 0; +#ifdef RT_USING_HEAP + dynamic_event_recv_thread_finish = 0; + dynamic_event_send_thread_finish = 0; +#endif + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_event_init); + UTEST_UNIT_RUN(test_event_detach); + UTEST_UNIT_RUN(test_static_event_send_recv); +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_event_create); + UTEST_UNIT_RUN(test_event_delete); + UTEST_UNIT_RUN(test_dynamic_event_send_recv); +#endif +} +UTEST_TC_EXPORT(testcase, "src.ipc.event_tc", utest_tc_init, utest_tc_cleanup, 60); diff --git a/examples/utest/testcases/kernel/irq_tc.c b/examples/utest/testcases/kernel/irq_tc.c new file mode 100644 index 0000000000..86b6c144f1 --- /dev/null +++ b/examples/utest/testcases/kernel/irq_tc.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-15 supperthomas add irq_test + */ + +#include <rtthread.h> +#include "utest.h" +#include "rthw.h" + +#define UTEST_NAME "irq_tc" +static uint32_t irq_count = 0; +static uint32_t max_get_nest_count = 0; + +static void irq_callback() +{ + if(rt_interrupt_get_nest() > max_get_nest_count) + { + max_get_nest_count = rt_interrupt_get_nest(); + } + irq_count ++; +} + +static void irq_test(void) +{ + irq_count = 0; + rt_interrupt_enter_sethook(irq_callback); + rt_interrupt_leave_sethook(irq_callback); + rt_thread_mdelay(2); + LOG_D("%s test irq_test! irq_count %d max_get_nest_count %d\n", UTEST_NAME, irq_count, max_get_nest_count); + uassert_int_not_equal(0, irq_count); + uassert_int_not_equal(0, max_get_nest_count); + rt_interrupt_enter_sethook(RT_NULL); + rt_interrupt_leave_sethook(RT_NULL); + LOG_D("irq_test OK!\n"); +} + +static rt_err_t utest_tc_init(void) +{ + irq_count = 0; + max_get_nest_count = 0; + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void interrupt_test(void) +{ + rt_base_t level; + uint32_t i = 1000; + + rt_interrupt_enter_sethook(irq_callback); + rt_interrupt_leave_sethook(irq_callback); + irq_count = 0; + level = rt_hw_interrupt_disable(); + while(i) + { + i --; + } + uassert_int_equal(0, irq_count); + rt_hw_interrupt_enable(level); + rt_interrupt_enter_sethook(RT_NULL); + rt_interrupt_leave_sethook(RT_NULL); + +} +static void testcase(void) +{ + UTEST_UNIT_RUN(irq_test); + UTEST_UNIT_RUN(interrupt_test); +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.irq_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/examples/utest/testcases/kernel/mailbox_tc.c b/examples/utest/testcases/kernel/mailbox_tc.c new file mode 100644 index 0000000000..e18613b559 --- /dev/null +++ b/examples/utest/testcases/kernel/mailbox_tc.c @@ -0,0 +1,371 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09-08 liukang the first version + */ + +#include <rtthread.h> +#include "utest.h" +#include <stdlib.h> + +static struct rt_mailbox test_static_mb; +static char mb_pool[128]; + +static rt_mailbox_t test_dynamic_mb; + +static uint8_t static_mb_recv_thread_finish, static_mb_send_thread_finish; +static uint8_t dynamic_mb_recv_thread_finish, dynamic_mb_send_thread_finish; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; + +#define THREAD_PRIORITY 9 +#define THREAD_TIMESLICE 5 + +static rt_thread_t mb_send = RT_NULL; +static rt_thread_t mb_recv = RT_NULL; + +static rt_uint8_t mb_send_str1[] = "this is first mail!"; +static rt_uint8_t mb_send_str2[] = "this is second mail!"; +static rt_uint8_t mb_send_str3[] = "this is thirdy mail!"; + +static rt_uint8_t *mb_recv_str1; +static rt_uint8_t *mb_recv_str2; +static rt_uint8_t *mb_recv_str3; + +static void test_mailbox_init(void) +{ + rt_err_t result; + + result = rt_mb_init(&test_static_mb, "mbt", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_mb_detach(&test_static_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_mb_init(&test_static_mb, "mbt", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_mb_detach(&test_static_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_mailbox_deatch(void) +{ + rt_err_t result; + + result = rt_mb_init(&test_static_mb, "mbt", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_mb_detach(&test_static_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_mb_init(&test_static_mb, "mbt", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_mb_detach(&test_static_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_mailbox_create(void) +{ + rt_err_t result; + + test_dynamic_mb = rt_mb_create("test_dynamic_mb", sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (test_dynamic_mb == RT_NULL) + { + uassert_false(1); + } + result = rt_mb_delete(test_dynamic_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + test_dynamic_mb = rt_mb_create("test_dynamic_mb", sizeof(mb_pool) / 4, RT_IPC_FLAG_PRIO); + if (test_dynamic_mb == RT_NULL) + { + uassert_false(1); + } + result = rt_mb_delete(test_dynamic_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_mailbox_delete(void) +{ + rt_err_t result; + + test_dynamic_mb = rt_mb_create("test_dynamic_mb", sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (test_dynamic_mb == RT_NULL) + { + uassert_false(1); + } + result = rt_mb_delete(test_dynamic_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + test_dynamic_mb = rt_mb_create("test_dynamic_mb", sizeof(mb_pool) / 4, RT_IPC_FLAG_PRIO); + if (test_dynamic_mb == RT_NULL) + { + uassert_false(1); + } + result = rt_mb_delete(test_dynamic_mb); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread2_send_static_mb(void *arg) +{ + rt_err_t res = RT_EOK; + + res = rt_mb_send(&test_static_mb, (rt_ubase_t)&mb_send_str1); + if (res != RT_EOK) + { + uassert_false(1); + } + rt_thread_mdelay(100); + + res = rt_mb_send_wait(&test_static_mb, (rt_ubase_t)&mb_send_str2, 10); + if (res != RT_EOK) + { + uassert_false(1); + } + rt_thread_mdelay(100); + + res = rt_mb_urgent(&test_static_mb, (rt_ubase_t)&mb_send_str3); + if (res != RT_EOK) + { + uassert_false(1); + } + + static_mb_send_thread_finish = 1; +} + +static void thread1_recv_static_mb(void *arg) +{ + rt_err_t result = RT_EOK; + + result = rt_mb_recv(&test_static_mb, (rt_ubase_t *)&mb_recv_str1, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str1, mb_send_str1) != 0) + { + uassert_false(1); + } + + result = rt_mb_recv(&test_static_mb, (rt_ubase_t *)&mb_recv_str2, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str2, mb_send_str2) != 0) + { + uassert_false(1); + } + + result = rt_mb_recv(&test_static_mb, (rt_ubase_t *)&mb_recv_str3, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str3, mb_send_str3) != 0) + { + uassert_false(1); + } + + static_mb_recv_thread_finish = 1; +} + +static void test_static_mailbox_send_recv(void) +{ + rt_err_t result; + + result = rt_mb_init(&test_static_mb, "mbt", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + uassert_false(1); + } + + rt_thread_init(&thread1, + "thread1", + thread1_recv_static_mb, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + thread2_send_static_mb, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + while (static_mb_recv_thread_finish != 1 || static_mb_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (rt_mb_detach(&test_static_mb) != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread4_send_dynamic_mb(void *arg) +{ + rt_err_t res = RT_EOK; + + res = rt_mb_send(test_dynamic_mb, (rt_ubase_t)&mb_send_str1); + if (res != RT_EOK) + { + uassert_false(1); + } + rt_thread_mdelay(100); + + res = rt_mb_send_wait(test_dynamic_mb, (rt_ubase_t)&mb_send_str2, 10); + if (res != RT_EOK) + { + uassert_false(1); + } + rt_thread_mdelay(100); + + res = rt_mb_urgent(test_dynamic_mb, (rt_ubase_t)&mb_send_str3); + if (res != RT_EOK) + { + uassert_false(1); + } + + dynamic_mb_send_thread_finish = 1; +} + +static void thread3_recv_dynamic_mb(void *arg) +{ + rt_err_t result = RT_EOK; + + result = rt_mb_recv(test_dynamic_mb, (rt_ubase_t *)&mb_recv_str1, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str1, mb_send_str1) != 0) + { + uassert_false(1); + } + + result = rt_mb_recv(test_dynamic_mb, (rt_ubase_t *)&mb_recv_str2, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str2, mb_send_str2) != 0) + { + uassert_false(1); + } + + result = rt_mb_recv(test_dynamic_mb, (rt_ubase_t *)&mb_recv_str3, RT_WAITING_FOREVER); + if (result != RT_EOK || strcmp(mb_recv_str3, mb_send_str3) != 0) + { + uassert_false(1); + } + + dynamic_mb_recv_thread_finish = 1; +} + +static void test_dynamic_mailbox_send_recv(void) +{ + test_dynamic_mb = rt_mb_create("mbt", sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO); + if (test_dynamic_mb == RT_NULL) + { + uassert_false(1); + } + + mb_recv = rt_thread_create("mb_recv_thread", + thread3_recv_dynamic_mb, + RT_NULL, + 1024, + THREAD_PRIORITY - 1, + THREAD_TIMESLICE); + if (mb_recv == RT_NULL) + { + uassert_false(1); + } + rt_thread_startup(mb_recv); + + mb_send = rt_thread_create("mb_send_thread", + thread4_send_dynamic_mb, + RT_NULL, + 1024, + THREAD_PRIORITY - 1, + THREAD_TIMESLICE); + if (mb_send == RT_NULL) + { + uassert_false(1); + } + rt_thread_startup(mb_send); + + while (dynamic_mb_recv_thread_finish != 1 || dynamic_mb_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (rt_mb_delete(test_dynamic_mb) != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_mailbox_init); + UTEST_UNIT_RUN(test_mailbox_deatch); + UTEST_UNIT_RUN(test_mailbox_create); + UTEST_UNIT_RUN(test_mailbox_delete); + UTEST_UNIT_RUN(test_static_mailbox_send_recv); + UTEST_UNIT_RUN(test_dynamic_mailbox_send_recv); +} +UTEST_TC_EXPORT(testcase, "src.ipc.mailbox_tc", utest_tc_init, utest_tc_cleanup, 60); diff --git a/examples/utest/testcases/kernel/messagequeue_tc.c b/examples/utest/testcases/kernel/messagequeue_tc.c new file mode 100644 index 0000000000..335c591ab6 --- /dev/null +++ b/examples/utest/testcases/kernel/messagequeue_tc.c @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-28 Sherman the first version + */ + +#include <rtthread.h> +#include "utest.h" + +#define MSG_SIZE 4 +#define MAX_MSGS 5 + +static struct rt_messagequeue static_mq; +static rt_uint8_t mq_buf[(MSG_SIZE + 4) * MAX_MSGS]; + +static struct rt_thread mq_send_thread; +static struct rt_thread mq_recv_thread; +static rt_uint8_t mq_send_stack[512]; +static rt_uint8_t mq_recv_stack[512]; + +static struct rt_event finish_e; +#define MQSEND_FINISH 0x01 +#define MQRECV_FINIHS 0x02 + +#ifdef RT_USING_HEAP +static rt_mq_t dynamic_mq; +#endif /* RT_USING_HEAP */ + +static void test_mq_init(void) +{ + rt_err_t ret; + ret = rt_mq_init(&static_mq,"testmq1", mq_buf, MSG_SIZE, sizeof(mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); +} + +static void test_mq_create(void) +{ +#ifdef RT_USING_HEAP + dynamic_mq = rt_mq_create("testmq2", MSG_SIZE, MAX_MSGS, RT_IPC_FLAG_FIFO); + uassert_true(dynamic_mq != RT_NULL); +#endif /* RT_USING_HEAP */ +} + +static void mq_send_case(rt_mq_t testmq) +{ + rt_uint32_t send_buf[MAX_MSGS+1] = {0}; + rt_err_t ret = RT_EOK; + + for (int var = 0; var < MAX_MSGS; ++var) + { + send_buf[var] = var + 1; + ret = rt_mq_send_wait(testmq, &send_buf[var], sizeof(send_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + } + send_buf[MAX_MSGS] = MAX_MSGS + 1; + ret = rt_mq_send(testmq, &send_buf[MAX_MSGS], sizeof(send_buf[0])); + uassert_true(ret == -RT_EFULL); + + ret = rt_mq_send_wait(testmq, &send_buf[MAX_MSGS], sizeof(send_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + + while (testmq->entry != 0) + { + rt_thread_delay(100); + } + + ret = rt_mq_send(testmq, &send_buf[1], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + ret = rt_mq_send(testmq, &send_buf[2], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + ret = rt_mq_urgent(testmq, &send_buf[0], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + while (testmq->entry != 0) + { + rt_thread_delay(100); + } + + ret = rt_mq_send(testmq, &send_buf[1], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + ret = rt_mq_control(testmq, RT_IPC_CMD_RESET, RT_NULL); + uassert_true(ret == RT_EOK); + uassert_true(testmq->entry == 0); +} + +static void mq_send_entry(void *param) +{ + mq_send_case(&static_mq); + +#ifdef RT_USING_HEAP + if(dynamic_mq != RT_NULL) + { + mq_send_case(dynamic_mq); + } +#endif /* RT_USING_HEAP */ + + rt_event_send(&finish_e, MQSEND_FINISH); +} + +static void mq_recv_case(rt_mq_t testmq) +{ + rt_uint32_t recv_buf[MAX_MSGS+1] = {0}; + rt_err_t ret = RT_EOK; + + for (int var = 0; var < MAX_MSGS + 1; ++var) + { + ret = rt_mq_recv(testmq, &recv_buf[var], sizeof(recv_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + uassert_true(recv_buf[var] == (var + 1)); + } + + for (int var = 0; var < 3; ++var) + { + ret = rt_mq_recv(testmq, &recv_buf[var], sizeof(recv_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + uassert_true(recv_buf[var] == (var + 1)); + } +} + +static void mq_recv_entry(void *param) +{ + mq_recv_case(&static_mq); + +#ifdef RT_USING_HEAP + if(dynamic_mq != RT_NULL) + { + mq_recv_case(dynamic_mq); + } +#endif /* RT_USING_HEAP */ + rt_event_send(&finish_e, MQRECV_FINIHS); +} + +static void test_mq_testcase(void) +{ + rt_thread_startup(&mq_send_thread); + rt_thread_startup(&mq_recv_thread); + + rt_event_recv(&finish_e, MQSEND_FINISH | MQRECV_FINIHS, RT_EVENT_FLAG_AND, RT_WAITING_FOREVER, RT_NULL); +} + +static void test_mq_detach(void) +{ + rt_err_t ret = rt_mq_detach(&static_mq); + uassert_true(ret == RT_EOK); +} + +static void test_mq_delete(void) +{ +#ifdef RT_USING_HEAP + rt_err_t ret = rt_mq_delete(dynamic_mq); + uassert_true(ret == RT_EOK); +#endif /* RT_USING_HEAP */ +} + +static rt_err_t utest_tc_init(void) +{ + rt_err_t ret ; + ret = rt_thread_init(&mq_send_thread, "mq_send", mq_send_entry, RT_NULL, mq_send_stack, sizeof(mq_send_stack), 22, 20); + if(ret != RT_EOK) + return -RT_ERROR; + + ret = rt_thread_init(&mq_recv_thread, "mq_recv", mq_recv_entry, RT_NULL, mq_recv_stack, sizeof(mq_recv_stack), 23, 20); + if(ret != RT_EOK) + return -RT_ERROR; + + ret = rt_event_init(&finish_e, "finish", RT_IPC_FLAG_FIFO); + if(ret != RT_EOK) + return -RT_ERROR; + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_mq_init); + UTEST_UNIT_RUN(test_mq_create); + UTEST_UNIT_RUN(test_mq_testcase); + UTEST_UNIT_RUN(test_mq_detach); + UTEST_UNIT_RUN(test_mq_delete); +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.messagequeue_tc", utest_tc_init, utest_tc_cleanup, 1000); diff --git a/examples/utest/testcases/kernel/mutex_tc.c b/examples/utest/testcases/kernel/mutex_tc.c new file mode 100644 index 0000000000..f50e3e43f7 --- /dev/null +++ b/examples/utest/testcases/kernel/mutex_tc.c @@ -0,0 +1,676 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09.01 luckyzjq the first version + */ + +#include <rtthread.h> +#include <stdlib.h> +#include "utest.h" + +static struct rt_mutex static_mutex; + +#ifdef RT_USING_HEAP +static rt_mutex_t dynamic_mutex; +#endif /* RT_USING_HEAP */ + +/* init test */ +static void test_static_mutex_init(void) +{ + rt_err_t result = -RT_ERROR; + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_FIFO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + uassert_true(RT_TRUE); +} + +/* static take test */ +static void static_mutex_take_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + int rand_num = rand() % 0x1000; + mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, rand_num); + if (RT_EOK == result) + { + uassert_true(RT_FALSE); + } +} +static void test_static_mutex_take(void) +{ + rt_err_t result; + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex and not release */ + result = rt_mutex_take(&static_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + static_mutex_take_entry, + &static_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread take second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* static release test */ +static void static_mutex_release_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + int rand_num = rand() % 0x1000; + mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, rand_num); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} +static void test_static_mutex_release(void) +{ + rt_err_t result; + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex */ + result = rt_mutex_take(&static_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + /* release mutex */ + result = rt_mutex_release(&static_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + static_mutex_release_entry, + &static_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread and take mutex second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* static trytake test */ +static void static_mutex_trytake_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + mutex = (rt_mutex_t)param; + + result = rt_mutex_trytake(mutex); + if (RT_EOK == result) + { + uassert_true(RT_FALSE); + } +} +static void test_static_mutex_trytake(void) +{ + rt_err_t result; + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex and not release */ + result = rt_mutex_take(&static_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + static_mutex_trytake_entry, + &static_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread and trytake mutex second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +static rt_thread_t tid1 = RT_NULL; +static rt_thread_t tid2 = RT_NULL; +static rt_thread_t tid3 = RT_NULL; + +/* static mutex priority reverse test */ +static void static_thread1_entry(void *param) +{ + /* let system schedule */ + rt_thread_mdelay(100); + + /* thread3 hode mutex thread2 take mutex */ + /* check thread2 and thread3 priority */ + if (tid2->current_priority != tid3->current_priority) + { + uassert_true(RT_FALSE); + } + else + { + uassert_true(RT_TRUE); + } +} + +static void static_thread2_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex = (rt_mutex_t)param; + + /* let system schedule */ + rt_thread_mdelay(50); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result == RT_EOK) + { + rt_mutex_release(mutex); + } +} +static void static_thread3_entry(void *param) +{ + rt_tick_t tick; + rt_err_t result; + rt_mutex_t mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + uassert_true(RT_FALSE); + } + + tick = rt_tick_get(); + while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)); + + rt_mutex_release(mutex); +} + +static void test_static_pri_reverse(void) +{ + rt_err_t result; + tid1 = RT_NULL; + tid2 = RT_NULL; + tid3 = RT_NULL; + + result = rt_mutex_init(&static_mutex, "static_mutex", RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* thread1 */ + tid1 = rt_thread_create("thread1", + static_thread1_entry, + &static_mutex, + 1024, + 10 - 1, + 10); + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + /* thread2 */ + tid2 = rt_thread_create("thread2", + static_thread2_entry, + &static_mutex, + 1024, + 10, + 10); + if (tid2 != RT_NULL) + rt_thread_startup(tid2); + + /* thread3 */ + tid3 = rt_thread_create("thread3", + static_thread3_entry, + &static_mutex, + 1024, + 10 + 1, + 10); + if (tid3 != RT_NULL) + rt_thread_startup(tid3); + + rt_thread_mdelay(1000); + + result = rt_mutex_detach(&static_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* create test */ +static void test_dynamic_mutex_create(void) +{ + rt_err_t result = -RT_ERROR; + + /* PRIO mode */ + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_PRIO); + if (RT_NULL == result) + { + uassert_true(RT_FALSE); + } + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* FIFO mode */ + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_FIFO); + if (RT_NULL == dynamic_mutex) + { + uassert_true(RT_FALSE); + } + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + uassert_true(RT_TRUE); +} + +/* dynamic take test */ +static void dynamic_mutex_take_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + int rand_num = rand() % 0x1000; + mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, rand_num); + if (RT_EOK == result) + { + uassert_true(RT_FALSE); + } +} +static void test_dynamic_mutex_take(void) +{ + rt_err_t result; + + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_mutex) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex and not release */ + result = rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + dynamic_mutex_take_entry, + dynamic_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread take second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* dynamic release test */ +static void dynamic_mutex_release_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + int rand_num = rand() % 0x1000; + mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, rand_num); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} +static void test_dynamic_mutex_release(void) +{ + rt_err_t result; + + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_mutex) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex */ + result = rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + /* release mutex */ + result = rt_mutex_release(dynamic_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + dynamic_mutex_release_entry, + dynamic_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread and take mutex second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* dynamic trytake test */ +static void dynamic_mutex_trytake_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex; + + mutex = (rt_mutex_t)param; + + result = rt_mutex_trytake(mutex); + if (RT_EOK == result) + { + uassert_true(RT_FALSE); + } +} +static void test_dynamic_mutex_trytake(void) +{ + rt_err_t result; + + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_mutex) + { + uassert_true(RT_FALSE); + return; + } + + /* take mutex and not release */ + result = rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + rt_thread_t tid = rt_thread_create("mutex_th", + dynamic_mutex_trytake_entry, + dynamic_mutex, + 2048, + 10, + 10); + if (RT_NULL == tid) + { + uassert_true(RT_FALSE); + return; + } + + /* startup thread and trytake mutex second */ + rt_thread_startup(tid); + + /* let system schedule */ + rt_thread_mdelay(5); + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +/* dynamic mutex priority reverse test */ +static void dynamic_thread1_entry(void *param) +{ + /* let system schedule */ + rt_thread_mdelay(100); + + /* thread3 hode mutex thread2 take mutex */ + /* check thread2 and thread3 priority */ + if (tid2->current_priority != tid3->current_priority) + { + uassert_true(RT_FALSE); + } + else + { + uassert_true(RT_TRUE); + } +} + +static void dynamic_thread2_entry(void *param) +{ + rt_err_t result; + rt_mutex_t mutex = (rt_mutex_t)param; + + /* let system schedule */ + rt_thread_mdelay(50); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result == RT_EOK) + { + rt_mutex_release(mutex); + } +} +static void dynamic_thread3_entry(void *param) +{ + rt_tick_t tick; + rt_err_t result; + rt_mutex_t mutex = (rt_mutex_t)param; + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + uassert_true(RT_FALSE); + } + + tick = rt_tick_get(); + while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)); + + rt_mutex_release(mutex); +} + +static void test_dynamic_pri_reverse(void) +{ + rt_err_t result; + tid1 = RT_NULL; + tid2 = RT_NULL; + tid3 = RT_NULL; + + dynamic_mutex = rt_mutex_create("dynamic_mutex", RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_mutex) + { + uassert_true(RT_FALSE); + return; + } + + /* thread1 */ + tid1 = rt_thread_create("thread1", + dynamic_thread1_entry, + dynamic_mutex, + 1024, + 10 - 1, + 10); + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + /* thread2 */ + tid2 = rt_thread_create("thread2", + dynamic_thread2_entry, + dynamic_mutex, + 1024, + 10, + 10); + if (tid2 != RT_NULL) + rt_thread_startup(tid2); + + /* thread3 */ + tid3 = rt_thread_create("thread3", + dynamic_thread3_entry, + dynamic_mutex, + 1024, + 10 + 1, + 10); + if (tid3 != RT_NULL) + rt_thread_startup(tid3); + + rt_thread_mdelay(1000); + + result = rt_mutex_delete(dynamic_mutex); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + uassert_true(RT_TRUE); +} + +static rt_err_t utest_tc_init(void) +{ +#ifdef RT_USING_HEAP + dynamic_mutex = RT_NULL; +#endif /* RT_USING_HEAP */ + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ +#ifdef RT_USING_HEAP + dynamic_mutex = RT_NULL; +#endif /* RT_USING_HEAP */ + + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_static_mutex_init); + UTEST_UNIT_RUN(test_static_mutex_take); + UTEST_UNIT_RUN(test_static_mutex_release); + UTEST_UNIT_RUN(test_static_mutex_trytake); + UTEST_UNIT_RUN(test_static_pri_reverse); +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_dynamic_mutex_create); + UTEST_UNIT_RUN(test_dynamic_mutex_take); + UTEST_UNIT_RUN(test_dynamic_mutex_release); + UTEST_UNIT_RUN(test_dynamic_mutex_trytake); + UTEST_UNIT_RUN(test_dynamic_pri_reverse); +#endif +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.mutex_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/********************* end of file ************************/ diff --git a/examples/utest/testcases/kernel/semaphore_tc.c b/examples/utest/testcases/kernel/semaphore_tc.c new file mode 100644 index 0000000000..2992831d87 --- /dev/null +++ b/examples/utest/testcases/kernel/semaphore_tc.c @@ -0,0 +1,559 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-12 luckyzjq the first version + */ + +#include <rtthread.h> +#include <stdlib.h> +#include "utest.h" + +static struct rt_semaphore static_semaphore; +#ifdef RT_USING_HEAP +static rt_sem_t dynamic_semaphore; +#endif /* RT_USING_HEAP */ + +static void test_static_semaphore_init(void) +{ + rt_err_t result; + int rand_num = rand() % 0x10000; + + for (int i = 0; i < rand_num; i++) + { + result = rt_sem_init(&static_semaphore, "static_sem", i, RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + rt_sem_detach(&static_semaphore); + + result = rt_sem_init(&static_semaphore, "static_sem", i, RT_IPC_FLAG_FIFO); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + rt_sem_detach(&static_semaphore); + } + + uassert_true(RT_TRUE); +} + +static void test_static_semaphore_detach(void) +{ + rt_err_t result; + int rand_num = rand() % 0x10000; + + for (int i = 0; i < rand_num; i++) + { + result = rt_sem_init(&static_semaphore, "static_sem", i, RT_IPC_FLAG_PRIO); + if (RT_EOK != result) + { + break; + } + + result = rt_sem_detach(&static_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + + result = rt_sem_init(&static_semaphore, "static_sem", i, RT_IPC_FLAG_FIFO); + if (RT_EOK != result) + { + break; + } + result = rt_sem_detach(&static_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + } + + uassert_true(RT_TRUE); +} + +static void test_static_semaphore_take(void) +{ + rt_err_t result; + result = rt_sem_init(&static_semaphore, "static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_EOK == result) + { + /* first take */ + result = rt_sem_take(&static_semaphore, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + /* second take */ + result = rt_sem_take(&static_semaphore, 100); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_detach(&static_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_static_semaphore_trytake(void) +{ + rt_err_t result; + result = rt_sem_init(&static_semaphore, "static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_EOK == result) + { + /* first take */ + result = rt_sem_trytake(&static_semaphore); + if (RT_EOK != result) + uassert_true(RT_FALSE); + /* second take */ + result = rt_sem_trytake(&static_semaphore); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_detach(&static_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_static_semaphore_release(void) +{ + rt_err_t result; + result = rt_sem_init(&static_semaphore, "static_sem", 0, RT_IPC_FLAG_PRIO); + if (RT_EOK == result) + { + /* first take */ + result = rt_sem_take(&static_semaphore, 100); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + + /* release */ + result = rt_sem_release(&static_semaphore); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + /* second take */ + result = rt_sem_take(&static_semaphore, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_detach(&static_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_static_semaphore_control(void) +{ + rt_err_t result; + int value = 0; + + value = rand() % 100; + result = rt_sem_init(&static_semaphore, "static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_EOK == result) + { + result = rt_sem_control(&static_semaphore, RT_IPC_CMD_RESET, &value); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + else + { + return; + } + + for (int i = 0; i < value; i++) + { + result = rt_sem_take(&static_semaphore, 10); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + + rt_sem_detach(&static_semaphore); + uassert_true(RT_TRUE); +} + +static void static_release_isr_hardware_callback(void *param) +{ + rt_err_t result; + + result = rt_sem_release(&static_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} + +static void static_release_isr_software_callback(void *param) +{ + rt_err_t result; + + result = rt_sem_release(&static_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} + +static void test_static_semaphore_release_isr(void) +{ + rt_err_t result; + rt_timer_t hardware_timer; + rt_timer_t software_timer; + + /* create timer */ + hardware_timer = rt_timer_create("release_isr", + static_release_isr_hardware_callback, + RT_NULL, + 100, + RT_TIMER_FLAG_HARD_TIMER | RT_TIMER_FLAG_ONE_SHOT); + software_timer = rt_timer_create("release_isr", + static_release_isr_software_callback, + RT_NULL, + 100, + RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); + /* start tiemr */ + if (hardware_timer) + rt_timer_start(hardware_timer); + if (software_timer) + rt_timer_start(software_timer); + + result = rt_sem_init(&static_semaphore, "static_sem", 0, RT_IPC_FLAG_PRIO); + if (RT_EOK == result) + { + for (int i = 0; i < 2; i++) + { + result = rt_sem_take(&static_semaphore, 1000); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + } + else + { + return; + } + + rt_sem_detach(&static_semaphore); + rt_timer_delete(hardware_timer); + rt_timer_delete(software_timer); + + uassert_true(RT_TRUE); +} + +#ifdef RT_USING_HEAP +static void test_dynamic_semaphore_create(void) +{ + rt_err_t result; + int rand_num = rand() % 0x10000; + + for (int i = 0; i < rand_num; i++) + { + dynamic_semaphore = rt_sem_create("static_sem", i, RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_semaphore) + { + uassert_true(RT_FALSE); + break; + } + rt_sem_delete(dynamic_semaphore); + + dynamic_semaphore = rt_sem_create("static_sem", i, RT_IPC_FLAG_FIFO); + if (RT_NULL == dynamic_semaphore) + { + uassert_true(RT_FALSE); + break; + } + rt_sem_delete(dynamic_semaphore); + } + + uassert_true(RT_TRUE); +} + +static void test_dynamic_semaphore_delete(void) +{ + rt_err_t result; + int rand_num = rand() % 0x10000; + + for (int i = 0; i < rand_num; i++) + { + dynamic_semaphore = rt_sem_create("static_sem", i, RT_IPC_FLAG_PRIO); + if (RT_NULL == dynamic_semaphore) + { + break; + } + + result = rt_sem_delete(dynamic_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + + dynamic_semaphore = rt_sem_create("static_sem", i, RT_IPC_FLAG_FIFO); + if (RT_NULL == dynamic_semaphore) + { + break; + } + result = rt_sem_delete(dynamic_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + break; + } + } + + uassert_true(RT_TRUE); +} + +static void test_dynamic_semaphore_take(void) +{ + rt_err_t result; + dynamic_semaphore = rt_sem_create("static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_NULL != dynamic_semaphore) + { + /* first take */ + result = rt_sem_take(dynamic_semaphore, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + /* second take */ + result = rt_sem_take(dynamic_semaphore, 100); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_delete(dynamic_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_dynamic_semaphore_trytake(void) +{ + rt_err_t result; + dynamic_semaphore = rt_sem_create("static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_NULL != dynamic_semaphore) + { + /* first take */ + result = rt_sem_trytake(dynamic_semaphore); + if (RT_EOK != result) + uassert_true(RT_FALSE); + /* second take */ + result = rt_sem_trytake(dynamic_semaphore); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_delete(dynamic_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_dynamic_semaphore_release(void) +{ + rt_err_t result; + dynamic_semaphore = rt_sem_create("static_sem", 0, RT_IPC_FLAG_PRIO); + if (RT_NULL != dynamic_semaphore) + { + /* first take */ + result = rt_sem_take(dynamic_semaphore, 100); + if (-RT_ETIMEOUT != result) + uassert_true(RT_FALSE); + + /* release */ + result = rt_sem_release(dynamic_semaphore); + if (RT_EOK != result) + uassert_true(RT_FALSE); + + /* second take */ + result = rt_sem_take(dynamic_semaphore, RT_WAITING_FOREVER); + if (RT_EOK != result) + uassert_true(RT_FALSE); + } + else + { + return; + } + + rt_sem_delete(dynamic_semaphore); + uassert_true(RT_TRUE); + + return; +} + +static void test_dynamic_semaphore_control(void) +{ + rt_err_t result; + int value = 0; + + value = rand() % 100; + dynamic_semaphore = rt_sem_create("static_sem", 1, RT_IPC_FLAG_PRIO); + if (RT_NULL != dynamic_semaphore) + { + result = rt_sem_control(dynamic_semaphore, RT_IPC_CMD_RESET, &value); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + else + { + return; + } + + for (int i = 0; i < value; i++) + { + result = rt_sem_take(dynamic_semaphore, 10); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + + rt_sem_delete(dynamic_semaphore); + uassert_true(RT_TRUE); +} + +static void dynamic_release_isr_hardware_callback(void *param) +{ + rt_err_t result; + + result = rt_sem_release(dynamic_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} + +static void dynamic_release_isr_software_callback(void *param) +{ + rt_err_t result; + + result = rt_sem_release(dynamic_semaphore); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } +} + +static void test_dynamic_semaphore_release_isr(void) +{ + rt_err_t result; + rt_timer_t hardware_timer; + rt_timer_t software_timer; + + /* create timer */ + hardware_timer = rt_timer_create("release_isr", + dynamic_release_isr_hardware_callback, + RT_NULL, + 100, + RT_TIMER_FLAG_HARD_TIMER | RT_TIMER_FLAG_ONE_SHOT); + software_timer = rt_timer_create("release_isr", + dynamic_release_isr_software_callback, + RT_NULL, + 100, + RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); + /* start tiemr */ + if (hardware_timer) + rt_timer_start(hardware_timer); + if (software_timer) + rt_timer_start(software_timer); + + dynamic_semaphore = rt_sem_create("static_sem", 0, RT_IPC_FLAG_PRIO); + if (RT_NULL != dynamic_semaphore) + { + for (int i = 0; i < 2; i++) + { + result = rt_sem_take(dynamic_semaphore, 1000); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + } + } + else + { + return; + } + + rt_sem_delete(dynamic_semaphore); + rt_timer_delete(hardware_timer); + rt_timer_delete(software_timer); + + uassert_true(RT_TRUE); +} + +#endif /* RT_USING_HEAP */ + +static rt_err_t utest_tc_init(void) +{ +#ifdef RT_USING_HEAP + dynamic_semaphore = RT_NULL; +#endif /* RT_USING_HEAP */ + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ +#ifdef RT_USING_HEAP + dynamic_semaphore = RT_NULL; +#endif /* RT_USING_HEAP */ + + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_static_semaphore_init); + UTEST_UNIT_RUN(test_static_semaphore_take); + UTEST_UNIT_RUN(test_static_semaphore_release); + UTEST_UNIT_RUN(test_static_semaphore_detach); + UTEST_UNIT_RUN(test_static_semaphore_trytake); + UTEST_UNIT_RUN(test_static_semaphore_control); + UTEST_UNIT_RUN(test_static_semaphore_release_isr); + +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_dynamic_semaphore_create); + UTEST_UNIT_RUN(test_dynamic_semaphore_take); + UTEST_UNIT_RUN(test_dynamic_semaphore_release); + UTEST_UNIT_RUN(test_dynamic_semaphore_delete); + UTEST_UNIT_RUN(test_dynamic_semaphore_trytake); + UTEST_UNIT_RUN(test_dynamic_semaphore_control); + UTEST_UNIT_RUN(test_dynamic_semaphore_release_isr); +#endif /* RT_USING_HEAP */ +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.semaphore_tc", utest_tc_init, utest_tc_cleanup, 1000); diff --git a/examples/utest/testcases/kernel/signal_tc.c b/examples/utest/testcases/kernel/signal_tc.c new file mode 100644 index 0000000000..40c08f7402 --- /dev/null +++ b/examples/utest/testcases/kernel/signal_tc.c @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-12 flybreak the first version + * + * case 1:rt_signal_install, install all available signal + * case 2:rt_signal_install, install illegal signal + * case 3:rt_signal_mask/unmask, one thread self, install and unmask, then kill, should received. + * case 4:rt_signal_mask/unmask, one thread self, install and unmask and mask, then kill, should can't received. + * case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. + * case 6:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: sleep 2s then kill, should can't received. + * case 7:rt_signal_kill, kill legal thread, return 0; + * case 8:rt_signal_kill, kill illegal thread, return failed (unused); + * case 9:rt_signal_kill, kill illegal signo, return -RT_EINVAL; + * + */ + +#include <rtthread.h> +#include "utest.h" + +int recive_sig = 0; + +void sig_handle_default(int signo) +{ + recive_sig = signo; +} + +static void rt_signal_install_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 1:rt_signal_install, install all available signal. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + } + /* case 2:rt_signal_install, install illegal signal. */ + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result == SIG_ERR); + + return; +} + +static void rt_signal_mask_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 3:rt_signal_mask/unmask, one thread self, install and unmask, then kill, should received. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, signo); + } + + return; +} + +static void rt_signal_unmask_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 4:rt_signal_mask/unmask, one thread self, install and unmask and mask, then kill, should can't received. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + rt_signal_mask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_not_equal(recive_sig, signo); + } + + return; +} + +static void rt_signal_kill_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 7:rt_signal_kill, kill legal thread, return 0; */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, signo); + } + /* case 8:rt_signal_kill, kill illegal thread, return failed; */ +// uassert_true(rt_thread_kill((rt_thread_t)-1, signo) == -RT_ERROR); + + /* case 9:rt_signal_kill, kill illegal signo, return -RT_EINVAL; */ + uassert_true(rt_thread_kill(rt_thread_self(), -1) == -RT_EINVAL); + + return; +} + +void rt_signal_wait_thread(void *parm) +{ + sigset_t selectset; + siginfo_t recive_si; + + rt_signal_install(SIGUSR1, sig_handle_default); + rt_signal_unmask(SIGUSR1); + + sigemptyset(&selectset); + sigaddset(&selectset, SIGUSR1); + + /* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */ + if (rt_signal_wait(&selectset, &recive_si, RT_TICK_PER_SECOND) != RT_EOK) + { + return; + } + + recive_sig = recive_si.si_signo; +} + +static void rt_signal_wait_test(void) +{ + rt_thread_t t1; + + recive_sig = -1; + t1 = rt_thread_create("sig_t1", rt_signal_wait_thread, 0, 4096, 14, 10); + if (t1) + { + rt_thread_startup(t1); + } + + rt_thread_mdelay(1); + /* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */ + uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, SIGUSR1); + + return; +} + +static void rt_signal_wait_test2(void) +{ + rt_thread_t t1; + + recive_sig = -1; + t1 = rt_thread_create("sig_t1", rt_signal_wait_thread, 0, 4096, 14, 10); + if (t1) + { + rt_thread_startup(t1); + } + + /* case 6:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: sleep 2s then kill, should can't received. */ + rt_thread_mdelay(2000); + uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK); + rt_thread_mdelay(1); + uassert_int_not_equal(recive_sig, SIGUSR1); + + return; +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(rt_signal_install_test); + UTEST_UNIT_RUN(rt_signal_mask_test); + UTEST_UNIT_RUN(rt_signal_unmask_test); + UTEST_UNIT_RUN(rt_signal_kill_test); + UTEST_UNIT_RUN(rt_signal_wait_test); + UTEST_UNIT_RUN(rt_signal_wait_test2); +#endif /* RT_USING_HEAP */ +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.signal_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/*********************** end of file ****************************/ diff --git a/examples/utest/testcases/kernel/thread_tc.c b/examples/utest/testcases/kernel/thread_tc.c new file mode 100644 index 0000000000..e95c97c293 --- /dev/null +++ b/examples/utest/testcases/kernel/thread_tc.c @@ -0,0 +1,642 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09.01 yangjie the firet version + * 2021-10.11 mazhiyuan add idle, yield, suspend, control, priority, delay_until + */ + +#include <rtthread.h> +#include <stdlib.h> +#include "utest.h" + +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 10 + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; +#ifdef RT_USING_HEAP + static rt_thread_t tid1 = RT_NULL; + static rt_thread_t tid3 = RT_NULL; + static rt_thread_t tid4 = RT_NULL; + static rt_thread_t tid5 = RT_NULL; + static rt_thread_t tid6 = RT_NULL; + static rt_thread_t tid7 = RT_NULL; +#endif /* RT_USING_HEAP */ + +static rt_uint32_t tid3_delay_pass_flag = 0; +static rt_uint32_t tid3_finish_flag = 0; +static rt_uint32_t tid4_finish_flag = 0; +static rt_uint32_t tid6_finish_flag = 0; +static rt_uint32_t thread5_source = 0; + +#ifndef RT_USING_SMP + static rt_uint32_t thread_yield_flag = 0; +#endif +static rt_uint32_t entry_idle_hook_times = 0; +static rt_thread_t __current_thread; +static rt_uint8_t change_priority; +static rt_uint32_t count = 0; + +void thread1_entry(void *param) +{ + while (1); +} + +static void test_dynamic_thread(void) +{ + rt_err_t ret_startup = -RT_ERROR; + rt_err_t ret_delete = -RT_ERROR; + + tid1 = rt_thread_create("thread1", + thread1_entry, + (void *)1, + THREAD_STACK_SIZE, + __current_thread->current_priority + 1, + THREAD_TIMESLICE - 5); + if (tid1 == RT_NULL) + { + uassert_false(tid1 == RT_NULL); + goto __exit; + } + + ret_startup = rt_thread_startup(tid1); + if (ret_startup != RT_EOK) + { + uassert_false(ret_startup != RT_EOK); + goto __exit; + } + + ret_delete = rt_thread_delete(tid1); + if (ret_delete != RT_EOK) + { + uassert_false(ret_delete != RT_EOK); + goto __exit; + } + + uassert_true(tid1 != RT_NULL && ret_startup == RT_EOK && ret_delete == RT_EOK); + +__exit: + if (tid1 != RT_NULL && ret_delete != RT_EOK) + { + rt_thread_delete(tid1); + } + return; +} + +void thread2_entry(void *param) +{ + while (1); +} + +static void test_static_thread(void) +{ + rt_err_t ret_init = -RT_ERROR; + rt_err_t ret_startup = -RT_ERROR; + rt_err_t ret_detach = - RT_ERROR; + + ret_init = rt_thread_init(&thread2, + "thread2", + thread2_entry, + (void *)2, + &thread2_stack[0], + sizeof(thread2_stack), + __current_thread->current_priority + 1, + THREAD_TIMESLICE); + if (ret_init != RT_EOK) + { + uassert_false(ret_init != RT_EOK); + goto __exit; + } + + ret_startup = rt_thread_startup(&thread2); + if (ret_startup != RT_EOK) + { + uassert_false(ret_startup != RT_EOK); + goto __exit; + } + + ret_detach = rt_thread_detach(&thread2); + if (ret_detach != RT_EOK) + { + uassert_false(ret_detach != RT_EOK); + goto __exit; + } + + uassert_true(ret_init == RT_EOK && ret_startup == RT_EOK && ret_detach == RT_EOK); + +__exit: + if (ret_init == RT_EOK && ret_detach != RT_EOK) + { + rt_thread_detach(&thread2); + } + return; +} + +static void thread3_entry(void *parameter) +{ + rt_tick_t tick; + tick = rt_tick_get(); + rt_thread_delay(15); + if (rt_tick_get() - tick > 16) + { + tid3_finish_flag = 1; + tid3_delay_pass_flag = 0; + return; + } + tid3_delay_pass_flag = 1; + tid3_finish_flag = 1; +} + +static void test_thread_delay(void) +{ + rt_err_t ret_startup = -RT_ERROR; + + tid3 = rt_thread_create("thread3", + thread3_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority - 1, + THREAD_TIMESLICE); + if (tid3 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid3 == RT_NULL); + goto __exit; + } + + ret_startup = rt_thread_startup(tid3); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + goto __exit; + } + + while (tid3_finish_flag != 1); + uassert_true(tid3_delay_pass_flag == 1); + +__exit: + return; +} + +static void idle_hook(void) +{ + entry_idle_hook_times ++; +} + +static void thread4_entry(void *parameter) +{ + rt_uint32_t delay_times = 5; + while (delay_times --) + { + rt_thread_mdelay(300); + } + rt_thread_idle_delhook(idle_hook); + tid4_finish_flag = 1; +} + +static void test_idle_hook(void) +{ + rt_err_t ret_startup = -RT_ERROR; + + rt_thread_idle_sethook(idle_hook); + + tid4 = rt_thread_create("thread4", + thread4_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority - 1, + THREAD_TIMESLICE); + if (tid4 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid4 == RT_NULL); + goto __exit; + } + + ret_startup = rt_thread_startup(tid4); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + goto __exit; + } + + while (tid4_finish_flag != 1) + { + rt_thread_mdelay(200); + } + uassert_true(entry_idle_hook_times > 0); + +__exit: + return; +} + +static void thread5_entry(void *parameter) +{ + while (1) + { + thread5_source ++; + rt_thread_delay(5); + if (thread5_source == 5) + { + rt_thread_yield(); + } + } +} + +static void thread6_entry(void *parameter) +{ + while (++ thread5_source <= 9); + tid6_finish_flag = 1; +} + +static void test_thread_yield(void) +{ + rt_err_t ret_startup = -RT_ERROR; + tid5 = rt_thread_create("thread5", + thread5_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority - 1, + THREAD_TIMESLICE); + if (tid5 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid5 == RT_NULL); + goto __exit; + } + ret_startup = rt_thread_startup(tid5); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + goto __exit; + } + tid6 = rt_thread_create("thread6", + thread6_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority - 1, + THREAD_TIMESLICE); + if (tid6 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid6 == RT_NULL); + goto __exit; + } + ret_startup = rt_thread_startup(tid6); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + goto __exit; + } + + while (tid6_finish_flag != 1); + uassert_true(thread5_source == 10); + +__exit: + if (tid5 != RT_NULL) + { + rt_thread_delete(tid5); + } + return; +} + +static void thread7_entry(void *parameter) +{ + while (1); +} + +static void test_thread_control(void) +{ + rt_err_t ret_control = -RT_ERROR; + rt_err_t rst_delete = -RT_ERROR; + + tid7 = rt_thread_create("thread7", + thread7_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority + 1, + THREAD_TIMESLICE); + if (tid7 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid7 == RT_NULL); + goto __exit; + } + + ret_control = rt_thread_control(tid7, RT_THREAD_CTRL_STARTUP, RT_NULL); + if (ret_control != RT_EOK) + { + LOG_E("rt_thread_control failed!"); + uassert_false(1); + goto __exit; + } + rt_thread_mdelay(200); + rt_thread_control(tid7, RT_THREAD_CTRL_CHANGE_PRIORITY, &change_priority); + if (tid7->current_priority != change_priority) + { + LOG_E("rt_thread_control failed!"); + uassert_false(1); + goto __exit; + } + rst_delete = rt_thread_control(tid7, RT_THREAD_CTRL_CLOSE, RT_NULL); + if (rst_delete != RT_EOK) + { + LOG_E("rt_thread_control failed!"); + uassert_false(rst_delete != RT_EOK); + goto __exit; + } + + uassert_true(1); + +__exit: + if (tid7 != RT_NULL && rst_delete != RT_EOK) + { + rt_thread_delete(tid7); + } + return; +} + +static void thread8_entry(void *parameter) +{ + for (; count < 10; count ++); +} + +static void test_thread_priority(void) +{ + rt_err_t ret_startup = -RT_ERROR; + rt_thread_t tid8 = RT_NULL; + + tid8 = rt_thread_create("thread8", + thread8_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority - 1, + THREAD_TIMESLICE); + if (tid8 == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid8 == RT_NULL); + return; + } + count = 0; + ret_startup = rt_thread_startup(tid8); + if (ret_startup != RT_EOK) + { + uassert_false(ret_startup != RT_EOK); + return ; + } + uassert_true(count == 10); + + return; +} + +static void test_delay_until(void) +{ + rt_tick_t tick; + rt_tick_t check_tick = 0; + rt_tick_t delta = 0; + + tick = rt_tick_get(); + + check_tick = tick; + rt_thread_delay_until(&tick, 100); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[100] -> %d\n", delta); + uassert_int_equal(delta, 100); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 200); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[200] -> %d\n", delta); + uassert_int_equal(delta, 200); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 300); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[300] -> %d\n", delta); + uassert_int_equal(delta, 300); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 100); + delta = rt_tick_get() - check_tick; + uassert_int_equal(delta, 100); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 50); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[50] -> %d\n", delta); + uassert_int_equal(delta, 50); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 20); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[20] -> %d\n", delta); + uassert_int_equal(delta, 20); + + check_tick = tick; + rt_thread_delay(2); + rt_thread_delay_until(&tick, 10); + delta = rt_tick_get() - check_tick; + rt_kprintf("delta[10] -> %d\n", delta); + uassert_int_equal(delta, 10); +} + +#ifndef RT_USING_SMP +static volatile rt_uint32_t yield_count; + +static void test_thread_yield_inc_entry(void *parameter) +{ + rt_uint32_t loop = 0; + + while (1) + { + if (loop++ > 10001) + break; + yield_count++; + rt_thread_yield(); + } +} + +static void test_thread_yield_entry(void *parameter) +{ + rt_err_t ret_startup = -RT_ERROR; + + rt_thread_t tid; + rt_uint32_t loop = 0; + rt_uint32_t count_before; + + tid = rt_thread_create("inc", test_thread_yield_inc_entry, RT_NULL, + 2048, 1, 10); + if (!tid) + { + LOG_E("rt_thread_create failed!"); + return; + } + + ret_startup = rt_thread_startup(tid); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + return ; + } + + while (1) + { + if (loop++ > 10000) + break; + + count_before = yield_count; + rt_thread_yield(); + if (yield_count == count_before) + { + LOG_E("yield error!"); + return; + } + } + thread_yield_flag = 1; +} + +void test_thread_yield_nosmp(void) +{ + rt_err_t ret_startup = -RT_ERROR; + + rt_thread_t tid; + + yield_count = 0; + + tid = rt_thread_create("chkcnt", test_thread_yield_entry, RT_NULL, + 2048, 1, 10); + if (!tid) + { + LOG_E("rt_thread_create failed!"); + return; + } + + ret_startup = rt_thread_startup(tid); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + return ; + } + + uassert_true(thread_yield_flag == 1); +} + +static rt_uint32_t thread9_count = 0; +static void thread9_entry(void *parameter) +{ + while (1) + { + thread9_count ++; + } + +} +static void test_thread_suspend(void) +{ + static rt_thread_t tid; + rt_err_t ret_startup = -RT_ERROR; + uint32_t count_before_suspend, count_before_resume, count_after_resume; + tid = rt_thread_create("thread9", + thread9_entry, + RT_NULL, + THREAD_STACK_SIZE, + __current_thread->current_priority + 1, + THREAD_TIMESLICE); + if (tid == RT_NULL) + { + LOG_E("rt_thread_create failed!"); + uassert_false(tid4 == RT_NULL); + goto __exit; + } + + ret_startup = rt_thread_startup(tid); + if (ret_startup != RT_EOK) + { + LOG_E("rt_thread_startup failed!"); + uassert_false(1); + goto __exit; + } + rt_thread_delay(5); + rt_thread_suspend(tid); + count_before_suspend = thread9_count; + uassert_true(count_before_suspend != 0); + rt_thread_delay(5); + count_before_resume = thread9_count; + uassert_true(count_before_suspend == count_before_resume); + rt_thread_resume(tid); + rt_thread_delay(5); + count_after_resume = thread9_count; + uassert_true(count_after_resume != count_before_resume); + +__exit: + if (tid != RT_NULL) + { + rt_thread_delete(tid); + } + return; +} +#endif + +static rt_err_t utest_tc_init(void) +{ + __current_thread = rt_thread_self(); + change_priority = __current_thread->current_priority + 5; + tid3_delay_pass_flag = 0; + tid3_finish_flag = 0; + tid4_finish_flag = 0; + tid6_finish_flag = 0; + entry_idle_hook_times = 0; + count = 0; + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + /* init, detach */ + UTEST_UNIT_RUN(test_static_thread); + /* create, delete */ + UTEST_UNIT_RUN(test_dynamic_thread); + /* delay */ + UTEST_UNIT_RUN(test_thread_delay); + /* idle_sethook, idle_delhook */ + UTEST_UNIT_RUN(test_idle_hook); + /* yield */ + UTEST_UNIT_RUN(test_thread_yield); +#ifndef RT_USING_SMP + /* yield_nosmp */ + UTEST_UNIT_RUN(test_thread_yield_nosmp); + /* suspend, resume */ + UTEST_UNIT_RUN(test_thread_suspend); +#endif + /* control */ + UTEST_UNIT_RUN(test_thread_control); + UTEST_UNIT_RUN(test_thread_priority); + /* delay_until */ + UTEST_UNIT_RUN(test_delay_until); +} + + +UTEST_TC_EXPORT(testcase, "testcases.kernel.thread_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/********************* end of file ************************/ diff --git a/examples/utest/testcases/kernel/timer_tc.c b/examples/utest/testcases/kernel/timer_tc.c new file mode 100644 index 0000000000..e7fffcb617 --- /dev/null +++ b/examples/utest/testcases/kernel/timer_tc.c @@ -0,0 +1,520 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-12 luckyzjq the first version + */ + +#include <rtthread.h> +#include <stdlib.h> +#include "utest.h" + +static rt_uint8_t timer_flag_oneshot[] = { + RT_TIMER_FLAG_ONE_SHOT, + RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER, + RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER, +}; + +static rt_uint8_t timer_flag_periodic[] = { + RT_TIMER_FLAG_PERIODIC, + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER, + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER, +}; + +typedef struct test_timer_struct +{ + struct rt_timer static_timer; /* static timer handler */ + rt_timer_t dynamic_timer; /* dynamic timer pointer */ + rt_tick_t expect_tick; /* expect tick */ + rt_uint8_t test_flag; /* timer callback done flag */ +} timer_struct; +static timer_struct timer; + +#define test_static_timer_start test_static_timer_init +#define test_static_timer_stop test_static_timer_init +#define test_static_timer_detach test_static_timer_init + +static void static_timer_oneshot(void *param) +{ + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + return; +} +static void static_timer_periodic(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(&timer_call->static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_static_timer_init(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + + /* one shot timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_oneshot); i++) + { + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_oneshot, + &timer, + time_out, + timer_flag_oneshot[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + /* periodic timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_periodic); i++) + { + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_periodic, + &timer, + time_out, + timer_flag_periodic[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); + + return; +} + +static void static_timer_control(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(&timer_call->static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_static_timer_control(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + int set_data; + int get_data; + + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_control, + &timer, + 5, + RT_TIMER_FLAG_PERIODIC); + + /* test set data */ + set_data = rand_num; + result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_SET_TIME, &set_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* test get data */ + result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_GET_TIME, &get_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* a set of test */ + if (set_data != get_data) + { + uassert_true(RT_FALSE); + } + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + set_data; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + rt_thread_delay(set_data + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); +} + +#ifdef RT_USING_HEAP + +#define test_dynamic_timer_start test_dynamic_timer_create +#define test_dynamic_timer_stop test_dynamic_timer_create +#define test_dynamic_timer_delete test_dynamic_timer_create + +static void dynamic_timer_oneshot(void *param) +{ + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + return; +} +static void dynamic_timer_periodic(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(timer_call->dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_dynamic_timer_create(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + + /* one shot timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_oneshot); i++) + { + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_oneshot, + &timer, + time_out, + timer_flag_oneshot[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + /* periodic timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_periodic); i++) + { + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_periodic, + &timer, + time_out, + timer_flag_periodic[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); + + return; +} + +static void dynamic_timer_control(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(timer_call->dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_dynamic_timer_control(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + int set_data; + int get_data; + + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_control, + &timer, + 5, + RT_TIMER_FLAG_PERIODIC); + + /* test set data */ + set_data = rand_num; + result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_SET_TIME, &set_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* test get data */ + result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_GET_TIME, &get_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* a set of test */ + if (set_data != get_data) + { + uassert_true(RT_FALSE); + } + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + set_data; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + rt_thread_delay(set_data + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); +} + +#endif /* RT_USING_HEAP */ + +static rt_err_t utest_tc_init(void) +{ + timer.dynamic_timer = RT_NULL; + timer.test_flag = RT_FALSE; + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + timer.dynamic_timer = RT_NULL; + timer.test_flag = RT_FALSE; + + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_static_timer_init); + UTEST_UNIT_RUN(test_static_timer_start); + UTEST_UNIT_RUN(test_static_timer_stop); + UTEST_UNIT_RUN(test_static_timer_detach); + UTEST_UNIT_RUN(test_static_timer_control); +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_dynamic_timer_create); + UTEST_UNIT_RUN(test_dynamic_timer_start); + UTEST_UNIT_RUN(test_dynamic_timer_stop); + UTEST_UNIT_RUN(test_dynamic_timer_delete); + UTEST_UNIT_RUN(test_dynamic_timer_control); +#endif /* RT_USING_HEAP */ +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.timer_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/*********************** end of file ****************************/ diff --git a/examples/var_export/var_export_example.c b/examples/var_export/var_export_example.c new file mode 100644 index 0000000000..9ea45de1c3 --- /dev/null +++ b/examples/var_export/var_export_example.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-09-02 WillianChan add var_export example code + */ + +#include <var_export.h> + +/* use VAR_EXPOR() to export 10 pieces of data into sections */ +VAR_EXPORT(module0, identi0, 0); +VAR_EXPORT(module1, identi1, 1); +VAR_EXPORT(module2, identi2, 2); +VAR_EXPORT(module3, identi3, 3); +VAR_EXPORT(module4, identi4, 4); +VAR_EXPORT(module5, identi5, 5); +VAR_EXPORT(module6, identi6, 6); +VAR_EXPORT(module7, identi7, 7); +VAR_EXPORT(module8, identi8, 8); +VAR_EXPORT(module9, identi9, 9); + +void found_by_module(void) +{ + ve_iterator_t iter; + const ve_exporter_t* exporter; + ve_module_t module; + rt_base_t value; + + /* query all exporters with the same name as module1 */ + if (!ve_module_init(&module, "module1")) + /* initialize the iterator */ + ve_iter_init(&module, &iter); + else + return; + + while (1) + { + /* start iterating */ + exporter = ve_iter_next(&iter); + if (exporter == RT_NULL) + break; + else + { + /* checks whether the value exists */ + if (ve_value_exist(&module, "identi1") == RT_TRUE) + { + value = ve_value_get(&module, "identi1"); + rt_kprintf("[ve_example] value = %dn", value); + return; + } + else + { + rt_kprintf("[ve_example] value not exist.\n"); + return; + } + } + } +} +#ifdef RT_USING_FINSH +#include <finsh.h> +MSH_CMD_EXPORT(found_by_module, found by module); +#endif /* RT_USING_FINSH */
\ No newline at end of file |
