From e965be19659434cb0cfc9af8d2d31aaf2e65c750 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Sat, 21 Oct 2023 18:47:27 +0200 Subject: [PATCH] Port to embedded-hal 1.0.0-rc1 --- Cargo.toml | 4 +-- README.md | 2 +- src/gpio/hal.rs | 28 ++++++++--------- src/hal.rs | 4 +-- src/i2c/hal.rs | 2 +- src/lib.rs | 2 +- src/spi/hal.rs | 81 +++++++++++++++---------------------------------- src/uart/hal.rs | 7 ++--- 8 files changed, 49 insertions(+), 81 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 604ee42f..196cc095 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/README.md b/README.md index c264b4e0..05576033 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/gpio/hal.rs b/src/gpio/hal.rs index 91308499..63aebb87 100644 --- a/src/gpio/hal.rs +++ b/src/gpio/hal.rs @@ -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 { Ok(Self::read(self) == Level::High) @@ -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 { Ok(Self::is_high(self)) @@ -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 { Ok(Self::is_high(self)) @@ -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 { Ok(Self::is_set_high(self)) @@ -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); @@ -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 { Ok(OutputPin::is_set_high(self)) @@ -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); @@ -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); @@ -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 { Ok(IoPin::is_high(self)) @@ -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); diff --git a/src/hal.rs b/src/hal.rs index 64058dd5..de74e967 100644 --- a/src/hal.rs +++ b/src/hal.rs @@ -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 { @@ -71,7 +71,7 @@ impl embedded_hal_0::blocking::delay::DelayUs 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())); diff --git a/src/i2c/hal.rs b/src/i2c/hal.rs index 09c09930..6a6dfe22 100644 --- a/src/i2c/hal.rs +++ b/src/i2c/hal.rs @@ -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, diff --git a/src/lib.rs b/src/lib.rs index c504d3df..e6fd92f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 diff --git a/src/spi/hal.rs b/src/spi/hal.rs index 3eb7785a..a0052247 100644 --- a/src/spi/hal.rs +++ b/src/spi/hal.rs @@ -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; @@ -14,11 +17,20 @@ impl spi::Error for Error { } } -/// `Transfer` trait implementation for `embedded-hal` v1.0.0-alpha.8. +/// `SpiBus` trait implementation for `embedded-hal` v1.0.0. impl SpiBus 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(()) } @@ -26,6 +38,10 @@ impl SpiBus for Spi { let write_buffer = buffer.to_vec(); self.transfer(buffer, &write_buffer) } + + fn flush(&mut self) -> Result<(), Self::Error> { + Ok(()) + } } /// `Transfer` trait implementation for `embedded-hal` v0.2.7. @@ -39,41 +55,16 @@ impl embedded_hal_0::blocking::spi::Transfer for Spi { } } -/// `SpiBusWrite` trait implementation for `embedded-hal` v1.0.0-alpha.8. -impl SpiBusWrite for Spi { - fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - Spi::write(self, buffer)?; - - Ok(()) - } -} - /// `Write` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::spi::Write for Spi { type Error = Error; fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - SpiBusWrite::write(self, buffer) - } -} - -/// `SpiBusRead` trait implementation for `embedded-hal` v1.0.0-alpha.8. -impl SpiBusRead 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` trait implementation for `embedded-hal` v1.0.0-alpha.8. +/// `FullDuplex` trait implementation for `embedded-hal` v1.0.0 impl FullDuplex for Spi { fn read(&mut self) -> nb::Result { if let Some(last_read) = self.last_read.take() { @@ -123,31 +114,6 @@ impl> SimpleHalSpiDevice { } } -impl> SpiDeviceRead for SimpleHalSpiDevice -{ - fn read_transaction( - &mut self, - operations: &mut [&mut [u8]] - ) -> Result<(), Error> { - for op in operations { - self.transaction(&mut [Operation::Read(op)])?; - } - Ok(()) - } -} - -impl> SpiDeviceWrite for SimpleHalSpiDevice { - fn write_transaction( - &mut self, - operations: &[&[u8]] - ) -> Result<(), Error> { - for op in operations { - self.transaction(&mut [Operation::Write(op)])?; - } - Ok(()) - } -} - impl> SpiDevice for SimpleHalSpiDevice { fn transaction( &mut self, @@ -187,6 +153,9 @@ impl> SpiDevice for SimpleHalSpiDevice { )) })?; } + Operation::DelayUs(us) => { + Delay::new().delay_us(*us); + } } } Ok(()) diff --git a/src/uart/hal.rs b/src/uart/hal.rs index b16e30c9..f5be015d 100644 --- a/src/uart/hal.rs +++ b/src/uart/hal.rs @@ -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}; @@ -13,7 +12,7 @@ impl serial::Error for Error { } } -/// `Read` trait implementation for `embedded-hal` v1.0.0-alpha.9. +/// `Read` trait implementation for `embedded-hal` v1.0.0. impl Read for Uart { fn read(&mut self) -> nb::Result { let mut buffer = [0u8; 1]; @@ -34,7 +33,7 @@ impl embedded_hal_0::serial::Read for Uart { } } -/// `Write` trait implementation for `embedded-hal` v1.0.0-alpha.9. +/// `Write` trait implementation for `embedded-hal` v1.0.0. impl Write for Uart { fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { if Uart::write(self, &[word])? == 0 {