Remove OnDrop handler, start sampling in new

This commit is contained in:
Henrik Alsér 2022-05-12 15:35:32 +02:00
parent 0be9184efc
commit 93cbd079ec

View File

@ -11,7 +11,7 @@ use core::task::Poll;
use embassy::interrupt::InterruptExt; use embassy::interrupt::InterruptExt;
use embassy::util::Unborrow; use embassy::util::Unborrow;
use embassy::waitqueue::AtomicWaker; use embassy::waitqueue::AtomicWaker;
use embassy_hal_common::{drop::OnDrop, unborrow}; use embassy_hal_common::unborrow;
use futures::future::poll_fn; use futures::future::poll_fn;
/// Quadrature decoder /// Quadrature decoder
@ -128,6 +128,9 @@ impl<'d> Qdec<'d> {
// Enable peripheral // Enable peripheral
r.enable.write(|w| w.enable().set_bit()); r.enable.write(|w| w.enable().set_bit());
// Start sampling
unsafe { r.tasks_start.write(|w| w.bits(1)) };
irq.disable(); irq.disable();
irq.set_handler(|_| { irq.set_handler(|_| {
let r = Self::regs(); let r = Self::regs();
@ -155,16 +158,8 @@ impl<'d> Qdec<'d> {
/// let delta = q.read().await; /// let delta = q.read().await;
/// ``` /// ```
pub async fn read(&mut self) -> i16 { pub async fn read(&mut self) -> i16 {
// In case the future is dropped, stop the task and reset events.
let on_drop = OnDrop::new(|| {
let t = Self::regs();
t.tasks_stop.write(|w| unsafe { w.bits(1) });
t.events_reportrdy.reset();
});
let t = Self::regs(); let t = Self::regs();
t.intenset.write(|w| w.reportrdy().set()); t.intenset.write(|w| w.reportrdy().set());
unsafe { t.tasks_start.write(|w| w.bits(1)) };
unsafe { t.tasks_readclracc.write(|w| w.bits(1)) }; unsafe { t.tasks_readclracc.write(|w| w.bits(1)) };
let value = poll_fn(|cx| { let value = poll_fn(|cx| {
@ -178,7 +173,6 @@ impl<'d> Qdec<'d> {
} }
}) })
.await; .await;
on_drop.defuse();
value value
} }