This is a platform agnostic Rust driver for the for TCA9548A and PCA9548A I2C
switches/multiplexers using the [embedded-hal
] traits.
This driver allows you to:
- Enable one or multiple I2C channels. See: select_channels()
.
- Communicate with the slaves connected to the enabled channels transparently.
- Split the device into slave (virtual) I2C devices (one per channel). See: split()
.
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: - TCA9548A - PCA9548A
To use this driver, import this crate and an embedded_hal
implementation,
then instantiate the device.
Please find additional examples using hardware in this repository: [driver-examples]
```rust extern crate embeddedhal; extern crate linuxembedded_hal; extern crate xca9548a;
use embeddedhal::blocking::i2c::{Read, Write, WriteRead}; use linuxembedded_hal::I2cdev; use xca9548a::{Error, SlaveAddr, Xca9548a};
fn main() { let slaveaddress = 0b0100000; // example slave address let writedata = [0b01010101, 0b1010_1010]; // some data to be sent let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut switch = Xca9548a::new(dev, SlaveAddr::default());
// Enable channel 0
switch.select_channels(0b0000_0001).unwrap();
// write to slave connected to channel 0 using
// the I2C switch just as a normal I2C device
if switch.write(slave_address, &write_data).is_err() {
println!("Error received!");
}
// read from the slave connected to channel 0 using the
// I2C switch just as a normal I2C device
let mut read_data = [0; 2];
if switch.read(slave_address, &mut read_data).is_err() {
println!("Error received!");
}
// write_read from the slave connected to channel 0 using
// the I2C switch just as a normal I2C device
if switch
.write_read(slave_address, &write_data, &mut read_data)
.is_err()
{
println!("Error received!");
}
// Split the device and pass the slave (virtual) I2C devices
// to an external driver
let parts = switch.split();
let mut some_driver = Driver::new(parts.i2c1);
let mut some_other_driver = Driver::new(parts.i2c2);
some_driver.do_something().unwrap();
some_other_driver.do_something().unwrap();
}
/// Some driver defined in a different crate.
/// Defined here for completeness.
struct Driver
impl
For questions, issues, feature requests, and other changes, please file an issue in the github project.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.