RT-Thread的小例子

概述

嵌入式中最简单的就是按键,点亮LED,很多入门教程中,将这个作为例子来讲解
下面我就用这个最简单的例子,来演示RT-Thread线程通讯组件的不同使用方法

  • 关中断
  • 关调度器
  • 信号量
  • 互斥量
  • 事件
  • 邮箱
  • 消息队列

关于按键检测,为了简单,我用了轮询的模式,100ms轮询一次,然后检测电平是否有变,并做判断;
当检测到按键按下时,就翻转LED电平

最后,我们尝试用消息队列来实现:按下键后,LED闪烁多次

测试环境

  1. RT-Thread stable 2.0.0
  2. STM32F446RE Nucleo板子
  3. IAR For ARM 7.4

准备工作

  1. 搭建IAR开发环境,调整启动代码和链接脚本
  2. 修改gpio.c里面的static const struct pin_index pins[]数组,因为我们的F446RE Nucleo板子按键和LED分别接到
    PC13和PA5上面,所以需要在pins表示里面包含这两个管脚,我这里显示的27和54,如下所示
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
static const struct pin_index pins[] =
{
{ 0, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_7},
{ 1, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_6},
{ 2, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_8},
{ 3, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_11},
{ 4, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_14},
{ 5, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_13},
{ 6, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_11},
{ 7, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_9},

{ 8, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_12},
{ 9, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_13},
{10, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_14},
{11, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_15},
{12, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_6},
{13, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_5},

{14, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_8},
{15, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_9},
{16, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_5},
{17, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_6},
{18, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_6},
{19, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_7},
{20, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_9},
{21, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_8},

{22, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_12},
{23, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_2},
{24, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_1},
{25, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_0},
{26, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_9},
{27, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_13}, // KEY Pin
{28, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_15},
{29, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_12},
{30, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_10},
{31, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_8},
{32, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_7},
{33, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_4},
{34, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_3},
{35, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_2},
{36, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_1},
{37, RCC_AHB1Periph_GPIOE, GPIOE, GPIO_Pin_0},
{38, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_11},
{39, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_10},
{40, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_7},
{41, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_3},
{42, RCC_AHB1Periph_GPIOD, GPIOD, GPIO_Pin_4},
{43, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_8},
{44, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_15},
{45, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_14},
{46, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_11},
{47, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_5},
{48, RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_10},
{49, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_15},
{50, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_4},
{51, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_7},
{52, RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_3},
{53, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_4},
{54, RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_5}, // LED Pin
};

参考代码

关中断

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
///////// BEGIN OF INT /////////////
#if 0
volatile int LedFlag = 0;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
if(LedFlag)
{
rt_base_t in;
in = rt_hw_interrupt_disable();
LedFlag = 0;
rt_hw_interrupt_enable(in);

if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
rt_thread_delay(50);
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

// Delay 200 ms
rt_thread_delay(200);

while(1)
{
rt_thread_delay(100);

KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
rt_base_t in;
in = rt_hw_interrupt_disable();
LedFlag = 1;
rt_hw_interrupt_enable(in);
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF INT /////////////

关调度

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
123
124
125
126
127
128
129
130
131
132
133
134
///////// BEGIN OF CONTEXT /////////////
#if 0
volatile int LedFlag = 0;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
if(LedFlag)
{
rt_enter_critical();
LedFlag = 0;
rt_exit_critical();

if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
rt_thread_delay(50);
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

// Delay 200 ms
rt_thread_delay(200);

while(1)
{
rt_thread_delay(100);

KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
rt_enter_critical();
LedFlag = 1;
rt_exit_critical();
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF CONTEXT /////////////

信号量

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
///////// BEGIN OF SEM /////////////
#if 0
volatile int SemFlag = 0;
rt_sem_t led_sem;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;
rt_err_t result;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
result = rt_sem_take(led_sem, RT_WAITING_FOREVER);
//if(SemFlag)
if (result == RT_EOK)
{
//SemFlag = 0;
if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
rt_sem_release(led_sem);
//rt_thread_delay(50);
}

}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;
rt_err_t result;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

// Delay 200 ms
rt_thread_delay(200);

while(1)
{
//rt_thread_delay(100);

result = rt_sem_take(led_sem, 100);

if(result == -RT_ETIMEOUT)
{
KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
//SemFlag = 1;
rt_sem_release(led_sem);
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
else if (result == -RT_ERROR)
{
// Do Nothing
while(1);
}
else if (result == RT_EOK)
{
// Do Nothing
}
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Sem */
led_sem = rt_sem_create ("LedSem", 1, RT_IPC_FLAG_PRIO);
if(RT_NULL == led_sem)
{
return -1;
}

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF SEM /////////////

互斥量

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
///////// BEGIN OF MUTEX /////////////
#if 0
rt_mutex_t led_mutex;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;
rt_err_t result;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
result = rt_mutex_take(led_mutex, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
rt_mutex_release(led_mutex);
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;
rt_err_t result;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

result = rt_mutex_take(led_mutex, RT_WAITING_FOREVER);

// Delay 200 ms
rt_thread_delay(200);
result = rt_mutex_take(led_mutex, 100);
result = rt_mutex_take(led_mutex, 100);
rt_mutex_release(led_mutex);
rt_mutex_release(led_mutex);
rt_mutex_release(led_mutex);

return ;

while(1)
{
if(result == RT_EOK)
{
rt_thread_delay(100);
KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
rt_mutex_release(led_mutex);
rt_kprintf("Key Pressed\r\n");
rt_thread_delay(20);
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
//result = rt_mutex_take(led_mutex, 100);
}
else if (result == -RT_ERROR)
{
// Do Nothing
while(1);
}
else if (result == -RT_ETIMEOUT)
{
// Do Nothing
}
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Sem */
led_mutex = rt_mutex_create ("LedMutex", RT_IPC_FLAG_PRIO);
if(RT_NULL == led_mutex)
{
return -1;
}

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF MUTEX /////////////

事件

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
123
124
125
126
127
128
129
130
131
132
133
134
///////// BEGIN OF EVENT /////////////
#if 0
rt_event_t led_event;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;
rt_err_t result;
uint32_t event_value;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
result = rt_event_recv(led_event, 0x01, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event_value);
if (result == RT_EOK)
{
if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;
rt_err_t result;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

while(1)
{
rt_thread_delay(100);
KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
rt_event_send(led_event, 0x01);
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Event */
led_event = rt_event_create ("LedEvent", RT_IPC_FLAG_PRIO);
if(RT_NULL == led_event)
{
return -1;
}

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF EVENT /////////////

邮箱

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
123
124
125
126
127
128
129
130
131
132
133
134
///////// BEGIN OF Mailbox /////////////
#if 0
rt_mailbox_t led_mailbox;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;
rt_err_t result;
uint32_t mailbox_value;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
result = rt_mb_recv (led_mailbox, &mailbox_value, RT_WAITING_FOREVER);
if ((result == RT_EOK) && (mailbox_value == 0x01))
{
if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;
rt_err_t result;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

while(1)
{
rt_thread_delay(100);
KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
rt_mb_send (led_mailbox, 0x01);
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Event */
led_mailbox = rt_mb_create ( "LedMailbox", 4*16, RT_IPC_FLAG_PRIO);
if(RT_NULL == led_mailbox)
{
return -1;
}

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF Mailbox /////////////

消息队列

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
///////// BEGIN OF Queue /////////////
#if 0
rt_mq_t led_queue;

void rt_thread_led(void* parameter)
{

/* Init LED (54-PA5)*/
uint8_t flag = 0;
rt_device_t LEDs;
rt_err_t result;
uint32_t queue_value;

LEDs = rt_device_find("pin");
if(RT_NULL == LEDs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 54,
.mode = PIN_MODE_OUTPUT,
};

LEDs->control(LEDs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 54,
.status = PIN_LOW,
};

while(1)
{
result = rt_mq_recv (led_queue, &queue_value, sizeof(queue_value), RT_WAITING_FOREVER);
if ((result == RT_EOK) && (queue_value == 0x01))
{
if(flag)
{
pin_status.status = PIN_LOW;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
else
{
pin_status.status = PIN_HIGH;
LEDs->write(LEDs, 0, &pin_status, sizeof(struct rt_device_pin_status));
}
flag = !flag;
}
rt_thread_delay(20);
}
}

void rt_thread_key(void* parameter)
{

// Init Key(27-PC13)
rt_device_t KEYs;
rt_err_t result;

struct rt_device_pin_status old_value = {
.pin = 27,
.status = PIN_HIGH,
};

KEYs = rt_device_find("pin");
if(RT_NULL == KEYs)
{
return;
}

struct rt_device_pin_mode pin_cfg = {
.pin = 27,
.mode = PIN_MODE_INPUT,
};

KEYs->control(KEYs, RT_NULL, &pin_cfg);

struct rt_device_pin_status pin_status ={
.pin = 27,
.status = PIN_HIGH,
};

while(1)
{
rt_thread_delay(100);
KEYs->read(KEYs, 0, &pin_status, sizeof(struct rt_device_pin_status));
if(old_value.status != pin_status.status)
{
if(PIN_LOW == pin_status.status)
{
uint32_t value = 0x01;
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_mq_send (led_queue, (void*)&value, sizeof(value));
rt_kprintf("Key Pressed\r\n");
}
else if(PIN_HIGH == pin_status.status)
{
rt_kprintf("Key Released\r\n");
}
else
{
rt_kprintf("Unkown Action\r\n");
}
}

old_value.status = pin_status.status;
}
}

int rt_application_init()
{

rt_thread_t thread_handler_led;
rt_thread_t thread_handler_key;

/* Init Event */
led_queue = rt_mq_create("LedQueue", 4, 16, RT_IPC_FLAG_PRIO);
if(RT_NULL == led_queue)
{
return -1;
}

/* Init Led Thread */
thread_handler_led = rt_thread_create("led",
rt_thread_led, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3, 20);
if (thread_handler_led != RT_NULL)
rt_thread_startup(thread_handler_led);

/* Init Key Thread */
thread_handler_key = rt_thread_create("key",
rt_thread_key, RT_NULL,
512, RT_THREAD_PRIORITY_MAX/3 - 1, 20);
if (thread_handler_key != RT_NULL)
rt_thread_startup(thread_handler_key);

return 0;
}
#endif
///////// END OF Queue /////////////

参考资料

  1. RT-Thread user manual