I was checking out some code written for STM32F microcontroller and I found these keywords used before initializing a variable. I would like to know what is the significance of using this "__IO" & "static" keywords?
The line of code was given like that:
static __IO uint32_t sysTickCounter;
Volatile is used in C programming when we need to go and read the value stored by the pointer at the address pointed by the pointer. If you need to change anything in your code that is out of compiler reach you can use this volatile keyword before the variable for which you want to change the value.
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.
__IO
/ volatile
__IO
is not a C keyword. __IO
is a macro for volatile
- defined in STM32 standard peripheral library header files. E.g., in core_cm4.h
(might be in a CMSIS
sub-folder), you will find
#define __IO volatile
(If you use gcc
's -E
option to use only the pre-processor stage, you can see the macro's expansion.)
The volatile
keyword, in turn, is often applied to a variable to prevent the compiler from 'optimizing it out'. This is useful in embedded systems - where a variable might be used within an interrupt - and compiler optimizations could cause problems.
Short example ...
int main(void) {
int ms = 0;
ms++;
while (1);
return 0;
}
Here is the generated assembly (using sdcc
compiler for PIC12f629 target). As you can see, the ms
variable has been 'optimized out'.
_main:
; 2 exit points
_00113_DS_:
; .line 18; "main.c" while (1)
GOTO _00113_DS_
RETURN
; exit point of _main
If, on the other hand, we declare the variable as volatile
...
volatile int ms = 0;
ms++;
// etc.
the relevant instructions are preserved:
_main:
; 2 exit points
; .line 16; "main.c" volatile int ms = 0;
CLRF _main_ms_1_5
CLRF (_main_ms_1_5 + 1)
; .line 19; "main.c" ms++;
INCF _main_ms_1_5,F
BTFSC STATUS,2
INCF (_main_ms_1_5 + 1),F
_00113_DS_:
; .line 21; "main.c" while (1)
GOTO _00113_DS_
RETURN
; exit point of _main
static
The effect of the static
keyword depends on the scope in which the variable is declared.
#include
d header files).volatile
is for accessing peripheral registers (although you would use a pointer in that case).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With