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

#1175 in Hardware support

Download history 155/week @ 2023-10-16 184/week @ 2023-10-23 172/week @ 2023-10-30 144/week @ 2023-11-06 161/week @ 2023-11-13 190/week @ 2023-11-20 160/week @ 2023-11-27 130/week @ 2023-12-04 163/week @ 2023-12-11 190/week @ 2023-12-18 147/week @ 2023-12-25 139/week @ 2024-01-01 150/week @ 2024-01-08 149/week @ 2024-01-15 152/week @ 2024-01-22 136/week @ 2024-01-29

606 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