1 /*
2 * 程序清单:事件例程
3 *
4 * 这个程序会创建3个动态线程及初始化一个静态事件对象
5 * 一个线程等于事件对象上以接收事件;
6 * 一个线程定时发送事件 (事件3)
7 * 一个线程定时发送事件 (事件5)
8 */
9 #include <rtthread.h>
10 #include <time.h>
11 #include "tc_comm.h"
12
13 /* 指向线程控制块的指针 */
14 static rt_thread_t tid1 = RT_NULL;
15 static rt_thread_t tid2 = RT_NULL;
16 static rt_thread_t tid3 = RT_NULL;
17
18 /* 事件控制块 */
19 static struct rt_event event;
20
21 /* 线程1入口函数 */
thread1_entry(void * param)22 static void thread1_entry(void *param)
23 {
24 rt_uint32_t e;
25
26 while (1)
27 {
28 /* receive first event */
29 if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
30 RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
31 RT_WAITING_FOREVER, &e) == RT_EOK)
32 {
33 rt_kprintf("thread1: AND recv event 0x%x\n", e);
34 }
35
36 rt_kprintf("thread1: delay 1s to prepare second event\n");
37 rt_thread_delay(10);
38
39 /* receive second event */
40 if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
41 RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
42 RT_WAITING_FOREVER, &e) == RT_EOK)
43 {
44 rt_kprintf("thread1: OR recv event 0x%x\n", e);
45 }
46
47 rt_thread_delay(5);
48 }
49 }
50
51 /* 线程2入口函数 */
thread2_entry(void * param)52 static void thread2_entry(void *param)
53 {
54 while (1)
55 {
56 rt_kprintf("thread2: send event1\n");
57 rt_event_send(&event, (1 << 3));
58
59 rt_thread_delay(10);
60 }
61 }
62
63 /* 线程3入口函数 */
thread3_entry(void * param)64 static void thread3_entry(void *param)
65 {
66 while (1)
67 {
68 rt_kprintf("thread3: send event2\n");
69 rt_event_send(&event, (1 << 5));
70
71 rt_thread_delay(20);
72 }
73 }
74
event_simple_init()75 int event_simple_init()
76 {
77 /* 初始化事件对象 */
78 rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
79
80 /* 创建线程1 */
81 tid1 = rt_thread_create("t1",
82 thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
83 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
84 if (tid1 != RT_NULL)
85 rt_thread_startup(tid1);
86 else
87 tc_stat(TC_STAT_END | TC_STAT_FAILED);
88
89 /* 创建线程2 */
90 tid2 = rt_thread_create("t2",
91 thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
92 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
93 if (tid2 != RT_NULL)
94 rt_thread_startup(tid2);
95 else
96 tc_stat(TC_STAT_END | TC_STAT_FAILED);
97
98 /* 创建线程3 */
99 tid3 = rt_thread_create("t3",
100 thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
101 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
102 if (tid3 != RT_NULL)
103 rt_thread_startup(tid3);
104 else
105 tc_stat(TC_STAT_END | TC_STAT_FAILED);
106
107 return 0;
108 }
109
110 #ifdef RT_USING_TC
_tc_cleanup()111 static void _tc_cleanup()
112 {
113 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
114 rt_enter_critical();
115
116 /* 删除线程 */
117 if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
118 rt_thread_delete(tid1);
119 if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
120 rt_thread_delete(tid2);
121 if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
122 rt_thread_delete(tid3);
123
124 /* 执行事件对象脱离 */
125 rt_event_detach(&event);
126
127 /* 调度器解锁 */
128 rt_exit_critical();
129
130 /* 设置TestCase状态 */
131 tc_done(TC_STAT_PASSED);
132 }
133
_tc_event_simple()134 int _tc_event_simple()
135 {
136 /* 设置TestCase清理回调函数 */
137 tc_cleanup(_tc_cleanup);
138 event_simple_init();
139
140 /* 返回TestCase运行的最长时间 */
141 return 100;
142 }
143 /* 输出函数命令到finsh shell中 */
144 FINSH_FUNCTION_EXPORT(_tc_event_simple, a simple event example);
145 #else
146 /* 用户应用入口 */
rt_application_init()147 int rt_application_init()
148 {
149 event_simple_init();
150
151 return 0;
152 }
153 #endif
154