【发布时间】:2019-02-07 20:55:25
【问题描述】:
我正在编写触发 DMA 的代码。一旦 DMA 完成其操作,它将调用ISR_Routine。问题是我想确保在 DMA 运行之前将refreshComplete 设置为0。如果 DMA 先运行,在将 refreshComplete 设置为 0 之前,则可能首先调用 ISR_Routine,从而导致 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 的代码之前运行?
【问题讨论】:
标签: c memory arm embedded race-condition