#gpio-pin #peripheral #hal #devices #microcontrollers #embedded-hal #lpc81x

nightly lpc81x-hal

High-level API and HAL implementations for LPC81x microcontrollers

2 unstable releases

0.2.0 Jul 6, 2019
0.1.0 Jul 4, 2019

#32 in #microcontrollers

MIT license

77KB
1.5K SLoC

High-level API and HAL implementations for LPC81x microcontrollers.

This library wraps the lower-level lpc81x-pac to provide a more ergonomic interface to the LPC81x peripherals, including implementations of some of the embedded-hal crates so that these peripheraps can be used with platform-agnostic external device drivers in other crates.


Warning: The interfaces in this crate are not stable. Any existing API may see breaking changes in subsequent versions until all of the peripherals have basic support and until embedded-hal itself is stable enough to commit to compatibility with it.

Pin to a specific version in your application to avoid unexpected breakage.


The core concept in this library is objects representing the I/O pins. LPC81x parts include a switch matrix that allow most integrated peripherals to be assigned to any of the available pins, and so this library models that capability by having the peripheral objects take ownership of the pins they are assigned to. That allows compile-time checks to ensure that only one output function is assigned to each pin, as is required by the switch matrix hardware.

The following example shows how a calling application might activate the SPI0 periopheral and switch a GPIO pin to digital output mode in order to serve as a chip-select signal:

// Consume the peripherals singleton. This also consumes the corresponding
// singleton from `lpc81x-pac`, so all peripheral access must be via
// this object alone.
let p = hal::Peripherals::take().unwrap();

// Most of the peripheral APIs require pins as arguments. Unassigned pins
// initially live in p.pins and can be moved out into other devices as
// needed, which automatically configures the switch matrix.
let pins = p.pins;

// Use GPIO pins 12 and 14 as the SCLK and MOSI signals respectively.
// An SCLK pin is always required to activate an SPI peripheral, but
// the other signals can be assigned selectively depending on the needs
// of the application. Must assign at least MOSI or MISO for anything
// useful to happen, though.
let spi = p
    .spi0
    .activate_as_host(
        pins.gpio12,
        hal::spi::cfg::Config {
            sclk_mode: embedded_hal::spi::MODE_0,
            bit_order: hal::spi::cfg::BitOrder::MSBFirst,
        },
    )
    .with_mosi(pins.gpio14);

// GPIO pin 13 will be the chip select signal. Calling to_digital_output
// consumes the unassigned pin and returns an assigned pin that has been
// configured to act as a digital output.
let cs = pins.gpio13.to_digital_output(true);

// Can now pass the "spi" and "cs" objects into any `embedded-hal` device
// driver that works with a SPI interface and a chip select signal.
let dev = any_driver::Driver::new(spi, cs);

There are more examples in the examples directory within the crate source code. The above example is a cut-down version of the ssd1322 example.

Dependencies

~8.5MB
~194K SLoC