2

I'm using nrf52 microcontroller (CORTEX 34F) processor. I have a variable check on the main loop which is modified both on the main loop and timer interrupt routine.

  1. main loop check if variable is true :

    • execute condition code

    • set variable to false

  2. timer interrupt routine set variable to true every 10 ms

without volatile keyword, the code seems not working, but when I set the variable to volatile it seems to work but I'm not convinced because :

  1. first I think cortex M4f doesn't contain data cache memory
  2. second : this case is handeld by the compiler (arm keil)

any answer please ;

if true execute body code 2. List item

bouqbouq
  • 973
  • 2
  • 14
  • 34
  • 1
    The answer is here http://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c – dkolmakov Nov 23 '16 at 09:25
  • 2
    @dkolmakov : That is a more-or-less duplicate, but crucially perhaps does not address the misunderstanding between the hardware function of *cache memory* and the software technique of *"caching a value in a register"*. – Clifford Nov 23 '16 at 09:44
  • It isn't needed if the compiler is good and realizes that it can't run around and assume things about variables shared with ISRs/callbacks. Many embedded system compilers are not that good, however. – Lundin Nov 23 '16 at 13:40

2 Answers2

12

You have a misunderstanding regarding the volatile keyword; specifically it is not related to caching - cache consistency is handled entirely by hardware and volatile has no effect on that.

The purpose of volatile is to prevent the compiler from generating code that assumes the value cannot have changed. The C language does not provide support to threads of execution, and the code is generated as if there were a single thread; if the compiler can observe within a single thread of execution that a variable has not been explicitly modified, it may remove the explicit read and use an already known value (stored in a register for example).

The code in your main() function is "unaware" that the interrupt may occur between reads, and can therefore optimise out the read. The volatile keyword instructs the compiler to generate code to explicitly read the memory. It does not matter one way or the other whether that read results in a cache-hit or miss or if there is no cache at all - that is a hardware issue.

You might benefit from reading Introduction to the volatile keyword on Embedded.com. It covers exactly this issue.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

Adding to above comment, most application developer get confused about the validity of following statement. Confusions arises with co existence of volatile and const keyword in the declaration i.e. How can be a variable a const yet volatile?

volatile const char *const ptr = 0x4000;

It is important to understand that volatile indicates that variable can changed outside the scope of the program. Above statement/similar statement is useful for mapping the hardware registers of the device.

MGpro
  • 33
  • 4