AVR Variable Attributes (Using the GNU Compiler Collection (GCC)) (original) (raw)
progmem
¶
The progmem
attribute is used on the AVR to place read-only data in the non-volatile program memory (flash). The progmem
attribute accomplishes this by putting respective variables into a section whose name starts with .progmem
.
This attribute works similar to the section
attribute but adds additional checking.
• Ordinary AVR cores with 32 general purpose registers:
progmem
affects the location of the data but not how this data is accessed. In order to read data located with the progmem
attribute (inline) assembler must be used.
/* Use custom macros from AVR-LibC */ #include <avr/pgmspace.h>
/* Locate var in flash memory */ const int var[2] PROGMEM = { 1, 2 };
int read_var (int i) { /* Access var[] by accessor macro from avr/pgmspace.h */ return (int) pgm_read_word (& var[i]); }
AVR is a Harvard architecture processor and data and read-only data normally resides in the data memory (RAM).
See also the AVR Named Address Spaces section for an alternate way to locate and access data in flash memory.
• AVR cores with flash memory visible in the RAM address range:
On such devices, there is no need for attribute progmem
or__flash qualifier at all. Just use standard C / C++. The compiler will generate LD*
instructions. As flash memory is visible in the RAM address range, and the default linker script does not locate .rodata
in RAM, no special features are needed in order not to waste RAM for read-only data or to read from flash. You might even get slightly better performance by avoiding progmem
and __flash
. This applies to devices from families avrtiny
and avrxmega3
, see AVR Options for an overview.
• Reduced AVR Tiny cores like ATtiny40:
The compiler adds 0x4000
to the addresses of objects and declarations in progmem
and locates the objects in flash memory, namely in section .progmem.data
. The offset is needed because the flash memory is visible in the RAM address space starting at address 0x4000
.
Data in progmem
can be accessed by means of ordinary C code, no special functions or macros are needed.
/* var is located in flash memory */ extern const int var[2] attribute((progmem));
int read_var (int i) { return var[i]; }
Please notice that on these devices, there is no need for progmem
at all.
io
¶
io (addr)
Variables with the io
attribute are used to address memory-mapped peripherals in the I/O address range. No memory is allocated. If an address is specified, the variable is assigned that address, and the value is interpreted as an address in the data address space. Example:
volatile int porta attribute((io (AVR_SFR_OFFSET + 0x2)));
Otherwise, the variable is not assigned an address, but the compiler will still use in
and out
instructions where applicable, assuming some other module assigns an address in the I/O address range. Example:
extern volatile int porta attribute((io));
io_low
¶
io_low (addr)
This is like the io
attribute, but additionally it informs the compiler that the object lies in the lower half of the I/O area, allowing the use of cbi
, sbi
, sbic
and sbis
instructions.
address (addr)
¶
Variables with the address
attribute can be used to address memory-mapped peripherals that may lie outside the I/O address range. Just like with the io
and io_low
attributes, no memory is allocated.
volatile int porta attribute((address (0x600)));
This attribute can also be used to define symbols in C/C++ code which otherwise would require assembly, a linker description file or command-line options like -Wl,--defsym,a_symbol=value
. For example,
int a_symbol attribute((weak, address (1234)));
will be compiled to
.weak a_symbol a_symbol = 1234
absdata
¶
Variables in static storage and with the absdata
attribute can be accessed by the LDS
and STS
instructions which take absolute addresses.
- This attribute is only supported for the reduced AVR Tiny core like ATtiny40.
- You must make sure that respective data is located in the address range
0x40
…0xbf
accessible byLDS
andSTS
. One way to achieve this as an appropriate linker description file. - If the location does not fit the address range of
LDS
andSTS
, there is currently (Binutils 2.26) just an unspecific warning likemodule.cc:(.text+0x1c): warning: internal error: out of range error
See also the -mabsdata command-line option.