#i2c #gpio-pin #gpio #pca #gpio-expander #pcf

no-std port-expander

Abstraction for I2C port-expanders

14 releases

0.6.4 Feb 1, 2025
0.6.3 Sep 16, 2024
0.6.2 Jul 7, 2024
0.5.0 Feb 19, 2024
0.2.1 Apr 26, 2021

#91 in Embedded development

Download history 20/week @ 2024-10-25 5/week @ 2024-11-01 1/week @ 2024-11-08 4/week @ 2024-11-15 1/week @ 2024-11-22 17/week @ 2024-12-06 19/week @ 2024-12-13 6/week @ 2024-12-20 4/week @ 2024-12-27 10/week @ 2025-01-03 17/week @ 2025-01-10 18/week @ 2025-01-24 172/week @ 2025-01-31 32/week @ 2025-02-07

222 downloads per month
Used in 2 crates

MIT/Apache

145KB
3K SLoC

port-expander crates.io page docs.rs page

This is a crate providing a common abstraction for I²C port-expanders. This abstraction is not necessarily the most performant, but it allows using the pins just like direct GPIOs. Because the pin types also implement the embedded-hal digital IO traits, they can also be passed to further drivers downstream (e.g. as a reset or chip-select pin).

Example

// Initialize I2C peripheral from HAL
let i2c = todo!();

// A0: HIGH, A1: LOW, A2: LOW
let mut pca9555 = port_expander::Pca9555::new(i2c, true, false, false);
let pca_pins = pca9555.split();

let io0_0 = pca_pins.io0_0.into_output().unwrap();
let io1_5 = pca_pins.io0_1; // default is input

io0_0.set_high().unwrap();
assert!(io1_5.is_high().unwrap());

Accessing multiple pins at the same time

Sometimes timing constraints mandate that multiple pin accesses (reading or writing) happen at the same time. The write_multiple() and read_multiple() methods are designed for doing this.

Supported Devices

The following list is what port-expander currently supports. If you needs support for an additional device, it should be easy to add. It's best to take a similar existing implementation as inspiration. Contributions welcome!

Non-local sharing

port-expander uses a custom trait for abstracting different kinds of mutexes: PortMutex. This means you can also make the pins shareable across task/thread boundaries, given that you provide an appropriate mutex type:

// Initialize I2C peripheral from HAL
let i2c = todo!();

// A0: HIGH, A1: LOW, A2: LOW
let mut pca9555: port_expander::Pca9555<std::sync::Mutex<_>> =
    port_expander::Pca9555::with_mutex(i2c, true, false, false);
let pca_pins = pca9555.split();

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~64KB