pub struct WordPart { /* private fields */ }Expand description
A part on an I2C bus whose registers are sixteen bits wide, answering from them.
This is how Texas Instruments lays out parts such as the TMP117, the INA219 and INA226, the OPT3001, the ADS1115, and the HDC1080. A pointer byte names a register and a register travels most significant byte first. A write of the pointer alone aims the next read; a write of the pointer and a word stores the word and leaves the pointer where it was; a read takes words from the pointer on, carrying into the next register when it runs past one, as an HDC1080 does after a triggered measurement.
Some bits are the part’s to set, however a driver writes the register: a conversion-ready
flag, an overflow flag. WordPart::read_only marks them, and they then read as the part
holds them.
§Examples
use pamoja_hal::i2c::I2c;
use pamoja_hal::sim::WordPart;
// A TMP117, from its datasheet: the device id register reads 0x0117, and Data_Ready,
// bit 13 of the configuration register, is the part's to set.
const TMP117: u8 = 0x48;
const CONFIGURATION: u8 = 0x01;
const DEVICE_ID: u8 = 0x0F;
const DATA_READY: u16 = 1 << 13;
let mut part = WordPart::new(TMP117)
.holding(DEVICE_ID, 0x0117)
.holding(CONFIGURATION, DATA_READY)
.read_only(CONFIGURATION, DATA_READY);
let mut id = [0u8; 2];
part.write_read(TMP117, &[DEVICE_ID], &mut id).unwrap();
assert_eq!(u16::from_be_bytes(id), 0x0117);
// A driver writing the factory configuration leaves the flag as the part holds it.
const CONFIG_RESET: u16 = 0x0220;
let [high, low] = CONFIG_RESET.to_be_bytes();
part.write(TMP117, &[CONFIGURATION, high, low]).unwrap();
assert_eq!(part.word(CONFIGURATION), CONFIG_RESET | DATA_READY);Implementations§
Source§impl WordPart
impl WordPart
Sourcepub fn set(&mut self, register: u8, value: u16)
pub fn set(&mut self, register: u8, value: u16)
Puts a value in one register, read-only bits included, the way the part itself would.
§Arguments
register- the register.value- what it holds.