UCOS learning (4) - task creation, deletion, suspension, recovery


  • ⏩ Hello everyone! I'm Xiaoguang, an embedded enthusiast, a sophomore who wants to become a system architect.
  • ⏩I recently started to learn the UCOS operating system, and I will update some UCOS study notes later.
  • ⏩ Today I learned how to create, delete, suspend, and resume tasks.
  • ⏩Thank you for reading, please correct me if I am wrong.

UCOSIII startup and initialization

UCOS startup and initialization steps:

  1. OSInit()Initialize UCOSIII
  2. OS_CRITICAL_ENTER()enter the critical section
  3. OSTaskCreate()create task
  4. OS_CRITICAL_EXIT()exit critical section
  5. OSStart()Open UCOS III
int main(void)
{
    
    
	OS_ERR err;
	CPU_SR_ALLOC();
	
	delay_init();       //延时初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组配置
	uart_init(115200);    //串口波特率设置
	LED_Init();         //LED初始化
	
	OSInit(&err);		//初始化UCOSIII
	OS_CRITICAL_ENTER();//进入临界区
	//创建任务1、任务2、任务3.。。。
	OSTaskCreate();				
	
	OS_CRITICAL_EXIT();	//退出临界区	 
	OSStart(&err);  //开启UCOSIII
	while(1);
}

create task

To create a task, you need to define task priority, task stack size, task control block, task stack and task function, and then call OSTaskCreate(); to create a task

//任务优先级
#define START_TASK_PRIO		3
//任务堆栈大小	
#define START_STK_SIZE 		512
//任务控制块
OS_TCB StartTaskTCB;
//任务堆栈	
CPU_STK START_TASK_STK[START_STK_SIZE];
//任务函数
void start_task(void *p_arg);

Call OSTaskCreate() to create a task, with as many as thirteen parameters

void  OSTaskCreate (OS_TCB        *p_tcb,    //任务控制块
                    CPU_CHAR      *p_name,   //任务名
                    OS_TASK_PTR    p_task,   //任务函数
                    void          *p_arg,    //传递给任务函数的参数
                    OS_PRIO        prio,     //任务优先级
                    CPU_STK       *p_stk_base,//任务堆栈数组基地址
                    CPU_STK_SIZE   stk_limit,//任务堆栈深度限位(一般是任务堆栈大小/10)
                    CPU_STK_SIZE   stk_size, //任务堆栈大小
                    OS_MSG_QTY     q_size,   //内部任务消息队列
                    OS_TICK        time_quanta,//当使能时间片轮转时的时间片长度,为0时为默认长度
                    void          *p_ext,    //指向用户补充的存储区
                    OS_OPT         opt,      //任务特定选项
                    OS_ERR        *p_err)    //保存该函数调用的错误码

Among them, the more important parameters we need to set are:

                    OS_TCB        *p_tcb,    //任务控制块
                    CPU_CHAR      *p_name,   //任务名
                    OS_TASK_PTR    p_task,   //任务函数
                    OS_PRIO        prio,     //任务优先级
                    CPU_STK       *p_stk_base,//任务堆栈数组基地址
                    CPU_STK_SIZE   stk_limit,//任务深度大小(一般是任务堆栈大小/10)
                    CPU_STK_SIZE   stk_size, //任务堆栈大小
                    OS_OPT         opt,      //任务特定选项
                    OS_ERR        *p_err)    //保存该函数调用的错误码

Other parameters can be set to 0 if not used .
Error code :

    OS_ERR_NONE                    创建成功
    OS_ERR_ILLEGAL_CREATE_RUN_TIME 试图创建一个任务在调用OSSafetyCriticalStart()后
    OS_ERR_NAME                    p_name是一个空指针
    OS_ERR_PRIO_INVALID            优先级有问题,注意五个不能使用的优先级
    OS_ERR_STK_INVALID             p_stk_base是一个空指针
    OS_ERR_STK_SIZE_INVALID        堆栈深度限位为零
    OS_ERR_TASK_CREATE_ISR         在中断中调用该函数
    OS_ERR_TASK_INVALID            p_task是一个空指针
    OS_ERR_TCB_INVALID             p_tcb是一个空指针

delete task

Delete task can call function OSTaskDel():

void  OSTaskDel (OS_TCB  *p_tcb,//任务控制块
                 OS_ERR  *p_err)//保存调用该函数的错误码

Note : Try not to delete tasks while they are running.
Error code :

    OS_ERR_NONE                  删除成功
    OS_ERR_STATE_INVALID         任务的状态无效
    OS_ERR_TASK_DEL_IDLE         试图删除空闲任务
    OS_ERR_TASK_DEL_INVALID      试图删除中断服务任务
    OS_ERR_TASK_DEL_ISR          在中断中调用该函数

suspend task

Here we need to distinguish between suspend and delete. Suspend means that the task will not be executed temporarily, and the task can be resumed later by restoring the task.
Also call function OSTaskSuspend():

void   OSTaskSuspend (OS_TCB  *p_tcb,//任务控制块
                      OS_ERR  *p_err)//错误码

error code:

     OS_ERR_NONE                      该任务已经挂起(成功)
     OS_ERR_SCHED_LOCKED              任务被锁定
     OS_ERR_TASK_SUSPEND_ISR          在中断中调用该函数
     OS_ERR_TASK_SUSPEND_IDLE         挂起空闲任务
     OS_ERR_TASK_SUSPEND_INT_HANDLER  挂起中断服务函数

recovery task

Resuming a task can resume execution of a suspended task by calling OSTaskResume():

void  OSTaskResume (OS_TCB  *p_tcb,//任务控制块
                    OS_ERR  *p_err)//错误码

error code:

     OS_ERR_NONE                  该任务已被恢复(成功)
     OS_ERR_STATE_INVALID         任务处于无效状态
     OS_ERR_TASK_RESUME_ISR       在中断中调用
     OS_ERR_TASK_RESUME_SELF      恢复自己
     OS_ERR_TASK_NOT_SUSPENDED    要恢复的任务尚未挂起

Guess you like

Origin blog.csdn.net/qq_52608074/article/details/122391574