μcos-II task scheduling

I saw the blog of a netizen and I felt it was better, so I copied it

The main reason is that I know that task scheduling is to call OS_Sched, but I have not seen the specific implementation of this function, or that I have seen this implementation

Didn't realize that the soft interrupt was used to save the current task on-site.

It's connected here, and I wondered if I did this last night, it seems that it is.

 

//Task scheduling
//uCOS-II always runs the highest priority task in the ready state. Determine which task has the highest priority, and which task will be
executed next is done by the scheduler (Scheduler). Task-level scheduling is completed by the function OSSched(). Interrupt-level scheduling is
the Scheduling completed by another function OSIntExt() .
//Note: 1) This is an uCOS-II internal function, you can't use it in the application
// 2) Lock the scheduler to prohibit task scheduling (see OSSchedLock() function)
//Description: 1) Task switching is very simple. It is completed by the following two steps. The microprocessor registers of the suspended task are pushed onto the stack, and then
the register value of the higher priority // level task is restored from the stack to the register. In uCOS-II, the stack structure of ready tasks always looks
like an interrupt has just occurred, and all microprocessor registers are stored in the stack. In other words, all uCOS-II
has to do to run the task in the ready state is to restore all CPU registers and run the interrupt return instruction. In order to do task switching, run
// OS_TASK_SW(), artificially imitating an interrupt. Most microprocessors have soft interrupt instructions or trap instructions TRAP to achieve the above
operations. Interrupt service subroutine or trap hardler, also called exception handler,
// must provide interrupt vector to assembly language function OSCtxSw(). OSCtxSw() requires OS_TCBHighRdy to point to the upcoming
// For suspended tasks, you also need to make the current task control block OSTCBCur point to the task that is about to be suspended, port uCOS-II, there is
a more detailed explanation about //OSCtxSw().
// 2) All codes of OS_Sched() are critical section codes. In the process of looking for the task with the highest priority to enter the ready state, in order to prevent the
interrupt service subroutine from setting the ready bit of one or several tasks, the interrupt is turned off. In order to shorten the switching time, all OSSched()
// codes can be written in assembly language. To increase readability, portability and minimize assembly language code, OSSched() is written in C.

//Task scheduling function
void OS_Sched (void)
{ #if OS_CRITICAL_METHOD == 3 //Interrupt function is set to mode 3     OS_CPU_SR cpu_sr; #endif         INT8U y; //Define an 8-bit integer y




    OS_ENTER_CRITICAL(); //Turn off interrupts
    //If the number of interrupt nesting times> 0 and the number of lock (scheduler) nesting times> 0, the function exits without any scheduling
    if ((OSIntNesting == 0) && (OSLockNesting = = 0)) { 
        //If the function is not called in the interrupt service subroutine and scheduling is allowed, the task scheduling function will find the
        highest priority task that enters the ready state, and the task that enters the ready state is in the ready table The corresponding bit in OSRdyTbl[].
        y = OSUnMapTbl[OSRdyGrp]; /* Get pointer to HPT ready to run */
        OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
        //After finding the highest priority task, function Check whether the task with the highest priority is the currently running task to avoid
        unnecessary task scheduling and spend more time
        if (OSPrioHighRdy != OSPrioCur) {/* No Ctx Sw if current task is highest rdy */
            / /In order to achieve task switching, OSTCBHighRdy must point to the task control block OS_TCB with the highest priority.
            //The element in the OSTCBPrioTbl[] array with OSPrioHighRdy as the subscript is assigned to OSTCBHighRdy realized by
            OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
            OSCtxSwCtr++; //Statistical counter OSCtxSwCtr + 1 to track the number of task switching
            OS_TASK_SW(); //Finally The macro calls OS_TASK_SW() to complete the actual task switching
        }
    }
    OS_EXIT_CRITICAL(); //Open interrupt
}

Guess you like

Origin blog.csdn.net/yangkunhenry/article/details/105134635