diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index b6d6cdd4c0b..8aeb56bebce 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -44,12 +44,10 @@ esp-synopsys-usb-otg = { version = "0.4.2", optional = true, features = ["fs fugit = "0.3.7" instability = "0.3" log = { version = "0.4.22", optional = true } -nb = "1.1.0" paste = "1.0.15" portable-atomic = { version = "1.9.0", default-features = false } procmacros = { version = "0.15.0", package = "esp-hal-procmacros", path = "../esp-hal-procmacros" } strum = { version = "0.26.3", default-features = false, features = ["derive"] } -void = { version = "1.0.2", default-features = false } usb-device = { version = "0.3.2", optional = true } rand_core = "0.6.4" ufmt-write = "0.1.0" diff --git a/esp-hal/src/analog/adc/mod.rs b/esp-hal/src/analog/adc/mod.rs index e247e82aadc..40315242be4 100644 --- a/esp-hal/src/analog/adc/mod.rs +++ b/esp-hal/src/analog/adc/mod.rs @@ -43,7 +43,11 @@ //! let mut delay = Delay::new(); //! //! loop { -//! let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut pin)).unwrap(); +//! let pin_value: u16 = loop { +//! if let Some(value) = adc1.read_oneshot(&mut pin) { +//! break value; +//! } +//! }; //! //! delay.delay_millis(1500); //! } diff --git a/esp-hal/src/hmac.rs b/esp-hal/src/hmac.rs index 0b18fb8564d..e6ff6cef4e6 100644 --- a/esp-hal/src/hmac.rs +++ b/esp-hal/src/hmac.rs @@ -34,8 +34,6 @@ //! //! [HMAC]: https://github.com/esp-rs/esp-hal/blob/main/examples/src/bin/hmac.rs -use core::convert::Infallible; - use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::HMAC, @@ -175,8 +173,8 @@ impl<'d> Hmac<'d> { let msg_len = self.byte_written as u64; - nb::block!(self.write_data(&[0x80])).unwrap(); - nb::block!(self.flush_data()).unwrap(); + self.write_data(&[0x80])?; + self.flush_data()?; self.next_command(); debug_assert!(self.byte_written % 4 == 0); diff --git a/esp-hal/src/rng.rs b/esp-hal/src/rng.rs index 2efa64fc13c..72167018148 100644 --- a/esp-hal/src/rng.rs +++ b/esp-hal/src/rng.rs @@ -81,10 +81,13 @@ /// Attenuation::Attenuation11dB /// ); /// let mut adc1 = Adc::::new(peripherals.ADC1, adc1_config); -/// let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap(); +/// let pin_value: u16 = loop { +/// if let Some(value) = adc1.read_oneshot(&mut adc1_pin) { +/// break value; +/// } +/// }; /// rng.read(&mut buf); /// true_rand = rng.random(); -/// let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap(); /// # } /// ``` use core::marker::PhantomData; diff --git a/esp-hal/src/rsa/esp32.rs b/esp-hal/src/rsa/esp32.rs index 687eb1e2476..3a68d61c461 100644 --- a/esp-hal/src/rsa/esp32.rs +++ b/esp-hal/src/rsa/esp32.rs @@ -1,5 +1,3 @@ -use core::convert::Infallible; - use crate::rsa::{ implement_op, Multi, diff --git a/esp-hal/src/rsa/esp32cX.rs b/esp-hal/src/rsa/esp32cX.rs index 9a95ecbdf44..fa403a1b85c 100644 --- a/esp-hal/src/rsa/esp32cX.rs +++ b/esp-hal/src/rsa/esp32cX.rs @@ -1,5 +1,3 @@ -use core::convert::Infallible; - use crate::rsa::{ implement_op, Multi, diff --git a/esp-hal/src/rsa/esp32sX.rs b/esp-hal/src/rsa/esp32sX.rs index 0624a916b10..d9b881bac16 100644 --- a/esp-hal/src/rsa/esp32sX.rs +++ b/esp-hal/src/rsa/esp32sX.rs @@ -1,5 +1,3 @@ -use core::convert::Infallible; - use crate::rsa::{ implement_op, Multi, diff --git a/esp-hal/src/sha.rs b/esp-hal/src/sha.rs index 418369c0629..dbc88aab1e6 100644 --- a/esp-hal/src/sha.rs +++ b/esp-hal/src/sha.rs @@ -33,7 +33,6 @@ #![doc = crate::before_snippet!()] //! # use esp_hal::sha::Sha; //! # use esp_hal::sha::Sha256; -//! # use nb::block; //! let mut source_data = "HELLO, ESPRESSIF!".as_bytes(); //! let mut sha = Sha::new(peripherals.SHA); //! let mut hasher = sha.start::(); @@ -41,22 +40,21 @@ //! // desired length //! let mut output = [0u8; 32]; //! -//! while !source_data.is_empty() { -//! // All the HW Sha functions are infallible so unwrap is fine to use if -//! // you use block! -//! source_data = block!(hasher.update(source_data)).unwrap(); -//! } +//! let source_data = loop { +//! if let Some(data) = hasher.finish(&mut output) { +//! break data; +//! } +//! }; //! //! // Finish can be called as many times as desired to get multiple copies of //! // the output. -//! block!(hasher.finish(output.as_mut_slice())).unwrap(); -//! +//! while hasher.finish(output.as_mut_slice()).is_none() {} //! # } //! ``` //! ## Implementation State //! - DMA-SHA Mode is not supported. -use core::{borrow::BorrowMut, convert::Infallible, marker::PhantomData, mem::size_of}; +use core::{borrow::BorrowMut, marker::PhantomData, mem::size_of}; /// Re-export digest for convenience #[cfg(feature = "digest")] @@ -225,9 +223,7 @@ impl<'d, A: ShaAlgorithm, S: BorrowMut>> ShaDigest<'d, A, S> { // Store message length for padding let length = (self.cursor as u64 * 8).to_be_bytes(); // Append "1" bit - if self.update(&[0x80]).is_none() { - return None; - } + self.update(&[0x80])?; // Flush partial data, ensures aligned cursor { @@ -540,8 +536,8 @@ impl<'d, A: ShaAlgorithm, S: BorrowMut>> digest::OutputSizeUser for ShaD impl<'d, A: ShaAlgorithm, S: BorrowMut>> digest::Update for ShaDigest<'d, A, S> { fn update(&mut self, data: &[u8]) { let mut remaining = data.as_ref(); - while !remaining.is_empty() { - remaining = nb::block!(Self::update(self, remaining)).unwrap(); + while let Some(rem) = Self::update(self, remaining) { + remaining = rem; } } } @@ -549,7 +545,7 @@ impl<'d, A: ShaAlgorithm, S: BorrowMut>> digest::Update for ShaDigest<'d #[cfg(feature = "digest")] impl<'d, A: ShaAlgorithm, S: BorrowMut>> digest::FixedOutput for ShaDigest<'d, A, S> { fn finalize_into(mut self, out: &mut digest::Output) { - nb::block!(self.finish(out)).unwrap(); + while self.finish(out).is_none() {} } } diff --git a/esp-hal/src/spi/master.rs b/esp-hal/src/spi/master.rs index 1099c46abb9..8fdefeae9bd 100644 --- a/esp-hal/src/spi/master.rs +++ b/esp-hal/src/spi/master.rs @@ -2211,14 +2211,16 @@ mod ehal1 { T: Instance, Dm: DriverMode, { - fn read(&mut self) -> nb::Result { - self.driver().read_byte().ok_or(nb::Error::WouldBlock) + fn read(&mut self) -> embedded_hal_nb::nb::Result { + self.driver() + .read_byte() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { self.driver() .write_byte(word) - .map_or_else(|| Err(nb::Error::WouldBlock), |_| Ok(())) + .map_or_else(|| Err(embedded_hal_nb::nb::Error::WouldBlock), |_| Ok(())) } } diff --git a/esp-hal/src/timer/mod.rs b/esp-hal/src/timer/mod.rs index 837c776629a..29505c9b698 100644 --- a/esp-hal/src/timer/mod.rs +++ b/esp-hal/src/timer/mod.rs @@ -35,7 +35,7 @@ //! //! periodic.start(1.secs()); //! loop { -//! nb::block!(periodic.wait()); +//! while periodic.wait().is_none() {} //! } //! # } //! ``` diff --git a/esp-hal/src/twai/mod.rs b/esp-hal/src/twai/mod.rs index 884d2e70d31..c8c928663e5 100644 --- a/esp-hal/src/twai/mod.rs +++ b/esp-hal/src/twai/mod.rs @@ -31,7 +31,6 @@ //! # use esp_hal::twai::TwaiConfiguration; //! # use esp_hal::twai::BaudRate; //! # use esp_hal::twai::TwaiMode; -//! # use nb::block; //! // Use GPIO pins 2 and 3 to connect to the respective pins on the TWAI //! // transceiver. //! let twai_rx_pin = peripherals.GPIO3; @@ -62,10 +61,14 @@ //! //! loop { //! // Wait for a frame to be received. -//! let frame = block!(twai.receive()).unwrap(); +//! let frame = loop { +//! if let Ok(frame) = twai.receive() { +//! break frame; +//! } +//! }; //! //! // Transmit the frame back. -//! let _result = block!(twai.transmit(&frame)).unwrap(); +//! while twai.transmit(&frame).is_err() {} //! } //! # } //! ``` @@ -81,7 +84,6 @@ //! # use esp_hal::twai::EspTwaiFrame; //! # use esp_hal::twai::StandardId; //! # use esp_hal::twai::TwaiMode; -//! # use nb::block; //! // Use GPIO pins 2 and 3 to connect to the respective pins on the TWAI //! // transceiver. //! let can_rx_pin = peripherals.GPIO3; @@ -111,7 +113,11 @@ //! //! let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO, //! &[1, 2, 3]).unwrap(); // Wait for a frame to be received. -//! let frame = block!(can.receive()).unwrap(); +//! let frame = loop { +//! if let Ok(frame) = can.receive() { +//! break frame; +//! } +//! }; //! //! # loop {} //! # } @@ -1179,12 +1185,12 @@ where } /// Sends the specified `EspTwaiFrame` over the TWAI bus. - pub fn transmit(&mut self, frame: &EspTwaiFrame) -> nb::Result<(), EspTwaiError> { + pub fn transmit(&mut self, frame: &EspTwaiFrame) -> Result<(), EspTwaiError> { self.tx.transmit(frame) } /// Receives a TWAI frame from the TWAI bus. - pub fn receive(&mut self) -> nb::Result { + pub fn receive(&mut self) -> Result { self.rx.receive() } @@ -1219,17 +1225,18 @@ where /// NOTE: TODO: This may not work if using the self reception/self test /// functionality. See notes 1 and 2 in the "Frame Identifier" section /// of the reference manual. - pub fn transmit(&mut self, frame: &EspTwaiFrame) -> nb::Result<(), EspTwaiError> { + pub fn transmit(&mut self, frame: &EspTwaiFrame) -> Result<(), EspTwaiError> { let register_block = self.twai.register_block(); let status = register_block.status().read(); // Check that the peripheral is not in a bus off state. if status.bus_off_st().bit_is_set() { - return nb::Result::Err(nb::Error::Other(EspTwaiError::BusOff)); + return Err(EspTwaiError::BusOff); } // Check that the peripheral is not already transmitting a packet. if !status.tx_buf_st().bit_is_set() { - return nb::Result::Err(nb::Error::WouldBlock); + return Err(EspTwaiError::WouldBlock); // TODO: Is this the right + // error? } write_frame(register_block, frame); @@ -1251,28 +1258,26 @@ where Dm: crate::DriverMode, { /// Receive a frame - pub fn receive(&mut self) -> nb::Result { + pub fn receive(&mut self) -> Result { let register_block = self.twai.register_block(); let status = register_block.status().read(); // Check that the peripheral is not in a bus off state. if status.bus_off_st().bit_is_set() { - return nb::Result::Err(nb::Error::Other(EspTwaiError::BusOff)); + return Err(EspTwaiError::BusOff); } // Check that we actually have packets to receive. if !status.rx_buf_st().bit_is_set() { - return nb::Result::Err(nb::Error::WouldBlock); + return Err(EspTwaiError::WouldBlock); } // Check if the packet in the receive buffer is valid or overrun. if status.miss_st().bit_is_set() { - return nb::Result::Err(nb::Error::Other(EspTwaiError::EmbeddedHAL( - ErrorKind::Overrun, - ))); + return Err(EspTwaiError::EmbeddedHAL(ErrorKind::Overrun)); } - Ok(read_frame(register_block)?) + read_frame(register_block) } } @@ -1288,6 +1293,8 @@ pub enum EspTwaiError { NonCompliantDlc(u8), /// Encapsulates errors defined by the embedded-hal crate. EmbeddedHAL(ErrorKind), + /// This operation requires blocking behavior to complete + WouldBlock, } #[cfg(any(doc, feature = "unstable"))] @@ -1344,18 +1351,21 @@ where type Error = EspTwaiError; /// Transmit a frame. - fn transmit(&mut self, frame: &Self::Frame) -> nb::Result, Self::Error> { + fn transmit( + &mut self, + frame: &Self::Frame, + ) -> embedded_hal_nb::nb::Result, Self::Error> { self.tx.transmit(frame)?; // Success in readying packet for transmit. No packets can be replaced in the // transmit buffer so return None in accordance with the // embedded-can/embedded-hal trait. - nb::Result::Ok(None) + embedded_hal_nb::nb::Result::Ok(None) } /// Return a received frame if there are any available. - fn receive(&mut self) -> nb::Result { - self.rx.receive() + fn receive(&mut self) -> embedded_hal_nb::nb::Result { + Ok(self.rx.receive()?) } } diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index f14a73a73fe..5ebbfd4ba62 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -198,7 +198,7 @@ //! let serial = serial.as_mut().unwrap(); //! //! let mut cnt = 0; -//! while let nb::Result::Ok(_c) = serial.read_byte() { +//! while let Some(_c) = serial.read_byte() { //! cnt += 1; //! } //! writeln!(serial, "Read {} bytes", cnt).ok(); @@ -285,6 +285,9 @@ pub enum Error { /// match the expected parity configuration. /// with the `async` feature. RxParityError, + + /// This operation requires blocking behavior to complete + WouldBlock, } impl core::error::Error for Error {} @@ -297,6 +300,7 @@ impl core::fmt::Display for Error { Error::RxGlitchDetected => write!(f, "A glitch was detected on the RX line"), Error::RxFrameError => write!(f, "A framing error was detected on the RX line"), Error::RxParityError => write!(f, "A parity error was detected on the RX line"), + Error::WouldBlock => write!(f, "Would block"), } } } @@ -661,21 +665,24 @@ where pub fn write_bytes(&mut self, data: &[u8]) -> Result { let count = data.len(); - data.iter() - .try_for_each(|c| nb::block!(self.write_byte(*c)))?; + for &byte in data { + if self.write_byte(byte).is_none() { + return Err(Error::WouldBlock); + } + } Ok(count) } - fn write_byte(&mut self, word: u8) -> nb::Result<(), Error> { + fn write_byte(&mut self, word: u8) -> Option<()> { if self.tx_fifo_count() < UART_FIFO_SIZE { self.register_block() .fifo() .write(|w| unsafe { w.rxfifo_rd_byte().bits(word) }); - Ok(()) + Some(()) } else { - Err(nb::Error::WouldBlock) + None } } @@ -692,11 +699,11 @@ where } /// Flush the transmit buffer of the UART - pub fn flush(&mut self) -> nb::Result<(), Error> { + pub fn flush(&mut self) -> Option<()> { if self.is_tx_idle() { - Ok(()) + Some(()) } else { - Err(nb::Error::WouldBlock) + None } } @@ -883,7 +890,7 @@ where } /// Read a byte from the UART - pub fn read_byte(&mut self) -> nb::Result { + pub fn read_byte(&mut self) -> Option { cfg_if::cfg_if! { if #[cfg(esp32s2)] { // On the ESP32-S2 we need to use PeriBus2 to read the FIFO: @@ -897,9 +904,9 @@ where } if self.rx_fifo_count() > 0 { - Ok(fifo.read().rxfifo_rd_byte().bits()) + Some(fifo.read().rxfifo_rd_byte().bits()) } else { - Err(nb::Error::WouldBlock) + None } } @@ -908,7 +915,7 @@ where pub fn drain_fifo(&mut self, buf: &mut [u8]) -> usize { let mut count = 0; while count < buf.len() { - if let Ok(byte) = self.read_byte() { + if let Some(byte) = self.read_byte() { buf[count] = byte; count += 1; } else { @@ -1189,17 +1196,17 @@ where } /// Write a byte out over the UART - pub fn write_byte(&mut self, word: u8) -> nb::Result<(), Error> { + pub fn write_byte(&mut self, word: u8) -> Option<()> { self.tx.write_byte(word) } /// Flush the transmit buffer of the UART - pub fn flush(&mut self) -> nb::Result<(), Error> { + pub fn flush(&mut self) -> Option<()> { self.tx.flush() } /// Read a byte from the UART - pub fn read_byte(&mut self) -> nb::Result { + pub fn read_byte(&mut self) -> Option { self.rx.read_byte() } @@ -1392,8 +1399,9 @@ where T: Instance, Dm: DriverMode, { - fn read(&mut self) -> nb::Result { + fn read(&mut self) -> embedded_hal_nb::nb::Result { self.read_byte() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -1402,8 +1410,9 @@ where T: Instance, Dm: DriverMode, { - fn read(&mut self) -> nb::Result { + fn read(&mut self) -> embedded_hal_nb::nb::Result { self.read_byte() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -1412,12 +1421,13 @@ where T: Instance, Dm: DriverMode, { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { self.write_byte(word) + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } - fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.flush() + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { + self.flush().ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -1426,12 +1436,13 @@ where T: Instance, Dm: DriverMode, { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { self.write_byte(word) + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } - fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.flush() + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { + self.flush().ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -1539,9 +1550,8 @@ where fn flush(&mut self) -> Result<(), Self::Error> { loop { match self.flush() { - Ok(_) => break, - Err(nb::Error::WouldBlock) => { /* Wait */ } - Err(nb::Error::Other(e)) => return Err(e), + Some(_) => break, + None => { /* Wait */ } } } diff --git a/esp-hal/src/usb_serial_jtag.rs b/esp-hal/src/usb_serial_jtag.rs index 2db2268dbf2..c366e9907ab 100644 --- a/esp-hal/src/usb_serial_jtag.rs +++ b/esp-hal/src/usb_serial_jtag.rs @@ -281,7 +281,7 @@ where /// number of bytes in the FIFO is larger than `buf`. pub fn drain_rx_fifo(&mut self, buf: &mut [u8]) -> usize { let mut count = 0; - while let Ok(value) = self.read_byte() { + while let Some(value) = self.read_byte() { buf[count] = value; count += 1; if count == buf.len() { @@ -568,7 +568,7 @@ impl embedded_hal_nb::serial::Read for UsbSerialJtag<'_, Dm> where Dm: DriverMode, { - fn read(&mut self) -> nb::Result { + fn read(&mut self) -> embedded_hal_nb::nb::Result { embedded_hal_nb::serial::Read::read(&mut self.rx) } } @@ -577,8 +577,9 @@ impl embedded_hal_nb::serial::Read for UsbSerialJtagRx<'_, Dm> where Dm: DriverMode, { - fn read(&mut self) -> nb::Result { - self.read_byte().ok_or(nb::Error::WouldBlock) + fn read(&mut self) -> embedded_hal_nb::nb::Result { + self.read_byte() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -586,11 +587,11 @@ impl embedded_hal_nb::serial::Write for UsbSerialJtag<'_, Dm> where Dm: DriverMode, { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { embedded_hal_nb::serial::Write::write(&mut self.tx, word) } - fn flush(&mut self) -> nb::Result<(), Self::Error> { + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { embedded_hal_nb::serial::Write::flush(&mut self.tx) } } @@ -599,12 +600,14 @@ impl embedded_hal_nb::serial::Write for UsbSerialJtagTx<'_, Dm> where Dm: DriverMode, { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { - self.write_byte_nb(word).ok_or(nb::Error::WouldBlock) + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { + self.write_byte_nb(word) + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } - fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.flush_tx_nb().ok_or(nb::Error::WouldBlock) + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { + self.flush_tx_nb() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } diff --git a/esp-lp-hal/src/uart.rs b/esp-lp-hal/src/uart.rs index db09f0be2a7..9ef7873867a 100644 --- a/esp-lp-hal/src/uart.rs +++ b/esp-lp-hal/src/uart.rs @@ -46,8 +46,13 @@ pub unsafe fn conjure() -> LpUart { } /// UART Error -#[derive(Debug)] -pub enum Error {} +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[non_exhaustive] +pub enum Error { + /// This operation requires blocking behavior to complete + WouldBlock, +} #[cfg(feature = "embedded-hal")] impl embedded_hal_nb::serial::Error for Error { @@ -169,24 +174,23 @@ pub struct LpUart { impl LpUart { /// Read a single byte from the UART in a non-blocking manner. - pub fn read_byte(&mut self) -> nb::Result { + pub fn read_byte(&mut self) -> Option { if self.rx_fifo_count() > 0 { - let byte = self.uart.fifo().read().rxfifo_rd_byte().bits(); - Ok(byte) + Some(self.uart.fifo().read().rxfifo_rd_byte().bits()) } else { - Err(nb::Error::WouldBlock) + None } } /// Write a single byte to the UART in a non-blocking manner. - pub fn write_byte(&mut self, byte: u8) -> nb::Result<(), Error> { + pub fn write_byte(&mut self, byte: u8) -> Option<()> { if self.tx_fifo_count() < UART_FIFO_SIZE { self.uart .fifo() .write(|w| unsafe { w.rxfifo_rd_byte().bits(byte) }); - Ok(()) + Some(()) } else { - Err(nb::Error::WouldBlock) + None } } @@ -195,18 +199,21 @@ impl LpUart { pub fn write_bytes(&mut self, data: &[u8]) -> Result { let count = data.len(); - data.iter() - .try_for_each(|c| nb::block!(self.write_byte(*c)))?; + for &byte in data { + if self.write_byte(byte).is_none() { + return Err(Error::WouldBlock); + } + } Ok(count) } - /// Flush the UART's transmit buffer in a non-blocking manner. - pub fn flush_tx(&mut self) -> nb::Result<(), Error> { + /// Flush the transmit buffer of the UART + pub fn flush(&mut self) -> Option<()> { if self.is_tx_idle() { - Ok(()) + Some(()) } else { - Err(nb::Error::WouldBlock) + None } } @@ -238,19 +245,21 @@ impl embedded_hal_nb::serial::ErrorType for LpUart { #[cfg(feature = "embedded-hal")] impl embedded_hal_nb::serial::Read for LpUart { - fn read(&mut self) -> nb::Result { + fn read(&mut self) -> embedded_hal_nb::nb::Result { self.read_byte() + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } #[cfg(feature = "embedded-hal")] impl embedded_hal_nb::serial::Write for LpUart { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { self.write_byte(word) + .ok_or(embedded_hal_nb::nb::Error::WouldBlock) } - fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.flush_tx() + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { + self.flush().ok_or(embedded_hal_nb::nb::Error::WouldBlock) } } @@ -295,11 +304,9 @@ impl embedded_io::Write for LpUart { fn flush(&mut self) -> Result<(), Self::Error> { loop { - match self.flush_tx() { - Ok(_) => break, - Err(nb::Error::WouldBlock) => { /* Wait */ } - #[allow(unreachable_patterns)] - Err(nb::Error::Other(e)) => return Err(e), + match self.flush() { + Some(_) => break, + None => { /* Wait */ } } } diff --git a/examples/src/bin/hmac.rs b/examples/src/bin/hmac.rs index e9aba42642c..b0915e0194f 100644 --- a/examples/src/bin/hmac.rs +++ b/examples/src/bin/hmac.rs @@ -66,7 +66,6 @@ use esp_hal::{ }; use esp_println::println; use hmac::{Hmac as HmacSw, Mac}; -use nb::block; use sha2::Sha256; type HmacSha256 = HmacSw; @@ -93,12 +92,14 @@ fn main() -> ! { let (nsrc, _) = src.split_at(i); let mut remaining = nsrc; hw_hmac.init(); - block!(hw_hmac.configure(HmacPurpose::ToUser, KeyId::Key0)).expect("Key purpose mismatch"); + hw_hmac + .configure(HmacPurpose::ToUser, KeyId::Key0) + .expect("Key purpose mismatch"); let pre_hw_hmac = esp_hal::time::now(); - while remaining.len() > 0 { - remaining = block!(hw_hmac.update(remaining)).unwrap(); + while let Some(rem) = hw_hmac.update(remaining) { + remaining = rem; } - block!(hw_hmac.finalize(output.as_mut_slice())).unwrap(); + while hw_hmac.finalize(output.as_mut_slice()).is_none() {} let post_hw_hmac = esp_hal::time::now(); let hw_time = post_hw_hmac - pre_hw_hmac; let mut sw_hmac = HmacSha256::new_from_slice(key).expect("HMAC can take key of any size"); diff --git a/examples/src/bin/ieee802154_sniffer.rs b/examples/src/bin/ieee802154_sniffer.rs index d53228ccbf0..e345c97b029 100644 --- a/examples/src/bin/ieee802154_sniffer.rs +++ b/examples/src/bin/ieee802154_sniffer.rs @@ -42,7 +42,11 @@ fn main() -> ! { let mut cnt = 0; let mut read = [0u8; 2]; loop { - let c = nb::block!(uart0.read_byte()).unwrap(); + let c = loop { + if let Some(c) = uart0.read_byte() { + break c; + } + }; if c == b'r' { continue; } @@ -77,7 +81,7 @@ fn main() -> ! { println!("@RAW {:02x?}", &frame.data); } - if let nb::Result::Ok(c) = uart0.read_byte() { + if let Some(c) = uart0.read_byte() { if c == b'r' { software_reset(); } diff --git a/examples/src/bin/twai.rs b/examples/src/bin/twai.rs index d2b3e490e5a..e49e04a014c 100644 --- a/examples/src/bin/twai.rs +++ b/examples/src/bin/twai.rs @@ -34,7 +34,6 @@ use esp_hal::{ twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId, TwaiMode}, }; use esp_println::println; -use nb::block; #[entry] fn main() -> ! { @@ -82,21 +81,26 @@ fn main() -> ! { // Send a frame to the other ESP // Use `new_self_reception` if you want to use self-testing. let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap(); - block!(twai.transmit(&frame)).unwrap(); + while twai.transmit(&frame).is_err() {} + println!("Sent a frame"); } let delay = Delay::new(); loop { // Wait for a frame to be received. - let frame = block!(twai.receive()).unwrap(); + let frame = loop { + if let Ok(frame) = twai.receive() { + break frame; + } + }; println!("Received a frame: {frame:?}"); delay.delay_millis(250); let frame = EspTwaiFrame::new(StandardId::ZERO, &[1, 2, 3]).unwrap(); // Transmit a new frame back to the other ESP - block!(twai.transmit(&frame)).unwrap(); + while twai.transmit(&frame).is_err() {} println!("Sent a frame"); } } diff --git a/hil-test/Cargo.toml b/hil-test/Cargo.toml index 97204555d4e..419ad853c7e 100644 --- a/hil-test/Cargo.toml +++ b/hil-test/Cargo.toml @@ -223,7 +223,6 @@ embassy-executor = { version = "0.6.0", default-features = false } embedded-test = { version = "0.5.0", git = "https://github.com/probe-rs/embedded-test.git", rev = "7109473", default-features = false } fugit = "0.3.7" hex-literal = "0.4.1" -nb = "1.1.0" p192 = { version = "0.13.0", default-features = false, features = ["arithmetic"] } p256 = { version = "0.13.2", default-features = false, features = ["arithmetic"] } sha1 = { version = "0.10.6", default-features = false } diff --git a/hil-test/tests/embassy_timers_executors.rs b/hil-test/tests/embassy_timers_executors.rs index 942b287ce96..1c887686f14 100644 --- a/hil-test/tests/embassy_timers_executors.rs +++ b/hil-test/tests/embassy_timers_executors.rs @@ -69,7 +69,8 @@ mod test_cases { let t1 = esp_hal::time::now(); periodic.start(100.millis()).unwrap(); - nb::block!(periodic.wait()).unwrap(); + while periodic.wait().is_none() {} + let t2 = esp_hal::time::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); diff --git a/hil-test/tests/rsa.rs b/hil-test/tests/rsa.rs index 0b39046f6ce..b4ff6a018c0 100644 --- a/hil-test/tests/rsa.rs +++ b/hil-test/tests/rsa.rs @@ -56,7 +56,7 @@ mod tests { fn init() -> Context<'static> { let peripherals = esp_hal::init(esp_hal::Config::default()); let mut rsa = Rsa::new(peripherals.RSA); - nb::block!(rsa.ready()).unwrap(); + while rsa.ready().is_none() {} Context { rsa } } diff --git a/hil-test/tests/rsa_async.rs b/hil-test/tests/rsa_async.rs index 21ba4d4e229..7b5a1cf633e 100644 --- a/hil-test/tests/rsa_async.rs +++ b/hil-test/tests/rsa_async.rs @@ -56,7 +56,7 @@ mod tests { fn init() -> Context<'static> { let peripherals = esp_hal::init(esp_hal::Config::default()); let mut rsa = Rsa::new(peripherals.RSA).into_async(); - nb::block!(rsa.ready()).unwrap(); + while rsa.ready().is_none() {} Context { rsa } } diff --git a/hil-test/tests/sha.rs b/hil-test/tests/sha.rs index 4f67c0573d6..6bc6f9b2dbf 100644 --- a/hil-test/tests/sha.rs +++ b/hil-test/tests/sha.rs @@ -18,7 +18,6 @@ use esp_hal::{ sha::{Sha, Sha1, Sha256, ShaAlgorithm, ShaDigest}, }; use hil_test as _; -use nb::block; /// Dummy data used to feed the hasher. const SOURCE_DATA: &[u8] = &[b'a'; 258]; @@ -34,10 +33,10 @@ fn assert_sw_hash(input: &[u8], expected_output: &[u8]) { fn hash_sha(sha: &mut Sha<'static>, mut input: &[u8], output: &mut [u8]) { let mut digest = sha.start::(); - while !input.is_empty() { - input = block!(digest.update(input)).unwrap(); + while let Some(digest) = digest.update(input) { + input = digest; } - block!(digest.finish(output)).unwrap(); + while digest.finish(output).is_none() {} } fn hash_digest<'a, S: ShaAlgorithm>(sha: &'a mut Sha<'static>, input: &[u8], output: &mut [u8]) { @@ -269,22 +268,28 @@ mod tests { let mut all_done = true; if !sha1_remaining.is_empty() { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha1); - sha1_remaining = block!(digest.update(sha1_remaining)).unwrap(); - block!(digest.save(&mut sha1)); + while let Some(remaining) = digest.update(sha1_remaining) { + sha1_remaining = remaining; + } + while digest.save(&mut sha1).is_none() {} all_done = false; } #[cfg(not(feature = "esp32"))] if !sha224_remaining.is_empty() { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha224); - sha224_remaining = block!(digest.update(sha224_remaining)).unwrap(); - block!(digest.save(&mut sha224)); + while let Some(remaining) = digest.update(sha224_remaining) { + sha224_remaining = remaining; + } + while digest.save(&mut sha224).is_none() {} all_done = false; } if !sha256_remaining.is_empty() { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha256); - sha256_remaining = block!(digest.update(sha256_remaining)).unwrap(); - block!(digest.save(&mut sha256)); + while let Some(remaining) = digest.update(sha256_remaining) { + sha256_remaining = remaining; + } + while digest.save(&mut sha256).is_none() {} all_done = false; } @@ -292,15 +297,19 @@ mod tests { { if !sha384_remaining.is_empty() { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha384); - sha384_remaining = block!(digest.update(sha384_remaining)).unwrap(); - block!(digest.save(&mut sha384)); + while let Some(remaining) = digest.update(sha384_remaining) { + sha384_remaining = remaining; + } + while digest.save(&mut sha384).is_none() {} all_done = false; } if !sha512_remaining.is_empty() { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha512); - sha512_remaining = block!(digest.update(sha512_remaining)).unwrap(); - block!(digest.save(&mut sha512)); + while let Some(remaining) = digest.update(sha512_remaining) { + sha512_remaining = remaining; + } + while digest.save(&mut sha512).is_none() {} all_done = false; } } @@ -311,17 +320,17 @@ mod tests { } let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha1); - block!(digest.finish(sha1_p.1)).unwrap(); + while digest.finish(sha1_p.1).is_none() {} let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha224); - block!(digest.finish(sha224_p.1)).unwrap(); + while digest.finish(sha224_p.1).is_none() {} let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha256); - block!(digest.finish(sha256_p.1)).unwrap(); + while digest.finish(sha256_p.1).is_none() {} #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha384); - block!(digest.finish(sha384_p.1)).unwrap(); + while digest.finish(sha384_p.1).is_none() {} let mut digest = ShaDigest::restore(&mut ctx.sha, &mut sha512); - block!(digest.finish(sha512_p.1)).unwrap(); + while digest.finish(sha512_p.1).is_none() {} } }); } diff --git a/hil-test/tests/twai.rs b/hil-test/tests/twai.rs index b244dd8d289..e0d78496e0c 100644 --- a/hil-test/tests/twai.rs +++ b/hil-test/tests/twai.rs @@ -11,7 +11,6 @@ use esp_hal::{ Blocking, }; use hil_test as _; -use nb::block; struct Context { twai: twai::Twai<'static, Blocking>, @@ -52,9 +51,13 @@ mod tests { #[test] fn test_send_receive(mut ctx: Context) { let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO, &[1, 2, 3]).unwrap(); - block!(ctx.twai.transmit(&frame)).unwrap(); + while ctx.twai.transmit(&frame).is_err() {} - let frame = block!(ctx.twai.receive()).unwrap(); + let frame = loop { + if let Ok(frame) = ctx.twai.receive() { + break frame; + } + }; assert_eq!(frame.data(), &[1, 2, 3]) } diff --git a/hil-test/tests/uart.rs b/hil-test/tests/uart.rs index 350d8986311..6f38c3bb17e 100644 --- a/hil-test/tests/uart.rs +++ b/hil-test/tests/uart.rs @@ -11,7 +11,6 @@ use esp_hal::{ Blocking, }; use hil_test as _; -use nb::block; struct Context { uart: Uart<'static, Blocking>, @@ -38,8 +37,13 @@ mod tests { #[test] fn test_send_receive(mut ctx: Context) { ctx.uart.write(0x42).ok(); - let read = block!(ctx.uart.read()); - assert_eq!(read, Ok(0x42)); + let read = loop { + if let Ok(byte) = ctx.uart.read() { + break byte; + } + }; + + assert_eq!(read, 0x42); } #[test] @@ -59,8 +63,8 @@ mod tests { buffer[i] = byte; i += 1; } - Err(nb::Error::WouldBlock) => continue, - Err(nb::Error::Other(_)) => panic!(), + Err(embedded_hal_nb::nb::Error::WouldBlock) => continue, + Err(embedded_hal_nb::nb::Error::Other(_)) => panic!(), } } @@ -93,8 +97,12 @@ mod tests { ) .unwrap(); ctx.uart.write(byte_to_write).ok(); - let read = block!(ctx.uart.read()); - assert_eq!(read, Ok(byte_to_write)); + let read = loop { + if let Ok(byte) = ctx.uart.read() { + break byte; + } + }; + assert_eq!(read, byte_to_write); byte_to_write = !byte_to_write; } } diff --git a/hil-test/tests/uart_regression.rs b/hil-test/tests/uart_regression.rs index de1d9ad0275..51c5c4a3613 100644 --- a/hil-test/tests/uart_regression.rs +++ b/hil-test/tests/uart_regression.rs @@ -13,7 +13,6 @@ mod tests { uart::{self, UartRx, UartTx}, }; use hil_test as _; - use nb::block; #[test] fn test_that_creating_tx_does_not_cause_a_pulse() { @@ -33,8 +32,12 @@ mod tests { tx.flush().unwrap(); tx.write_bytes(&[0x42]).unwrap(); - let read = block!(rx.read_byte()); + let read = loop { + if let Some(byte) = rx.read_byte() { + break byte; + } + }; - assert_eq!(read, Ok(0x42)); + assert_eq!(read, 0x42); } } diff --git a/hil-test/tests/uart_tx_rx.rs b/hil-test/tests/uart_tx_rx.rs index 10840df1c3d..f475143e693 100644 --- a/hil-test/tests/uart_tx_rx.rs +++ b/hil-test/tests/uart_tx_rx.rs @@ -10,7 +10,6 @@ use esp_hal::{ Blocking, }; use hil_test as _; -use nb::block; struct Context { rx: UartRx<'static, Blocking>, @@ -40,9 +39,13 @@ mod tests { ctx.tx.flush().unwrap(); ctx.tx.write_bytes(&byte).unwrap(); - let read = block!(ctx.rx.read_byte()); + let read = loop { + if let Some(byte) = ctx.rx.read_byte() { + break byte; + } + }; - assert_eq!(read, Ok(0x42)); + assert_eq!(read, 0x42); } #[test]