Ucos ii kernel 2: clock beat
In ucos ii, clock beats are as important as human hearts ,. It plays a vital role in the smooth switch between tasks of the CPU.
Ucos ii requires the user to provide the clock source. In the muc21 project, I used the timer 0 as the clock source. Note the following when using the clock source:RequiredStart a multi-task SystemLaterEnable the timer.,That is, after osstart () is called.
The clock cycle service in ucos ii is implemented by calling ostimetick () in the timer interrupt service subroutine.
The ostimetick () code is as follows:
Void ostimetick (void) |
{ |
OS _tcb * ptcb; |
|
Ostimetickhook (); (1) |
Ptcb = ostcblist; (2) |
While (ptcb-> ostcbprio! = OS _idle_prio) {(3) |
OS _enter_critical (); |
If (ptcb-> ostcbdly! = 0 ){ |
If (-- ptcb-> ostcbdly = 0 ){ |
If (! (Ptcb-> ostcbstat & OS _stat_suspend) {(4) |
Osrdygrp | = ptcb-> ostcbbity; (5) |
Osrdytbl [ptcb-> ostcby] | = ptcb-> ostcbbitx; |
} Else { |
Ptcb-> ostcbdly = 1; |
} |
} |
} |
Ptcb = ptcb-> ostcbnext; |
OS _exit_critical (); |
} |
OS _enter_critical (); (6) |
Ostime ++; (7) |
OS _exit_critical (); |
} |
We know that the CPU always executes the tasks with the highest priority in the ready table. If a task is in the ready table, you have to rely on ostimetick. Ostimetick () greatly reduces the time delay item ostcbdly in OS _tcb for each user's task control block by 1. When the time delay item ostcbdly in the task control block of a task is reduced to zero, this task enters the ready state.
The following is an analysis of the above Code.
Ostimtick () first calls the ostimetickhook () function, which can be defined by the user. This function can extend the ostimetick () function [l3.2 (1)]. The author of ucos ii decided that the first call of ostimtickhook () was intended to give the user a chance to do something at the beginning of the clock cycle interruption service, because the user may have time to do demanding work. A large amount of work in ostimtick () is to give each user a task control block OS _tcb a time delay item ostcbdly minus 1 (if this item is not zero ). Ostimtick () starts from ostcblist and runs it along the OS _tcb linked list. It keeps idle tasks [l3.21 (3)]. When the time delay item ostcbdly in the task control block of a task is reduced to zero, the task enters the ready state [l3.21 (5)]. The exact function ostasksuspend () suspended by the task does not enter the ready state [l3.21 (4)]. The execution time of ostimtick () is directly proportional to the number of tasks created in the application. Ostimetick () also uses an unsigned 32-bit variable by calling ostime [l3.21 (7. Note that the disconnection is used before the ostime is added to 1, because most microprocessors have to use multiple commands for 32-digit plus 1 operations.