5 unstable releases

Uses old Rust 2015

0.4.1 Mar 13, 2018
0.4.0 Mar 4, 2018
0.3.0 Jun 16, 2017
0.2.1 Jun 16, 2017
0.2.0 Jun 14, 2017

#1864 in Embedded development

Download history 866/week @ 2024-07-23 418/week @ 2024-07-30 356/week @ 2024-08-06 256/week @ 2024-08-13 665/week @ 2024-08-20 608/week @ 2024-08-27 487/week @ 2024-09-03 675/week @ 2024-09-10 973/week @ 2024-09-17 609/week @ 2024-09-24 909/week @ 2024-10-01 660/week @ 2024-10-08 1007/week @ 2024-10-15 1322/week @ 2024-10-22 1053/week @ 2024-10-29 593/week @ 2024-11-05

4,254 downloads per month
Used in 6 crates

MIT license

13KB
233 lines

Rust GPIO

Deals with GPIO access on Linux and bare metal embedded systems, through sysfs and direct memory access. Works on stable Rust.

Roadmap

  • GPIO write support
  • Read support
  • Interrupt support

Other libraries

Other libraries can be found on crates.io. These include:

  • sysfs_gpio <https://github.com/rust-embedded/rust-sysfs-gpio>_ handles GPIO only via SysFS, but exposes all features. Slightly lower level.

  • cylus <https://github.com/Vikaton/cylus>_ Documentation is dead, does a few questionable things like unwrapping()

  • cupi <https://github.com/cuprumpi/cupi>_ Most comprehensive GPIO library, includes almost all features planned for gpio. Does not use volatile.

    TODO: Benchmark


lib.rs:

GPIO interface

The GPIO crate allows easy and fast access to GPIO pins. It aims to provide an ergonomic interface while being lower overhead, enabling high-frequency output without complicating simple tasks.

The core interface is defined using GpioValue and the GpioOut/GpioIn traits. All backends implement at least some of these traits, making them interchangeable, e.g. for testing.

The most commonly used implementation is based on the Linux GPIO Sysfs interface, found inside the sysfs crate.

Example

use gpio::{GpioIn, GpioOut};
use std::{thread, time};

// Let's open GPIO23 and -24, e.g. on a Raspberry Pi 2.
let mut gpio23 = gpio::sysfs::SysFsGpioInput::open(23).unwrap();
let mut gpio24 = gpio::sysfs::SysFsGpioOutput::open(24).unwrap();

// GPIO24 will be toggled every second in the background by a different thread
let mut value = false;
thread::spawn(move || loop {
    gpio24.set_value(value).expect("could not set gpio24");
    thread::sleep(time::Duration::from_millis(1000));
    value = !value;
});

// The main thread will simply display the current value of GPIO23 every 100ms.
loop {
    println!("GPIO23: {:?}", gpio23.read_value().unwrap());
    thread::sleep(time::Duration::from_millis(100));
}

TODO

  • /dev/mem interface: Higher frequency port usage

No runtime deps