Skip to main content

WordPart

Struct WordPart 

Source
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

Source

pub const fn new(address: u8) -> WordPart

A part answering at one address, with every register reading zero.

§Arguments
  • address - the address it answers to.
§Returns

The part.

Source

pub fn holding(self, register: u8, value: u16) -> WordPart

The same part, holding a value in one register.

§Arguments
  • register - the register.
  • value - what it holds, read-only bits included.
§Returns

The part.

Source

pub fn read_only(self, register: u8, mask: u16) -> WordPart

The same part, with some bits of one register left as the part holds them however a driver writes the register.

§Arguments
  • register - the register.
  • mask - the bits that are the part’s to set.
§Returns

The part.

Source

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.
Source

pub const fn word(&self, register: u8) -> u16

What one register holds now.

§Arguments
  • register - which one.
§Returns

Its value, which is what a driver wrote there if it wrote one, apart from the read-only bits.

Source

pub const fn address(&self) -> u8

The address this part answers to.

§Returns

The address.

Source

pub const fn transfers(&self) -> usize

How many transfers the part has served.

§Returns

The count, one for each transfer a driver made, however many operations it held.

Trait Implementations§

Source§

impl Clone for WordPart

Source§

fn clone(&self) -> WordPart

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WordPart

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ErrorType for WordPart

Source§

type Error = PartError

Error type
Source§

impl From<WordPart> for Part

Available on crate feature alloc only.
Source§

fn from(part: WordPart) -> Part

Converts to this type from the input type.
Source§

impl FromPart for WordPart

Available on crate feature alloc only.
Source§

fn from_part(part: Part) -> Option<WordPart>

The part, when it is this kind. Read more
Source§

impl I2c for WordPart

Source§

fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>

Execute the provided operations on the I2C bus. Read more
§

fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error>

Reads enough bytes from slave with address to fill read. Read more
§

fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>

Writes bytes to slave with address address. Read more
§

fn write_read( &mut self, address: A, write: &[u8], read: &mut [u8], ) -> Result<(), Self::Error>

Writes bytes to slave with address address and then reads enough bytes to fill read in a single transaction. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.