Next: , Previous: Common Variable Attributes, Up: Variable Attributes

6.32.2 AVR Variable Attributes

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. Notice that just like the section attribute, 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.

io (addr)
Variables with the io attribute are used to address memory-mapped peripherals in the io address range. 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 (0x22)));

The address specified in the address in the data address range.

Otherwise, the variable it is not assigned an address, but the compiler will still use in/out instructions where applicable, assuming some other module assigns an address in the io address range. Example:

          extern volatile int porta __attribute__((io));

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 are used to address memory-mapped peripherals that may lie outside the io address range.
          volatile int porta __attribute__((address (0x600)));