Is it bad if all variables are defined as mutable when programming AVR?

I read that in some cases (global variable or while (variable), etc.), if the variables are not defined as volatile , this can cause problems.

May there be a problem if I define all variables as mutable?

+7
c avr volatile atmega
source share
4 answers

If something outside the current region or any subsequent child region (think: function calls) can change the variable that you are working in (there is a timer interrupt that will increase your variable, you pointed to var for some other code that can do something in response to an interrupt, etc.), then the variable must be declared mutable.

volatile is a hint to the compiler that says, "something else can change this variable." and the compiler’s answer: “Oh, I never trust a copy of this variable that I have in the register or on the stack. Every time I need to use this variable, I will read it from memory, because my copy in the register may be deprecated "

By announcing that everything is volatile, your code will significantly slow down and lead to a significant increase in binary code. Instead, the correct answer is to understand what needs to be marked by volatility, why this is so and appropriately labeled.

+11
source share

The volatile variable must have memory access executed by the compiler.

It means that:

  • If you read the value of a variable, the compiler is not allowed to reuse the value that it already "knows" about. For example, it can be in the register of external devices and can be changed by peripheral equipment.
  • If you write something to a variable, the compiler must write to this memory. You can’t look back and say, “Well, nothing else uses this variable, so I don’t need to write it.” For example, it may again be an external peripheral register (possibly Tx FIFO UART).

Note that volatile not (always) sufficient for communication between threads (or between main loops and interrupt service routines: https://stackoverflow.com/a/social/) . See also https://www.kernel.org/doc/ Documentation / volatile-considered-harmful.txt


Just make things unstable, which should be. If you have to make things unstable to make them work, it comes down to one of two things:

  • Compiler error
  • Your code does not fit.

In my experience, this is almost always the last, but you may have to consult with c-lawyer about why!


But in response to your real question, if you make everything unstable, the code will still work fine, although you may have performance limitations that you don't need!

+3
source share

A variable is called mutable if its value can change at any time regardless of the program. This is useful if another program (or stream) or an external event (keyboard, network ...) can change the variable. It tells the compiler to reread the value of the variable from its original location each time the variable is accessed. This prevents the compiler from optimizing memory access. Therefore, declaring each volatile variable can slow down the program.

By the way: I know nothing about the specifics of AVR programming.

+1
source share

If you need to configure the entire variable as mutable, then some deep-rooted design problem will arise with your software. Yes, it will decrease in performance. But how much? We do not know if you have not specified the specification of the CPU and its instructions.

+1
source share

All Articles