table of contents
avr_pgmspace(3avr) | avr_pgmspace(3avr) |
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
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.
Version 2.2.1 | AVR-LibC |