ARM RFEIA指令作用
FreeRTOS第一个任务如何run起来的
在给ARM cortex R5适配FreeRTOS的过程中,在执行第一个task时,都是使用vTaskStartScheduler()函数,把第一个task运行起来的,其中比较关键在port.c实现的xPortStartScheduler()函数中,在这个函数,会调用vPortRestoreTaskContext()函数,把第一个task给运行起来。主要流程如下:
vTaskStartScheduler()xPortStartScheduler()vPortRestoreTaskContext()
在vPortRestoreTaskContext()的实现主要如下:
portRESTORE_CONTEXT MACRO/* Set the SP to point to the stack of the task being restored. */LDR R0, pxCurrentTCBConstLDR R1, [R0]LDR SP, [R1]/* Is there a floating point context to restore? If the restoredulPortTaskHasFPUContext is zero then no. */LDR R0, ulPortTaskHasFPUContextConstPOP {R1}STR R1, [R0]CMP R1, #0/* Restore the floating point context, if any. */POPNE {R0}/*VPOPNE {D16-D31}*/VPOPNE {D0-D15}VMSRNE FPSCR, R0/* Restore the critical section nesting depth. */LDR R0, ulCriticalNestingConstPOP {R1}STR R1, [R0]/* Ensure the priority mask is correct for the critical nesting depth. */CMP R1, #0MOVEQ R0, #255LDRNE R0, ulMaxAPIPriorityConstLDRNE R0, [R0]LDR R4, vPortSetInterruptMaskAsmConstBLX R4/* Restore all system mode registers other than the SP (which is alreadybeing used). */POP {R0-R12, R14}/* Return to the task code, loading CPSR on the way. */RFEIA sp!ENDMvPortRestoreTaskContext:/* Switch to system mode. */CPS #SYS_MODEportRESTORE_CONTEXT
从portRESTORE_CONTEXT宏的实现来看,代码非常简单,就是从当前任务中获取到栈指针,然后赋值给sp,然后就把该task的stack内容给恢复出来。获取栈指针如下图所示:
未获取栈指针之前,系统使用的sp如下:
获取task栈指针之后,系统使用的sp如下:
恢复了task的cpu context后,没有看到给PC指针赋值的情况,依赖RFEIA sp!这条指令,就可以把pc赋值了,如下所示:
可以看出,RFEIA sp!这条指令是从栈里把pc恢复了,pc就会往0x0040f160这个地址跳转过去,这个地址刚好是当前运行任务的入口地址,如下所示:
ARM技术手册对RFE指令说明如下: