Atmel CAVR-4 ユーザーズマニュアル
CAVR-4
Part 1. Using the compiler
Placing code and data
39
Initialized data
In ISO/ANSI C all static variables—variables that are allocated at a fixed memory
address—have to be initialized by the run-time system to a known value. This value is
either an explicit value assigned to the variable, or if no value is given, it is cleared to
zero.
address—have to be initialized by the run-time system to a known value. This value is
either an explicit value assigned to the variable, or if no value is given, it is cleared to
zero.
In the
AVR IAR C/C++ Compiler, there are two exceptions to this rule and they both
use keywords for ISO/ANSI C language extensions. Variables declared
_ _no_init
are not initialized at all. Variables declared
_ _eeprom
are allocated in the EEPROM
memory and because the EEPROM memory can typically be used for storing
configuration data—data that need to live through a reset—these variables will not be
initialized by the runtime system. Instead, these initializers are stored in a separate
segment, which can be loaded to the EEPROM as part of the program download.
configuration data—data that need to live through a reset—these variables will not be
initialized by the runtime system. Instead, these initializers are stored in a separate
segment, which can be loaded to the EEPROM as part of the program download.
Initialization at system startup
When an application is started, the system startup code initializes static and global
variables in three steps:
variables in three steps:
1 It clears the memory of the variables that should be initialized to zero; these
variables are located in segments with the suffix
_Z
.
2 It initializes the non-zero variables by copying a block of ROM to the location of the
variables in RAM. This means that the data in the ROM segment with the suffix
ID
is copied to the corresponding
I
segment.
This works when both segments are placed in continuous memory. However, if one
of the segments is divided into smaller pieces, it is important that:
of the segments is divided into smaller pieces, it is important that:
●
The other segment is divided in exactly the same way
●
It is legal to read and write the memory that represents the gaps in the sequence.
For example, if the segments are assigned the following ranges, the copy will fail:
However, in the following example, the linker will place the content of the segments
in identical order, which means that the copy will work appropriately:
in identical order, which means that the copy will work appropriately:
Note that the gap between the ranges will also be copied. Note also that the
NEAR_ID
segment holding the initializers is always located in flash memory and that these
initializers are only used once, that is before reaching the
initializers are only used once, that is before reaching the
main
function.
NEAR_I
0x1000-0x10FF
and
0x1200-0x12FF
NEAR_ID
0x4000-0x41FF
NEAR_I
0x1000-0x10FF
and
0x1200-0x12FF
NEAR_ID
0x4000-0x40FF
and
0x4200-0x42FF