---
name: "avr_pgmspace"
section: "3avr"
platform: "linux"
package: "avr-libc"
version: "1:2.2.1-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/avr_pgmspace"
---

# avr\_pgmspace(3avr)

\<avr/pgmspace.h\>: Program Space Utilities

## NAME

avr\_pgmspace - \<avr/pgmspace.h\>: Program Space Utilities

## SYNOPSIS

### Macros

\#define **PROGMEM\_FAR**   \_\_attribute\_\_((\_\_section\_\_('.progmemx.data')))\
 \#define **PROGMEM**   \_\_attribute\_\_((\_\_progmem\_\_))\
 \#define **PSTR**(str)   ({ static const **PROGMEM** char c\[\] = (str); &c\[0\]; })\
 \#define **PSTR\_FAR**(str)   ({ static const **PROGMEM\_FAR** char c\[\] = (str); **pgm\_get\_far\_address**(c\[0\]); })\
 \#define **pgm\_read\_byte\_near**(\_\_addr)   \_\_LPM ((**uint16\_t**)(\_\_addr))\
 \#define **pgm\_read\_word\_near**(\_\_addr)   \_\_LPM\_word ((**uint16\_t**)(\_\_addr))\
 \#define **pgm\_read\_dword\_near**(\_\_addr)       \_\_LPM\_dword ((**uint16\_t**)(\_\_addr))\
 \#define **pgm\_read\_qword\_near**(\_\_addr)   \_\_LPM\_qword ((**uint16\_t**)(\_\_addr))\
 \#define **pgm\_read\_float\_near**(addr)   pgm\_read\_float (addr)\
 \#define **pgm\_read\_ptr\_near**(\_\_addr)       ((void\*) \_\_LPM\_word ((**uint16\_t**)(\_\_addr)))\
 \#define **pgm\_read\_byte\_far**(\_\_addr)   \_\_ELPM (\_\_addr)\
 \#define **pgm\_read\_word\_far**(\_\_addr)   \_\_ELPM\_word (\_\_addr)\
 \#define **pgm\_read\_dword\_far**(\_\_addr)   \_\_ELPM\_dword (\_\_addr)\
 \#define **pgm\_read\_qword\_far**(\_\_addr)   \_\_ELPM\_qword (\_\_addr)\
 \#define **pgm\_read\_ptr\_far**(\_\_addr)   ((void\*) \_\_ELPM\_word (\_\_addr))\
 \#define **pgm\_read\_byte**(\_\_addr)   **pgm\_read\_byte\_near**(\_\_addr)\
 \#define **pgm\_read\_word**(\_\_addr)   **pgm\_read\_word\_near**(\_\_addr)\
 \#define **pgm\_read\_dword**(\_\_addr)   **pgm\_read\_dword\_near**(\_\_addr)\
 \#define **pgm\_read\_qword**(\_\_addr)   **pgm\_read\_qword\_near**(\_\_addr)\
 \#define **pgm\_read\_ptr**(\_\_addr)   **pgm\_read\_ptr\_near**(\_\_addr)\
 \#define **pgm\_get\_far\_address**(var)\

## Detailed Description

```
#include <avr/io.h>
#include <avr/pgmspace.h>
```

The functions in this module provide interfaces for a program to access data stored in program space (flash memory) of the device.

**Note**

These functions are an attempt to provide some compatibility with header files that come with IAR C, to make porting applications between different compilers easier. This is not 100% compatibility though (GCC does not have full support for multiple address spaces yet).

If you are working with strings which are completely based in RAM, use the standard string functions described in **\<string.h\>: Strings**.

If possible, put your constant tables in the lower 64 KB and use **pgm\_read\_byte\_near()** or **pgm\_read\_word\_near()** instead of **pgm\_read\_byte\_far()** or **pgm\_read\_word\_far()** since it is more efficient that way, and you can still use the upper 64K for executable code. All functions that are suffixed with a `_P` *require* their arguments to be in the lower 64 KB of the flash ROM, as they do not use ELPM instructions. This is normally not a big concern as the linker setup arranges any program space constants declared using the macros from this header file so they are placed right after the interrupt vectors, and in front of any executable code. However, it can become a problem if there are too many of these constants, or for bootloaders on devices with more than 64 KB of ROM. *All these functions will not work in that situation.*

For **Xmega** devices, make sure the NVM controller command register (`NVM.CMD` or `NVM_CMD`) is set to 0x00 (NOP) before using any of these functions.

## Macro Definition Documentation

### #define pgm\_get\_far\_address(var)

This macro evaluates to a **uint\_farptr\_t** 32-bit 'far' pointer (only 24 bits used) to data even beyond the 64 KiB limit for the 16-bit ordinary pointer. It is similar to the '&' operator, with some limitations. Example:

```
#include <avr/pgmspace.h>
// Section .progmemx.data is located after all the code sections.
PROGMEM_FAR
const int data[] = { 2, 3, 5, 7, 9, 11 };
int get_data (uint8_t idx)
{
    uint_farptr_t pdata = pgm_get_far_address (data[0]);
    return pgm_read_int_far (pdata + idx * sizeof(int));
}
```

Comments:

- •

  The overhead is minimal and it's mainly due to the 32-bit size operation.

- •

  24 bit sizes guarantees the code compatibility for use in future devices.

- •

  `var` has to be resolved at link-time as an existing symbol, i.e. a simple variable name, an array name, or an array or structure element provided the offset is known at compile-time, and `var` is located in static storage, etc.

### #define pgm\_read\_byte(\_\_addr) pgm\_read\_byte\_near(\_\_addr)

Read a byte from the program space with a 16-bit (near) nyte-address.

### #define pgm\_read\_byte\_far(\_\_addr) \_\_ELPM (\_\_addr)

Read a byte from the program space with a 32-bit (far) byte-address.

### #define pgm\_read\_byte\_near(\_\_addr) \_\_LPM ((uint16\_t)(\_\_addr))

Read a byte from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_dword(\_\_addr) pgm\_read\_dword\_near(\_\_addr)

Read a double word from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_dword\_far(\_\_addr) \_\_ELPM\_dword (\_\_addr)

Read a double word from the program space with a 32-bit (far) byte-address.

### #define pgm\_read\_dword\_near(\_\_addr) \_\_LPM\_dword ((uint16\_t)(\_\_addr))

Read a double word from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_float\_near(addr) pgm\_read\_float (addr)

Read a `float` from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_ptr(\_\_addr) pgm\_read\_ptr\_near(\_\_addr)

Read a pointer from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_ptr\_far(\_\_addr) ((void\*) \_\_ELPM\_word (\_\_addr))

Read a pointer from the program space with a 32-bit (far) byte-address.

### #define pgm\_read\_ptr\_near(\_\_addr) ((void\*) \_\_LPM\_word ((uint16\_t)(\_\_addr)))

Read a pointer from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_qword(\_\_addr) pgm\_read\_qword\_near(\_\_addr)

Read a quad-word from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_qword\_far(\_\_addr) \_\_ELPM\_qword (\_\_addr)

Read a quad-word from the program space with a 32-bit (far) byte-address.

### #define pgm\_read\_qword\_near(\_\_addr) \_\_LPM\_qword ((uint16\_t)(\_\_addr))

Read a quad-word from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_word(\_\_addr) pgm\_read\_word\_near(\_\_addr)

Read a word from the program space with a 16-bit (near) byte-address.

### #define pgm\_read\_word\_far(\_\_addr) \_\_ELPM\_word (\_\_addr)

Read a word from the program space with a 32-bit (far) byte-address.

### #define pgm\_read\_word\_near(\_\_addr) \_\_LPM\_word ((uint16\_t)(\_\_addr))

Read a word from the program space with a 16-bit (near) byte-address.

### #define PROGMEM \_\_attribute\_\_((\_\_progmem\_\_))

Attribute to use in order to declare an object being located in flash ROM.

### #define PROGMEM\_FAR \_\_attribute\_\_((\_\_section\_\_('.progmemx.data')))

Attribute to use in order to declare an object being located in far flash ROM. This is similar to **PROGMEM**, except that it puts the static storage object in section **.progmemx.data. In order to access the object, the** `pgm_read_*_far` **and** `_PF` **functions declare in this header can be used. In order to get its address, see pgm\_get\_far\_address().**

**It only makes sense to put read-only objects in this section, though the compiler does not diagnose when this is not the case.**

### #define PSTR(str) ({ static const PROGMEM char c\[\] = (str); &c\[0\]; })

**Used to declare a static pointer to a string in program space.**

### #define PSTR\_FAR(str) ({ static const PROGMEM\_FAR char c\[\] = (str); pgm\_get\_far\_address(c\[0\]); })

**Used to define a string literal in far program space, and to return its address of type uint\_farptr\_t.**

## Author

**Generated automatically by Doxygen for AVR-LibC from the source code.**
