RAM汇编指令DMB、DSB、ISB、SEV等

最近用keil调试STM32时,在代码中遇到了一些汇编指令,如DMB、DSB、ISB、SEV,现总结如下:

DMB、DSB、ISB、SEV等指令都属于RAM汇编指令,在《ARM Cortex-M0权威指南》和《ARM Cortex-M3权威指南》中,都有这些指令。读者可以参考这两本书的附录部分。其他RAM系列的权威指南应该也有这些指令,我这里只有这两本权威指南,用到其他系列时,读者可自行查证。

以《ARM Cortex-M0权威指南》为例,我在这里列举一些我用到的汇编指令及其用法,更多汇编指令请参考权威指南。(这些汇编指令都是不带参数的,以下指令只适用于使用Cortex-M0作为内核的芯片,使用其他架构的芯片不一定适用,需要查询其参考手册确定

CPSIE I 清除PRIMASK(使能中断);在符合CMSIS的设备驱动库中,可以使用“__enable_irq()”实现该操作
CPSID I 设置PRIMASK(禁止中断);在符合CMSIS的设备驱动库中,可以使用“__disable_irq()”实现该操作
DMB 数据存储器屏障,确保在新的存储器访问开始之前,所有的存储器访问已经完成。在符合CMSIS的设备驱动库中,可以使用“__DMB”函数实现该操作
DSB 数据同步屏障,确保在下一条指令开始执行前,所有的存储器访问已经完成。在符合CMSIS的设备驱动库中,可以使用“__DSB”函数实现该操作
ISB 指令同步屏障,清除流水线并且确保在新指令执行时,之前的指令都已经执行完毕。在符合CMSIS的设备驱动库中,可以使用“__ISB”函数实现该操作
NOP 无操作。在符合CMSIS的设备驱动库中,可以使用“__NOP()”实现该操作
SEV 多处理器环境中向所有的处理器发送事件(包括自身)。在符合CMSIS的设备驱动库中,可以使用“__SEV()”实现该操作
WFE 等待事件,如果没有之前该事件的记录,进入休眠模式;如果有的话,则清除事件锁存并继续执行;在符合CMSIS的设备驱动库中,可以使用“__WFE()”函数实现该操作,不过若你使用供应商特定的休眠模式,效果会更好
WFI 等待中断,进入休眠模式。在符合CMSIS的设备驱动库中,可以使用“__WFI()”实现该操作,不过若你使用供应商特定的休眠模式,效果会更好
YIELD 用于线程切换,表明任务被延迟了,在Cortex-M0上效果和NOP一样

这些汇编指令初学者也许不会用到,一般我们都用符合CMSIS设备驱动库编程,读者可以在core_cminstr.h文件中中找到这些指令的声明和定义。部分源码如下:


/** \brief  No Operation

    No Operation does nothing. This instruction can be used for code alignment purposes.
 */
#define __NOP                             __nop


/** \brief  Wait For Interrupt
    Wait For Interrupt is a hint instruction that suspends execution
    until one of a number of events occurs.
 */
#define __WFI                             __wfi


/** \brief  Wait For Event
    Wait For Event is a hint instruction that permits the processor to enter
    a low-power state until one of a number of events occurs.
 */
#define __WFE                             __wfe


/** \brief  Send Event
    Send Event is a hint instruction. It causes an event to be signaled to the CPU.
 */
#define __SEV                             __sev


/** \brief  Instruction Synchronization Barrier
    Instruction Synchronization Barrier flushes the pipeline
    in the processor, so that all instructions following the ISB
    are fetched from cache or  memory, after the instruction
    has been completed.
 */
#define __ISB()                           __isb(0xF)


/** \brief  Data Synchronization Barrier
    This function acts as a special kind of Data Memory Barrier. 
    It completes when all explicit memory accesses before
    this instruction complete.
 */
#define __DSB()                           __dsb(0xF)

/** \brief  Data Memory Barrier
    This function ensures the apparent order of the explicit memory
    operations before and after the instruction,
    without ensuring their completion.
 */
#define __DMB()                           __dmb(0xF)


/** \brief  Reverse byte order (32 bit)
    This function reverses the byte order in integer value.
    \param [in] value Value to reverse
    \return Reversed value
 */
#define __REV                             __rev

当然这个头文件中还有其他cortex指令的定义,读者可自行查看。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章