AVR Libc Home Page |  | AVR Libc Development Pages
Main Page | User Manual | Library Reference | FAQ | Alphabetical Index | Example Projects

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

## 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. In order to use these functions, the target device must support either the `LPM` or `ELPM` instructions.

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.

## Defines
#define | PROGMEM __ATTR_PROGMEM__
#define | PSTR(s) ((const PROGMEM char *)(s))
#define | pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short))
#define | pgm_read_word_near(address_short) __LPM_word((uint16_t)(address_short))
#define | pgm_read_dword_near(address_short) __LPM_dword((uint16_t)(address_short))
#define | pgm_read_byte_far(address_long) __ELPM((uint32_t)(address_long))
#define | pgm_read_word_far(address_long) __ELPM_word((uint32_t)(address_long))
#define | pgm_read_dword_far(address_long) __ELPM_dword((uint32_t)(address_long))
#define | pgm_read_byte(address_short) pgm_read_byte_near(address_short)
#define | pgm_read_word(address_short) pgm_read_word_near(address_short)
#define | pgm_read_dword(address_short) pgm_read_dword_near(address_short)
#define | PGM_P const prog_char *
#define | PGM_VOID_P const prog_void *
## Typedefs
typedef void PROGMEM | prog_void
typedef char PROGMEM | prog_char
typedef unsigned char PROGMEM | prog_uchar
typedef int8_t PROGMEM | prog_int8_t
typedef uint8_t PROGMEM | prog_uint8_t
typedef int16_t PROGMEM | prog_int16_t
typedef uint16_t PROGMEM | prog_uint16_t
typedef int32_t PROGMEM | prog_int32_t
typedef uint32_t PROGMEM | prog_uint32_t
typedef int64_t PROGMEM | prog_int64_t
typedef uint64_t PROGMEM | prog_uint64_t
## Functions
PGM_VOID_P | memchr_P (PGM_VOID_P s, int val, size_t len)
int | memcmp_P (const void *, PGM_VOID_P, size_t) __ATTR_PURE__
void * | memcpy_P (void *, PGM_VOID_P, size_t)
PGM_VOID_P | memrchr_P (PGM_VOID_P s, int val, size_t len)
int | strcasecmp_P (const char *, PGM_P) __ATTR_PURE__
char * | strcat_P (char *, PGM_P)
PGM_P | strchr_P (PGM_P s, int val)
PGM_P | strchrnul_P (PGM_P s, int val)
int | strcmp_P (const char *, PGM_P) __ATTR_PURE__
char * | strcpy_P (char *, PGM_P)
size_t | strcspn_P (const char *s, PGM_P reject) __ATTR_PURE__
size_t | strlcat_P (char *, PGM_P, size_t)
size_t | strlcpy_P (char *, PGM_P, size_t)
size_t | strlen_P (PGM_P)
int | strncasecmp_P (const char *, PGM_P, size_t) __ATTR_PURE__
char * | strncat_P (char *, PGM_P, size_t)
int | strncmp_P (const char *, PGM_P, size_t) __ATTR_PURE__
char * | strncpy_P (char *, PGM_P, size_t)
size_t | strnlen_P (PGM_P, size_t)
char * | strpbrk_P (const char *s, PGM_P accept) __ATTR_PURE__
PGM_P | strrchr_P (PGM_P s, int val)
char * | strsep_P (char **sp, PGM_P delim)
size_t | strspn_P (const char *s, PGM_P accept) __ATTR_PURE__
char * | strstr_P (const char *, PGM_P) __ATTR_PURE__
void * | memmem_P (const void *, size_t, PGM_VOID_P, size_t) __ATTR_PURE__
char * | strcasestr_P (const char *, PGM_P) __ATTR_PURE__

## Define Documentation

#define PGM_P const prog_char *

Used to declare a variable that is a pointer to a string in program space.

#define pgm_read_byte | ( | address_short |  | ) | pgm_read_byte_near(address_short)

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_byte_far | ( | address_long |  | ) | __ELPM((uint32_t)(address_long))

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_byte_near | ( | address_short |  | ) | __LPM((uint16_t)(address_short))

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_dword | ( | address_short |  | ) | pgm_read_dword_near(address_short)

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_dword_far | ( | address_long |  | ) | __ELPM_dword((uint32_t)(address_long))

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_dword_near | ( | address_short |  | ) | __LPM_dword((uint16_t)(address_short))

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_word | ( | address_short |  | ) | pgm_read_word_near(address_short)

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_word_far | ( | address_long |  | ) | __ELPM_word((uint32_t)(address_long))

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

Note:
  The address is a byte address. The address is in the program space.

#define pgm_read_word_near | ( | address_short |  | ) | __LPM_word((uint16_t)(address_short))

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

Note:
  The address is a byte address. The address is in the program space.

#define PGM_VOID_P const prog_void *

Used to declare a generic pointer to an object in program space.

#define PROGMEM __ATTR_PROGMEM__

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

#define PSTR | ( | s |  | ) | ((const PROGMEM char *)(s))

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

## Typedef Documentation

prog_char

Type of a "char" object located in flash ROM.

prog_int16_t

Type of an "int16_t" object located in flash ROM.

prog_int32_t

Type of an "int32_t" object located in flash ROM.

prog_int64_t

Type of an "int64_t" object located in flash ROM.

Note:
  This type is not available when the compiler option -mint8 is in effect.

prog_int8_t

Type of an "int8_t" object located in flash ROM.

prog_uchar

Type of an "unsigned char" object located in flash ROM.

prog_uint16_t

Type of an "uint16_t" object located in flash ROM.

prog_uint32_t

Type of an "uint32_t" object located in flash ROM.

prog_uint64_t

Type of an "uint64_t" object located in flash ROM.

Note:
  This type is not available when the compiler option -mint8 is in effect.

prog_uint8_t

Type of an "uint8_t" object located in flash ROM.

prog_void

Type of a "void" object located in flash ROM. Does not make much sense by itself, but can be used to declare a "void *" object in flash ROM.

## Function Documentation

PGM_VOID_P memchr_P | ( | PGM_VOID_P | s,
|  | int | val,
|  | size_t | len |
| ) |  |  |

Scan flash memory for a character.

The memchr_P() function scans the first `len` bytes of the flash memory area pointed to by `s` for the character `val`. The first byte to match `val` (interpreted as an unsigned character) stops the operation.

Returns:
  The memchr_P() function returns a pointer to the matching byte or `NULL` if the character does not occur in the given memory area.

int memcmp_P | ( | const void * | s1,
|  | PGM_VOID_P | s2,
|  | size_t | len |
| ) |  |  |

Compare memory areas.

The memcmp_P() function compares the first `len` bytes of the memory areas `s1` and flash `s2`. The comparision is performed using unsigned char operations.

Returns:
  The memcmp_P() function returns an integer less than, equal to, or greater than zero if the first `len` bytes of `s1` is found, respectively, to be less than, to match, or be greater than the first `len` bytes of `s2`.

void * memcpy_P | ( | void * | dest,
|  | PGM_VOID_P | src,
|  | size_t | n |
| ) |  |  |

The memcpy_P() function is similar to memcpy(), except the src string resides in program space.

Returns:
  The memcpy_P() function returns a pointer to dest.

void * memmem_P | ( | const void * | s1,
|  | size_t | len1,
|  | PGM_VOID_P | s2,
|  | size_t | len2 |
| ) |  |  |

The memmem_P() function is similar to memmem() except that `s2` is pointer to a string in program space.

PGM_VOID_P memrchr_P | ( | PGM_VOID_P | src,
|  | int | val,
|  | size_t | len |
| ) |  |  |

The memrchr_P() function is like the memchr_P() function, except that it searches backwards from the end of the `len` bytes pointed to by `src` instead of forwards from the front. (Glibc, GNU extension.)

Returns:
  The memrchr_P() function returns a pointer to the matching byte or `NULL` if the character does not occur in the given memory area.

int strcasecmp_P | ( | const char * | s1,
|  | PGM_P | s2 |
| ) |  |  |

Compare two strings ignoring case.

The strcasecmp_P() function compares the two strings `s1` and `s2`, ignoring the case of the characters.

Parameters:

  | s1 | A pointer to a string in the devices SRAM.
  | s2 | A pointer to a string in the devices Flash.

Returns:
  The strcasecmp_P() function returns an integer less than, equal to, or greater than zero if `s1` is found, respectively, to be less than, to match, or be greater than `s2`. A consequence of the ordering used by strcasecmp_P() is that if `s1` is an initial substring of `s2`, then `s1` is considered to be "less than" `s2`.

char * strcasestr_P | ( | const char * | s1,
|  | PGM_P | s2 |
| ) |  |  |

This funtion is similar to strcasestr() except that `s2` is pointer to a string in program space.

char * strcat_P | ( | char * | dest,
|  | PGM_P | src |
| ) |  |  |

The strcat_P() function is similar to strcat() except that the src string must be located in program space (flash).

Returns:
  The strcat() function returns a pointer to the resulting string dest.

PGM_P strchr_P | ( | PGM_P | s,
|  | int | val |
| ) |  |  |

Locate character in program space string.

The strchr_P() function locates the first occurrence of `val` (converted to a char) in the string pointed to by `s` in program space. The terminating null character is considered to be part of the string.

The strchr_P() function is similar to strchr() except that `s` is pointer to a string in program space.

Returns:
  The strchr_P() function returns a pointer to the matched character or `NULL` if the character is not found.

PGM_P strchrnul_P | ( | PGM_P | s,
|  | int | c |
| ) |  |  |

The strchrnul_P() function is like strchr_P() except that if `c` is not found in `s`, then it returns a pointer to the null byte at the end of `s`, rather than `NULL`. (Glibc, GNU extension.)

Returns:
  The strchrnul_P() function returns a pointer to the matched character, or a pointer to the null byte at the end of `s` (i.e., `s+strlen`(s)) if the character is not found.

int strcmp_P | ( | const char * | s1,
|  | PGM_P | s2 |
| ) |  |  |

The strcmp_P() function is similar to strcmp() except that `s2` is pointer to a string in program space.

Returns:
  The strcmp_P() function returns an integer less than, equal to, or greater than zero if `s1` is found, respectively, to be less than, to match, or be greater than `s2`. A consequence of the ordering used by strcmp_P() is that if `s1` is an initial substring of `s2`, then `s1` is considered to be "less than" `s2`.

char * strcpy_P | ( | char * | dest,
|  | PGM_P | src |
| ) |  |  |

The strcpy_P() function is similar to strcpy() except that src is a pointer to a string in program space.

Returns:
  The strcpy_P() function returns a pointer to the destination string dest.

size_t strcspn_P | ( | const char * | s,
|  | PGM_P | reject |
| ) |  |  |

The strcspn_P() function calculates the length of the initial segment of `s` which consists entirely of characters not in `reject`. This function is similar to strcspn() except that `reject` is a pointer to a string in program space.

Returns:
  The strcspn_P() function returns the number of characters in the initial segment of `s` which are not in the string `reject`. The terminating zero is not considered as a part of string.

size_t strlcat_P | ( | char * | dst,
|  | PGM_P | ,
|  | size_t | siz |
| ) |  |  |

Concatenate two strings.

The strlcat_P() function is similar to strlcat(), except that the src string must be located in program space (flash).

Appends src to string dst of size siz (unlike strncat(), siz is the full size of dst, not space left). At most siz-1 characters will be copied. Always NULL terminates (unless siz <= strlen(dst)).

Returns:
  The strlcat_P() function returns strlen(src) + MIN(siz, strlen(initial dst)). If retval >= siz, truncation occurred.

size_t strlcpy_P | ( | char * | dst,
|  | PGM_P | ,
|  | size_t | siz |
| ) |  |  |

Copy a string from progmem to RAM.

Copy src to string dst of size siz. At most siz-1 characters will be copied. Always NULL terminates (unless siz == 0).

Returns:
  The strlcpy_P() function returns strlen(src). If retval >= siz, truncation occurred.

size_t strlen_P | ( | PGM_P | src | ) |

The strlen_P() function is similar to strlen(), except that src is a pointer to a string in program space.

Returns:
  The strlen() function returns the number of characters in src.

int strncasecmp_P | ( | const char * | s1,
|  | PGM_P | s2,
|  | size_t | n |
| ) |  |  |

Compare two strings ignoring case.

The strncasecmp_P() function is similar to strcasecmp_P(), except it only compares the first `n` characters of `s1`.

Parameters:

  | s1 | A pointer to a string in the devices SRAM.
  | s2 | A pointer to a string in the devices Flash.
  | n | The maximum number of bytes to compare.

Returns:
  The strncasecmp_P() function returns an integer less than, equal to, or greater than zero if `s1` (or the first `n` bytes thereof) is found, respectively, to be less than, to match, or be greater than `s2`. A consequence of the ordering used by strncasecmp_P() is that if `s1` is an initial substring of `s2`, then `s1` is considered to be "less than" `s2`.

char * strncat_P | ( | char * | dest,
|  | PGM_P | src,
|  | size_t | len |
| ) |  |  |

Concatenate two strings.

The strncat_P() function is similar to strncat(), except that the src string must be located in program space (flash).

Returns:
  The strncat_P() function returns a pointer to the resulting string dest.

int strncmp_P | ( | const char * | s1,
|  | PGM_P | s2,
|  | size_t | n |
| ) |  |  |

The strncmp_P() function is similar to strcmp_P() except it only compares the first (at most) n characters of s1 and s2.

Returns:
  The strncmp_P() function returns an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

char * strncpy_P | ( | char * | dest,
|  | PGM_P | src,
|  | size_t | n |
| ) |  |  |

The strncpy_P() function is similar to strcpy_P() except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated.

In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls.

Returns:
  The strncpy_P() function returns a pointer to the destination string dest.

size_t strnlen_P | ( | PGM_P | src,
|  | size_t | len |
| ) |  |  |

Determine the length of a fixed-size string.

The strnlen_P() function is similar to strnlen(), except that `src` is a pointer to a string in program space.

Returns:
  The strnlen_P function returns strlen_P(src), if that is less than `len`, or `len` if there is no '\0' character among the first `len` characters pointed to by `src`.

char * strpbrk_P | ( | const char * | s,
|  | PGM_P | accept |
| ) |  |  |

The strpbrk_P() function locates the first occurrence in the string `s` of any of the characters in the flash string `accept`. This function is similar to strpbrk() except that `accept` is a pointer to a string in program space.

Returns:
  The strpbrk_P() function returns a pointer to the character in `s` that matches one of the characters in `accept`, or `NULL` if no such character is found. The terminating zero is not considered as a part of string: if one or both args are empty, the result will `NULL`.

PGM_P strrchr_P | ( | PGM_P | s,
|  | int | val |
| ) |  |  |

Locate character in string.

The strrchr_P() function returns a pointer to the last occurrence of the character `val` in the flash string `s`.

Returns:
  The strrchr_P() function returns a pointer to the matched character or `NULL` if the character is not found.

char * strsep_P | ( | char ** | sp,
|  | PGM_P | delim |
| ) |  |  |

Parse a string into tokens.

The strsep_P() function locates, in the string referenced by `*sp`, the first occurrence of any character in the string `delim` (or the terminating '\0' character) and replaces it with a '\0'. The location of the next character after the delimiter character (or `NULL`, if the end of the string was reached) is stored in `*sp`. An ``empty'' field, i.e. one caused by two adjacent delimiter characters, can be detected by comparing the location referenced by the pointer returned in `*sp` to '\0'. This function is similar to strsep() except that `delim` is a pointer to a string in program space.

Returns:
  The strsep_P() function returns a pointer to the original value of `*sp`. If `*sp` is initially `NULL`, strsep_P() returns `NULL`.

size_t strspn_P | ( | const char * | s,
|  | PGM_P | accept |
| ) |  |  |

The strspn_P() function calculates the length of the initial segment of `s` which consists entirely of characters in `accept`. This function is similar to strspn() except that `accept` is a pointer to a string in program space.

Returns:
  The strspn_P() function returns the number of characters in the initial segment of `s` which consist only of characters from `accept`. The terminating zero is not considered as a part of string.

char * strstr_P | ( | const char * | s1,
|  | PGM_P | s2 |
| ) |  |  |

Locate a substring.

The strstr_P() function finds the first occurrence of the substring `s2` in the string `s1`. The terminating '\0' characters are not compared. The strstr_P() function is similar to strstr() except that `s2` is pointer to a string in program space.

Returns:
  The strstr_P() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. If `s2` points to a string of zero length, the function returns `s1`.

Automatically generated by Doxygen 1.5.6 on 19 Nov 2008.