1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
| #include "rtthread.h"
#include "main.h"
/*
*************************************************************************
* 变量
*************************************************************************
*/
/* 定义线程控制块 */
static rt_thread_t receive_thread = RT_NULL;
static rt_thread_t send_thread = RT_NULL;
/* 定义消息队列控制块 */
static rt_mq_t test_mq = RT_NULL;
/*
*************************************************************************
* 函数声明
*************************************************************************
*/
static void receive_thread_entry(void* parameter);
static void send_thread_entry(void* parameter);
int main(void)
{
test_mq = rt_mq_create("test_mq",/* 消息队列名字 */
4, /* 消息的最大长度 */
20, /* 消息队列的最大容量 */
RT_IPC_FLAG_FIFO);/* 队列模式 FIFO(0x00)*/
if (test_mq != RT_NULL)
rt_kprintf("消息队列创建成功!\n\n");
receive_thread = /* 线程控制块指针 */
rt_thread_create( "receive", /* 线程名字 */
receive_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
3, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (receive_thread != RT_NULL)
rt_thread_startup(receive_thread);
else
return -1;
send_thread = /* 线程控制块指针 */
rt_thread_create( "send", /* 线程名字 */
send_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
2, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (send_thread != RT_NULL)
rt_thread_startup(send_thread);
else
return -1;
}
/**
* @brief receive_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void receive_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
uint32_t r_queue;
/* 任务都是一个无限循环,不能返回 */
while (1)
{
/* 队列读取(接收),等待时间为一直等待 */
uwRet = rt_mq_recv(test_mq, /* 读取(接收)队列的ID(句柄) */
&r_queue, /* 读取(接收)的数据保存位置 */
sizeof(r_queue), /* 读取(接收)的数据的长度 */
RT_WAITING_FOREVER); /* 等待时间:一直等 */
if(RT_EOK == uwRet)
{
rt_kprintf("receive data:%d\n",r_queue);
}
else
{
rt_kprintf("receive err:0x%lx\n",uwRet);
}
rt_thread_delay(200);
}
}
/**
* @brief send_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void send_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
uint32_t send_data1 = 1;
uint32_t send_data2 = 2;
while (1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )/* K1 被按下 */
{
/* 将数据写入(发送)到队列中,等待时间为 0 */
uwRet = rt_mq_send( test_mq, /* 写入(发送)队列的ID(句柄) */
&send_data1, /* 写入(发送)的数据 */
sizeof(send_data1)); /* 数据的长度 */
if(RT_EOK != uwRet)
{
rt_kprintf("err:%lx\n",uwRet);
}
}
if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )/* K1 被按下 */
{
/* 将数据写入(发送)到队列中,等待时间为 0 */
uwRet = rt_mq_send( test_mq, /* 写入(发送)队列的ID(句柄) */
&send_data2, /* 写入(发送)的数据 */
sizeof(send_data2)); /* 数据的长度 */
if(RT_EOK != uwRet)
{
rt_kprintf("err:%lx\n",uwRet);
}
}
rt_thread_delay(20);
}
}
|