1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-08-15 supperthomas add irq_test
9 */
10
11 #include <rtthread.h>
12 #include "utest.h"
13 #include "rthw.h"
14
15 #define UTEST_NAME "irq_tc"
16 static volatile uint32_t irq_count = 0;
17 static volatile uint32_t max_get_nest_count = 0;
18
irq_callback()19 static void irq_callback()
20 {
21 if(rt_interrupt_get_nest() > max_get_nest_count)
22 {
23 max_get_nest_count = rt_interrupt_get_nest();
24 }
25 irq_count ++;
26 }
27
irq_test(void)28 static void irq_test(void)
29 {
30 irq_count = 0;
31 rt_interrupt_enter_sethook(irq_callback);
32 rt_interrupt_leave_sethook(irq_callback);
33 rt_thread_mdelay(2);
34 LOG_D("%s test irq_test! irq_count %d max_get_nest_count %d\n", UTEST_NAME, irq_count, max_get_nest_count);
35 uassert_int_not_equal(0, irq_count);
36 uassert_int_not_equal(0, max_get_nest_count);
37 rt_interrupt_enter_sethook(RT_NULL);
38 rt_interrupt_leave_sethook(RT_NULL);
39 LOG_D("irq_test OK!\n");
40 }
41
utest_tc_init(void)42 static rt_err_t utest_tc_init(void)
43 {
44 irq_count = 0;
45 max_get_nest_count = 0;
46 return RT_EOK;
47 }
48
utest_tc_cleanup(void)49 static rt_err_t utest_tc_cleanup(void)
50 {
51 return RT_EOK;
52 }
53
interrupt_test(void)54 static void interrupt_test(void)
55 {
56 rt_base_t level;
57 uint32_t i = 1000;
58
59 rt_interrupt_enter_sethook(irq_callback);
60 rt_interrupt_leave_sethook(irq_callback);
61 irq_count = 0;
62 level = rt_hw_interrupt_disable();
63 while(i)
64 {
65 i --;
66 }
67 uassert_int_equal(0, irq_count);
68 rt_hw_interrupt_enable(level);
69 rt_interrupt_enter_sethook(RT_NULL);
70 rt_interrupt_leave_sethook(RT_NULL);
71
72 }
testcase(void)73 static void testcase(void)
74 {
75 UTEST_UNIT_RUN(irq_test);
76 UTEST_UNIT_RUN(interrupt_test);
77 }
78 UTEST_TC_EXPORT(testcase, "testcases.kernel.irq_tc", utest_tc_init, utest_tc_cleanup, 10);
79