在执行后面的代码之前强制执行内存写入

Enforcement of memory write before execution of later code

我正在编写触发 DMA 的代码。一旦 DMA 完成其操作,它将调用 ISR_Routine。问题是我想确保在 DMA 运行 之前将 refreshComplete 设置为 0。如果 DMA 运行 在 refreshComplete 设置为 0 之前首先被调用,则 ISR_Routine 可能首先被调用导致 refreshComplete0 即使在 DMA 运行 成功之后。这意味着 ready() 函数将始终 return 0,阻止对 DMA 的任何进一步使用。

我现在编写代码的方式是 refreshComplete 变量是 volatile 我忙着等到回读变量是 0 在 DMA 运行就像这样:

volatile uint8 refreshComplete = 0u;

void trigger(void)
{
    /* Write 0 and then busy wait */
    refreshComplete = 0;
    while (refreshComplete != 0);

    /* Code to start the DMA */
    ...
}

/* ISR called once the DMA has completed its operation */
void ISR_Routine(void)
{
    refreshComplete = 1u;
}

/* Function to check the status of the DMA */
uint8 ready(void)
{
    return refreshComplete;
}

有没有一种方法可以保证设置 refreshComplete 的代码始终在设置代码和 运行 DMA 之前 运行s?

此时您应该查看处理器的体系结构信息和指令集。

您会发现 DMBDSBISB,也许还有其他一些,具体取决于您的处理器的先进程度。这些涉及强制执行数据传输的顺序,以及与其他指令相关的指令(因此 DMB, ISB 是一个常见的顺序)。当然,如果你在 'C' 中使用这些,你也需要担心语言的顺序保证。