Skip to content

Commit

Permalink
Merge pull request #130 from mbuesch/embedded-hal-rc1
Browse files Browse the repository at this point in the history
Port to embedded-hal 1.0.0-rc.1
  • Loading branch information
golemparts authored Oct 24, 2023
2 parents 461939d + e965be1 commit d8ad172
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ keywords = ["raspberry", "pi", "embedded-hal", "embedded-hal-impl", "hal"]
libc = "0.2"
nb = { version = "0.1.1", optional = true }
embedded-hal-0 = { version = "0.2.7", optional = true, package = "embedded-hal" }
embedded-hal = { version = "=1.0.0-alpha.10", optional = true }
embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true }
embedded-hal = { version = "=1.0.0-rc.1", optional = true }
embedded-hal-nb = { version = "=1.0.0-rc.1", optional = true }
void = { version = "1.0.2", optional = true }
spin_sleep = { version = "1.0.0", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

RPPAL provides access to the Raspberry Pi's GPIO, I2C, PWM, SPI and UART peripherals through a user-friendly interface. In addition to peripheral access, RPPAL also offers support for USB to serial adapters.

The library can be used in conjunction with a variety of platform-agnostic drivers through its `embedded-hal` trait implementations. Both `embedded-hal` v0.2.7 and v1.0.0-alpha.9 are supported.
The library can be used in conjunction with a variety of platform-agnostic drivers through its `embedded-hal` trait implementations. Both `embedded-hal` v0.2.7 and v1.0.0-rc.1 are supported.

RPPAL requires Raspberry Pi OS or any similar, recent, Linux distribution. Both `gnu` and `musl` libc targets are supported. RPPAL is compatible with the Raspberry Pi A, A+, B, B+, 2B, 3A+, 3B, 3B+, 4B, 5, CM, CM 3, CM 3+, CM 4, 400, Zero, Zero W and Zero 2 W. Backwards compatibility for minor revisions isn't guaranteed until v1.0.0.

Expand Down
28 changes: 14 additions & 14 deletions src/gpio/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use embedded_hal::digital::{

use super::{InputPin, IoPin, Level, OutputPin, Pin};

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for Pin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for Pin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::read(self) == Level::High)
Expand All @@ -23,12 +23,12 @@ impl InputPinHal for Pin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for InputPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for InputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
Expand All @@ -39,12 +39,12 @@ impl InputPinHal for InputPin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for IoPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for IoPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
Expand All @@ -55,12 +55,12 @@ impl InputPinHal for IoPin {
}
}

/// `ErrorType` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ErrorType` trait implementation for `embedded-hal` v1.0.0.
impl ErrorType for OutputPin {
type Error = Infallible;
}

/// `InputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `InputPin` trait implementation for `embedded-hal` v1.0.0.
impl InputPinHal for OutputPin {
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(Self::is_set_high(self))
Expand All @@ -71,7 +71,7 @@ impl InputPinHal for OutputPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `OutputPin` trait implementation for `embedded-hal` v1.0.0.
impl OutputPinHal for OutputPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
OutputPin::set_low(self);
Expand Down Expand Up @@ -99,7 +99,7 @@ impl embedded_hal_0::digital::v2::OutputPin for OutputPin {
}
}

/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl StatefulOutputPinHal for OutputPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(OutputPin::is_set_high(self))
Expand All @@ -110,7 +110,7 @@ impl StatefulOutputPinHal for OutputPin {
}
}

/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl ToggleableOutputPinHal for OutputPin {
fn toggle(&mut self) -> Result<(), Self::Error> {
OutputPin::toggle(self);
Expand All @@ -119,7 +119,7 @@ impl ToggleableOutputPinHal for OutputPin {
}
}

/// `OutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `OutputPin` trait implementation for `embedded-hal` v1.0.0.
impl OutputPinHal for IoPin {
fn set_low(&mut self) -> Result<(), Self::Error> {
IoPin::set_low(self);
Expand Down Expand Up @@ -147,7 +147,7 @@ impl embedded_hal_0::digital::v2::OutputPin for IoPin {
}
}

/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl StatefulOutputPinHal for IoPin {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(IoPin::is_high(self))
Expand All @@ -158,7 +158,7 @@ impl StatefulOutputPinHal for IoPin {
}
}

/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0.
impl ToggleableOutputPinHal for IoPin {
fn toggle(&mut self) -> Result<(), Self::Error> {
IoPin::toggle(self);
Expand Down
4 changes: 2 additions & 2 deletions src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use void::Void;
#[derive(Debug, Default)]
pub struct Delay;

/// `Delay` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `Delay` trait implementation for `embedded-hal` v1.0.0.
impl Delay {
/// Constructs a new `Delay`.
pub fn new() -> Delay {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl embedded_hal_0::blocking::delay::DelayUs<u16> for Delay {
}
}

/// `DelayUs` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `DelayUs` trait implementation for `embedded-hal` v1.0.0.
impl DelayUs for Delay {
fn delay_us(&mut self, us: u32) {
sleep(Duration::from_micros(us.into()));
Expand Down
2 changes: 1 addition & 1 deletion src/i2c/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl i2c::Error for Error {
}
}

/// `I2c` trait implementation for `embedded-hal` v1.0.0-alpha.10.
/// `I2c` trait implementation for `embedded-hal` v1.0.0.
impl I2cHal for I2c {
fn transaction(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! The library can be used in conjunction with a variety of platform-agnostic
//! drivers through its `embedded-hal` trait implementations. Both `embedded-hal`
//! v0.2.7 and v1.0.0-alpha.9 are supported.
//! v0.2.7 and v1.0.0-rc.1 are supported.
//!
//! RPPAL requires Raspberry Pi OS or any similar, recent, Linux distribution.
//! Both `gnu` and `musl` libc targets are supported. RPPAL is compatible with the
Expand Down
81 changes: 25 additions & 56 deletions src/spi/hal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use embedded_hal::spi::{self, ErrorType, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice, SpiDeviceWrite, SpiDeviceRead, Operation};
use embedded_hal::{
delay::DelayUs,
spi::{self, ErrorType, SpiBus, SpiDevice, Operation},
};
use embedded_hal_nb::spi::FullDuplex;
use std::io;

use super::{Error, Spi};
use super::{super::hal::Delay, Error, Spi};

impl ErrorType for Spi {
type Error = Error;
Expand All @@ -14,18 +17,31 @@ impl spi::Error for Error {
}
}

/// `Transfer<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.8.
/// `SpiBus<u8>` trait implementation for `embedded-hal` v1.0.0.
impl SpiBus<u8> for Spi {
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
Spi::read(self, words)?;
Ok(())
}

fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
Spi::write(self, words)?;
Ok(())
}

fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
Spi::transfer(self, read, write)?;

Ok(())
}

fn transfer_in_place(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
let write_buffer = buffer.to_vec();
self.transfer(buffer, &write_buffer)
}

fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

/// `Transfer<u8>` trait implementation for `embedded-hal` v0.2.7.
Expand All @@ -39,41 +55,16 @@ impl embedded_hal_0::blocking::spi::Transfer<u8> for Spi {
}
}

/// `SpiBusWrite<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.8.
impl SpiBusWrite<u8> for Spi {
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
Spi::write(self, buffer)?;

Ok(())
}
}

/// `Write<u8>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::spi::Write<u8> for Spi {
type Error = Error;

fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
SpiBusWrite::write(self, buffer)
}
}

/// `SpiBusRead<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.8.
impl SpiBusRead<u8> for Spi {
fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
Spi::read(self, buffer)?;

Ok(())
SpiBus::write(self, buffer)
}
}

/// `SpiBusFlush` trait implementation for `embedded-hal` v1.0.0-alpha.8.
impl SpiBusFlush for Spi {
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

/// `FullDuplex<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.8.
/// `FullDuplex<u8>` trait implementation for `embedded-hal` v1.0.0
impl FullDuplex<u8> for Spi {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
if let Some(last_read) = self.last_read.take() {
Expand Down Expand Up @@ -123,31 +114,6 @@ impl<B: SpiBus<u8>> SimpleHalSpiDevice<B> {
}
}

impl<B: SpiBus<u8>> SpiDeviceRead<u8> for SimpleHalSpiDevice<B>
{
fn read_transaction(
&mut self,
operations: &mut [&mut [u8]]
) -> Result<(), Error> {
for op in operations {
self.transaction(&mut [Operation::Read(op)])?;
}
Ok(())
}
}

impl<B: SpiBus<u8>> SpiDeviceWrite<u8> for SimpleHalSpiDevice<B> {
fn write_transaction(
&mut self,
operations: &[&[u8]]
) -> Result<(), Error> {
for op in operations {
self.transaction(&mut [Operation::Write(op)])?;
}
Ok(())
}
}

impl<B: SpiBus<u8>> SpiDevice<u8> for SimpleHalSpiDevice<B> {
fn transaction(
&mut self,
Expand Down Expand Up @@ -187,6 +153,9 @@ impl<B: SpiBus<u8>> SpiDevice<u8> for SimpleHalSpiDevice<B> {
))
})?;
}
Operation::DelayUs(us) => {
Delay::new().delay_us(*us);
}
}
}
Ok(())
Expand Down
7 changes: 3 additions & 4 deletions src/uart/hal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use embedded_hal::serial::{self, ErrorType};
use embedded_hal_nb::serial::{Read, Write};
use embedded_hal_nb::serial::{self, ErrorType, Read, Write};

use super::{Error, Queue, Uart};

Expand All @@ -13,7 +12,7 @@ impl serial::Error for Error {
}
}

/// `Read<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `Read<u8>` trait implementation for `embedded-hal` v1.0.0.
impl Read<u8> for Uart {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
let mut buffer = [0u8; 1];
Expand All @@ -34,7 +33,7 @@ impl embedded_hal_0::serial::Read<u8> for Uart {
}
}

/// `Write<u8>` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `Write<u8>` trait implementation for `embedded-hal` v1.0.0.
impl Write<u8> for Uart {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
if Uart::write(self, &[word])? == 0 {
Expand Down

0 comments on commit d8ad172

Please sign in to comment.