Using the GNU Compiler Collection (GCC): AVR Variable Attributes

Next: Blackfin Variable Attributes, Previous: ARC Variable Attributes, Up: Variable Attributes [Contents][Index]

#### 6.34.3 AVR Variable Attributes

`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 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`
`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`
`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)));

`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 by `LDS` and `STS`. One way to achieve this as an appropriate linker description file.
  - If the location does not fit the address range of `LDS` and `STS`, there is currently (Binutils 2.26) just an unspecific warning like

    > `module.cc:(.text+0x1c): warning: internal error: out of range error`

  See also the `-mabsdata` command-line option.

Next: Blackfin Variable Attributes, Previous: ARC Variable Attributes, Up: Variable Attributes [Contents][Index]