Skip to content

Bikeshed: Should (Embedded) Linux Be Able to Implement These Traits? #17

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
posborne opened this issue Jun 28, 2017 · 2 comments
Closed

Comments

@posborne
Copy link
Member

In looking to address comments like some of those raised on the i2cdev crate (rust-embedded/rust-i2cdev#27 / rust-embedded/rust-i2cdev#28) I went around to the embedded RFCs and found time to review some of this for the first time.

The basic question here is whether it should be a goal or whether (and how) a concerted effort should be made to support the embedded-hal traits under Linux for usage/testing from a Linux userspace system such as a raspberry pi.

Benefits of Supporting Linux:

  • Vastly increased hardware support. Once a trait is implemented for Linux, the support is available on a huge number of embedded devices, common ones being boards like the Raspberry Pi, Beaglebone Black, CHIP board, ...
  • Easier test environment for device drivers. If I am adding support for some device, I might like to do more of that development in a less-embedded environment for easier debugging. If the traits are supported under Linux that fits the bill. Everyone has a RPi and can wire up a few peripherals quickly.

Challenges to Supporting LInux:

  • Many of the Linux APIs are blocking but the embedded-hal APIs are non-blocking.

Life without Linux Support:

The dream is to be able to write a piece of higher-level code using a set of base APIs/Traits to access hardware and be able to have this code work in a variety of platforms, including Linux. If we end up needing a separate set of traits for systems like MCUs and systems like Linux then driver work for a particular sensor cannot be easily shared (there may still be opportunity for reuse in a crate but it would be additional work for the author).

I have not dug deeply into what it would look like to attempt to implement any of the existing or proposed traits for Linux but it would be an interesting thing to explore as the support would, in my opinion, be extremely nice.

All of this being said, I wholeheartedly believe that what is proposed here is the right approach when working with the actual hardware in a least-common-denominator system like an MCU.

@posborne posborne changed the title Bikeshed: Should (Embedded) Linux Be Able to Implement? Bikeshed: Should (Embedded) Linux Be Able to Implement These Traits? Jun 28, 2017
@japaric
Copy link
Member

japaric commented Jun 28, 2017

The short answer is: yes.

The long answer is: but probably not the lowest level traits because they are tailored for MCUs where non-blocking operations are retry-able without having to track state in the program (the state is in the registers). That form of non-blocking operation is covered by the nb crate and the block!, try_nb! macros.

More concretely apart from the low level traits which are already in tree we want to add traits tailored for blocking operation and another set of traits for interoperation with futures. Where possible we want those traits to built on top of the existing low level traits to avoid rewriting register manipulation code in the implementation of the higher level traits.

I was originally envisioning something like this:

// write half the Serial trait we already have
trait Write<Word> {
    type Error;

    fn write(&self, word: Word) -> nb::Result<(), Self::Error>;
}

// Blocking write operations
trait BWrite {
    type Error;

    fn bwrite_all(&self, buffer: &[u8]) -> Result<(), Self::Error>;
    // ..
}

// overridable blanket implementation
default impl<S> BWrite for S where S: Write<u8> {
    type Error = S::Error;

    fn write_all(&self, buffer: &[u8]) -> Result<(), S::Error> {
        for byte in buffer {
            block!(self.write(*byte))?;
        }
        Ok(())
    }
}

However the default impl doesn't work today because of limitations of the feature (it doesn't work when associated types are involved) which throws a wrench into my plans. However, I think I have come up with a reasonable workaround. I'll test it and send a PR to show the approach.

@japaric
Copy link
Member

japaric commented Jan 20, 2018

Closing as this now exists in the form of the linux-embedded-hal crate.

@japaric japaric closed this as completed Jan 20, 2018
peckpeck pushed a commit to peckpeck/embedded-hal that referenced this issue Nov 10, 2022
17: Remove #[deny(warnings)] r=therealprof a=dbrgn

The library currently does not currently build anymore, because upstream libraries have added new deprecation warnings. A deprecation warning should not break compilation though. There was a post on Reddit about this about a year ago, but I can't find it anymore.

I think adding a CI check step would be fine, but `#[deny(warnings)]` is a bit too much.

These are the warnings:

```
warning: use of deprecated item 'hal::digital::OutputPin': Deprecated because the methods cannot return errors. Users should use the traits in digital::v2.
   --> src/lib.rs:111:6                                                       
    |                                                                         
111 | impl hal::digital::OutputPin for Pin {                                  
    |      ^^^^^^^^^^^^^^^^^^^^^^^                                            
    |                                                                         
    = note: #[warn(deprecated)] on by default                                 
                                                                              
warning: use of deprecated item 'hal::digital::InputPin': Deprecated because the methods cannot return errors. Users should use the traits in digital::v2.
   --> src/lib.rs:121:6                                                       
    |                                                                         
121 | impl hal::digital::InputPin for Pin {                                   
    |      ^^^^^^^^^^^^^^^^^^^^^^                                             
                                                                              
warning: use of deprecated item 'hal::digital::InputPin::is_high': Deprecated because the methods cannot return errors. Users should use the traits in digital::v2.
   --> src/lib.rs:131:15                                                      
    |                                                                         
131 |         !self.is_high()                                                 
    |               ^^^^^^^                                                                                                                             
```

Co-authored-by: Danilo Bargen <[email protected]>
peckpeck pushed a commit to peckpeck/embedded-hal that referenced this issue Nov 10, 2022
18: Move to digital v2 traits r=nastevens a=dbrgn

The v1 traits are deprecated because they don't have a way to report errors.

Gets rid of the deprecation warnings (which are hard errors until rust-embedded#17 is merged).

Co-authored-by: Danilo Bargen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants