1 /*
2  * Copyright (c) 2006-2024, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-08-10     RV           the first version
9  */
10 
11 #include <rtthread.h>
12 #include "utest.h"
13 #include <interrupt.h>
14 
15 /**
16  * @brief   Setting the Interrupt Priority Test.
17  *
18  * @note    Without turning off interrupts, interrupts respond in the order in which they are triggered.
19  *          With interrupts turned off, low and high priority interrupts are triggered sequentially,
20  *          and when interrupts are turned on, high priority interrupts respond first.
21  */
22 
23 #define RES_VAL         0X0
24 #define SET_VAL         0XA
25 #define RT_SPI_1        1
26 #define RT_SPI_2        2
27 #define RT_SPI_PRI_HIGH 120
28 #define RT_SPI_PRI_LOW  140
29 
30 static int mode       = 0;
31 static int ipi_val[2] = {0, 0};
32 
33 /* Software Interrupt 1 Service Functions */
rt_scheduler_ipi1_handler(int vector,void * param)34 static void rt_scheduler_ipi1_handler(int vector, void *param)
35 {
36     ipi_val[0] = SET_VAL;
37     if (mode == 0)
38     {
39         uassert_true(ipi_val[0] > ipi_val[1]);
40     }
41     else
42     {
43         ipi_val[0] = RES_VAL;
44         ipi_val[1] = RES_VAL;
45     }
46 }
47 
48 /* Software Interrupt 2 Service Functions */
rt_scheduler_ipi2_handler(int vector,void * param)49 static void rt_scheduler_ipi2_handler(int vector, void *param)
50 {
51     ipi_val[1] = SET_VAL;
52     if (mode == 0)
53     {
54         ipi_val[0] = RES_VAL;
55         ipi_val[1] = RES_VAL;
56     }
57     else
58     {
59         uassert_true(ipi_val[0] < ipi_val[1]);
60     }
61 }
62 
63 /* Interrupt priority testcases 1 */
int_pri1_tc(void)64 static void int_pri1_tc(void)
65 {
66     mode = 0;
67     unsigned int pri1, pri2;
68     pri1 = rt_hw_interrupt_get_priority(RT_SPI_1);
69     pri2 = rt_hw_interrupt_get_priority(RT_SPI_2);
70 
71     if (pri1 < pri2)
72         uassert_true(pri1 < pri2);
73 
74     /* Trigger interrupt */
75     rt_hw_ipi_send(RT_SPI_1, 0x1);
76     rt_hw_ipi_send(RT_SPI_2, 0x1);
77     rt_thread_delay(5);
78 }
79 
80 /* Interrupt priority testcases 2 */
int_pri2_tc(void)81 static void int_pri2_tc(void)
82 {
83     mode = 1;
84     unsigned int pri1, pri2;
85     pri1 = rt_hw_interrupt_get_priority(RT_SPI_1);
86     pri2 = rt_hw_interrupt_get_priority(RT_SPI_2);
87 
88     if (pri1 < pri2)
89         uassert_true(pri1 < pri2);
90 
91     rt_base_t level = rt_hw_local_irq_disable();
92     /* Trigger interrupt */
93     rt_hw_ipi_send(RT_SPI_1, 0x1);
94     rt_hw_ipi_send(RT_SPI_2, 0x1);
95     rt_hw_local_irq_enable(level);
96     rt_thread_delay(5);
97 }
98 
utest_tc_init(void)99 static rt_err_t utest_tc_init(void)
100 {
101     /* Setting the priority of software interrupts */
102     rt_hw_interrupt_set_priority(RT_SPI_1, RT_SPI_PRI_LOW);
103     rt_hw_interrupt_set_priority(RT_SPI_2, RT_SPI_PRI_HIGH);
104     /* Register software interrupt service functions */
105     rt_hw_ipi_handler_install(RT_SPI_1, rt_scheduler_ipi1_handler);
106     rt_hw_ipi_handler_install(RT_SPI_2, rt_scheduler_ipi2_handler);
107     return RT_EOK;
108 }
109 
utest_tc_cleanup(void)110 static rt_err_t utest_tc_cleanup(void)
111 {
112     return RT_EOK;
113 }
114 
testcase(void)115 static void testcase(void)
116 {
117     UTEST_UNIT_RUN(int_pri1_tc);
118     UTEST_UNIT_RUN(int_pri2_tc);
119 }
120 UTEST_TC_EXPORT(testcase, "testcases.smp.interrupt_pri_tc", utest_tc_init, utest_tc_cleanup, 10);
121