When do I need to use volatile in ISRs?

倾然丶 夕夏残阳落幕 提交于 2020-01-15 12:25:10

问题


I am making embedded firmware where everything after initialization happens in ISRs. I have variables that are shared between them, and I am wondering in which cases they need to be volatile. I never block, waiting for a change in another ISR.

When can I be certain that actual memory is read or written to when not using volatile? Once every ISR?

Addendum:
This is for ARM Cortex-M0, but this isn't really a question about ISRs as much as it is about compiler optimization, and as such, the platform shouldn't really be important.


回答1:


The question is entirely answerable, and the answer is simple:

Without volatile you (simplistically) can't assume that actual memory access will ever happen - the compiler is free to conclude that results are either entirely unused (if that is apparent in what it can see), or that they may be safely cached in a register, or computed out of order (as long as visible dependencies are maintained).

You need volatile to tell the compiler that the side effects of access may be important to something the optimizer is unable to analyze, such as an interrupt context or the context of a different "thread".

In effect volatile is how you say to the compiler "I know something you don't, so don't try to be clever here"

Beware that volatile does not guarantee atomicity of read-modify-write, or of even read or write alone where the data type (or its misalignment) requires muti-step access. In those cases, you risk not just getting a stale value, but an entirely erroneous one.




回答2:


it is already mentioned that actual write to memory/cache is not exactly predictable when using non volatile variables. but it is also worth mentioning about another aspect where the volatile variable might get cached and might require a forced cache flush to write in to the actual memory ( depends on whether a write-through or a write-back policy is used).

consider another case where the volatile variable is not cached ( placed in non-cacheable area) but due to the presence of write buffers and bus bridges sometimes it is not predictable when the real write happens to the intended register and it requires a dummy read to ensure that write actually happened to the real register/memory. This is particularly helpful to avoid race conditions in interrupt clearing/masking.

even though compilers are not supposed to be clever around volatile variables.it is free to do some optimizations with respect to volatile sequence points ( optimization across sequence points not permitted, but optimization between sequence points are permitted)




回答3:


The variables that need volatile are:

1) Share data between ISR and program data or other threads.
Preferable these are flags that indicate block access to various data structures.

// main() code;
disable_interrupts();
if (flag == 0) {
  flag = 1;
  enable_interrupts();
  Manipulate_data();
  flag = 0;
} else {
  enable_interrupts();
  Cope_with_data_unavailable();
}

2) Memory mapped hardware registers.
This "memory" can change at any time due to hardware conditions and the complier needs to know that their values are not necessarily consistent. Without volatile, a naive comapiler would only sample fred once resulting in a potential endless loop.

volatile int *fred = 0x1234; // Hardware reg at address 0x1234;
while (*fred);


来源:https://stackoverflow.com/questions/26188819/when-do-i-need-to-use-volatile-in-isrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!