UCOS operating system - task management (2)

UCOS operating system


foreword

A task in UCOSIII consists of three parts: task stack, task control block and task function.

1. Task stack

The task stack is an important part of the task. The stack is a continuous storage space organized according to the principle of "first in, first out (FIFO)" in RAM. In order to meet the needs of saving the contents of CPU registers when switching tasks and responding to interrupts and when tasks call other functions, each task should have its own stack.

1. Create a task stack

#define START_STK_SIZE 		512	//堆栈大小
CPU_STK START_TASK_STK[START_STK_SIZE];	//定义一个数组来作为任务堆栈

CPU_STK is of CPU_INT32U type, that is, unsigned int type, which is 4 bytes, then the size of the task stack START_TASK_STK is: 512 X 4=2048 bytes!

Second, the task control block

1. Create a Mission Control Block

A task control block is a data structure used to record task-related information, and each task must have its own task control block. The task control block is created by the user

OS_TCB StartTaskTCB;  //创建一个任务控制块

OS_TCB is a structure that describes the task control block. The member variables in the task control block cannot be directly accessed by the user, let alone changed.
insert image description here
A complete creation priority stack size task control block task stack task function is actually the same as FreeRTOS
. If there are multiple tasks under the same priority, the task pointed to by HeadPtr will always run first!

3. UCOSIII system initialization

The first step is how to initialize UCOS. The function OSInit() is used to complete the initialization of UCOSIII, and OSInit() must be called before other UCOSIII functions, including OSStart().

int main(void)
{
    
    
   OS_ERR err;
   ……
   //其他函数,一般为外设初始化函数
   ……
   OSInit(&err);
   ……
   //其他函数,一般为创建任务函数
   ……
   OSStart(&err);
}

Use the function OSStart() to start UCOSIII

Fourth, task scheduling

1. Preemptive task scheduling

Task scheduling is to abort the currently running task and perform other tasks.
UCOSIII is a deprivable kernel, so when a high-priority task is ready, and task scheduling occurs at this time, then this high-priority task will get the right to use the CPU!
The task scheduling in UCOSIII is completed by the task scheduler. There are two kinds of task schedulers: task-level scheduler and interrupt-level scheduler.
The task-level scheduler is the function OSSched().
The interrupt-level scheduler is the function OSIntExit(), which uses interrupt-level task scheduling when exiting the external interrupt service function.

2. Lock and unlock task scheduling

Sometimes we don't want task scheduling to happen because there is always some code execution that cannot be interrupted. At this point, we can use the function OSSchedLock() to lock the scheduler. When we want to restore the task scheduling, we can use the function OSSchedUnlock() to unlock the locked task scheduler.

3. Time slice round robin scheduling

UCOSIII allows multiple tasks under one priority. Each task can execute a specified time (time slice), and then it is the turn of the next task. This process is time slice round-robin scheduling. When a task does not want to run, it can be abandoned. its time slice.
The time slice round robin scheduler is: OS_SchedRoundRobin().

Five, task switching

When UCOSIII needs to switch to another task, it will save the scene of the current task to the stack of the current task, mainly the CPU register value, and then restore the new scene and execute the new task. This process is task switching.

任务切换分为两种:任务级切换和中断级切换。
任务级切换函数为:OSCtxSw()
中断级切换函数为:OSIntCtxSw()

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/123873719