1 /*
2 * 程序清单:邮箱例程
3 *
4 * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
5 * 一个线程往邮箱中收取邮件。
6 */
7 #include <rtthread.h>
8 #include "tc_comm.h"
9
10 /* 指向线程控制块的指针 */
11 static rt_thread_t tid1 = RT_NULL;
12 static rt_thread_t tid2 = RT_NULL;
13
14 /* 邮箱控制块 */
15 static struct rt_mailbox mb;
16 /* 用于放邮件的内存池 */
17 static char mb_pool[128];
18
19 static char mb_str1[] = "I'm a mail!";
20 static char mb_str2[] = "this is another mail!";
21
22 /* 线程1入口 */
thread1_entry(void * parameter)23 static void thread1_entry(void* parameter)
24 {
25 unsigned char* str;
26
27 while (1)
28 {
29 rt_kprintf("thread1: try to recv a mail\n");
30
31 /* 从邮箱中收取邮件 */
32 if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
33 {
34 rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
35
36 /* 延时10个OS Tick */
37 rt_thread_delay(10);
38 }
39 }
40 }
41
42 /* 线程2入口 */
thread2_entry(void * parameter)43 static void thread2_entry(void* parameter)
44 {
45 rt_uint8_t count;
46
47 count = 0;
48 while (1)
49 {
50 count ++;
51 if (count & 0x1)
52 {
53 /* 发送mb_str1地址到邮箱中 */
54 rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
55 }
56 else
57 {
58 /* 发送mb_str2地址到邮箱中 */
59 rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
60 }
61
62 /* 延时20个OS Tick */
63 rt_thread_delay(20);
64 }
65 }
66
mbox_simple_init()67 int mbox_simple_init()
68 {
69 /* 初始化一个mailbox */
70 rt_mb_init(&mb,
71 "mbt", /* 名称是mbt */
72 &mb_pool[0], /* 邮箱用到的内存池是mb_pool */
73 sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4,因为一封邮件的大小是4字节 */
74 RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
75
76 /* 创建线程1 */
77 tid1 = rt_thread_create("t1",
78 thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
79 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
80 if (tid1 != RT_NULL)
81 rt_thread_startup(tid1);
82 else
83 tc_stat(TC_STAT_END | TC_STAT_FAILED);
84
85 /* 创建线程2 */
86 tid2 = rt_thread_create("t2",
87 thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
88 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
89 if (tid2 != RT_NULL)
90 rt_thread_startup(tid2);
91 else
92 tc_stat(TC_STAT_END | TC_STAT_FAILED);
93
94 return 0;
95 }
96
97 #ifdef RT_USING_TC
_tc_cleanup()98 static void _tc_cleanup()
99 {
100 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
101 rt_enter_critical();
102
103 /* 删除线程 */
104 if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
105 rt_thread_delete(tid1);
106 if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
107 rt_thread_delete(tid2);
108
109 /* 执行邮箱对象脱离 */
110 rt_mb_detach(&mb);
111
112 /* 调度器解锁 */
113 rt_exit_critical();
114
115 /* 设置TestCase状态 */
116 tc_done(TC_STAT_PASSED);
117 }
118
_tc_mbox_simple()119 int _tc_mbox_simple()
120 {
121 /* 设置TestCase清理回调函数 */
122 tc_cleanup(_tc_cleanup);
123 mbox_simple_init();
124
125 /* 返回TestCase运行的最长时间 */
126 return 100;
127 }
128 /* 输出函数命令到finsh shell中 */
129 FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
130 #else
131 /* 用户应用入口 */
rt_application_init()132 int rt_application_init()
133 {
134 mbox_simple_init();
135
136 return 0;
137 }
138 #endif
139