Skip to content

Commit

Permalink
feat: Only remove nb from stabilizing drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Jan 9, 2025
1 parent 6c91fb4 commit 4652d56
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 161 deletions.
2 changes: 2 additions & 0 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ 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"
Expand Down
8 changes: 4 additions & 4 deletions esp-hal/src/analog/adc/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ where
/// This method takes an [AdcPin](super::AdcPin) reference, as it is
/// expected that the ADC will be able to sample whatever channel
/// underlies the pin.
pub fn read_oneshot<PIN>(&mut self, _pin: &mut super::AdcPin<PIN, ADCI>) -> Option<u16>
pub fn read_oneshot<PIN>(&mut self, _pin: &mut super::AdcPin<PIN, ADCI>) -> nb::Result<u16, ()>
where
PIN: super::AdcChannel,
{
Expand All @@ -301,7 +301,7 @@ where
// - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress
if active_channel != PIN::CHANNEL {
return None;
return Err(nb::Error::WouldBlock);
}
} else {
// If no conversions are in progress, start a new one for given channel
Expand All @@ -316,7 +316,7 @@ where
// Wait for ADC to finish conversion
let conversion_finished = ADCI::read_done_sar();
if !conversion_finished {
return None;
return Err(nb::Error::WouldBlock);
}

// Get converted value
Expand All @@ -325,7 +325,7 @@ where
// Mark that no conversions are currently in progress
self.active_channel = None;

Some(converted_value)
Ok(converted_value)
}
}

Expand Down
6 changes: 1 addition & 5 deletions esp-hal/src/analog/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
//! let mut delay = Delay::new();
//!
//! loop {
//! let pin_value: u16 = loop {
//! if let Some(value) = adc1.read_oneshot(&mut pin) {
//! break value;
//! }
//! };
//! let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut pin)).unwrap();
//!
//! delay.delay_millis(1500);
//! }
Expand Down
11 changes: 7 additions & 4 deletions esp-hal/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@ where
/// This method takes an [AdcPin](super::AdcPin) reference, as it is
/// expected that the ADC will be able to sample whatever channel
/// underlies the pin.
pub fn read_oneshot<PIN, CS>(&mut self, pin: &mut super::AdcPin<PIN, ADCI, CS>) -> Option<u16>
pub fn read_oneshot<PIN, CS>(
&mut self,
pin: &mut super::AdcPin<PIN, ADCI, CS>,
) -> nb::Result<u16, ()>
where
PIN: super::AdcChannel,
CS: super::AdcCalScheme<ADCI>,
Expand All @@ -447,7 +450,7 @@ where
// - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress
if active_channel != PIN::CHANNEL {
return None;
return Err(nb::Error::WouldBlock);
}
} else {
// If no conversions are in progress, start a new one for given channel
Expand All @@ -473,7 +476,7 @@ where
// Wait for ADC to finish conversion
let conversion_finished = ADCI::is_done();
if !conversion_finished {
return None;
return Err(nb::Error::WouldBlock);
}

// Get converted value
Expand All @@ -496,7 +499,7 @@ where
// Mark that no conversions are currently in progress
self.active_channel = None;

Some(converted_value)
Ok(converted_value)
}
}

Expand Down
11 changes: 7 additions & 4 deletions esp-hal/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,10 @@ where
/// This method takes an [AdcPin](super::AdcPin) reference, as it is
/// expected that the ADC will be able to sample whatever channel
/// underlies the pin.
pub fn read_oneshot<PIN, CS>(&mut self, pin: &mut super::AdcPin<PIN, ADCI, CS>) -> Option<u16>
pub fn read_oneshot<PIN, CS>(
&mut self,
pin: &mut super::AdcPin<PIN, ADCI, CS>,
) -> nb::Result<u16, ()>
where
PIN: super::AdcChannel,
CS: super::AdcCalScheme<ADCI>,
Expand All @@ -512,7 +515,7 @@ where
// - if it's for a different channel try again later
// - if it's for the given channel, go ahead and check progress
if active_channel != PIN::CHANNEL {
return None;
return Err(nb::Error::WouldBlock);
}
} else {
// If no conversions are in progress, start a new one for given channel
Expand All @@ -524,7 +527,7 @@ where
// Wait for ADC to finish conversion
let conversion_finished = ADCI::is_done();
if !conversion_finished {
return None;
return Err(nb::Error::WouldBlock);
}

// Get converted value
Expand All @@ -537,7 +540,7 @@ where
// Mark that no conversions are currently in progress
self.active_channel = None;

Some(converted_value)
Ok(converted_value)
}

fn start_sample<PIN, CS>(&mut self, pin: &mut AdcPin<PIN, ADCI, CS>)
Expand Down
41 changes: 27 additions & 14 deletions esp-hal/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
//!
//! [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,
Expand Down Expand Up @@ -131,7 +133,7 @@ impl<'d> Hmac<'d> {
}

/// Step 2. Configure HMAC keys and key purposes.
pub fn configure(&mut self, m: HmacPurpose, key_id: KeyId) -> Result<(), Error> {
pub fn configure(&mut self, m: HmacPurpose, key_id: KeyId) -> nb::Result<(), Error> {
self.hmac
.set_para_purpose()
.write(|w| unsafe { w.purpose_set().bits(m as u8) });
Expand All @@ -143,33 +145,39 @@ impl<'d> Hmac<'d> {
.write(|w| w.set_para_end().set_bit());

if self.hmac.query_error().read().query_check().bit_is_set() {
return Err(Error::KeyPurposeMismatch);
return Err(nb::Error::Other(Error::KeyPurposeMismatch));
}

Ok(())
}

/// Process the msg block after block
pub fn update<'a>(&mut self, msg: &'a [u8]) -> &'a [u8] {
while (*self).is_busy() {}
///
/// Call this function as many times as necessary (msg.len() > 0)
pub fn update<'a>(&mut self, msg: &'a [u8]) -> nb::Result<&'a [u8], Infallible> {
if self.is_busy() {
return Err(nb::Error::WouldBlock);
}

self.next_command();

let remaining = self.write_data(msg);
let remaining = self.write_data(msg).unwrap();

remaining
Ok(remaining)
}

/// Finalizes the HMAC computation and retrieves the resulting hash output.
pub fn finalize(&mut self, output: &mut [u8]) {
while (*self).is_busy() {}
pub fn finalize(&mut self, output: &mut [u8]) -> nb::Result<(), Infallible> {
if self.is_busy() {
return Err(nb::Error::WouldBlock);
}

self.next_command();

let msg_len = self.byte_written as u64;

self.write_data(&[0x80]);
self.flush_data();
nb::block!(self.write_data(&[0x80])).unwrap();
nb::block!(self.flush_data()).unwrap();
self.next_command();
debug_assert!(self.byte_written % 4 == 0);

Expand All @@ -196,6 +204,7 @@ impl<'d> Hmac<'d> {
.write(|w| w.set_result_end().set_bit());
self.byte_written = 64;
self.next_command = NextCommand::None;
Ok(())
}

fn is_busy(&mut self) -> bool {
Expand All @@ -219,7 +228,7 @@ impl<'d> Hmac<'d> {
self.next_command = NextCommand::None;
}

fn write_data<'a>(&mut self, incoming: &'a [u8]) -> &'a [u8] {
fn write_data<'a>(&mut self, incoming: &'a [u8]) -> nb::Result<&'a [u8], Infallible> {
let mod_length = self.byte_written % 64;

let (remaining, bound_reached) = self.alignment_helper.aligned_volatile_copy(
Expand Down Expand Up @@ -248,11 +257,13 @@ impl<'d> Hmac<'d> {
}
}

remaining
Ok(remaining)
}

fn flush_data(&mut self) {
while self.is_busy() {}
fn flush_data(&mut self) -> nb::Result<(), Infallible> {
if self.is_busy() {
return Err(nb::Error::WouldBlock);
}

let flushed = self.alignment_helper.flush_to(
#[cfg(esp32s2)]
Expand All @@ -270,6 +281,8 @@ impl<'d> Hmac<'d> {
while self.is_busy() {}
self.next_command = NextCommand::MessagePad;
}

Ok(())
}

fn padding(&mut self, msg_len: u64) {
Expand Down
7 changes: 2 additions & 5 deletions esp-hal/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@
/// Attenuation::Attenuation11dB
/// );
/// let mut adc1 = Adc::<ADC1>::new(peripherals.ADC1, adc1_config);
/// let pin_value: u16 = loop {
/// if let Some(value) = adc1.read_oneshot(&mut adc1_pin) {
/// break value;
/// }
/// };
/// let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap();
/// 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;
Expand Down
9 changes: 7 additions & 2 deletions esp-hal/src/rsa/esp32.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::convert::Infallible;

use crate::rsa::{
implement_op,
Multi,
Expand All @@ -12,8 +14,11 @@ impl<Dm: crate::DriverMode> Rsa<'_, Dm> {
/// After the RSA Accelerator is released from reset, the memory blocks
/// needs to be initialized, only after that peripheral should be used.
/// This function would return without an error if the memory is initialized
pub fn ready(&mut self) {
while !self.rsa.clean().read().clean().bit_is_clear() {}
pub fn ready(&mut self) -> nb::Result<(), Infallible> {
if self.rsa.clean().read().clean().bit_is_clear() {
return Err(nb::Error::WouldBlock);
}
Ok(())
}

/// Writes the multi-mode configuration to the RSA hardware.
Expand Down
9 changes: 7 additions & 2 deletions esp-hal/src/rsa/esp32cX.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::convert::Infallible;

use crate::rsa::{
implement_op,
Multi,
Expand All @@ -12,8 +14,11 @@ impl<Dm: crate::DriverMode> Rsa<'_, Dm> {
/// After the RSA Accelerator is released from reset, the memory blocks
/// needs to be initialized, only after that peripheral should be used.
/// This function would return without an error if the memory is initialized
pub fn ready(&mut self) {
while !self.rsa.query_clean().read().query_clean().bit_is_clear() {}
pub fn ready(&mut self) -> nb::Result<(), Infallible> {
if self.rsa.query_clean().read().query_clean().bit_is_clear() {
return Err(nb::Error::WouldBlock);
}
Ok(())
}

/// Enables/disables rsa interrupt.
Expand Down
9 changes: 7 additions & 2 deletions esp-hal/src/rsa/esp32sX.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::convert::Infallible;

use crate::rsa::{
implement_op,
Multi,
Expand All @@ -13,8 +15,11 @@ impl<Dm: crate::DriverMode> Rsa<'_, Dm> {
/// needs to be initialized, only after that peripheral should be used.
/// This function would return without an error if the memory is
/// initialized.
pub fn ready(&mut self) {
while !self.rsa.clean().read().clean().bit_is_clear() {}
pub fn ready(&mut self) -> nb::Result<(), Infallible> {
if self.rsa.clean().read().clean().bit_is_clear() {
return Err(nb::Error::WouldBlock);
}
Ok(())
}

/// Enables/disables rsa interrupt.
Expand Down
Loading

0 comments on commit 4652d56

Please sign in to comment.