Merge pull request #205 from jannic/eh-1_alpha6

Update eh1_0_alpha feature for embedded-hal 1.0.0-alpha.6
This commit is contained in:
Jonathan Pallant 2021-11-25 14:30:02 +00:00 committed by GitHub
commit 292a6f13c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 7 deletions

View file

@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
[dependencies] [dependencies]
cortex-m = "0.7.2" cortex-m = "0.7.2"
embedded-hal = { version = "0.2.5", features = ["unproven"] } embedded-hal = { version = "0.2.5", features = ["unproven"] }
eh1_0_alpha = { version = "=1.0.0-alpha.5", package="embedded-hal", optional=true } eh1_0_alpha = { version = "=1.0.0-alpha.6", package="embedded-hal", optional=true }
embedded-time = "0.12.0" embedded-time = "0.12.0"
itertools = { version = "0.10.1", default-features = false } itertools = { version = "0.10.1", default-features = false }
nb = "1.0" nb = "1.0"

View file

@ -79,6 +79,31 @@ pub enum Error {
AddressReserved(u16), AddressReserved(u16),
} }
#[cfg(feature = "eh1_0_alpha")]
impl eh1_0_alpha::i2c::Error for Error {
fn kind(&self) -> eh1_0_alpha::i2c::ErrorKind {
match &self {
Error::Abort(v) if v & 1<<12 != 0 // ARB_LOST
=> eh1_0_alpha::i2c::ErrorKind::ArbitrationLoss,
Error::Abort(v) if v & 1<<7 != 0 // ABRT_SBYTE_ACKDET
=> eh1_0_alpha::i2c::ErrorKind::Bus,
Error::Abort(v) if v & 1<<6 != 0 // ABRT_HS_ACKDET
=> eh1_0_alpha::i2c::ErrorKind::Bus,
Error::Abort(v) if v & 1<<4 != 0 // ABRT_GCALL_NOACK
=> eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address),
Error::Abort(v) if v & 1<<3 != 0 // ABRT_TXDATA_NOACK
=> eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Data),
Error::Abort(v) if v & 1<<2 != 0 // ABRT_10ADDR2_NOACK
=> eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address),
Error::Abort(v) if v & 1<<1 != 0 // ABRT_10ADDR1_NOACK
=> eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address),
Error::Abort(v) if v & 1<<0 != 0 // ABRT_7B_ADDR_NOACK
=> eh1_0_alpha::i2c::ErrorKind::NoAcknowledge(eh1_0_alpha::i2c::NoAcknowledgeSource::Address),
_ => eh1_0_alpha::i2c::ErrorKind::Other,
}
}
}
/// SCL pin /// SCL pin
pub trait SclPin<I2C>: Sealed {} pub trait SclPin<I2C>: Sealed {}

View file

@ -278,7 +278,7 @@ impl<T: Deref<Target = Block>, PINS> Write for I2C<T, PINS, Controller> {
impl<T: Deref<Target = Block>, PINS> eh1::Write for I2C<T, PINS, Controller> { impl<T: Deref<Target = Block>, PINS> eh1::Write for I2C<T, PINS, Controller> {
type Error = Error; type Error = Error;
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
Write::write(self, addr, bytes) Write::write(self, addr, bytes)
} }
} }

View file

@ -186,6 +186,29 @@ impl<D: SpiDevice, const DS: u8> Spi<Enabled, D, DS> {
} }
} }
/// Same as core::convert::Infallible, but implementing spi::Error
///
/// For eh 1.0.0-alpha.6, Infallible doesn't implement spi::Error,
/// so use a locally defined type instead.
/// This should be removed with the next release of e-h.
/// (https://github.com/rust-embedded/embedded-hal/pull/328)
#[cfg(feature = "eh1_0_alpha")]
pub enum SpiInfallible {}
#[cfg(feature = "eh1_0_alpha")]
impl core::fmt::Debug for SpiInfallible {
fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {}
}
}
#[cfg(feature = "eh1_0_alpha")]
impl eh1::Error for SpiInfallible {
fn kind(&self) -> eh1::ErrorKind {
match *self {}
}
}
macro_rules! impl_write { macro_rules! impl_write {
($type:ident, [$($nr:expr),+]) => { ($type:ident, [$($nr:expr),+]) => {
@ -221,16 +244,16 @@ macro_rules! impl_write {
#[cfg(feature = "eh1_0_alpha")] #[cfg(feature = "eh1_0_alpha")]
impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> { impl<D: SpiDevice> eh1::nb::FullDuplex<$type> for Spi<Enabled, D, $nr> {
type Error = Infallible; type Error = SpiInfallible;
fn read(&mut self) -> Result<$type, nb::Error<Infallible>> { fn read(&mut self) -> Result<$type, nb::Error<SpiInfallible>> {
if !self.is_readable() { if !self.is_readable() {
return Err(nb::Error::WouldBlock); return Err(nb::Error::WouldBlock);
} }
Ok(self.device.sspdr.read().data().bits() as $type) Ok(self.device.sspdr.read().data().bits() as $type)
} }
fn write(&mut self, word: $type) -> Result<(), nb::Error<Infallible>> { fn write(&mut self, word: $type) -> Result<(), nb::Error<SpiInfallible>> {
// Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX // Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX
// is full, PL022 inhibits RX pushes, and sets a sticky flag on // is full, PL022 inhibits RX pushes, and sets a sticky flag on
// push-on-full, but continues shifting. Safe if SSPIMSC_RORIM is not set. // push-on-full, but continues shifting. Safe if SSPIMSC_RORIM is not set.

View file

@ -82,6 +82,18 @@ pub enum ReadErrorType {
Framing, Framing,
} }
#[cfg(feature = "eh1_0_alpha")]
impl eh1_0_alpha::serial::Error for ReadErrorType {
fn kind(&self) -> eh1_0_alpha::serial::ErrorKind {
match self {
ReadErrorType::Overrun => eh1_0_alpha::serial::ErrorKind::Overrun,
ReadErrorType::Break => eh1_0_alpha::serial::ErrorKind::Other,
ReadErrorType::Parity => eh1_0_alpha::serial::ErrorKind::Parity,
ReadErrorType::Framing => eh1_0_alpha::serial::ErrorKind::FrameFormat,
}
}
}
/// State of the UART Peripheral. /// State of the UART Peripheral.
pub trait State {} pub trait State {}
@ -516,6 +528,29 @@ impl<D: UartDevice> eh1::Read<u8> for UartPeripheral<Enabled, D> {
} }
} }
/// Same as core::convert::Infallible, but implementing spi::Error
///
/// For eh 1.0.0-alpha.6, Infallible doesn't implement spi::Error,
/// so use a locally defined type instead.
/// This should be removed with the next release of e-h.
/// (https://github.com/rust-embedded/embedded-hal/pull/328)
#[cfg(feature = "eh1_0_alpha")]
pub enum SerialInfallible {}
#[cfg(feature = "eh1_0_alpha")]
impl core::fmt::Debug for SerialInfallible {
fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {}
}
}
#[cfg(feature = "eh1_0_alpha")]
impl eh1_0_alpha::serial::Error for SerialInfallible {
fn kind(&self) -> eh1_0_alpha::serial::ErrorKind {
match *self {}
}
}
impl<D: UartDevice> Write<u8> for UartPeripheral<Enabled, D> { impl<D: UartDevice> Write<u8> for UartPeripheral<Enabled, D> {
type Error = Infallible; type Error = Infallible;
@ -534,7 +569,7 @@ impl<D: UartDevice> Write<u8> for UartPeripheral<Enabled, D> {
#[cfg(feature = "eh1_0_alpha")] #[cfg(feature = "eh1_0_alpha")]
impl<D: UartDevice> eh1::Write<u8> for UartPeripheral<Enabled, D> { impl<D: UartDevice> eh1::Write<u8> for UartPeripheral<Enabled, D> {
type Error = Infallible; type Error = SerialInfallible;
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
if self.write_raw(&[word]).is_err() { if self.write_raw(&[word]).is_err() {
@ -545,7 +580,10 @@ impl<D: UartDevice> eh1::Write<u8> for UartPeripheral<Enabled, D> {
} }
fn flush(&mut self) -> nb::Result<(), Self::Error> { fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.transmit_flushed() self.transmit_flushed().map_err(|e| match e {
WouldBlock => WouldBlock,
Other(v) => match v {},
})
} }
} }