首先必须了解,在将ucos-II移植到ARM920T时,使用了如下结构的任务栈:
程序清单:基于ARM920T的OSCtxSw实现分析
注意:ARM需手动保存pc和psw
OSCtxSw
; Special optimised code below:
;1、根据任务栈结构,分别压栈,保存旧任务的现场
stmfd sp!,{lr}; push pc (lr should be pushed in place of PC)
stmfd sp!,{r0-r12,lr}; push lr & register file
mrs r4,cpsr
stmfd sp!,{r4}; push current psr
mrs r4,spsr
stmfd sp!,{r4}; push current spsr
; 2、OSPrioCur = OSPrioHighRdy
ldr r4,=OSPrioCur
ldr r5,=OSPrioHighRdy
ldrb r6,[r5]
strb r6,[r4]
; 3、Get current task TCB address:r5=OSTCBCur
ldr r4,=OSTCBCur; r4 = &OSTCBCur
ldr r5,[r4]
;4、OSTCBCur->OSTCBStkPtr = SP;
str sp,[r5]; store sp in preempted tasks's TCB
bl OSTaskSwHook; call Task Switch Hook
; 5、Get highest priority task TCB address:r6=OSTCBHighRdy
ldr r6,=OSTCBHighRdy
ldr r6,[r6]
;6、SP = OSTCBHighRdy ->OSTCBStkPtr;
ldr sp,[r6]; get new task's stack pointer
; 7、OSTCBCur = OSTCBHighRdy
str r6,[r4]; set new current task TCB address
;8、根据任务栈结构,分别出栈,恢复新任务的现场
ldmfd sp!,{r4}; pop new task's spsr
msr SPSR_cxsf,r4
ldmfd sp!,{r4}; pop new task's psr
msr CPSR_cxsf,r4
ldmfd sp!,{r0-r12,lr,pc}; pop new task's r0-r12,lr & pc