From c692a97b654adc50f997852a360ce3277cb73db4 Mon Sep 17 00:00:00 2001 From: Matthew Tran <0e4ef622@gmail.com> Date: Sun, 21 Sep 2025 23:01:03 -0500 Subject: nrf: impl Drop for Timer --- embassy-nrf/src/timer.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'embassy-nrf/src') diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index de2875765..b6a77bd2e 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs @@ -104,7 +104,7 @@ impl<'d, T: Instance> Timer<'d, T> { Self::new_inner(timer, true) } - fn new_inner(timer: Peri<'d, T>, _is_counter: bool) -> Self { + fn new_inner(timer: Peri<'d, T>, is_counter: bool) -> Self { let regs = T::regs(); let this = Self { _p: timer }; @@ -114,7 +114,7 @@ impl<'d, T: Instance> Timer<'d, T> { this.stop(); regs.mode().write(|w| { - w.set_mode(match _is_counter { + w.set_mode(match is_counter { #[cfg(not(feature = "_nrf51"))] true => vals::Mode::LOW_POWER_COUNTER, #[cfg(feature = "_nrf51")] @@ -218,6 +218,12 @@ impl<'d, T: Instance> Timer<'d, T> { } } +impl<'d, T: Instance> Drop for Timer<'d, T> { + fn drop(&mut self) { + self.stop(); + } +} + /// A representation of a timer's Capture/Compare (CC) register. /// /// A CC register holds a 32-bit value. -- cgit From d463a57879c0e02c188f63ab99c40d6ab91ea54e Mon Sep 17 00:00:00 2001 From: Matthew Tran <0e4ef622@gmail.com> Date: Sun, 21 Sep 2025 23:01:11 -0500 Subject: nrf: add persist() method for gpiote and timer --- embassy-nrf/src/gpiote.rs | 18 ++++++++++++++++++ embassy-nrf/src/timer.rs | 9 +++++++++ 2 files changed, 27 insertions(+) (limited to 'embassy-nrf/src') diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index d169b49f9..43e43f0bf 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -193,6 +193,15 @@ pub struct InputChannel<'d> { pin: Input<'d>, } +impl InputChannel<'static> { + /// Persist the channel's configuration for the rest of the program's lifetime. This method + /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents + /// accidental reuse of the underlying peripheral. + pub fn persist(self) { + core::mem::forget(self); + } +} + impl<'d> Drop for InputChannel<'d> { fn drop(&mut self) { let g = regs(); @@ -263,6 +272,15 @@ pub struct OutputChannel<'d> { _pin: Output<'d>, } +impl OutputChannel<'static> { + /// Persist the channel's configuration for the rest of the program's lifetime. This method + /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents + /// accidental reuse of the underlying peripheral. + pub fn persist(self) { + core::mem::forget(self); + } +} + impl<'d> Drop for OutputChannel<'d> { fn drop(&mut self) { let g = regs(); diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index b6a77bd2e..5d6afe49b 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs @@ -218,6 +218,15 @@ impl<'d, T: Instance> Timer<'d, T> { } } +impl Timer<'static, T> { + /// Persist the timer's configuration for the rest of the program's lifetime. This method + /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents + /// accidental reuse of the underlying peripheral. + pub fn persist(self) { + core::mem::forget(self); + } +} + impl<'d, T: Instance> Drop for Timer<'d, T> { fn drop(&mut self) { self.stop(); -- cgit