reg-map

Crates.ioreg-map
lib.rsreg-map
version0.1.1
sourcesrc
created_at2024-09-20 12:57:33.714976
updated_at2024-10-09 11:03:20.098625
descriptionDerive volatile accesses to a register map and memory-mapped IO
homepage
repositoryhttps://github.com/rikyborg/reg-map
max_upload_size
id1381226
size68,487
Riccardo Borgani (rikyborg)

documentation

https://docs.rs/reg-map

README

reg-map

Crates.io Version docs.rs

Derive volatile accesses to a register map and memory-mapped IO.

Documentation

API documentation in available at docs.rs/reg-map.

Usage

Here's a quick-and-dirty example, see the API documentation for more use cases such as iteration and a more in-depth description of the functionality.

use reg_map::RegMap;

// define struct Registers with the register map
// and derive the pointer RegistersPtr using the RegMap macro
#[repr(C)]
#[derive(RegMap, Default)]
struct Registers {
    field1: u64,
    field2: u32,
    #[reg(RO)]
    read_only_field: i8,
    #[reg(WO)]
    write_only_field: u128,
    #[reg(RW)]
    read_write_is_default: i16,
}

// initialize the base struct
// and obtain a pointer to the registers
let mut regs = Registers::default();
let ptr = RegistersPtr::from_mut(&mut regs);

// when dealing with e.g. memory-mapped IO (MMIO),
// you'd probably just get a pointer to the data from a known base address
// let ptr = unsafe { RegistersPtr::from_ptr(0xAA55_000 as *mut _) };

// all write() operations are volatile
ptr.field1().write(10);
ptr.field2().write(32);
ptr.write_only_field().write(76);
ptr.read_write_is_default().write(98);

// all read() operations are volatile
assert_eq!(ptr.field1().read(), 10);
assert_eq!(ptr.field2().read(), 32);
assert_eq!(ptr.read_only_field().read(), 0);
assert_eq!(ptr.read_write_is_default().read(), 98);

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 3

cargo fmt