diff options
| author | jonas <[email protected]> | 2021-09-02 10:36:07 +0800 |
|---|---|---|
| committer | jonas <[email protected]> | 2021-09-02 10:36:07 +0800 |
| commit | d72ee7b6cc19e3b0d5721b2b5fa047ed1a5fc7c4 (patch) | |
| tree | ab7034a4df4cb5171a68c2236f9f831c8cd27ed0 /examples | |
| parent | ab43390fe2c0e672771b73cf610e739a419aa06f (diff) | |
| parent | 8d175ab3d237dccc25450a8e12808fc87318eb2f (diff) | |
Merge branch 'master' of [email protected]:RT-Thread/rt-thread.git into bsp_hk32
# Conflicts:
# .ignore_format.yml
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/file/listdir.c | 2 | ||||
| -rw-r--r-- | examples/file/readspeed.c | 2 | ||||
| -rw-r--r-- | examples/file/readwrite.c | 2 | ||||
| -rw-r--r-- | examples/file/writespeed.c | 2 | ||||
| -rw-r--r-- | examples/kernel/tc_comm.c | 1 | ||||
| -rw-r--r-- | examples/libc/termios_test.c | 4 | ||||
| -rw-r--r-- | examples/test/hwtimer_test.c | 2 | ||||
| -rw-r--r-- | examples/utest/configs/kernel/irq.conf | 2 | ||||
| -rw-r--r-- | examples/utest/configs/kernel/timer.conf | 4 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/Kconfig | 4 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/SConscript | 3 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/timer_tc.c | 520 |
12 files changed, 536 insertions, 12 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..61b1a8a696 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; @@ -83,5 +82,4 @@ 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 */ #endif /* RT_USING_FINSH */ diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 56f3b7e16c..9b27da1b01 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; @@ -164,5 +163,4 @@ 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 */ #endif /* RT_USING_FINSH */ diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index 680761583c..8ae6f6e5a8 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; @@ -94,5 +93,4 @@ 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 */ #endif /* RT_USING_FINSH */ diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c index b98c97d9b3..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; diff --git a/examples/libc/termios_test.c b/examples/libc/termios_test.c index f65bff1a9f..f3a6ce3a1c 100644 --- a/examples/libc/termios_test.c +++ b/examples/libc/termios_test.c @@ -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/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/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/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 e8dd3c2e97..1854721193 100644 --- a/examples/utest/testcases/kernel/Kconfig +++ b/examples/utest/testcases/kernel/Kconfig @@ -19,4 +19,8 @@ config UTEST_EVENT_TC default n depends on RT_USING_EVENT +config UTEST_TIMER_TC + bool "timer test" + default n + endmenu diff --git a/examples/utest/testcases/kernel/SConscript b/examples/utest/testcases/kernel/SConscript index 26d5ddbf17..758dea4d3f 100644 --- a/examples/utest/testcases/kernel/SConscript +++ b/examples/utest/testcases/kernel/SConscript @@ -17,6 +17,9 @@ if GetDepend(['UTEST_SEMAPHORE_TC']): if GetDepend(['UTEST_EVENT_TC']): src += ['event_tc.c'] +if GetDepend(['UTEST_TIMER_TC']): + src += ['timer_tc.c'] + CPPPATH = [cwd] group = DefineGroup('utestcases', src, depend = [], CPPPATH = CPPPATH) 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 ****************************/ |
