[][src]Function avr_progmem::read_byte

pub unsafe fn read_byte(p_addr: *const u8) -> u8

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::read_byte;

// 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"]
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(&P_BYTE) };
assert_eq!(b'A', data);

Safety

The given point must be valid in the program domain which in AVR normal pointers (to data) are NOT, because they point into the data 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"].

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