RT_Thread learn to create the strongest notes of threads (tasks)

Introduction: Today, RT-Thread learned how to create a thread, because I came in contact with the two types RTOS, it is a freeRTOS, is a RT-Thread, so I compared those who view the process of establishing two RTOS thread is not much difference, basically exactly the same time. So there is no time to learn the feeling of learning FreeRTOS is so difficult.

Closer to home, we have to start a new thread.

1. First we define a thread control block, in fact, is the task handle  static rt_thread_t led1_thread = RT_NULL;

2. Then, we define a task entry function, in fact, create a new task.

static void led1_thread_entry(void* paremeter)
{
  while(1)
    {
        GPIO_ResetBits(GPIOD,GPIO_Pin_2);     
      rt_thread_delay (500);        
        
      GPIO_SetBits(GPIOD,GPIO_Pin_2);    
      rt_thread_delay (500);            
    
    }

}

3. Although RT-Thread * initialize hardware development board hardware initialization, RTT system initialization has been completed before the main function,
 namely rtthread_startup (in component.c file) function is complete.
 Therefore, in the main function, you only need to create a thread and start a thread.
 But for convenience and intuitive, I built a separate function board_Init used to store hardware initialization board

 

void board_Init (void)
{

     LED_Init(RCC_APB2Periph_GPIOD,GPIOD,GPIO_Pin_2,GPIO_Mode_Out_PP);
}

4.最后在主函数中调用一下就好了。

int main(void)
{
   board_Init();
    /****************创建动态线程函数************/
  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;
                                    
}

这样,我们今天新建一个RT-Thread线程就完成了。

最后,如果我写的文章真的帮了大家的一个小忙,请不要吝啬给点个赞,如果想同我一起学习进步的话,可以关注我一下,本人QQ:2992789432.
 

发布了45 篇原创文章 · 获赞 96 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_40831778/article/details/102581267