Detailed UCOS task switching

1 UCOS knowledge

(1) UCOS is a preemptive multitasking kernel. When a high-priority task is ready, and task scheduling occurs at this time, then the high-priority task will get the right to use the CPU.

(2) mission is to terminate the current task scheduler running instead to perform other tasks.

(3) Each task has its own task control block: OS_TCB type (type of structure). For maintenance tasks related information, such as address the task stack, stack capacity, the current task status. OS_TCB stored in the RAM.

(4) Each task has its own stack : OS_STK type (Array). Stack may static allocation may be used malloc () function is dynamically allocated from the heap. Due to the frequent create and delete tasks can lead to memory fragmentation produced, it is generally stack space does not need to be released. First, save the task stack effect of local variables; saving CPU registers (ie site) Second, when a task hangs.

(5) the task can access the global variables, but global variables are shared by multiple tasks, and the task is preemptive, and therefore unsafe to do so, UCOS provides protection mechanisms (semaphore, mutex semaphores).

2 UCOS task switching

(1) task in the sleep state (refer to the task has been stored in memory, but not UCOS management);

(2) by OSTaskCreate () function, the task in a ready state; task ready list of ready tasks have been saved.

(3) The Task scheduling occurred. Task scheduling in two ways: a task-level scheduler OSSched (); 2 interrupt level scheduler OSIntExit ()..

  OSSched ():

  (1) Scanning ready task table ready tasks the highest priority task;

  (2) Get the highest priority to the control block of the task;

  (3) the CPU to save the current task register to the value of the stack inside the CPU and the value of the stack pointer register (TSP) is stored in the OS_TCB;

  (4) the value holding TSP OS_TCB new tasks in the stack pointer register is loaded into the CPU (TSP);

  (5) from the new stack to restore CPU registers, at this time a new task on the run .

  OSIntExit():

  (1) Save interrupt nesting counter OSIntNestingCtr 1

  (2) If OSIntNestingCtr = 0, i.e. the last layer of nested interrupts (on return to the task level), and the scheduler is not locked

  (3) obtaining the highest priority to the control block of the task;

  (4) because before entering the interrupt service routine tasks scene has been saved, so here it goes again to save;

  (5) the value holding TSP OS_TCB new tasks in the stack pointer register is loaded into the CPU (TSP);

  (6) from the new stack to restore CPU registers, at this time a new task on the run .

Remarks:

OSSched () is actually called OS_TASK_SW (), OS_TASK_SW () is called OSCtxSw (), and OSCtxSw () the substance is entering an exception interrupt service routine OS_CPU_PendSVHandler

Task scheduling point 3

3.1 calls OSSched () case:

  (1) sends a message to another task or semaphore, OS ??? Post ()

  (2) mission call delay function OSTimeDly () or OSTimeDlyHMSM ()

  (3) a task waiting time occurs (semaphore or message) OS ??? Pend ()

  (4) Canceled task waiting OS ??? PendAbord ()

  (5) create a higher priority task OSTaskCreate ()

  (6) the current task is deleted OSTaskDel ()

  (7) the task itself is suspended OSTaskSuspend ()

  (8) users to directly call OSSched ()

3.2 calls OSIntExit () case:

  At the end of all the nested interrupts, before returning to the task you want to call OSIntExit () function

Guess you like

Origin www.cnblogs.com/Mike2019/p/12548536.html