pub struct I2cPart { /* private fields */ }Expand description
A part on an I2C bus, answering from its registers.
§Examples
use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::I2cPart;
// A BME280, whose chip id register holds the id its datasheet gives.
const BME280: u8 = 0x76;
const CHIP_ID_REGISTER: u8 = 0xD0;
const BME280_CHIP_ID: u8 = 0x60;
let mut part = I2cPart::new(BME280).holding(CHIP_ID_REGISTER, &[BME280_CHIP_ID]);
let mut id = [0u8; 1];
part.write_read(BME280, &[CHIP_ID_REGISTER], &mut id).unwrap();
assert_eq!(id, [BME280_CHIP_ID]);
// What a driver writes stays written, so a test can read its configuration back: here
// one temperature sample, one pressure sample, and forced mode, in ctrl_meas.
const CTRL_MEAS: u8 = 0xF4;
const FORCED_ONCE: u8 = 0b001_001_01;
part.write(BME280, &[CTRL_MEAS, FORCED_ONCE]).unwrap();
assert_eq!(part.register(CTRL_MEAS), FORCED_ONCE);Implementations§
Source§impl I2cPart
impl I2cPart
Sourcepub fn holding(self, first: u8, bytes: &[u8]) -> I2cPart
pub fn holding(self, first: u8, bytes: &[u8]) -> I2cPart
The same part, holding these bytes from a register on.
§Arguments
first- the register the bytes start at.bytes- what to put there. Past the last register it wraps to the first.
§Returns
The part.
§Examples
use pamoja_hal::sim::I2cPart;
// A 16-bit reading, high byte first, lands in two registers from the first one.
let [high, low] = 1000u16.to_be_bytes();
let part = I2cPart::new(0x48).holding(0x00, &[high, low]);
assert_eq!([part.register(0x00), part.register(0x01)], [high, low]);Sourcepub fn load(&mut self, first: u8, bytes: &[u8])
pub fn load(&mut self, first: u8, bytes: &[u8])
Puts bytes in the part, from a register on.
§Arguments
first- the register the bytes start at.bytes- what to put there. Past the last register it wraps to the first.
Trait Implementations§
Source§impl I2c for I2cPart
impl I2c for I2cPart
Source§fn transaction(
&mut self,
address: SevenBitAddress,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error>
fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>
Execute the provided operations on the I2C bus. Read more
Auto Trait Implementations§
impl Freeze for I2cPart
impl RefUnwindSafe for I2cPart
impl Send for I2cPart
impl Sync for I2cPart
impl Unpin for I2cPart
impl UnsafeUnpin for I2cPart
impl UnwindSafe for I2cPart
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