OSIntCtxSw()

OSIntCtxSw() (see os_cpu_a.asm) is called when the ISR level scheduler (OSIntExit()) determines that a new high priority task is ready to execute. The figure below shows the state of several µC/OS-III variables and data structures just prior to calling OSIntCtxSw().

Figure - Variables and data structures prior to calling OSIntCtxSw()


µC/OS-III assumes that CPU registers are saved onto the task’s stack at the beginning of an ISR (see Interrupt Management). Because of this, notice that OSTCBCurPtr->StkPtr contains a pointer to the top-of-stack pointer of the task being suspended (the one on the left). OSIntCtxSw() does not have to worry about saving the CPU registers of the suspended task since that has already been done.

The figure below shows the operations performed by OSIntCtxSw() to complete the second half of the context switch. This is exactly the same process as the second half of OSCtxSw().

Figure - Operations performed by OSIntCtxSw()

(1) OSIntCtxSw() loads the CPU stack pointer with the saved top-of-stack from the new task’s OS_TCB. R14 = OSTCBHighRdyPtr->StkPtr.

(2) OSIntCtxSw() then retrieves the CPU register contents from the new stack. The program counter and status registers are generally retrieved at the same time by executing a return from interrupt instruction.