Reputation: 133
I am programming a microcontroller in C language and I’ve faced the following situation: To enable/disable the internal clock generator, I need to first write a protection key to a specific register and after make the enable/disable operation, writing in another register. The user manual specify the following about this operation: snip from user manual
As you can see in the highlighted part of the text, the piece of code that make this two operation (writing protection key and enable the oscillator) is very sensitive, because any operation should be executed between this two register access. Therefore, I start to concern about any situation that could led to a non sequential execution of this two operations. I know that it is common to temporary disable interruption while executing sensitive piece of codes, but I was wonder if there is any compiler optimization that could insert another operation between this two register access. So, in C programming, there is any compiler directive to ensure that it will not happen?
Actually, I even do not know if make sense to think that the compiler will mix the sequence of instructions written in C language. I remember to have already heard that it could happens sometimes, in the speed optimization process. If I am wrong, sorry about and thanks in advance for the clarification.
Upvotes: 2
Views: 628
Reputation: 571
The process of toggling bits in C involves a read-modify-write sequence of instructions. Some unwanted operations may happen between those instructions and the usual approach is to either disable the interrupts or to use bit-banding.
What bit-banding is, it allows to you toggle bits using one instruction(without going into details how it happens).
If your microcontroller allows you to use bit-banding, then that should fix all your problems.
If you can't use bit-banding, then you have to sit and think what interrupts may cause problems to you and temporarily disable them.
To get back to the core of the question - you must declare all of your registers volatile for two main reasons:
To sum it up - use bit-banding and declare the registers volatile.
Upvotes: 1