Cortex M4F PendSV with FPU

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cortex M4F PendSV with FPU

665 Views
LijieDu
Contributor II

xPortPendSVHandler:

ldr r3, =pxCurrentTCB
ldr r2, [r3] /* r2 = pxCurrentTCB. */
ldr r1, [r2] /* r1 = Location where the context should be saved. */

/*------------ Save Context. ----------- */
mrs r3, control
mrs r0, psp
isb

add r0, r0, #0x20 /* Move r0 to location where s0 is saved. */
tst lr, #0x10
ittt eq
vstmiaeq r1!, {s16-s31} /* Store s16-s31. */
vldmiaeq r0, {s0-s16} /* Copy hardware saved FP context into s0-s16. */
vstmiaeq r1!, {s0-s16} /* Store hardware saved FP context. */
sub r0, r0, #0x20 /* Set r0 back to the location of hardware saved context. */

stmia r1!, {r3-r11, lr} /* Store CONTROL register, r4-r11 and LR. */
ldmia r0, {r4-r11} /* Copy hardware saved context into r4-r11. */
stmia r1!, {r0, r4-r11} /* Store original PSP (after hardware has saved context) and the hardware saved context. */
str r1, [r2] /* Save the location from where the context should be restored as the first member of TCB. */

/*---------- Select next task. --------- */
mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
#if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
cpsid i /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
#endif
msr basepri, r0
dsb
isb
#if ( configENABLE_ERRATA_837070_WORKAROUND == 1 )
cpsie i /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
#endif
bl vTaskSwitchContext
mov r0, #0
msr basepri, r0

/*------------ Program MPU. ------------ */
ldr r3, =pxCurrentTCB
ldr r2, [r3] /* r2 = pxCurrentTCB. */
add r2, r2, #4 /* r2 = Second item in the TCB which is xMPUSettings. */

dmb /* Complete outstanding transfers before disabling MPU. */
ldr r0, =0xe000ed94 /* MPU_CTRL register. */
ldr r3, [r0] /* Read the value of MPU_CTRL. */
bic r3, #1 /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
str r3, [r0] /* Disable MPU. */

ldr r0, =0xe000ed9c /* Region Base Address register. */
ldmia r2!, {r4-r11} /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
stmia r0, {r4-r11} /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */

#ifdef configTOTAL_MPU_REGIONS
#if ( configTOTAL_MPU_REGIONS == 16 )
ldmia r2!, {r4-r11} /* Read 4 sets of MPU registers [MPU Region # 4 - 7]. */
stmia r0, {r4-r11} /* Write 4 sets of MPU registers. [MPU Region # 4 - 7]. */
ldmia r2!, {r4-r11} /* Read 4 sets of MPU registers [MPU Region # 8 - 11]. */
stmia r0, {r4-r11} /* Write 4 sets of MPU registers. [MPU Region # 8 - 11]. */
#endif /* configTOTAL_MPU_REGIONS == 16. */
#endif

ldr r0, =0xe000ed94 /* MPU_CTRL register. */
ldr r3, [r0] /* Read the value of MPU_CTRL. */
orr r3, #1 /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
str r3, [r0] /* Enable MPU. */
dsb /* Force memory writes before continuing. */

/*---------- Restore Context. ---------- */
ldr r3, =pxCurrentTCB
ldr r2, [r3] /* r2 = pxCurrentTCB. */
ldr r1, [r2] /* r1 = Location of saved context in TCB. */

ldmdb r1!, {r0, r4-r11} /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
msr psp, r0
stmia r0!, {r4-r11} /* Copy the hardware saved context on the task stack. */
ldmdb r1!, {r3-r11, lr} /* r3 contains CONTROL register. r4-r11 and LR restored. */
msr control, r3

tst lr, #0x10
ittt eq
vldmdbeq r1!, {s0-s16} /* s0-s16 contain hardware saved FP context. */
vstmiaeq r0!, {s0-s16} /* Copy hardware saved FP context on the task stack. */
vldmdbeq r1!, {s16-s31} /* Restore s16-s31. */

str r1, [r2] /* Save the location where the context should be saved next as the first member of TCB. */
bx lr

 

I don't understand why it is S0~S16?

S0~S15 and status reg are pushed into stack by hardware, why copy it again?

Hope get your direction and advice!

0 Kudos
Reply
3 Replies

640 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

There seems to be an error in the code. Could you please indicate where this code is taken from?

0 Kudos
Reply

629 Views
LijieDu
Contributor II

LijieDu_0-1753141549396.png

 Hi davidtosenovjan

It comes from :FreeRTOS-Kernel/portable/IAR/ARM_CM4F_MPU at main · FreeRTOS/FreeRTOS-Kernel · GitHub

Hope to get your advice, thanks very much

0 Kudos
Reply

605 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

The code in the FreeRTOS-Kernel/portable/IAR/ARM_CM4F_MPU directory is maintained by Amazon.com, Inc.

You can do so by opening an issue or a pull request on the GitHub repository. The FreeRTOS team actively monitors these channels.

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2136881%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ECortex%20M4F%20PendSV%20with%20FPU%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2136881%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ExPortPendSVHandler%3A%3C%2FP%3E%3CP%3Eldr%20r3%2C%20%3DpxCurrentTCB%3CBR%20%2F%3Eldr%20r2%2C%20%5Br3%5D%20%2F*%20r2%20%3D%20pxCurrentTCB.%20*%2F%3CBR%20%2F%3Eldr%20r1%2C%20%5Br2%5D%20%2F*%20r1%20%3D%20Location%20where%20the%20context%20should%20be%20saved.%20*%2F%3C%2FP%3E%3CP%3E%2F*------------%20Save%20Context.%20-----------%20*%2F%3CBR%20%2F%3Emrs%20r3%2C%20control%3CBR%20%2F%3Emrs%20r0%2C%20psp%3CBR%20%2F%3Eisb%3C%2FP%3E%3CP%3Eadd%20r0%2C%20r0%2C%20%230x20%20%2F*%20Move%20r0%20to%20location%20where%20s0%20is%20saved.%20*%2F%3CBR%20%2F%3Etst%20lr%2C%20%230x10%3CBR%20%2F%3Eittt%20eq%3CBR%20%2F%3Evstmiaeq%20r1!%2C%20%7Bs16-s31%7D%20%2F*%20Store%20s16-s31.%20*%2F%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3Evldmiaeq%20r0%2C%20%7Bs0-s16%7D%20%2F*%20Copy%20hardware%20saved%20FP%20context%20into%20s0-s16.%20*%2F%3C%2FSTRONG%3E%3C%2FFONT%3E%3CBR%20%2F%3Evstmiaeq%20r1!%2C%20%7Bs0-s16%7D%20%2F*%20Store%20hardware%20saved%20FP%20context.%20*%2F%3CBR%20%2F%3Esub%20r0%2C%20r0%2C%20%230x20%20%2F*%20Set%20r0%20back%20to%20the%20location%20of%20hardware%20saved%20context.%20*%2F%3C%2FP%3E%3CP%3Estmia%20r1!%2C%20%7Br3-r11%2C%20lr%7D%20%2F*%20Store%20CONTROL%20register%2C%20r4-r11%20and%20LR.%20*%2F%3CBR%20%2F%3Eldmia%20r0%2C%20%7Br4-r11%7D%20%2F*%20Copy%20hardware%20saved%20context%20into%20r4-r11.%20*%2F%3CBR%20%2F%3Estmia%20r1!%2C%20%7Br0%2C%20r4-r11%7D%20%2F*%20Store%20original%20PSP%20(after%20hardware%20has%20saved%20context)%20and%20the%20hardware%20saved%20context.%20*%2F%3CBR%20%2F%3Estr%20r1%2C%20%5Br2%5D%20%2F*%20Save%20the%20location%20from%20where%20the%20context%20should%20be%20restored%20as%20the%20first%20member%20of%20TCB.%20*%2F%3C%2FP%3E%3CP%3E%2F*----------%20Select%20next%20task.%20---------%20*%2F%3CBR%20%2F%3Emov%20r0%2C%20%23configMAX_SYSCALL_INTERRUPT_PRIORITY%3CBR%20%2F%3E%23if%20(%20configENABLE_ERRATA_837070_WORKAROUND%20%3D%3D%201%20)%3CBR%20%2F%3Ecpsid%20i%20%2F*%20ARM%20Cortex-M7%20r0p1%20Errata%20837070%20workaround.%20*%2F%3CBR%20%2F%3E%23endif%3CBR%20%2F%3Emsr%20basepri%2C%20r0%3CBR%20%2F%3Edsb%3CBR%20%2F%3Eisb%3CBR%20%2F%3E%23if%20(%20configENABLE_ERRATA_837070_WORKAROUND%20%3D%3D%201%20)%3CBR%20%2F%3Ecpsie%20i%20%2F*%20ARM%20Cortex-M7%20r0p1%20Errata%20837070%20workaround.%20*%2F%3CBR%20%2F%3E%23endif%3CBR%20%2F%3Ebl%20vTaskSwitchContext%3CBR%20%2F%3Emov%20r0%2C%20%230%3CBR%20%2F%3Emsr%20basepri%2C%20r0%3C%2FP%3E%3CP%3E%2F*------------%20Program%20MPU.%20------------%20*%2F%3CBR%20%2F%3Eldr%20r3%2C%20%3DpxCurrentTCB%3CBR%20%2F%3Eldr%20r2%2C%20%5Br3%5D%20%2F*%20r2%20%3D%20pxCurrentTCB.%20*%2F%3CBR%20%2F%3Eadd%20r2%2C%20r2%2C%20%234%20%2F*%20r2%20%3D%20Second%20item%20in%20the%20TCB%20which%20is%20xMPUSettings.%20*%2F%3C%2FP%3E%3CP%3Edmb%20%2F*%20Complete%20outstanding%20transfers%20before%20disabling%20MPU.%20*%2F%3CBR%20%2F%3Eldr%20r0%2C%20%3D0xe000ed94%20%2F*%20MPU_CTRL%20register.%20*%2F%3CBR%20%2F%3Eldr%20r3%2C%20%5Br0%5D%20%2F*%20Read%20the%20value%20of%20MPU_CTRL.%20*%2F%3CBR%20%2F%3Ebic%20r3%2C%20%231%20%2F*%20r3%20%3D%20r3%20%26amp%3B%20~1%20i.e.%20Clear%20the%20bit%200%20in%20r3.%20*%2F%3CBR%20%2F%3Estr%20r3%2C%20%5Br0%5D%20%2F*%20Disable%20MPU.%20*%2F%3C%2FP%3E%3CP%3Eldr%20r0%2C%20%3D0xe000ed9c%20%2F*%20Region%20Base%20Address%20register.%20*%2F%3CBR%20%2F%3Eldmia%20r2!%2C%20%7Br4-r11%7D%20%2F*%20Read%204%20sets%20of%20MPU%20registers%20%5BMPU%20Region%20%23%200%20-%203%5D.%20*%2F%3CBR%20%2F%3Estmia%20r0%2C%20%7Br4-r11%7D%20%2F*%20Write%204%20sets%20of%20MPU%20registers%20%5BMPU%20Region%20%23%200%20-%203%5D.%20*%2F%3C%2FP%3E%3CP%3E%23ifdef%20configTOTAL_MPU_REGIONS%3CBR%20%2F%3E%23if%20(%20configTOTAL_MPU_REGIONS%20%3D%3D%2016%20)%3CBR%20%2F%3Eldmia%20r2!%2C%20%7Br4-r11%7D%20%2F*%20Read%204%20sets%20of%20MPU%20registers%20%5BMPU%20Region%20%23%204%20-%207%5D.%20*%2F%3CBR%20%2F%3Estmia%20r0%2C%20%7Br4-r11%7D%20%2F*%20Write%204%20sets%20of%20MPU%20registers.%20%5BMPU%20Region%20%23%204%20-%207%5D.%20*%2F%3CBR%20%2F%3Eldmia%20r2!%2C%20%7Br4-r11%7D%20%2F*%20Read%204%20sets%20of%20MPU%20registers%20%5BMPU%20Region%20%23%208%20-%2011%5D.%20*%2F%3CBR%20%2F%3Estmia%20r0%2C%20%7Br4-r11%7D%20%2F*%20Write%204%20sets%20of%20MPU%20registers.%20%5BMPU%20Region%20%23%208%20-%2011%5D.%20*%2F%3CBR%20%2F%3E%23endif%20%2F*%20configTOTAL_MPU_REGIONS%20%3D%3D%2016.%20*%2F%3CBR%20%2F%3E%23endif%3C%2FP%3E%3CP%3Eldr%20r0%2C%20%3D0xe000ed94%20%2F*%20MPU_CTRL%20register.%20*%2F%3CBR%20%2F%3Eldr%20r3%2C%20%5Br0%5D%20%2F*%20Read%20the%20value%20of%20MPU_CTRL.%20*%2F%3CBR%20%2F%3Eorr%20r3%2C%20%231%20%2F*%20r3%20%3D%20r3%20%7C%201%20i.e.%20Set%20the%20bit%200%20in%20r3.%20*%2F%3CBR%20%2F%3Estr%20r3%2C%20%5Br0%5D%20%2F*%20Enable%20MPU.%20*%2F%3CBR%20%2F%3Edsb%20%2F*%20Force%20memory%20writes%20before%20continuing.%20*%2F%3C%2FP%3E%3CP%3E%2F*----------%20Restore%20Context.%20----------%20*%2F%3CBR%20%2F%3Eldr%20r3%2C%20%3DpxCurrentTCB%3CBR%20%2F%3Eldr%20r2%2C%20%5Br3%5D%20%2F*%20r2%20%3D%20pxCurrentTCB.%20*%2F%3CBR%20%2F%3Eldr%20r1%2C%20%5Br2%5D%20%2F*%20r1%20%3D%20Location%20of%20saved%20context%20in%20TCB.%20*%2F%3C%2FP%3E%3CP%3Eldmdb%20r1!%2C%20%7Br0%2C%20r4-r11%7D%20%2F*%20r0%20contains%20PSP%20after%20the%20hardware%20had%20saved%20context.%20r4-r11%20contain%20hardware%20saved%20context.%20*%2F%3CBR%20%2F%3Emsr%20psp%2C%20r0%3CBR%20%2F%3Estmia%20r0!%2C%20%7Br4-r11%7D%20%2F*%20Copy%20the%20hardware%20saved%20context%20on%20the%20task%20stack.%20*%2F%3CBR%20%2F%3Eldmdb%20r1!%2C%20%7Br3-r11%2C%20lr%7D%20%2F*%20r3%20contains%20CONTROL%20register.%20r4-r11%20and%20LR%20restored.%20*%2F%3CBR%20%2F%3Emsr%20control%2C%20r3%3C%2FP%3E%3CP%3Etst%20lr%2C%20%230x10%3CBR%20%2F%3Eittt%20eq%3CBR%20%2F%3Evldmdbeq%20r1!%2C%20%7Bs0-s16%7D%20%2F*%20s0-s16%20contain%20hardware%20saved%20FP%20context.%20*%2F%3CBR%20%2F%3Evstmiaeq%20r0!%2C%20%7Bs0-s16%7D%20%2F*%20Copy%20hardware%20saved%20FP%20context%20on%20the%20task%20stack.%20*%2F%3CBR%20%2F%3Evldmdbeq%20r1!%2C%20%7Bs16-s31%7D%20%2F*%20Restore%20s16-s31.%20*%2F%3C%2FP%3E%3CP%3Estr%20r1%2C%20%5Br2%5D%20%2F*%20Save%20the%20location%20where%20the%20context%20should%20be%20saved%20next%20as%20the%20first%20member%20of%20TCB.%20*%2F%3CBR%20%2F%3Ebx%20lr%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CSTRONG%3EI%20don't%20understand%20why%20it%20is%20S0~S%3CFONT%20color%3D%22%23FF0000%22%3E16%3C%2FFONT%3E%3F%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ES0~S15%20and%20status%20reg%20are%20pushed%20into%20stack%20by%20hardware%2C%20why%20copy%20it%20again%3F%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EHope%20get%20your%20direction%20and%20advice%EF%BC%81%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2138321%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Cortex%20M4F%20PendSV%20with%20FPU%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2138321%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThe%20code%20in%20the%20FreeRTOS-Kernel%2Fportable%2FIAR%2FARM_CM4F_MPU%20directory%20is%20maintained%20by%20Amazon.com%2C%20Inc.%3C%2FP%3E%0A%3CP%3EYou%20can%20do%20so%20by%20opening%20an%20issue%20or%20a%20pull%20request%20on%20the%20GitHub%20repository.%20The%20FreeRTOS%20team%20actively%20monitors%20these%20channels.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2137775%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Cortex%20M4F%20PendSV%20with%20FPU%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2137775%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22LijieDu_0-1753141549396.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22LijieDu_0-1753141549396.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348648i9625467BF90FF6EB%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22LijieDu_0-1753141549396.png%22%20alt%3D%22LijieDu_0-1753141549396.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3BHi%20davidtosenovjan%3C%2FP%3E%3CP%3EIt%20comes%20from%20%3A%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FFreeRTOS%2FFreeRTOS-Kernel%2Ftree%2Fmain%2Fportable%2FIAR%2FARM_CM4F_MPU%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EFreeRTOS-Kernel%2Fportable%2FIAR%2FARM_CM4F_MPU%20at%20main%20%C2%B7%20FreeRTOS%2FFreeRTOS-Kernel%20%C2%B7%20GitHub%3C%2FA%3E%3C%2FP%3E%3CP%3EHope%20to%20get%20your%20advice%2C%20thanks%20very%20much%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2137345%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Cortex%20M4F%20PendSV%20with%20FPU%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2137345%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%3CSPAN%20class%3D%22HwtZe%22%3E%3CSPAN%20class%3D%22jCAhz%20ChMk0b%22%3E%3CSPAN%20class%3D%22ryNqvb%22%3EThere%20seems%20to%20be%20an%20error%20in%20the%20code.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22jCAhz%20ChMk0b%22%3E%3CSPAN%20class%3D%22ryNqvb%22%3ECould%20you%20please%20indicate%20where%20this%20code%20is%20taken%20from%3F%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E