#embedded-hal-driver #i2c #devices #io #board #platform

no-std sx1509

A platform agnostic driver for the SX1509

2 unstable releases

Uses old Rust 2015

0.2.0 May 14, 2018
0.1.0 May 12, 2018

#117 in #board

34 downloads per month

MIT/Apache

19KB
222 lines

sx1509 driver for Rust

This module implements a driver for the sx1509, an IO Expander device addressable via I2C. It was implemented against this board: https://www.sparkfun.com/products/13601

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.


lib.rs:

A platform agnostic Rust friver for the [sx1509], based on the embedded-hal traits.

The Device

The [sx1509] is a GPIO expander that also providers LED driver and keypad scanning functionality. The device has two banks of 8 GPIO pins.

Supported features

At the time of writing, the support is very bare bones: you can reset the device and configure basic GPIO functions by operating on a bank at a time.

Future

There's scope for some interesting API design choices; for example, it would be nice to expose the individual GPIO pins as instances that implement the GPIO traits from embedded-hal so that they could in turn be used to interface with other drivers.

The keypad and LED driver functionality has some potential for nice type safe API design by moving pins/banks into different modes.

Care needs to be taken to find and ergnomic and zero cost way to express these APIs.

For the moment we just have a fairly dumb pass through!

Usage

Import this crate an an embedded_hal implementation:

extern crate metro_m0 as hal;
extern crate sx1509;

Initialize the I2C bus (differs between the various hal implementations):

let mut i2c = hal::I2CMaster3::new(
&clocks,
400.khz(),
peripherals.SERCOM3,
&mut peripherals.PM,
&mut peripherals.GCLK,
// Metro M0 express has I2C on pins PA22, PA23
Sercom3Pad0::Pa22(pins.pa22.into_function_c(&mut pins.port)),
Sercom3Pad1::Pa23(pins.pa23.into_function_c(&mut pins.port)),
);

and then instantiate the driver:

let mut expander = sx1509::Sx1509::new(&mut i2c, sx1509::DEFAULT_ADDRESS);
expander.borrow(&mut i2c).software_reset()?;
expander.borrow(&mut i2c).set_bank_a_direction(0)?;
// read the pins from bank a
let pins = expander.borrow(&mut i2c).get_bank_a_data()?;

Note that you must borrow the i2c bus for the duration of each operation. This allows the bus to be shared and used to address other devices in between operations.

It is possible to take ownership of the bus for a longer period of time. That is required for Future based usage. While Future based usage isn't supported at this time, we do have support for this mode of operation:

let mut owned = expander.take(i2c);

// Now you can borrow and perform the IO when you are ready:
let pins = owned.borrow().get_bank_a_data()?;

// and relinquish the bus when done
let (expander, i2c) = owned.release();

Dependencies

~71KB