logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

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

AVR-LibC                                          Version 2.2.1                               avr_pgmspace(3avr)

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. Allthesefunctionswillnotworkinthatsituation.

           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

#definepgm_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.

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

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

   #definepgm_read_byte_near(__addr)__LPM((uint16_t)(__addr))
       Read a byte from the program space with a 16-bit (near) byte-address.

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

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

   #definepgm_read_dword_near(__addr)__LPM_dword((uint16_t)(__addr))
       Read a double word from the program space with a 16-bit (near) byte-address.

   #definepgm_read_float_near(addr)pgm_read_float(addr)
       Read a float from the program space with a 16-bit (near) byte-address.

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

   #definepgm_read_ptr_far(__addr)((void*)__ELPM_word(__addr))
       Read a pointer from the program space with a 32-bit (far) byte-address.

   #definepgm_read_ptr_near(__addr)((void*)__LPM_word((uint16_t)(__addr)))
       Read a pointer from the program space with a 16-bit (near) byte-address.

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

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

   #definepgm_read_qword_near(__addr)__LPM_qword((uint16_t)(__addr))
       Read a quad-word from the program space with a 16-bit (near) byte-address.

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

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

   #definepgm_read_word_near(__addr)__LPM_word((uint16_t)(__addr))
       Read a word from the program space with a 16-bit (near) byte-address.

   #definePROGMEM__attribute__((__progmem__))
       Attribute to use in order to declare an object being located in flash ROM.

   #definePROGMEM_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.Inordertoaccesstheobject,thepgm_read_*_farand_PFfunctionsdeclareinthisheadercanbeused.Inordertogetitsaddress,seepgm_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.

   #definePSTR(str)({staticconstPROGMEMcharc[]=(str);&c[0];})
       Used to declare a static pointer to a string in program space.

   #definePSTR_FAR(str)({staticconstPROGMEM_FARcharc[]=(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.

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)

See Also