From 7c9373dc9036a47ba174977473237b95c121a817 Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Tue, 15 Aug 2023 16:50:37 -0700 Subject: [PATCH] embedded-hal: 1.0.0-alpha.10 -> 1.0.0-rc.1 --- Cargo.toml | 6 +- src/eh1/serial.rs | 117 +--------------- src/eh1/spi.rs | 342 ++++++++++++++++++++++------------------------ 3 files changed, 168 insertions(+), 297 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c1384e..de545ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,9 +31,9 @@ default = ["eh0", "embedded-time"] [dependencies] eh0 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"], optional = true } -eh1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } -embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true } +eh1 = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-rc.1", optional = true } +embedded-hal-async = { version = "=1.0.0-rc.1", optional = true } embedded-time = { version = "0.12", optional = true } nb = { version = "0.1.1", optional = true} void = { version = "^1.0", optional = true } diff --git a/src/eh1/serial.rs b/src/eh1/serial.rs index 837c1e1..e592e37 100644 --- a/src/eh1/serial.rs +++ b/src/eh1/serial.rs @@ -46,46 +46,6 @@ //! serial.done(); //! ``` //! -//! ## Usage: Blocking serial trait -//! -//! ``` -//! # use eh1 as embedded_hal; -//! // Note that we're using the blocking serial write trait -//! use embedded_hal::serial::Write; -//! use embedded_hal_nb::serial::Read; -//! use embedded_hal_mock::eh1::serial::{ -//! Mock as SerialMock, -//! Transaction as SerialTransaction, -//! }; -//! -//! // Configure expectations -//! let expectations = [ -//! SerialTransaction::read(0x0A), -//! SerialTransaction::read_many(b"xy"), -//! SerialTransaction::write_many([1, 2]), // (2) -//! SerialTransaction::flush(), -//! ]; -//! -//! let mut serial = SerialMock::new(&expectations); -//! -//! // Expect three reads -//! assert_eq!(serial.read().unwrap(), 0x0A); -//! assert_eq!(serial.read().unwrap(), b'x'); -//! assert_eq!(serial.read().unwrap(), b'y'); -//! -//! // We use the blocking write here, and we assert that -//! // two words are written. See (2) above. -//! Write::write(&mut serial, &[1, 2]).unwrap(); -//! -//! // Finally, we expect a flush. Note that this is -//! // a *blocking* flush from the blocking serial trait. -//! Write::flush(&mut serial).unwrap(); -//! -//! // When you believe there are no more calls on the mock, -//! // call done() to assert there are no pending transactions. -//! serial.done(); -//! ``` -//! //! ## Testing Error Handling //! //! If you want to test error handling of your code, you can also add error @@ -98,8 +58,7 @@ //! # Transaction as SerialTransaction, //! # }; //! use embedded_hal_nb::nb; -//! use embedded_hal_nb::serial::{Read, Write}; -//! use embedded_hal::serial::ErrorKind; +//! use embedded_hal_nb::serial::{Read, Write, ErrorKind}; //! //! // Configure expectations //! let expectations = [ @@ -151,11 +110,10 @@ use std::{ sync::{Arc, Mutex}, }; -use eh1 as embedded_hal; -use embedded_hal::serial::ErrorKind; -use embedded_hal::serial::ErrorType; use embedded_hal_nb::nb; use embedded_hal_nb::serial; +use embedded_hal_nb::serial::ErrorKind; +use embedded_hal_nb::serial::ErrorType; use crate::common::DoneCallDetector; @@ -436,29 +394,11 @@ where } } -impl embedded_hal::serial::Write for Mock -where - Word: PartialEq + std::fmt::Debug + Clone + Copy, -{ - fn write(&mut self, buffer: &[Word]) -> Result<(), Self::Error> { - for word in buffer { - nb::block!(serial::Write::write(self, word.clone()))?; - } - Ok(()) - } - - fn flush(&mut self) -> Result<(), Self::Error> { - nb::block!(serial::Write::flush(self)) - } -} - #[cfg(test)] mod test { use super::*; - use eh1 as embedded_hal; - use embedded_hal::serial::ErrorKind; - use embedded_hal_nb::serial::{Read, Write}; + use embedded_hal_nb::serial::{ErrorKind, Read, Write}; #[test] fn test_serial_mock_read() { @@ -497,35 +437,6 @@ mod test { ser.done(); } - #[test] - fn test_serial_mock_blocking_write() { - use embedded_hal::serial::Write as BWrite; - let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF])]; - let mut ser = Mock::new(&ts); - BWrite::write(&mut ser, &[0xAB, 0xCD, 0xEF]).unwrap(); - ser.done(); - } - - #[test] - #[should_panic(expected = "called serial::write with no expectation")] - fn test_serial_mock_blocking_write_more_than_expected() { - use embedded_hal::serial::Write as BWrite; - let ts = [Transaction::write_many([0xAB, 0xCD])]; - let mut ser = Mock::new(&ts); - BWrite::write(&mut ser, &[0xAB, 0xCD, 0xEF]).unwrap(); - ser.done(); - } - - #[test] - #[should_panic(expected = "serial mock has unsatisfied expectations after call to done")] - fn test_serial_mock_blocking_write_not_enough() { - use embedded_hal::serial::Write as BWrite; - let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF, 0x00])]; - let mut ser = Mock::new(&ts); - BWrite::write(&mut ser, &[0xAB, 0xCD, 0xEF]).unwrap(); - ser.done(); - } - #[test] #[should_panic(expected = "serial::write expected to write 18 but actually wrote 20")] fn test_serial_mock_wrong_write() { @@ -542,15 +453,6 @@ mod test { ser.done(); } - #[test] - fn test_serial_mock_blocking_flush() { - use embedded_hal::serial::Write as BWrite; - let ts = [Transaction::flush()]; - let mut ser: Mock = Mock::new(&ts); - BWrite::flush(&mut ser).unwrap(); - ser.done(); - } - #[test] #[should_panic(expected = "serial mock has unsatisfied expectations after call to done")] fn test_serial_mock_pending_transactions() { @@ -571,17 +473,6 @@ mod test { ser.done(); } - #[test] - #[should_panic( - expected = "expected to perform a serial transaction 'Read(84)' but instead did a write of 119" - )] - fn test_serial_mock_expected_read() { - use embedded_hal::serial::Write as BWrite; - let ts = [Transaction::read(0x54)]; - let mut ser = Mock::new(&ts); - BWrite::write(&mut ser, &[0x77]).unwrap(); - } - #[test] #[should_panic( expected = "expected to perform a serial transaction 'Write(84)' but instead did a flush" diff --git a/src/eh1/spi.rs b/src/eh1/spi.rs index ab45ddf..2c590d8 100644 --- a/src/eh1/spi.rs +++ b/src/eh1/spi.rs @@ -8,8 +8,7 @@ //! ## Usage //! //! ``` -//! # use eh1 as embedded_hal; -//! use embedded_hal::spi::{SpiBus, SpiBusWrite}; +//! use eh1::spi::SpiBus; //! use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction}; //! use embedded_hal_nb::spi::FullDuplex; //! @@ -25,13 +24,13 @@ //! //! let mut spi = SpiMock::new(&expectations); //! // FullDuplex transfers -//! FullDuplex::write(&mut spi, 0x09); -//! assert_eq!(spi.read().unwrap(), 0x0A); -//! FullDuplex::write(&mut spi, 0xFE); -//! assert_eq!(spi.read().unwrap(), 0xFF); +//! FullDuplex::write(&mut spi, 0x09).unwrap(); +//! assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0x0A); +//! FullDuplex::write(&mut spi, 0xFE).unwrap(); +//! assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0xFF); //! //! // Writing -//! SpiBusWrite::write(&mut spi, &vec![1, 2]).unwrap(); +//! SpiBus::write(&mut spi, &vec![1, 2]).unwrap(); //! //! // Transferring //! let mut buf = vec![3, 4]; @@ -41,9 +40,8 @@ //! // Finalise expectations //! spi.done(); //! ``` -use eh1 as embedded_hal; -use embedded_hal::spi; -use embedded_hal::spi::{Operation, SpiBusRead, SpiBusWrite, SpiDeviceRead, SpiDeviceWrite}; +use eh1::spi; +use eh1::spi::{Operation, SpiBus, SpiDevice}; use embedded_hal_nb::nb; use embedded_hal_nb::spi::FullDuplex; @@ -66,6 +64,8 @@ pub enum Mode { TransactionStart, /// Mark the end of a group of transactions TransactionEnd, + /// A delay in the SPI transaction with the specified delay in microseconds + Delay(u32), } /// SPI transaction type @@ -159,6 +159,15 @@ impl Transaction { response: Vec::new(), } } + + /// Create a delay transaction + pub fn delay(delay: u32) -> Transaction { + Transaction { + expected_mode: Mode::Delay(delay), + expected_data: Vec::new(), + response: Vec::new(), + } + } } /// Mock SPI implementation @@ -171,25 +180,17 @@ impl Transaction { /// See the usage section in the module level docs for an example. pub type Mock = Generic; -impl embedded_hal::spi::ErrorType for Mock { - type Error = embedded_hal::spi::ErrorKind; -} - -impl embedded_hal::spi::SpiBusFlush for Mock { - fn flush(&mut self) -> Result<(), Self::Error> { - let w = self.next().expect("no expectation for spi::flush call"); - assert_eq!(w.expected_mode, Mode::Flush, "spi::flush unexpected mode"); - Ok(()) - } +impl spi::ErrorType for Mock { + type Error = spi::ErrorKind; } #[derive(Default)] -struct SpiBusFlushFuture { +struct SpiBusFuture { awaited: bool, } -impl std::future::Future for SpiBusFlushFuture { - type Output = Result<(), embedded_hal::spi::ErrorKind>; +impl std::future::Future for SpiBusFuture { + type Output = Result<(), spi::ErrorKind>; fn poll( mut self: std::pin::Pin<&mut Self>, @@ -200,45 +201,41 @@ impl std::future::Future for SpiBusFlushFuture { } } -impl Drop for SpiBusFlushFuture { +impl Drop for SpiBusFuture { fn drop(&mut self) { assert!(self.awaited, "spi::flush call was not awaited"); } } -#[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiBusFlush for Mock { - async fn flush(&mut self) -> Result<(), Self::Error> { - embedded_hal::spi::SpiBusFlush::flush(self) - } -} - -impl embedded_hal::spi::SpiBus for Mock { - /// spi::TransferInplace implementation for Mock +impl SpiBus for Mock { + /// spi::Read implementation for Mock /// - /// This writes the provided response to the buffer and will cause an assertion if the written data does not match the next expectation - fn transfer_in_place(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { - let w = self - .next() - .expect("no expectation for spi::transfer_in_place call"); - assert_eq!( - w.expected_mode, - Mode::TransferInplace, - "spi::transfer_in_place unexpected mode" - ); - assert_eq!( - &w.expected_data, &buffer, - "spi::transfer_in_place write data does not match expectation" - ); + /// This will cause an assertion if the read call does not match the next expectation + fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { + let w = self.next().expect("no expectation for spi::read call"); + assert_eq!(w.expected_mode, Mode::Read, "spi::read unexpected mode"); assert_eq!( buffer.len(), w.response.len(), - "mismatched response length for spi::transfer_in_place" + "spi:read mismatched response length" ); buffer.copy_from_slice(&w.response); Ok(()) } + /// spi::Write implementation for Mock + /// + /// This will cause an assertion if the write call does not match the next expectation + fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { + let w = self.next().expect("no expectation for spi::write call"); + assert_eq!(w.expected_mode, Mode::Write, "spi::write unexpected mode"); + assert_eq!( + &w.expected_data, &buffer, + "spi::write data does not match expectation" + ); + Ok(()) + } + fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { let w = self.next().expect("no expectation for spi::transfer call"); assert_eq!( @@ -248,7 +245,7 @@ impl embedded_hal::spi::SpiBus for Mock { ); assert_eq!( &w.expected_data, &write, - "spi::transfer write data does not match expectation" + "spi::write data does not match expectation" ); assert_eq!( read.len(), @@ -258,69 +255,62 @@ impl embedded_hal::spi::SpiBus for Mock { read.copy_from_slice(&w.response); Ok(()) } -} -#[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiBus for Mock { /// spi::TransferInplace implementation for Mock /// /// This writes the provided response to the buffer and will cause an assertion if the written data does not match the next expectation - async fn transfer_in_place<'a>(&'a mut self, buffer: &'a mut [u8]) -> Result<(), Self::Error> { - embedded_hal::spi::SpiBus::::transfer_in_place(self, buffer) - } - - async fn transfer<'a>( - &'a mut self, - read: &'a mut [u8], - write: &'a [u8], - ) -> Result<(), Self::Error> { - embedded_hal::spi::SpiBus::::transfer(self, read, write) - } -} - -impl embedded_hal::spi::SpiBusWrite for Mock { - /// spi::Write implementation for Mock - /// - /// This will cause an assertion if the write call does not match the next expectation - fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - let w = self.next().expect("no expectation for spi::write call"); - assert_eq!(w.expected_mode, Mode::Write, "spi::write unexpected mode"); + fn transfer_in_place(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { + let w = self + .next() + .expect("no expectation for spi::transfer_in_place call"); + assert_eq!( + w.expected_mode, + Mode::TransferInplace, + "spi::transfer_in_place unexpected mode" + ); assert_eq!( &w.expected_data, &buffer, - "spi::write data does not match expectation" + "spi::transfer_in_place write data does not match expectation" ); - Ok(()) - } -} - -#[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiBusWrite for Mock { - async fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { - embedded_hal::spi::SpiBusWrite::::write(self, words) - } -} - -impl embedded_hal::spi::SpiBusRead for Mock { - /// spi::Read implementation for Mock - /// - /// This will cause an assertion if the read call does not match the next expectation - fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { - let w = self.next().expect("no expectation for spi::read call"); - assert_eq!(w.expected_mode, Mode::Read, "spi::read unexpected mode"); assert_eq!( buffer.len(), w.response.len(), - "spi:read mismatched response length" + "mismatched response length for spi::transfer_in_place" ); buffer.copy_from_slice(&w.response); Ok(()) } + + fn flush(&mut self) -> Result<(), Self::Error> { + let w = self.next().expect("no expectation for spi::flush call"); + assert_eq!(w.expected_mode, Mode::Flush, "spi::flush unexpected mode"); + Ok(()) + } } #[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiBusRead for Mock { +impl embedded_hal_async::spi::SpiBus for Mock { async fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> { - embedded_hal::spi::SpiBusRead::::read(self, words) + eh1::spi::SpiBus::::read(self, words) + } + + async fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { + eh1::spi::SpiBus::::write(self, words) + } + + async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { + eh1::spi::SpiBus::::transfer(self, read, write) + } + + /// spi::TransferInplace implementation for Mock + /// + /// This writes the provided response to the buffer and will cause an assertion if the written data does not match the next expectation + async fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error> { + eh1::spi::SpiBus::::transfer_in_place(self, words) + } + + async fn flush(&mut self) -> Result<(), Self::Error> { + eh1::spi::SpiBus::flush(self) } } @@ -358,41 +348,7 @@ impl FullDuplex for Mock { } } -impl SpiDeviceRead for Mock { - fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> { - for op in operations { - SpiBusRead::read(self, op)?; - } - - Ok(()) - } -} - -#[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiDeviceRead for Mock { - async fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> { - embedded_hal::spi::SpiDeviceRead::::read_transaction(self, operations) - } -} - -impl SpiDeviceWrite for Mock { - fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> { - for op in operations { - SpiBusWrite::write(self, op)?; - } - - Ok(()) - } -} - -#[cfg(feature = "embedded-hal-async")] -impl embedded_hal_async::spi::SpiDeviceWrite for Mock { - async fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> { - embedded_hal::spi::SpiDeviceWrite::::write_transaction(self, operations) - } -} - -impl spi::SpiDevice for Mock { +impl SpiDevice for Mock { /// spi::SpiDevice implementation for Mock /// /// This writes the provided response to the buffer and will cause an assertion if the written data does not match the next expectation @@ -409,16 +365,24 @@ impl spi::SpiDevice for Mock { for op in operations { match op { Operation::Read(buffer) => { - SpiBusRead::read(self, buffer)?; + SpiBus::read(self, buffer)?; } Operation::Write(buffer) => { - SpiBusWrite::write(self, buffer)?; + SpiBus::write(self, buffer)?; } Operation::Transfer(read, write) => { - spi::SpiBus::transfer(self, read, write)?; + SpiBus::transfer(self, read, write)?; } Operation::TransferInPlace(buffer) => { - spi::SpiBus::transfer_in_place(self, buffer)?; + SpiBus::transfer_in_place(self, buffer)?; + } + Operation::DelayUs(delay) => { + let w = self.next().expect("no expectation for spi::delay call"); + assert_eq!( + w.expected_mode, + Mode::Delay(*delay), + "spi::transaction unexpected mode" + ); } } } @@ -453,16 +417,24 @@ impl embedded_hal_async::spi::SpiDevice for Mock { for op in operations { match op { Operation::Read(buffer) => { - SpiBusRead::read(self, buffer)?; + SpiBus::read(self, buffer)?; } Operation::Write(buffer) => { - SpiBusWrite::write(self, buffer)?; + SpiBus::write(self, buffer)?; } Operation::Transfer(read, write) => { - spi::SpiBus::transfer(self, read, write)?; + SpiBus::transfer(self, read, write)?; } Operation::TransferInPlace(buffer) => { - spi::SpiBus::transfer_in_place(self, buffer)?; + SpiBus::transfer_in_place(self, buffer)?; + } + Operation::DelayUs(delay) => { + let w = self.next().expect("no expectation for spi::delay call"); + assert_eq!( + w.expected_mode, + Mode::Delay(*delay), + "spi::transaction unexpected mode" + ); } } } @@ -486,11 +458,11 @@ mod test { #[test] fn test_spi_mock_write() { - use embedded_hal::spi::SpiBusWrite; + use eh1::spi::SpiBus; let mut spi = Mock::new(&[Transaction::write(10)]); - let _ = SpiBusWrite::write(&mut spi, &[10]).unwrap(); + let _ = SpiBus::write(&mut spi, &[10]).unwrap(); spi.done(); } @@ -498,11 +470,11 @@ mod test { #[tokio::test] #[cfg(feature = "embedded-hal-async")] async fn test_async_spi_mock_write() { - use embedded_hal_async::spi::SpiBusWrite; + use embedded_hal_async::spi::SpiBus; let mut spi = Mock::new(&[Transaction::write(10)]); - let _ = SpiBusWrite::write(&mut spi, &[10]).await.unwrap(); + let _ = SpiBus::write(&mut spi, &[10]).await.unwrap(); spi.done(); } @@ -522,12 +494,12 @@ mod test { #[test] fn test_spi_mock_read_bus() { - use embedded_hal::spi::SpiBusRead; + use eh1::spi::SpiBus; let mut spi = Mock::new(&[Transaction::read(10)]); let mut buff = vec![0u8; 1]; - SpiBusRead::read(&mut spi, &mut buff).unwrap(); + SpiBus::read(&mut spi, &mut buff).unwrap(); assert_eq!(buff, [10]); @@ -537,12 +509,12 @@ mod test { #[tokio::test] #[cfg(feature = "embedded-hal-async")] async fn test_async_spi_mock_read_bus() { - use embedded_hal_async::spi::SpiBusRead; + use embedded_hal_async::spi::SpiBus; let mut spi = Mock::new(&[Transaction::read(10)]); let mut buff = vec![0u8; 1]; - SpiBusRead::read(&mut spi, &mut buff).await.unwrap(); + SpiBus::read(&mut spi, &mut buff).await.unwrap(); assert_eq!(buff, [10]); @@ -551,7 +523,7 @@ mod test { #[test] fn test_spi_mock_flush() { - use embedded_hal::spi::SpiBusFlush; + use eh1::spi::SpiBus; let mut spi = Mock::new(&[Transaction::flush()]); spi.flush().unwrap(); @@ -561,16 +533,16 @@ mod test { #[tokio::test] #[cfg(feature = "embedded-hal-async")] async fn test_async_spi_mock_flush() { - use embedded_hal_async::spi::SpiBusFlush; + use embedded_hal_async::spi::SpiBus; let mut spi = Mock::new(&[Transaction::flush()]); - spi.flush().await.unwrap(); + SpiBus::flush(&mut spi).await.unwrap(); spi.done(); } #[test] fn test_spi_mock_multiple1() { - use embedded_hal::spi::{SpiBus, SpiBusWrite}; + use eh1::spi::SpiBus; let expectations = [ Transaction::write_vec(vec![1, 2]), @@ -582,11 +554,11 @@ mod test { ]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[1, 2]).unwrap(); + SpiBus::write(&mut spi, &[1, 2]).unwrap(); - let _ = SpiBusWrite::write(&mut spi, &[0x09]); + let _ = SpiBus::write(&mut spi, &[0x09]); assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0x0a); - let _ = SpiBusWrite::write(&mut spi, &[0xfe]); + let _ = SpiBus::write(&mut spi, &[0xfe]); assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0xFF); let mut v = vec![3, 4]; SpiBus::transfer_in_place(&mut spi, &mut v).unwrap(); @@ -601,6 +573,7 @@ mod test { Transaction::transaction_start(), Transaction::write_vec(vec![1, 2]), Transaction::write(9), + Transaction::delay(100), Transaction::read(10), Transaction::transaction_end(), ]; @@ -609,13 +582,14 @@ mod test { #[test] fn test_spi_mock_multiple_transaction() { - use embedded_hal::spi::SpiDevice; + use eh1::spi::SpiDevice; let mut spi = test_spi_mock_multiple_transaction_expectations(); let mut ans = [0u8; 1]; spi.transaction(&mut [ Operation::Write(&[1, 2]), Operation::Write(&[0x09]), + Operation::DelayUs(100), Operation::Read(&mut ans), ]) .unwrap(); @@ -632,11 +606,15 @@ mod test { let mut spi = test_spi_mock_multiple_transaction_expectations(); let mut ans = [0u8; 1]; - spi.transaction(&mut [ - Operation::Write(&[1, 2]), - Operation::Write(&[0x09]), - Operation::Read(&mut ans), - ]) + SpiDevice::transaction( + &mut spi, + &mut [ + Operation::Write(&[1, 2]), + Operation::Write(&[0x09]), + Operation::DelayUs(100), + Operation::Read(&mut ans), + ], + ) .await .unwrap(); @@ -647,12 +625,12 @@ mod test { #[test] fn test_spi_mock_write_vec() { - use embedded_hal::spi::SpiBusWrite; + use eh1::spi::SpiBus; let expectations = [Transaction::write_vec(vec![10, 12])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12]).unwrap(); + SpiBus::write(&mut spi, &[10, 12]).unwrap(); spi.done(); } @@ -660,19 +638,19 @@ mod test { #[tokio::test] #[cfg(feature = "embedded-hal-async")] async fn test_async_spi_mock_write_vec() { - use embedded_hal_async::spi::SpiBusWrite; + use embedded_hal_async::spi::SpiBus; let expectations = [Transaction::write_vec(vec![10, 12])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12]).await.unwrap(); + SpiBus::write(&mut spi, &[10, 12]).await.unwrap(); spi.done(); } #[test] fn test_spi_mock_transfer_in_place() { - use embedded_hal::spi::SpiBus; + use eh1::spi::SpiBus; let expectations = [Transaction::transfer_in_place(vec![10, 12], vec![12, 13])]; let mut spi = Mock::new(&expectations); @@ -703,7 +681,7 @@ mod test { #[test] fn test_spi_mock_transfer() { - use embedded_hal::spi::SpiBus; + use eh1::spi::SpiBus; let expectations = [Transaction::transfer(vec![10, 12], vec![12, 13])]; let mut spi = Mock::new(&expectations); @@ -734,7 +712,7 @@ mod test { #[test] fn test_spi_mock_multiple() { - use embedded_hal::spi::{SpiBus, SpiBusWrite}; + use eh1::spi::SpiBus; let expectations = [ Transaction::write_vec(vec![1, 2]), @@ -742,7 +720,7 @@ mod test { ]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[1, 2]).unwrap(); + SpiBus::write(&mut spi, &[1, 2]).unwrap(); let mut v = vec![3, 4]; SpiBus::transfer_in_place(&mut spi, &mut v).unwrap(); @@ -755,7 +733,7 @@ mod test { #[tokio::test] #[cfg(feature = "embedded-hal-async")] async fn test_async_spi_mock_multiple() { - use embedded_hal_async::spi::{SpiBus, SpiBusWrite}; + use embedded_hal_async::spi::SpiBus; let expectations = [ Transaction::write_vec(vec![1, 2]), @@ -763,7 +741,7 @@ mod test { ]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[1, 2]).await.unwrap(); + SpiBus::write(&mut spi, &[1, 2]).await.unwrap(); let mut v = vec![3, 4]; SpiBus::transfer_in_place(&mut spi, &mut v).await.unwrap(); @@ -776,26 +754,26 @@ mod test { #[test] #[should_panic(expected = "spi::write data does not match expectation")] fn test_spi_mock_write_err() { - use embedded_hal::spi::SpiBusWrite; + use eh1::spi::SpiBus; let expectations = [Transaction::write_vec(vec![10, 12])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 12]).unwrap(); + SpiBus::write(&mut spi, &[10, 12, 12]).unwrap(); } #[tokio::test] #[cfg(feature = "embedded-hal-async")] #[should_panic(expected = "spi::write data does not match expectation")] async fn test_async_spi_mock_write_err() { - use embedded_hal_async::spi::SpiBusWrite; + use embedded_hal_async::spi::SpiBus; let expectations = [Transaction::write_vec(vec![10, 12])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 12]).await.unwrap(); + SpiBus::write(&mut spi, &[10, 12, 12]).await.unwrap(); } #[test] #[should_panic(expected = "spi::transfer_in_place write data does not match expectation")] fn test_spi_mock_transfer_err() { - use embedded_hal::spi::SpiBus; + use eh1::spi::SpiBus; let expectations = [Transaction::transfer_in_place(vec![10, 12], vec![12, 15])]; let mut spi = Mock::new(&expectations); SpiBus::transfer_in_place(&mut spi, &mut vec![10, 13]).unwrap(); @@ -816,44 +794,46 @@ mod test { #[test] #[should_panic(expected = "spi::write unexpected mode")] fn test_spi_mock_mode_err() { - use embedded_hal::spi::SpiBusWrite; + use eh1::spi::SpiBus; let expectations = [Transaction::transfer_in_place(vec![10, 12], vec![])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 12]).unwrap(); + SpiBus::write(&mut spi, &[10, 12, 12]).unwrap(); } #[tokio::test] #[cfg(feature = "embedded-hal-async")] #[should_panic(expected = "spi::write unexpected mode")] async fn test_async_spi_mock_mode_err() { - use embedded_hal_async::spi::SpiBusWrite; + use embedded_hal_async::spi::SpiBus; let expectations = [Transaction::transfer_in_place(vec![10, 12], vec![])]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 12]).await.unwrap(); + SpiBus::write(&mut spi, &[10, 12, 12]).await.unwrap(); } #[test] #[should_panic(expected = "spi::write data does not match expectation")] fn test_spi_mock_multiple_transaction_err() { - use embedded_hal::spi::SpiBusWrite; + use eh1::spi::SpiBus; + let expectations = [ Transaction::write_vec(vec![10, 12]), Transaction::write_vec(vec![10, 12]), ]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 10]).unwrap(); + SpiBus::write(&mut spi, &[10, 12, 10]).unwrap(); } #[tokio::test] #[cfg(feature = "embedded-hal-async")] #[should_panic(expected = "spi::write data does not match expectation")] async fn test_async_spi_mock_multiple_transaction_err() { - use embedded_hal_async::spi::SpiBusWrite; + use embedded_hal_async::spi::SpiBus; + let expectations = [ Transaction::write_vec(vec![10, 12]), Transaction::write_vec(vec![10, 12]), ]; let mut spi = Mock::new(&expectations); - SpiBusWrite::write(&mut spi, &[10, 12, 12]).await.unwrap(); + SpiBus::write(&mut spi, &[10, 12, 12]).await.unwrap(); } }