avr_pgmspace(3avr)

๐Ÿ“ฆ
version 2.0.0+Atmel3.7.0 ยท 2 versions
, in ubuntu noble

๐Ÿ’– avr_pgmspace โ€” the <avr/pgmspace.h> glow-up ๐Ÿ’–

okay bestie, welcome to avr_pgmspace(3avr) from the avr-libc manual ๐Ÿ’… this is the page all about poking at your flash memory (program space) and honestly? it's giving โœจ main character energy โœจ for embedded girlies. let's get into it ๐Ÿซถ


๐ŸŒธ NAME ๐ŸŒธ

avr_pgmspace โ€” <avr/pgmspace.h>: Program Space Utilities ๐Ÿ’พโœจ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

Macros

#define PROGMEM   __ATTR_PROGMEM__
#define PGM_P   const char *
#define PGM_VOID_P   const void *
#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_float_near(address_short)
    __LPM_float((uint16_t)(address_short))
#define pgm_read_ptr_near(address_short)
    (void*)__LPM_word((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_float_far(address_long)
    __ELPM_float((uint32_t)(address_long))
#define pgm_read_ptr_far(address_long)
    (void*)__ELPM_word((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_read_float(address_short)
    pgm_read_float_near(address_short)
#define pgm_read_ptr(address_short)   pgm_read_ptr_near(address_short)
#define pgm_get_far_address(var)

๐Ÿฆ‹ Detailed Description ๐Ÿฆ‹

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

so like, the functions in this module hand you the interfaces to go grab data that lives in program space (that's flash memory, bestie ๐Ÿ“€) on your device. but lowkey there's a requirement: your target device has to support either the LPM or the ELPM instructions. no instruction, no vibes. periodt. ๐Ÿ”ฅ

Note ๐Ÿ’Œ these functions are basically an attempt to give you some compatibility with the header files that ship with IAR C, so porting apps between different compilers is less of a whole thing. ngl though, it is not 100% compatible (GCC does not have full support for multiple address spaces yet ๐Ÿ˜ญ). if you're working with strings that are completely based in ram, use the standard string functions described in <string.h>: Strings. easy ๐Ÿ’… 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(), because 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, since they do not use ELPM instructions. normally this is not a big concern because 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)

Value:

({                                                    \
    uint_farptr_t tmp;                                \
                                                      \
    __asm__ __volatile__(                             \
                                                      \
            "ldi    %A0, lo8(%1)"           "\n\t"    \
            "ldi    %B0, hi8(%1)"           "\n\t"    \
            "ldi    %C0, hh8(%1)"           "\n\t"    \
            "clr    %D0"                    "\n\t"    \
        :                                             \
            "=d" (tmp)                                \
        :                                             \
            "p"  (&(var))                             \
    );                                                \
    tmp;                                              \
})

this macro facilitates the obtention of a 32 bit 'far' pointer (only 24 bits used) to data even passed the 64KB limit for the 16 bit ordinary pointer. it is similar to the & operator, with some limitations ๐Ÿ‘€

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 ๐Ÿ”ฎ
  • hh8() is an undocumented feature but seems to give the third significant byte of a 32 bit data and accepts symbols, complementing the functionality of hi8() and lo8(). there is not an equivalent assembler function to get the high significant byte ๐Ÿ™ˆ
  • the returned value is the identifier's VMA (virtual memory address) determined by the linker and falls in the corresponding memory region. the AVR Harvard architecture requires non overlapping VMA areas for the multiple address spaces in the processor: Flash ROM, RAM, and EEPROM. typical offset for this are 0x00000000, 0x00800xx0, and 0x00810000 respectively, derived from the linker script used and linker options. the value returned can be seen then as a universal pointer ๐ŸŒˆ

#define PGM_P const 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_float(address_short) pgm_read_float_near(address_short)

read a float 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_float_far(address_long) __ELPM_float((uint32_t)(address_long))

read a float 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_float_near(address_short) __LPM_float((uint16_t)(address_short))

read a float 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_ptr(address_short) pgm_read_ptr_near(address_short)

read a pointer 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_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))

read a pointer 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_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))

read a pointer 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 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 ๐Ÿ’–


๐ŸŒŸ Author ๐ŸŒŸ

generated automatically by Doxygen for avr-libc from the source code ๐Ÿค–๐Ÿ’•


and that's the whole page, bestie!! this was Version 2.0.0, dated Fri Nov 24 2023 23:59:10 ๐ŸŽ€ thanks for reading, go read your flash memory responsibly, ily, bye ๐Ÿ’œ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from