RPPAL is a Rust library that provides access to the Raspberry Pi's GPIO and SPI peripherals. Support for additional peripherals will be added in future updates. The library is compatible with the Raspberry Pi A, A+, B, B+, 2B, 3B, 3B+, Compute, Compute 3, Zero and Zero W.
Backwards compatibility for minor revisions isn't guaranteed until the library reaches v1.0.0.
Documentation for the latest release can be found at docs.golemparts.com/rppal. Documentation for earlier releases is stored at docs.rs/rppal.
To ensure fast performance, RPPAL interfaces with the GPIO peripheral by directly accessing the registers through either /dev/gpiomem
or /dev/mem
. GPIO interrupts are controlled using the sysfs interface.
RPPAL accesses the Raspberry Pi's main and auxiliary SPI peripherals through the spidev device interface.
Several features offered by the generic spidev interface aren't fully supported by the underlying driver or the BCM283x SoC: SPILSBFIRST (LSB first bit order), SPI3WIRE (bidirectional mode), SPILOOP (loopback mode), SPINOCS (no Slave Select), SPIREADY (slave ready signal), SPITXDUAL/SPIRXDUAL (dual SPI), SPITXQUAD/SPIRX_QUAD (quad SPI), and any number of bits per word other than 8.
If your slave device requires SPILSBFIRST, you can use the
reverse_bits
function instead to reverse the bit order in software.
SPI_LOOP mode can be achieved by connecting the MOSI and MISO pins together.
SPINOCS can be implemented by connecting the Slave Select pin on your slave device to any other available GPIO pin on the Pi, and manually changing it to high and low as needed.
Add a dependency for rppal
to your Cargo.toml
.
toml
[dependencies]
rppal = "0.6"
Link and import rppal
from your crate root.
rust
extern crate rppal;
Call Gpio::new()
to create a new Gpio instance with the default settings. In production code, you'll want to parse the result rather than unwrap it.
```rust use rppal::gpio::Gpio;
let mut gpio = Gpio::new().unwrap(); ```
```rust extern crate rppal;
use std::thread; use std::time::Duration;
use rppal::gpio::{Gpio, Mode, Level}; use rppal::system::DeviceInfo;
// The GPIO module uses BCM pin numbering. BCM GPIO 18 can be accessed through physical pin 12. const GPIO_LED: u8 = 18;
fn main() { let deviceinfo = DeviceInfo::new().unwrap(); println!("Model: {} (SoC: {})", deviceinfo.model(), device_info.soc());
let mut gpio = Gpio::new().unwrap();
gpio.set_mode(GPIO_LED, Mode::Output);
// Blink an LED attached to the pin on and off
gpio.write(GPIO_LED, Level::High);
thread::sleep(Duration::from_millis(500));
gpio.write(GPIO_LED, Level::Low);
} ```
Always be careful when working with the Raspberry Pi's peripherals, especially if you attach any external components to the GPIO pins. Improper use can lead to permanent damage.
If you're not working directly on a Raspberry Pi, you'll likely need to cross compile your code for the appropriate ARM architecture. Check out this guide for more information, or try the cross project for "zero setup" cross compilation.
Copyright (c) 2017-2018 Rene van der Meer. Released under the MIT license.