Skip to content

Commit

Permalink
Expose timer methods without embedded-hal 0.2 traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Mar 6, 2024
1 parent b743642 commit 845658f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
1 change: 0 additions & 1 deletion examples/ppi-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use {
},
cortex_m::interrupt::Mutex,
cortex_m_rt::entry,
embedded_hal::timer::CountDown,
hal::{
pac::{interrupt, Interrupt, RADIO},
ppi,
Expand Down
5 changes: 1 addition & 4 deletions examples/wdt-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
#![no_std]
#![no_main]

use embedded_hal::{
digital::v2::{InputPin, OutputPin},
timer::CountDown,
};
use embedded_hal::digital::v2::{InputPin, OutputPin};
use {
core::panic::PanicInfo,
hal::{
Expand Down
4 changes: 1 addition & 3 deletions nrf-hal-common/src/ieee802154.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use core::{
sync::atomic::{self, Ordering},
};

use embedded_hal_02::timer::CountDown as _;

use crate::{
clocks::{Clocks, ExternalOscillator},
pac::{
Expand Down Expand Up @@ -366,7 +364,7 @@ impl<'c> Radio<'c> {
break;
}

if timer.wait().is_ok() {
if timer.reset_if_finished() {
// timeout
break;
}
Expand Down
47 changes: 32 additions & 15 deletions nrf-hal-common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use crate::pac::{
#[cfg(feature = "embedded-hal-02")]
use cast::u32;
use embedded_hal::delay::DelayNs;
use nb::{self, block};
use void::{unreachable, Void};

#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
use crate::pac::{TIMER3, TIMER4};
Expand All @@ -41,7 +39,7 @@ use crate::pac::timer0::{
RegisterBlock as RegBlock3, EVENTS_COMPARE as EventsCompare3, TASKS_CAPTURE as TasksCapture3,
};

use core::marker::PhantomData;
use core::{hint::spin_loop, marker::PhantomData};

pub struct OneShot;
pub struct Periodic;
Expand Down Expand Up @@ -142,11 +140,34 @@ where
self.0.disable_interrupt();
}

/// Starts the timer.
///
/// The timer will run for the given number of cycles, then it will stop and
/// reset.
pub fn start(&mut self, cycles: u32) {
self.0.timer_start(cycles)
}

/// If the timer has finished, resets it and returns true.
///
/// Returns false if the timer is still running.
pub fn reset_if_finished(&mut self) -> bool {
if self.0.timer_running() {
// EVENTS_COMPARE has not been triggered yet
return false;
}

self.0.timer_reset_event();

true
}

/// Starts the timer for the given number of cycles and waits for it to
/// finish.
pub fn delay(&mut self, cycles: u32) {
self.start(cycles);
match block!(self.wait()) {
Ok(_) => {}
Err(x) => unreachable(x),
while !self.reset_if_finished() {
spin_loop();
}
}

Expand Down Expand Up @@ -265,16 +286,12 @@ where
///
/// To block until the timer has stopped, use the `block!` macro from the
/// `nb` crate. Please refer to the documentation of `nb` for other options.
fn wait(&mut self) -> nb::Result<(), Void> {
if self.0.timer_running() {
// EVENTS_COMPARE has not been triggered yet
return Err(nb::Error::WouldBlock);
fn wait(&mut self) -> nb::Result<(), void::Void> {
if self.reset_if_finished() {
Ok(())
} else {
Err(nb::Error::WouldBlock)
}

// Reset the event, otherwise it will always read `1` from now on.
self.0.timer_reset_event();

Ok(())
}
}

Expand Down
3 changes: 1 addition & 2 deletions nrf-hal-common/src/uarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use core::hint::spin_loop;
use core::ops::Deref;
use core::sync::atomic::{compiler_fence, Ordering::SeqCst};
use embedded_hal::digital::OutputPin;
use embedded_hal_02::timer::CountDown as _;
use embedded_io::{ErrorKind, ErrorType, ReadReady, Write as _, WriteReady};

#[cfg(any(feature = "52833", feature = "52840"))]
Expand Down Expand Up @@ -261,7 +260,7 @@ where

loop {
event_complete |= self.0.events_endrx.read().bits() != 0;
timeout_occured |= timer.wait().is_ok();
timeout_occured |= timer.reset_if_finished();
if event_complete || timeout_occured {
break;
}
Expand Down

0 comments on commit 845658f

Please sign in to comment.