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
| #include "rtthread.h"
#include "main.h"
/*
*************************************************************************
* 变量
*************************************************************************
*/
/* 定义线程控制块 */
static rt_thread_t led1_thread = RT_NULL;
static rt_thread_t key_thread = RT_NULL;
/*
*************************************************************************
* 函数声明
*************************************************************************
*/
static void led1_thread_entry(void* parameter);
static void key_thread_entry(void* parameter);
int main(void)
{
rt_kprintf("prss K1 suspend,prss K2 resume\n");
led1_thread = /* 线程控制块指针 */
rt_thread_create( "led1", /* 线程名字 */
led1_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
3, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (led1_thread != RT_NULL)
rt_thread_startup(led1_thread);
else
return -1;
key_thread = /* 线程控制块指针 */
rt_thread_create( "key", /* 线程名字 */
key_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
2, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (key_thread != RT_NULL)
rt_thread_startup(key_thread);
else
return -1;
}
/**
* @brief led1_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void led1_thread_entry(void* parameter)
{
while (1)
{
LED1(ON);
rt_thread_delay(500); /* 延时500个tick */
rt_kprintf("led1_thread running,LED1_ON\r\n");
LED1(OFF);
rt_thread_delay(500); /* 延时500个tick */
rt_kprintf("led1_thread running,LED1_OFF\r\n");
}
}
/**
* @brief key_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void key_thread_entry(void* parameter)
{
rt_err_t uwRet = RT_EOK;
while (1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )/* K1 被按下 */
{
printf("suspend LED1 thread!\n");
uwRet = rt_thread_suspend(led1_thread);/* 挂起LED1线程 */
if(RT_EOK == uwRet)
{
rt_kprintf("suspend LED1 thread success! \n");
}
else
{
rt_kprintf("suspend LED1 thread fail,err:0x%lx\n",uwRet);
}
}
if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )/* K1 被按下 */
{
printf("resume LED1 thread!\n");
uwRet = rt_thread_resume(led1_thread);/* 恢复LED1线程! */
if(RT_EOK == uwRet)
{
rt_kprintf("resume LED1 thread success!\n");
}
else
{
rt_kprintf("resume LED1 thread fail,err:0x%lx\n",uwRet);
}
}
rt_thread_delay(20);
}
}
|