feat: Avoid using nb in uart and twai. Udpate examples and tests to avoid using block!

This commit is contained in:
Sergio Gasquez 2025-01-03 15:05:36 +01:00
parent b387472fbc
commit c49720f569
26 changed files with 223 additions and 167 deletions

View File

@ -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"

View File

@ -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);
//! }

View File

@ -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,
@ -176,8 +174,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);

View File

@ -81,10 +81,13 @@
/// Attenuation::Attenuation11dB
/// );
/// let mut adc1 = Adc::<ADC1>::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;

View File

@ -1,5 +1,3 @@
use core::convert::Infallible;
use crate::rsa::{
implement_op,
Multi,

View File

@ -1,5 +1,3 @@
use core::convert::Infallible;
use crate::rsa::{
implement_op,
Multi,

View File

@ -1,5 +1,3 @@
use core::convert::Infallible;
use crate::rsa::{
implement_op,
Multi,

View File

@ -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::<Sha256>();
@ -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<Sha<'d>>> 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<Sha<'d>>> digest::OutputSizeUser for ShaD
impl<'d, A: ShaAlgorithm, S: BorrowMut<Sha<'d>>> 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<Sha<'d>>> digest::Update for ShaDigest<'d
#[cfg(feature = "digest")]
impl<'d, A: ShaAlgorithm, S: BorrowMut<Sha<'d>>> digest::FixedOutput for ShaDigest<'d, A, S> {
fn finalize_into(mut self, out: &mut digest::Output<Self>) {
nb::block!(self.finish(out)).unwrap();
while self.finish(out).is_none() {}
}
}

View File

@ -2138,14 +2138,16 @@ mod ehal1 {
where
Dm: DriverMode,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.driver().read_byte().ok_or(nb::Error::WouldBlock)
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
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(()))
}
}

View File

@ -35,7 +35,7 @@
//!
//! periodic.start(1.secs());
//! loop {
//! nb::block!(periodic.wait());
//! while periodic.wait().is_none() {}
//! }
//! # }
//! ```

View File

@ -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 {}
//! # }
@ -1140,12 +1146,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<EspTwaiFrame, EspTwaiError> {
pub fn receive(&mut self) -> Result<EspTwaiFrame, EspTwaiError> {
self.rx.receive()
}
@ -1179,17 +1185,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);
@ -1210,28 +1217,26 @@ where
Dm: DriverMode,
{
/// Receive a frame
pub fn receive(&mut self) -> nb::Result<EspTwaiFrame, EspTwaiError> {
pub fn receive(&mut self) -> Result<EspTwaiFrame, 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 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)
}
}
@ -1247,6 +1252,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"))]
@ -1302,18 +1309,21 @@ where
type Error = EspTwaiError;
/// Transmit a frame.
fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
fn transmit(
&mut self,
frame: &Self::Frame,
) -> embedded_hal_nb::nb::Result<Option<Self::Frame>, 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::Frame, Self::Error> {
self.rx.receive()
fn receive(&mut self) -> embedded_hal_nb::nb::Result<Self::Frame, Self::Error> {
Ok(self.rx.receive()?)
}
}

View File

@ -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();
@ -645,21 +645,24 @@ where
pub fn write_bytes(&mut self, data: &[u8]) -> Result<usize, Error> {
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
}
}
@ -676,11 +679,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
}
}
@ -827,7 +830,7 @@ where
}
/// Read a byte from the UART
pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
pub fn read_byte(&mut self) -> Option<u8> {
cfg_if::cfg_if! {
if #[cfg(esp32s2)] {
// On the ESP32-S2 we need to use PeriBus2 to read the FIFO:
@ -850,9 +853,9 @@ where
}
}
Ok(byte)
Some(byte)
} else {
Err(nb::Error::WouldBlock)
None
}
}
@ -861,7 +864,7 @@ where
pub fn read_bytes(&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 {
@ -1134,17 +1137,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<u8, Error> {
pub fn read_byte(&mut self) -> Option<u8> {
self.rx.read_byte()
}
@ -1326,8 +1329,9 @@ impl<Dm> embedded_hal_nb::serial::Read for Uart<'_, Dm>
where
Dm: DriverMode,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
self.read_byte()
.ok_or(embedded_hal_nb::nb::Error::WouldBlock)
}
}
@ -1335,8 +1339,9 @@ impl<Dm> embedded_hal_nb::serial::Read for UartRx<'_, Dm>
where
Dm: DriverMode,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
self.read_byte()
.ok_or(embedded_hal_nb::nb::Error::WouldBlock)
}
}
@ -1344,12 +1349,13 @@ impl<Dm> embedded_hal_nb::serial::Write for Uart<'_, 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> {
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)
}
}
@ -1357,12 +1363,13 @@ impl<Dm> embedded_hal_nb::serial::Write for UartTx<'_, 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> {
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)
}
}
@ -1464,9 +1471,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 */ }
}
}

View File

@ -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<Dm> embedded_hal_nb::serial::Read for UsbSerialJtag<'_, Dm>
where
Dm: DriverMode,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
embedded_hal_nb::serial::Read::read(&mut self.rx)
}
}
@ -577,8 +577,9 @@ impl<Dm> embedded_hal_nb::serial::Read for UsbSerialJtagRx<'_, Dm>
where
Dm: DriverMode,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.read_byte().ok_or(nb::Error::WouldBlock)
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
self.read_byte()
.ok_or(embedded_hal_nb::nb::Error::WouldBlock)
}
}
@ -586,11 +587,11 @@ impl<Dm> 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<Dm> 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)
}
}

View File

@ -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<u8, Error> {
pub fn read_byte(&mut self) -> Option<u8> {
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<usize, Error> {
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<u8, Self::Error> {
fn read(&mut self) -> embedded_hal_nb::nb::Result<u8, Self::Error> {
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 */ }
}
}

View File

@ -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<Sha256>;
@ -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");

View File

@ -39,7 +39,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;
}
@ -74,7 +78,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();
}

View File

@ -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");
}
}

View File

@ -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 }

View File

@ -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);

View File

@ -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 }
}

View File

@ -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 }
}

View File

@ -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<D: Digest>(input: &[u8], expected_output: &[u8]) {
fn hash_sha<S: ShaAlgorithm>(sha: &mut Sha<'static>, mut input: &[u8], output: &mut [u8]) {
let mut digest = sha.start::<S>();
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() {}
}
});
}

View File

@ -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])
}

View File

@ -11,7 +11,6 @@ use esp_hal::{
Blocking,
};
use hil_test as _;
use nb::block;
struct Context {
uart: Uart<'static, Blocking>,
@ -39,8 +38,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]
@ -60,8 +64,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!(),
}
}
@ -94,8 +98,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;
}
}

View File

@ -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() {
@ -37,8 +36,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);
}
}

View File

@ -10,7 +10,6 @@ use esp_hal::{
Blocking,
};
use hil_test as _;
use nb::block;
struct Context {
rx: UartRx<'static, Blocking>,
@ -44,9 +43,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]