UCOSIII task creation, deletion, suspension and resumption

Publisher:SereneWandererLatest update time:2019-04-25 Source: eefocusKeywords:UCOSIII Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Task Creation


Task creation function: OSTaskCreate()


void OSTaskCreate (OS_TCB *p_tcb, //point to the task control block of the task

                                      CPU_CHAR *p_name, //task name

                                  OS_TASK_PTR p_task, //task function name

                                  void *p_arg, //Parameters passed to the task

                                  OS_PRIO prio, //Task priority

                                  CPU_STK *p_stk_base, //task stack base address

                                  CPU_STK_SIZE stk_limit, //Task stack depth

                                  CPU_STK_SIZE stk_size, //task stack size

                                  OS_MSG_QTY q_size, //Optional message queue

                                  OS_TICK time_quanta, // Enable time slice round-robin scheduling, the default clock beat is divided by 10

                                  void *p_ext, //points to the storage area supplemented by the user

                                  OS_OPT opt, //Task specific options

                                  OS_ERR *p_err) //Save error code

① First, define the priority, stack size, task control block and stack of each task


//Task priority


#define START_TASK_PRIO         3


//Task stack size    


#define START_STK_SIZE           512


//Task control block


OS_TCB StartTaskTCB;


//Task stack    


CPU_STK START_TASK_STK[START_STK_SIZE];


//Task function declaration


void start_task(void *p_arg);


② Write the main function. First, initialize the external function, then initialize UCOSIII, and then create the start task (before creating the start task, you must call the critical section function, and after creation, you must exit the critical section and then start UCOIII). The parameters passed to create the start task are some parameters defined by your own macro.


int main(void)

 

{

       OS_ERR err; //Error value: all are macro definitions. Find the meaning of the returned error value according to the corresponding value.

       CPU_SR_ALLOC();

       delay_init(168); //Clock initialization

 

       NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt group configuration

       uart_init(115200); //Serial port initialization

       LED_Init(); //LED initialization

 

       OSInit(&err); //Initialize UCOSIII

       OS_CRITICAL_ENTER(); //Enter the critical section

       //Create the start task

       OSTaskCreate((OS_TCB * )&StartTaskTCB, //Task control block

                             (CPU_CHAR * )"start task", //task name

                 (OS_TASK_PTR )start_task, //task function

                 (void *)0, //Parameters passed to the task function

                 (OS_PRIO )START_TASK_PRIO, //Task priority

                 (CPU_STK * )&START_TASK_STK[0], //task stack base address

                 (CPU_STK_SIZE)START_STK_SIZE/10, //Task stack depth limit

                 (CPU_STK_SIZE)START_STK_SIZE, //Task stack size

                 (OS_MSG_QTY) 0, //The maximum number of messages that the task internal message queue can receive. When it is 0, it is forbidden to receive messages.

                 (OS_TICK) 0, //When the time slice rotation is enabled, the time slice length is 0, which is the default length.

                 (void *) 0, // user-supplemented storage area

                 (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, //Task options

                 (OS_ERR * )&err); //Store the return value of the function when an error occurs

       OS_CRITICAL_EXIT(); //Exit the critical section

       OSStart(&err); //Start UCOSIII

       while(1);

}

③After the main function calls the creation of the start task function, the functional function of the start task function should be written. The functional function is also called in the start task function. The principle is the same as above. Then write the functions to be implemented by the functional function.


//Start task function

void start_task(void *p_arg)

{

       OS_ERR err;

       CPU_SR_ALLOC();

       p_arg = p_arg;

 

       CPU_Init();

#if OS_CFG_STAT_TASK_EN > 0u

   OSStatTaskCPUUsageInit(&err); //Statistical task               

#endif

      

#ifdef CPU_CFG_INT_DIS_MEAS_EN //If the measurement interrupt shutdown time is enabled

    CPU_IntDisMeasMaxCurReset(); 

#endif

 

#if OS_CFG_SCHED_ROUND_ROBIN_EN //When using time slice rotation

        // Enable the time slice round-robin scheduling function. The time slice length is 1 system clock beat, that is, 1*5=5ms

       OSSchedRoundRobinCfg(DEF_ENABLED,1,&err); 

#endif                 

 

       OS_CRITICAL_ENTER(); //Enter the critical section

       //Create LED0 task

       OSTaskCreate((OS_TCB       * )&Led0TaskTCB,       

                             (CPU_CHAR * )"led0 task",             

                 (OS_TASK_PTR )led0_task,                     

                 (void            * )0,                            

                 (OS_PRIO )LED0_TASK_PRIO,    

                 (CPU_STK   * )&LED0_TASK_STK[0],      

                 (CPU_STK_SIZE)LED0_STK_SIZE/10,  

                 (CPU_STK_SIZE)LED0_STK_SIZE,       

                 (OS_MSG_QTY  )0,                               

                 (OS_TICK        )0,                                  

                 (void          * )0,                            

                 (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,

                 (OS_ERR      * )&err);                                                  

       //Create LED1 task

       OSTaskCreate((OS_TCB       * )&Led1TaskTCB,       

                             (CPU_CHAR * )"led1 task",             

                 (OS_TASK_PTR )led1_task,                     

                 (void            * )0,                            

                 (OS_PRIO       )LED1_TASK_PRIO,      

                 (CPU_STK   * )&LED1_TASK_STK[0],      

                 (CPU_STK_SIZE)LED1_STK_SIZE/10,  

                 (CPU_STK_SIZE)LED1_STK_SIZE,       

                 (OS_MSG_QTY  )0,                               

                 (OS_TICK )0,                                  

                 (void          * )0,                     

                 (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,

                 (OS_ERR      * )&err);                           

       //Create a floating point test task

       OSTaskCreate((OS_TCB       * )&FloatTaskTCB,       

                             (CPU_CHAR * )"float test task",              

                 (OS_TASK_PTR )float_task,                     

                 (void            * )0,                            

                 (OS_PRIO )FLOAT_TASK_PRIO,           

                 (CPU_STK   * )&FLOAT_TASK_STK[0],    

                 (CPU_STK_SIZE)FLOAT_STK_SIZE/10,

                 (CPU_STK_SIZE)FLOAT_STK_SIZE,           

                 (OS_MSG_QTY  )0,                               

                 (OS_TICK )0,                                  

                 (void          * )0,                     

                 (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,

                 (OS_ERR      * )&err);                      

       OS_TaskSuspend((OS_TCB*)&StartTaskTCB,&err); //Suspend the start task                 

       OS_CRITICAL_EXIT(); //Enter the critical section

}

 

//led0 task function

void led0_task(void *p_arg)

{

    OS_ERR err;

    p_arg = p_arg;

    while(1)

    {

        LED0=0;

        OSTimeDlyHMSM(0,0,0,200,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 200ms

        LED0=1;

        OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 500ms

    }

}

 

//led1 task function

void led1_task(void *p_arg)

{

    OS_ERR err;

    p_arg = p_arg;

    while(1)

    {

        LED1=~LED1;

        OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 500ms

    }

}

 

 

//Floating point test task

void float_task(void *p_arg)

{

    CPU_SR_ALLOC();

    static float float_num=0.01;

    while(1)

    {

        float_num+=0.01f;

        OS_CRITICAL_ENTER(); //Enter the critical section

        printf("The value of float_num is: %.4frn",float_num);

        OS_CRITICAL_EXIT(); //Exit the critical section

        delay_ms(500); //delay 500ms

    }

}

2. Deleting a task


If we don't want to use a task anymore, we can delete it. To delete a task, we use the OSTaskDel() function. The function prototype is as follows:


void  OSTaskDel (OS_TCB  *p_tcb,


                 OS_ERR  *p_err)


   1. After deleting a task, the OS_TCB and stack it occupies can be reused to create other tasks.


   2. Although UCOSIII allows tasks to be deleted while the system is running, this operation should be avoided as much as possible. If the task may occupy resources shared with other tasks, strange results may occur if the occupied resources are not released before deleting the task.


//led0 task function

void led0_task(void *p_arg)

{

    OS_ERR err;

    p_arg = p_arg;

    while(1)

    {

        task1_num++; //Increase the number of task executions by 1. Note that task1_num1 will be cleared when it reaches 255!

        LED0=~LED0;

        OSTimeDlyHMSM(0,0,0,200,OS_OPT_TIME_HMSM_STRICT,&err); //Delay 200ms

        printf("Task 1 has been executed: %d timesrn",task1_num);

 

        if(task1_num==5)

        {

            OSTaskDel((OS_TCB*)&Task2_TaskTCB,&err); //Task 1 executes 5 and then deletes Task 2

                     printf("Task 1 deleted Task 2!rn");

        }      

    }

}

----------------------------------------------------------------------------------------------------------------------------------------------


3. Suspend and resume tasks


When we want to suspend a task but don't want to delete it, we can use the OSTaskSuspend() function to suspend the task. The function prototype is as follows:


void   OSTaskSuspend ( OS_TCB  *p_tcb,OS_ERR  *p_err)


When we want to resume a suspended task, we can call the function OSTaskResume(). The function prototype is as follows


void  OSTaskResume (OS_TCB  *p_tcb, OS_ERR  *p_err)


//led0 task function

void led0_task(void *p_arg)

{

    OS_ERR err;

    p_arg = p_arg;

    while(1)

    {

        task1_num++; //Task 1 execution times plus 1 Note that task1_num1 will be cleared when it is added to 255!!

[1] [2]
Keywords:UCOSIII Reference address:UCOSIII task creation, deletion, suspension and resumption

Previous article:UCOSIII time slice round-robin scheduler
Next article:Scheduling and switching of tasks in UCOIII

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号