Skip to content

Commit

Permalink
Merge pull request #129 from mbuesch/embedded-hal-a10
Browse files Browse the repository at this point in the history
Port to embedded-hal alpha.10
  • Loading branch information
golemparts authored Oct 21, 2023
2 parents 9a61c2c + 1623594 commit 461939d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 92 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.9", optional = true }
embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true }
embedded-hal = { version = "=1.0.0-alpha.10", optional = true }
embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true }
void = { version = "1.0.2", optional = true }
spin_sleep = { version = "1.0.0", optional = true }

Expand Down
29 changes: 12 additions & 17 deletions src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! This module is only included when either the `hal` or `hal-unproven` feature
//! flag is enabled.

use core::convert::Infallible;
use std::time::{Duration, Instant};

use embedded_hal::delay::DelayUs;
Expand All @@ -28,21 +27,21 @@ impl Delay {
/// `DelayMs<u8>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
DelayUs::delay_ms(self, ms as u32).unwrap()
DelayUs::delay_ms(self, ms as u32);
}
}

/// `DelayMs<u16>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
DelayUs::delay_ms(self, ms as u32).unwrap()
DelayUs::delay_ms(self, ms as u32);
}
}

/// `DelayMs<u32>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
DelayUs::delay_ms(self, ms).unwrap()
DelayUs::delay_ms(self, ms);
}
}

Expand All @@ -51,46 +50,42 @@ impl embedded_hal_0::blocking::delay::DelayMs<u64> for Delay {
fn delay_ms(&mut self, mut ms: u64) {
while ms > (u32::MAX as u64) {
ms -= u32::MAX as u64;
DelayUs::delay_ms(self, u32::MAX).unwrap();
DelayUs::delay_ms(self, u32::MAX);
}

DelayUs::delay_ms(self, ms as u32).unwrap()
DelayUs::delay_ms(self, ms as u32);
}
}

/// `DelayUs<u8>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
DelayUs::delay_us(self, us as u32).unwrap()
DelayUs::delay_us(self, us as u32);
}
}

/// `DelayUs<u16>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
DelayUs::delay_us(self, us as u32).unwrap()
DelayUs::delay_us(self, us as u32);
}
}

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

fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
fn delay_us(&mut self, us: u32) {
sleep(Duration::from_micros(us.into()));
Ok(())
}

fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
fn delay_ms(&mut self, ms: u32) {
sleep(Duration::from_millis(u64::from(ms)));
Ok(())
}
}

/// `DelayUs<u32>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
DelayUs::delay_us(self, us).unwrap()
DelayUs::delay_us(self, us);
}
}

Expand All @@ -99,10 +94,10 @@ impl embedded_hal_0::blocking::delay::DelayUs<u64> for Delay {
fn delay_us(&mut self, mut us: u64) {
while us > (u32::MAX as u64) {
us -= u32::MAX as u64;
DelayUs::delay_us(self, u32::MAX).unwrap();
DelayUs::delay_us(self, u32::MAX);
}

DelayUs::delay_us(self, us as u32).unwrap()
DelayUs::delay_us(self, us as u32);
}
}

Expand Down
60 changes: 1 addition & 59 deletions src/i2c/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,58 +54,8 @@ impl i2c::Error for Error {
}
}

/// `I2c` trait implementation for `embedded-hal` v1.0.0-alpha.9.
/// `I2c` trait implementation for `embedded-hal` v1.0.0-alpha.10.
impl I2cHal for I2c {
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.set_slave_address(u16::from(address))?;
I2c::write(self, bytes)?;

Ok(())
}

fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.set_slave_address(u16::from(address))?;
I2c::read(self, buffer)?;

Ok(())
}

fn write_iter<B>(&mut self, address: u8, bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
let bytes: Vec<_> = bytes.into_iter().collect();
I2cHal::write(self, address, &bytes)
}

fn write_read(
&mut self,
address: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.set_slave_address(u16::from(address))?;
I2c::write_read(self, bytes, buffer)?;

Ok(())
}

fn write_iter_read<B>(
&mut self,
address: u8,
bytes: B,
buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
let bytes: Vec<_> = bytes.into_iter().collect();
self.transaction(
address,
&mut [I2cOperation::Write(&bytes), I2cOperation::Read(buffer)],
)
}

fn transaction(
&mut self,
address: u8,
Expand All @@ -125,12 +75,4 @@ impl I2cHal for I2c {

Ok(())
}

fn transaction_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = I2cOperation<'a>>,
{
let mut ops: Vec<_> = operations.into_iter().collect();
self.transaction(address, &mut ops)
}
}
82 changes: 68 additions & 14 deletions src/spi/hal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_hal::spi::{self, ErrorType, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice};
use embedded_hal::spi::{self, ErrorType, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice, SpiDeviceWrite, SpiDeviceRead, Operation};
use embedded_hal_nb::spi::FullDuplex;
use std::io;

Expand Down Expand Up @@ -117,28 +117,82 @@ pub struct SimpleHalSpiDevice<B> {
bus: B,
}

impl<B> SimpleHalSpiDevice<B> {
impl<B: SpiBus<u8>> SimpleHalSpiDevice<B> {
pub fn new(bus: B) -> SimpleHalSpiDevice<B> {
SimpleHalSpiDevice { bus }
}
}

impl<B: ErrorType> SpiDevice for SimpleHalSpiDevice<B> {
type Bus = 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(())
}
}

fn transaction<R>(
impl<B: SpiBus<u8>> SpiDevice<u8> for SimpleHalSpiDevice<B> {
fn transaction(
&mut self,
f: impl FnOnce(&mut Self::Bus) -> Result<R, <Self::Bus as ErrorType>::Error>,
) -> Result<R, Self::Error> {
f(&mut self.bus).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice transaction error",
))
})
operations: &mut [Operation<'_, u8>]
) -> Result<(), Error> {
for op in operations {
match op {
Operation::Read(read) => {
self.bus.read(read).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice read transaction error",
))
})?;
}
Operation::Write(write) => {
self.bus.write(write).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice write transaction error",
))
})?;
}
Operation::Transfer(read, write) => {
self.bus.transfer(read, write).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice read/write transaction error",
))
})?;
}
Operation::TransferInPlace(words) => {
self.bus.transfer_in_place(words).map_err(|_| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
"SimpleHalSpiDevice in-place read/write transaction error",
))
})?;
}
}
}
Ok(())
}
}

impl<B: ErrorType> ErrorType for SimpleHalSpiDevice<B> {
impl<B: SpiBus<u8>> ErrorType for SimpleHalSpiDevice<B> {
type Error = Error;
}

0 comments on commit 461939d

Please sign in to comment.