pub struct Rules {
pub write: fn(registers: &mut [u8; 256], register: u8, value: u8),
pub read: fn(registers: &[u8; 256], register: u8) -> u8,
pub next: fn(registers: &[u8; 256], register: u8) -> u8,
}Expand description
The rules a part with byte-wide registers keeps beyond plain memory: which writes it takes, what a read returns, and where its register pointer moves.
Rules::MEMORY stores every byte written, reads back what it stored, and moves on one
register a byte, which is most parts. A part whose datasheet says otherwise, such as one
that ignores a register while it runs or loads many registers from one write, gives its
own functions. They are plain functions over the register map, so a part that keeps
rules still needs no allocator.
§Examples
use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::{I2cPart, Rules, REGISTERS};
// A PCA9685 takes a new PWM frequency only while it sleeps: its prescale register
// ignores a write unless the SLEEP bit of MODE1 is set.
const PCA9685: u8 = 0x40;
const MODE1: u8 = 0x00;
const SLEEP: u8 = 1 << 4;
const PRE_SCALE: u8 = 0xFE;
fn write(registers: &mut [u8; REGISTERS], register: u8, value: u8) {
if register != PRE_SCALE || registers[usize::from(MODE1)] & SLEEP != 0 {
registers[usize::from(register)] = value;
}
}
let rules = Rules { write, ..Rules::MEMORY };
// Awake, at its power-on prescale of 30, about 200 Hz from its 25 MHz clock.
let mut part = I2cPart::new(PCA9685).following(rules).holding(PRE_SCALE, &[30]);
// 121 is the prescale for the 50 Hz a servo wants.
part.write(PCA9685, &[PRE_SCALE, 121]).unwrap();
assert_eq!(part.register(PRE_SCALE), 30, "ignored while it runs");
part.write(PCA9685, &[MODE1, SLEEP]).unwrap();
part.write(PCA9685, &[PRE_SCALE, 121]).unwrap();
assert_eq!(part.register(PRE_SCALE), 121, "taken while it sleeps");Fields§
§write: fn(registers: &mut [u8; 256], register: u8, value: u8)Takes one byte a driver wrote to a register: stores it, drops it, or loads it elsewhere.
read: fn(registers: &[u8; 256], register: u8) -> u8What a read of a register returns.
next: fn(registers: &[u8; 256], register: u8) -> u8Where the register pointer goes after a byte at a register is written or read.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Rules
impl RefUnwindSafe for Rules
impl Send for Rules
impl Sync for Rules
impl Unpin for Rules
impl UnsafeUnpin for Rules
impl UnwindSafe for Rules
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more