Function avr_progmem::raw::read_byte

source ·
pub unsafe fn read_byte(p_addr: *const u8) -> u8
Expand description

Read a single byte from the progmem.

This function reads just a single byte from the program code memory domain. Thus this is essentially a Rust function around the AVR lpm instruction.

If you need to read from an array you might use read_slice or just generally for any value (including arrays) read_value.

Example

use avr_progmem::raw::read_byte;
use core::ptr::addr_of;

// This static must never be directly dereferenced/accessed!
// So a `let data: u8 = P_BYTE;` is Undefined Behavior!!!
/// Static byte stored in progmem!
#[link_section = ".progmem.data"]
static P_BYTE: u8 = b'A';

// Load the byte from progmem
// Here, it is sound, because due to the link_section it is indeed in the
// program code memory.
let data: u8 = unsafe { read_byte(addr_of!(P_BYTE)) };
assert_eq!(b'A', data);

Safety

The given point must be valid in the program domain. Notice that in AVR normal pointers (to data) are into the data domain, NOT the program domain.

Typically only function pointers (which make no sense here) and pointer to or into statics that are defined to be stored into progmem are valid. For instance, a valid progmem statics would be one, that is attributed with #[link_section = ".progmem.data"].

Also general Rust pointer dereferencing constraints apply, i.e. it must not be dangling.