20 releases

0.6.1 Nov 22, 2021
0.5.4 May 29, 2019
0.5.3 Apr 19, 2018
0.5.2 Mar 3, 2018
0.2.0 Mar 19, 2015

#353 in Embedded development

Download history 4645/week @ 2023-12-12 3704/week @ 2023-12-19 2362/week @ 2023-12-26 4113/week @ 2024-01-02 4649/week @ 2024-01-09 4480/week @ 2024-01-16 4398/week @ 2024-01-23 4690/week @ 2024-01-30 4915/week @ 2024-02-06 4742/week @ 2024-02-13 5223/week @ 2024-02-20 5736/week @ 2024-02-27 5750/week @ 2024-03-05 5167/week @ 2024-03-12 4951/week @ 2024-03-19 4325/week @ 2024-03-26

21,108 downloads per month
Used in 47 crates (17 directly)

MIT/Apache

32KB
469 lines

sysfs_gpio

Build Status Version Minimum Supported Rust Version License

The sysfs_gpio crate provides access to the Linux sysfs GPIO interface (https://www.kernel.org/doc/Documentation/gpio/sysfs.txt). It seeks to provide an API that is safe, convenient, and efficient and supports exporting, unexporting, reading, writing and waiting for interrupts on pins.

Many devices such as the Raspberry Pi or Beaglebone Black provide userspace access to a number of GPIO peripherals. The standard kernel API for providing access to these GPIOs is via sysfs.

You might want to also check out the gpio-utils Project for a convenient way to associate names with pins and export them as part of system boot. That project uses this library.

Install/Use

To use sysfs_gpio, first add this to your Cargo.toml:

[dependencies]
sysfs_gpio = "0.6"

Then, add this to your crate root:

use sysfs_gpio;

Example/API

Blinking an LED:

use sysfs_gpio::{Direction, Pin};
use std::thread::sleep;
use std::time::Duration;

fn main() {
    let my_led = Pin::new(127); // number depends on chip, etc.
    my_led.with_exported(|| {
        my_led.set_direction(Direction::Out).unwrap();
        loop {
            my_led.set_value(0).unwrap();
            sleep(Duration::from_millis(200));
            my_led.set_value(1).unwrap();
            sleep(Duration::from_millis(200));
        }
    }).unwrap();
}

More Examples:

Features

The following features are planned for the library:

  • Support for exporting a GPIO
  • Support for unexporting a GPIO
  • Support for setting the direction of a GPIO (in/out)
  • Support for reading the value of a GPIO input
  • Support for writing the value of a GPIO output
  • Support for configuring whether a pin is active low/high
  • Support for configuring interrupts on GPIO
  • Support for polling on GPIO with configured interrupt
  • Support for asynchronous polling using mio or tokio (requires enabling the mio-evented or async-tokio crate features, respectively)

Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.46.0 and up. It might compile with older versions but that may change in any new patch release.

Cross Compiling

Most likely, the machine you are running on is not your development machine (although it could be). In those cases, you will need to cross-compile. The rust-cross guide provides excellent, detailed instructions for cross-compiling.

Running the Example

Cross-compiling can be done by specifying an appropriate target. You can then move that to your device by whatever means and run it.

$ cargo build --target=arm-unknown-linux-gnueabihf --example blinky
$ scp target/arm-unknown-linux-gnueabihf/debug/examples/blinky ...

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Code of Conduct

Contribution to this crate is organized under the terms of the Rust Code of Conduct, the maintainer of this crate, the Embedded Linux Team, promises to intervene to uphold that code of conduct.

Dependencies

~2–12MB
~110K SLoC