__DSB() __ISB()命令

dsters發表於2020-12-23

__DSB() __ISB()命令

等待指令和資料同步,例如*ocramAddr = 0xCCU;執行完成後,實際微控制器可能還未完成執行,使用__DSB() __ISB()命令可以確保該行程式碼執行完成

static void OCRAM_Access(void)
{
    uint32_t *ocramAddr = (uint32_t *)APP_FLEXRAM_OCRAM_START_ADDR;

    /* enable FLEXRAM OCRAM access error interrupt*/
    FLEXRAM_EnableInterruptSignal(APP_FLEXRAM, kFLEXRAM_OCRAMAccessError);

    for (;;)
    {
        *ocramAddr = 0xCCU;
        /* Synchronizes the execution stream with memory accesses */
        APP_DSB();
        APP_ISB();

        /* check ocram access error event */
        if (s_flexram_ocram_access_error_match)
        {
            s_flexram_ocram_access_error_match = false;
            PRINTF("\r\nOCRAM access to 0x%x boundary.\r\n", ocramAddr);
            break;
        }

        ocramAddr++;
    }
}

 

程式通過中斷訊號進入中斷處理函式時,首先應當清除相應的中斷標誌位,但有些CPU的時鐘太快,快於中斷使用的時鐘,就會出現清除中斷標誌的動作還未完成,CPU就又一次重新進入同一個中斷處理函式,導致死迴圈,__DSB() 指令的作用就是避免上述情況的發生。以下程式碼是NXP RT1021 微控制器解決上述問題的程式碼:

/*! @name ISR exit barrier
 * @{
 *
 * ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
 * exception return operation might vector to incorrect interrupt.
 * For Cortex-M7, if core speed much faster than peripheral register write speed,
 * the peripheral interrupt flags may be still set after exiting ISR, this results to
 * the same error similar with errata 83869.
 */
#if (defined __CORTEX_M) && ((__CORTEX_M == 4U) || (__CORTEX_M == 7U))
#define SDK_ISR_EXIT_BARRIER __DSB()
#else
#define SDK_ISR_EXIT_BARRIER
#endif

 

相關連結

https://blog.csdn.net/missiler/article/details/107796862

https://blog.csdn.net/weixin_34384915/article/details/91637912

相關文章