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

3
  • 1
    The answer is here stackoverflow.com/questions/246127/why-is-volatile-needed-in-c Commented Nov 23, 2016 at 9: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". Commented Nov 23, 2016 at 9: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. Commented Nov 23, 2016 at 13:40

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.