FreeRTOS任务创建与删除

FreeRTOS的任务创建以及任务的删除。

句柄:当任务创建完成之后产生一个句柄,存储起来,主要用来给其他的API函数调用,例如:函数的删除、挂起、恢复等操作。

另外,当执行vTaskDelete函数之后,被删除的任务在vTaskDelete函数之后的代码不会被再次执行。

/***********************************************

************************************************
@filename main.c
@brief the entrance of the main function
@author dongdd
@date 2018-01-22
************************************************
***********************************************/


/*include the headfiles*/
#include "include.h"


/*create the handle of tasks*/
TaskHandle_t StartTask_Handler;
TaskHandle_t Led1Task_Handler;
TaskHandle_t Led2Task_Handler;


/** 
* the function of the LED1 Task.
* when the led turn five times,then delete the LED2 task 
* and output the message on the screen.
* @param[in]   void *pvParmeters
* @param[out]  void
* @retval  
* @retval       
*/
void led1(void *pvParmeters)
{
static unsigned char led1_count = 0;

while(1)
{
led_turn(LED1);
led1_count ++;
printf_str("led1_count=");
printf_int(led1_count);
printf_str("\r\n");

if(led1_count == 5)
{
vTaskDelete(Led2Task_Handler);
printf_str("task_led1 delete the task_led2\r\n");
}
else if(led1_count == 10)
{
printf_str("task_led1 will be deleted!\r\n");
vTaskDelete(Led1Task_Handler);
}
vTaskDelay(1000);
}
}


/** 
* the function of the LED2 Task.
* the LED2 Task will be deleted after turn five times
* and output the message on the screen.
* @param[in]   void *pvParmeters
* @param[out]  void
* @retval  
* @retval       
*/
void led2(void *parameters)
{
static unsigned char led2_count = 0;

while(1)
{
led_turn(LED2);
led2_count ++;
printf_str("led2_count=");
printf_int(led2_count);
printf_str("\r\n");
vTaskDelay(1000);
}
}


/** 
* the function of the Start Task.
* inlcude the creation of other task.
* @param[in]   void *pvParmeters
* @param[out]  void
* @retval  
* @retval       
*/
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //enter critical

//create task led
xTaskCreate(led1,"led1",100,NULL,2,&Led1Task_Handler);
xTaskCreate(led2,"led2",100,NULL,3,&Led2Task_Handler);

vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}


/** 
* the entrance of the main function. 
* @param[in]   void
* @param[out]  void
* @retval  
* @retval       
*/
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD,ENABLE);

usb_debug_init(); //init the usart
led_init(LED1,LED_ON); //init led
led_init(LED2,LED_ON);
led_off(LED1); //close the led
led_off(LED2);

xTaskCreate(start_task,"start",100,NULL,1,&StartTask_Handler); //create the function of StartTask,the size of  stack is 400Bytes. 
vTaskStartScheduler(); //open the scheduler
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值