Crate xca9548a[−][src]
This is a platform agnostic Rust driver for the TCA9548A and
PCA9548A I2C switches/multiplexers, based on the embedded-hal
traits.
This driver allows you to:
- Enable one or multiple I2C channels.
- Communicate with the slaves connected to the enabled channels transparently.
The devices
The TCA9548A and PCA9548 devices have eight bidirectional translating switches that can be controlled through the I2C bus. The SCL/SDA upstream pair fans out to eight downstream pairs, or channels. Any individual SCn/SDn channel or combination of channels can be selected, determined by the contents of the programmable control register. These downstream channels can be used to resolve I2C slave address conflicts. For example, if eight identical digital temperature sensors are needed in the application, one sensor can be connected at each channel: 0-7.
Datasheets
Usage examples (see also examples folder)
Instantiating with the default address
Import this crate and an embedded_hal
implementation, then instantiate
the device:
extern crate linux_embedded_hal as hal; extern crate xca9548a; use hal::I2cdev; use xca9548a::{TCA9548A, SlaveAddr}; let dev = I2cdev::new("/dev/i2c-1").unwrap(); let address = SlaveAddr::default(); let mut i2c_switch = TCA9548A::new(dev, address);
Providing an alternative address
extern crate linux_embedded_hal as hal; extern crate xca9548a; use hal::I2cdev; use xca9548a::{TCA9548A, SlaveAddr}; let dev = I2cdev::new("/dev/i2c-1").unwrap(); let (a2, a1, a0) = (false, false, true); let address = SlaveAddr::Alternative(a2, a1, a0); let mut i2c_switch = TCA9548A::new(dev, address);
Selecting channel 0 (SD0/SC0 pins)
extern crate linux_embedded_hal as hal; extern crate xca9548a; use hal::I2cdev; use xca9548a::{TCA9548A, SlaveAddr}; let dev = I2cdev::new("/dev/i2c-1").unwrap(); let address = SlaveAddr::default(); let mut i2c_switch = TCA9548A::new(dev, address); i2c_switch.select_channels(0b0000_0001).unwrap();
Reading and writing to device connected to channel 0 (SD0/SC0 pins)
extern crate embedded_hal; extern crate linux_embedded_hal as hal; extern crate xca9548a; use hal::I2cdev; use embedded_hal::blocking::i2c::{ Read, Write }; use xca9548a::{ TCA9548A, SlaveAddr }; let dev = I2cdev::new("/dev/i2c-1").unwrap(); let address = SlaveAddr::default(); let mut i2c_switch = TCA9548A::new(dev, address); i2c_switch.select_channels(0b0000_0001).unwrap(); let slave_address = 0b010_0000; // example slave address let data_for_slave = [0b0101_0101, 0b1010_1010]; // some data to be sent // Read some data from a slave connected to channel 0 using the // I2C switch just as a normal I2C device let mut read_data = [0; 2]; i2c_switch.read(slave_address, &mut read_data).unwrap(); // Write some data to the slave i2c_switch.write(slave_address, &data_for_slave).unwrap();
Structs
PCA9548A |
Device driver |
TCA9548A |
Device driver |
Enums
Error |
All possible errors in this crate |
SlaveAddr |
Possible slave addresses |