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