最近用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指令的定義,讀者可自行查看。