C语言嵌套汇编在嵌入式中的一些案例
作者 | strongerHuang
微信公众号 | 嵌入式专栏
做嵌入式底层软件开发的工程师,或多或少都会接触一些汇编代码。
前面为大家分享《如何在单片机中用汇编优雅的点灯》,你会发现,除了C语言,汇编也能点灯。
今天就来说说关于汇编在C中的定义和调用,以及C语言嵌套汇编代码在嵌入式中的一些案例。
嵌套汇编与编译器有关
; Assembler Code Here
C中嵌套汇编代码
1.在C函数中定义一段汇编代码;
2.在C文件中定义一个汇编函数;
__ASM uint32_t __get_PSP(void){ mrs r0, psp bx lr}uint32_t __get_PSP(void){ __ASM("mrs r0, psp"); __ASM("bx lr");}案例
static portFORCE_INLINE void vPortRaiseBASEPRI( void ){uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;
__asm { /* Set BASEPRI to the max syscall priority to effect a critical section. */ msr basepri, ulNewBASEPRI dsb isb }}__asm void xPortPendSVHandler( void ){ extern uxCriticalNesting; extern pxCurrentTCB; extern vTaskSwitchContext;
PRESERVE8
mrs r0, psp isb
ldr r3, =pxCurrentTCB /* Get the location of the current TCB. */ ldr r2, [r3]
stmdb r0!, {r4-r11} /* Save the remaining registers. */ str r0, [r2] /* Save the new top of stack into the first member of the TCB. */
stmdb sp!, {r3, r14} mov r0, msr basepri, r0 dsb isb bl vTaskSwitchContext mov r0, msr basepri, r0 ldmia sp!, {r3, r14}
ldr r1, [r3] ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */ ldmia r0!, {r4-r11} /* Pop the registers and the critical nesting count. */ msr psp, r0 isb bx r14 nop}__asm void NVIC_CoreReset_a(void){ LDR R0, =0xE000ED0C LDR R1, =0x05FA0001 STR R1, [R]deadloop_Core B deadloop_Core}相关文章