gpt4 book ai didi

c - 在执行后续代码之前强制执行内存写入

转载 作者:行者123 更新时间:2023-11-30 20:09:26 24 4
gpt4 key购买 nike

我正在编写触发 DMA 的代码。一旦 DMA 完成其操作,它将调用ISR_Routine。问题是我想确保在 DMA 运行之前将 refreshComplete 设置为 0。如果 DMA 在 refreshComplete 设置为 0 之前首先运行,则可能首先调用 ISR_Routine 导致 refreshComplete即使 DMA 成功运行后, 仍为 0。这意味着 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 的代码之前运行?

最佳答案

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

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

关于c - 在执行后续代码之前强制执行内存写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com