Skip to main content

Address

Struct Address 

Source
pub struct Address { /* private fields */ }
Expand description

An I2C device address, 7-bit or 10-bit, validated to its range.

I2C addresses come in two widths. The original 7-bit address shares its byte with the R/W bit, so it lands on the wire as (address << 1) | r/w. The later 10-bit extension stays backward compatible by spending the reserved 11110xx prefix: the first byte is 11110, then the top two address bits, then the R/W bit, and the second byte is the low eight address bits. Construct an address with seven_bit or ten_bit, which reject out-of-range values, then turn it into the bytes a controller sends with write_frame.

§Examples

use pamoja_gpio::i2c::{Address, Direction};

// 7-bit: a BME280 at 0x76 writes as 0xEC and reads as 0xED.
let bme = Address::seven_bit(0x76)?;
let mut buf = [0u8; 2];
assert_eq!(bme.write_frame(Direction::Write, &mut buf)?, 1);
assert_eq!(buf[0], 0xEC);
assert_eq!(bme.write_frame(Direction::Read, &mut buf)?, 1);
assert_eq!(buf[0], 0xED);

Implementations§

Source§

impl Address

Source

pub fn seven_bit(address: u8) -> Result<Address, GpioError>

Creates a 7-bit I2C address.

The whole range is accepted, including the addresses the specification reserves; those are still legal on the wire (the general call address 0x00 is a broadcast, for instance). Use is_reserved to test for them.

§Arguments
  • address - the 7-bit device address, 0x00..=0x7F.
§Returns

The validated address.

§Errors

GpioError::AddressOutOfRange if address exceeds 0x7F.

Source

pub fn ten_bit(address: u16) -> Result<Address, GpioError>

Creates a 10-bit I2C address.

§Arguments
  • address - the 10-bit device address, 0x000..=0x3FF.
§Returns

The validated address.

§Errors

GpioError::AddressOutOfRange if address exceeds 0x3FF.

Source

pub fn value(self) -> u16

Returns the address value, without the R/W bit.

§Returns

The 7- or 10-bit address as passed to the constructor.

Source

pub fn is_ten_bit(self) -> bool

Returns true if this is a 10-bit address.

Source

pub fn frame_len(self) -> usize

Returns the number of bytes write_frame emits.

§Returns

1 for a 7-bit address, 2 for a 10-bit address.

Source

pub fn is_reserved(self) -> bool

Returns true if a 7-bit address falls in a range the I2C specification reserves.

UM10204 reserves 0x00..=0x07 (general call and START byte, CBUS, a bus-format code, a future code, and the Hs-mode master codes) and 0x78..=0x7F (the 10-bit addressing prefix and the device-ID codes), leaving 0x08..=0x77 for ordinary devices. A 10-bit address is not reserved in this sense, so this returns false for one.

§Returns

true if this is a 7-bit address in 0x00..=0x07 or 0x78..=0x7F.

Source

pub fn is_general_call(self) -> bool

Returns true if this is the general call address 0x00, the broadcast every device on the bus listens to.

Source

pub fn frame(self, direction: Direction) -> AddressFrame

Returns the addressing frame this address puts on the bus.

The same bytes write_frame produces, as a value, so a caller does not have to size and pass a scratch buffer to find out what an address looks like on the wire.

§Arguments
  • direction - whether the transfer reads or writes, which sets the R/W bit.
§Returns

The frame, one byte for a 7-bit address and two for a 10-bit one.

Source

pub fn write_frame( self, direction: Direction, out: &mut [u8], ) -> Result<usize, GpioError>

Writes the address byte(s) a controller puts on the bus for a transfer.

For a 7-bit address this is the single byte (address << 1) | r/w. For a 10-bit address it is two bytes: 11110 then the top two address bits then the R/W bit, followed by the low eight address bits. A 10-bit read in practice first addresses the device with a write frame and then, after a repeated START, re-sends this first byte with the read bit set; this method emits the bytes for the direction asked for, leaving the START/repeated-START sequencing to the driver.

§Arguments
  • direction - whether the transfer reads or writes, which sets the R/W bit.
  • out - the buffer the frame is written into; it must hold at least frame_len bytes.
§Returns

The number of bytes written: 1 for a 7-bit address, 2 for a 10-bit address.

§Errors

GpioError::BufferTooSmall if out is shorter than frame_len.

Trait Implementations§

Source§

impl Clone for Address

Source§

fn clone(&self) -> Address

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 Copy for Address

Source§

impl Debug for Address

Source§

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

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

impl Eq for Address

Source§

impl PartialEq for Address

Source§

fn eq(&self, other: &Address) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Address

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