线程管理RT-Thread篇5

Posted:   2020-09-09

Status:   Completed

Tags :   STM32 RTOS RT-Thread

Categories :   STM32 RTOS RT-Thread

Previous:   重映射串口到rt_kprintf函数RT-Thread篇4

Next:   消息队列RT-Thread篇6


RT-Thread启动过程

graph LR
    A(Reset_Handler) --- B(SystemInit)
    A --- C(__main)
    C -->D($Sub$$main)
    D -->E(rtthread_startup)
    E ---F(rt_hw_interrupt_disable)
    E --bord.c---G(rt_hw_board_init)
    E ---H(rt_show_version)
    E ---I(rt_system_timer_init)
    E ---J(rt_system_scheduler_init)
    E ---K(rt_system_signal_init)
    E ---L(rt_application_init)
    E ---M(rt_system_timer_thread_init)
    E ---N(rt_thread_idle_init)
    E ---O(rt_system_scheduler_start)
    L -->P(main_thread_entry)
    P -->Q($Super$$main)
    Q -->S(main)

线程管理

  • 修改main.c
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);
    }
}

调试

  • 编译下载到开发板
  • 按下K1,可以看到开发板上的灯也不闪烁了,同时在串口调试助手也输出了相应的信息,说明线程已经被挂起,
  • 按下K2,可以看到开发板上的灯也恢复闪烁了,同时在串口调试助手也输出了相应的信息,说明线程已经被恢复