Cac*_*ito 5 c atomic interrupt volatile memory-barriers
让x和y成为在主代码和中断代码之间共享的变量。
我的想法volatile是,它只是并且始终需要用于也在主代码中使用的硬件变量和中断变量。
通过禁用中断,主代码中的x和 的每次使用y都保证是原子的。
做x和y真正需要的是volatile,还是足够使用它们来强制重装从RAM中的变量之前把内存屏障?
一种)
volatile bool x;
volatile int y[100];
int main(void)
{
while (true) {
disable_interrupts();
if (x)
work(y);
x = false;
enable_interrupts();
}
}
Run Code Online (Sandbox Code Playgroud)
乙)
bool x;
int y[100];
int main(void)
{
while (true) {
memory_barrier();
disable_interrupts();
if (x)
work(y);
x = false;
enable_interrupts();
}
}
Run Code Online (Sandbox Code Playgroud)
目标是:
让编译器优化work()。
能够使用标准库函数,例如memcpy()(那些不能与volatile变量一起使用)。
编辑:添加中断示例
interrupts.c:
extern volatile? int x;
extern volatile? int y;
void interrupt(void)
{
x = true;
REGY1 = y[7];
y[23] = REGY2;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
575 次 |
| 最近记录: |