首页 > 解决方案 > 在执行后面的代码之前强制执行内存写入

问题描述

我正在编写触发 DMA 的代码。一旦 DMA 完成其操作,它将调用ISR_Routine. 问题是我想确保在 DMA 运行之前refreshComplete设置为。0如果 DMA 先运行,beforerefreshComplete设置为0,则有可能ISR_Routine首先调用 DMA,从而导致refreshComplete即使0在 DMA 成功运行之后也是如此。这意味着该ready()函数将始终返回 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 的代码之前运行?

标签: cmemoryarmembeddedrace-condition

解决方案


这是您应该查看处理器的体系结构信息和指令集的地方。

根据您的处理器的先进程度,您会发现 和 ,也许还有DMB其他一些DSBISB这些涉及强制数据传输的顺序,以及相对于其他指令的指令的顺序(因此DMB, ISB是一个公共序列)。当然,如果你在'C'中使用这些,你也需要担心语言的顺序保证。


推荐阅读