Crate sx1509 [−] [src]
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();
Structs
Borrowed |
The |
Owned |
The |
Sx1509 |
The |
Enums
Register |
Constants
DEFAULT_ADDRESS |
The default address of the SparkFun board, with no jumpers applied to alter its address. |