Skip to content

Gpio cdev feature #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ name = "linux-embedded-hal"
repository = "https://github.com/japaric/linux-embedded-hal"
version = "0.3.0"

[features]
default = ["sysfs_gpio"]

gpio_cdev = ["gpio-cdev"]

[dependencies]
embedded-hal = { version = "0.2.3", features = ["unproven"] }
i2cdev = "0.4.3"
spidev = "0.4"
sysfs_gpio = "0.5"
serial-unix = "0.4.0"
serial-core = "0.4.0"
nb = "0.1.1"
sysfs_gpio = { version = "0.5", optional = true }
gpio-cdev = { version="0.2", optional = true }

[dev-dependencies]
openpty = "0.1.0"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ This project is developed and maintained by the [Embedded Linux team][team].

## [Documentation](https://docs.rs/linux-embedded-hal)

## GPIO character device

Since Linux kernel v4.4 the use of sysfs GPIO was deprecated and replaced by the character device GPIO.
See [gpio-cdev documentation](https://github.com/rust-embedded/gpio-cdev#sysfs-gpio-vs-gpio-character-device) for details.

This crate includes feature flag `gpio_cdev` that replaces [sysfs_gpio](https://crates.io/crates/sysfs_gpio) by [gpio-cdev](https://crates.io/crates/gpio-cdev).
To enable it update your Cargo.toml.
```
linux-embedded-hal = { version = "0.3", features = ["gpio_cdev"] }
```

## License

Licensed under either of
Expand Down
54 changes: 47 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
//! [0]: https://crates.io/keywords/embedded-hal

#![deny(missing_docs)]
#![feature(doc_cfg)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to remove this? i don't think we want to stop building on stable

error[E0554]: `#![feature]` may not be used on the stable release channel
  --> src/lib.rs:14:1
   |
14 | #![feature(doc_cfg)]
   | ^^^^^^^^^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I am afraid that #[doc(cfg(feature = "gpio_cdev"))] won't work then. So I guess no point in keeping them either. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh yeah, that's a bit frustrating, i think building on stable is more important for now. i'd also like to see ci coverage (even though it's only running cargo check).

i guess we could build both in sub-paths (under default-features gates) and choose the default export with a feature, which would put them both in the docs, not require an additional test run, and let people runtime select if required, but, seems more complicated than is probably necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I have created alternative PR #30 . Let me know which one we should proceed with. Thanks


extern crate cast;
extern crate embedded_hal as hal;
#[cfg(feature = "gpio_cdev")]
pub extern crate gpio_cdev;
pub extern crate i2cdev;
pub extern crate nb;
pub extern crate serial_core;
pub extern crate serial_unix;
pub extern crate spidev;
#[cfg(not(feature = "gpio_cdev"))]
pub extern crate sysfs_gpio;
pub extern crate serial_unix;
pub extern crate serial_core;
pub extern crate nb;

use std::io::{self, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -92,11 +96,32 @@ impl hal::blocking::delay::DelayMs<u64> for Delay {
}
}

/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
///
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
#[cfg(feature = "gpio_cdev")]
#[doc(cfg(feature = "gpio_cdev"))]
pub struct Pin(pub gpio_cdev::LineHandle, bool);

#[cfg(feature = "gpio_cdev")]
#[doc(cfg(feature = "gpio_cdev"))]
impl Pin {
/// See [`gpio_cdev::Line::request`][0] for details.
///
/// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
pub fn new(handle: gpio_cdev::LineHandle) -> Result<Pin, gpio_cdev::errors::Error> {
let info = handle.line().info()?;
Ok(Pin(handle, info.is_active_low()))
}
}

/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
///
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
#[cfg(not(feature = "gpio_cdev"))]
pub struct Pin(pub sysfs_gpio::Pin);

#[cfg(not(feature = "gpio_cdev"))]
impl Pin {
/// See [`sysfs_gpio::Pin::new`][0] for details.
///
Expand All @@ -117,6 +142,9 @@ impl Pin {
}

impl hal::digital::v2::OutputPin for Pin {
#[cfg(feature = "gpio_cdev")]
type Error = gpio_cdev::errors::Error;
#[cfg(not(feature = "gpio_cdev"))]
type Error = sysfs_gpio::Error;

fn set_low(&mut self) -> Result<(), Self::Error> {
Expand All @@ -129,8 +157,20 @@ impl hal::digital::v2::OutputPin for Pin {
}

impl hal::digital::v2::InputPin for Pin {
#[cfg(feature = "gpio_cdev")]
type Error = gpio_cdev::errors::Error;
#[cfg(not(feature = "gpio_cdev"))]
type Error = sysfs_gpio::Error;

#[cfg(feature = "gpio_cdev")]
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.1 {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
}
#[cfg(not(feature = "gpio_cdev"))]
fn is_high(&self) -> Result<bool, Self::Error> {
if !self.0.get_active_low()? {
self.0.get_value().map(|val| val != 0)
Expand All @@ -145,6 +185,9 @@ impl hal::digital::v2::InputPin for Pin {
}

impl ops::Deref for Pin {
#[cfg(feature = "gpio_cdev")]
type Target = gpio_cdev::LineHandle;
#[cfg(not(feature = "gpio_cdev"))]
type Target = sysfs_gpio::Pin;

fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -220,10 +263,7 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.set_address(address)?;
let mut messages = [
LinuxI2CMessage::write(bytes),
LinuxI2CMessage::read(buffer),
];
let mut messages = [LinuxI2CMessage::write(bytes), LinuxI2CMessage::read(buffer)];
self.inner.transfer(&mut messages).map(drop)
}
}
Expand Down