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
impl Address
Sourcepub fn seven_bit(address: u8) -> Result<Address, GpioError>
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.
Sourcepub fn ten_bit(address: u16) -> Result<Address, GpioError>
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.
Sourcepub fn value(self) -> u16
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.
Sourcepub fn is_ten_bit(self) -> bool
pub fn is_ten_bit(self) -> bool
Returns true if this is a 10-bit address.
Sourcepub fn frame_len(self) -> usize
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.
Sourcepub fn is_reserved(self) -> bool
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.
Sourcepub fn is_general_call(self) -> bool
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.
Sourcepub fn frame(self, direction: Direction) -> AddressFrame
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.
Sourcepub fn write_frame(
self,
direction: Direction,
out: &mut [u8],
) -> Result<usize, GpioError>
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 leastframe_lenbytes.
§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.