nrf/temp: switch to new interrupt binding.

This commit is contained in:
Dario Nieuwenhuis 2023-03-05 22:09:54 +01:00
parent 9e58d9274c
commit 36319fc121
2 changed files with 30 additions and 14 deletions

View File

@ -3,6 +3,7 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::task::Poll; use core::task::Poll;
use embassy_cortex_m::interrupt::Interrupt;
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker; use embassy_sync::waitqueue::AtomicWaker;
@ -12,27 +13,39 @@ use crate::interrupt::InterruptExt;
use crate::peripherals::TEMP; use crate::peripherals::TEMP;
use crate::{interrupt, pac, Peripheral}; use crate::{interrupt, pac, Peripheral};
/// Interrupt handler.
pub struct InterruptHandler {
_private: (),
}
impl interrupt::Handler<interrupt::TEMP> for InterruptHandler {
unsafe fn on_interrupt() {
let r = unsafe { &*pac::TEMP::PTR };
r.intenclr.write(|w| w.datardy().clear());
WAKER.wake();
}
}
/// Builtin temperature sensor driver. /// Builtin temperature sensor driver.
pub struct Temp<'d> { pub struct Temp<'d> {
_irq: PeripheralRef<'d, interrupt::TEMP>, _peri: PeripheralRef<'d, TEMP>,
} }
static WAKER: AtomicWaker = AtomicWaker::new(); static WAKER: AtomicWaker = AtomicWaker::new();
impl<'d> Temp<'d> { impl<'d> Temp<'d> {
/// Create a new temperature sensor driver. /// Create a new temperature sensor driver.
pub fn new(_t: impl Peripheral<P = TEMP> + 'd, irq: impl Peripheral<P = interrupt::TEMP> + 'd) -> Self { pub fn new(
into_ref!(_t, irq); _peri: impl Peripheral<P = TEMP> + 'd,
_irq: impl interrupt::Binding<interrupt::TEMP, InterruptHandler> + 'd,
) -> Self {
into_ref!(_peri);
// Enable interrupt that signals temperature values // Enable interrupt that signals temperature values
irq.disable(); unsafe { interrupt::TEMP::steal() }.unpend();
irq.set_handler(|_| { unsafe { interrupt::TEMP::steal() }.enable();
let t = Self::regs();
t.intenclr.write(|w| w.datardy().clear()); Self { _peri }
WAKER.wake();
});
irq.enable();
Self { _irq: irq }
} }
/// Perform an asynchronous temperature measurement. The returned future /// Perform an asynchronous temperature measurement. The returned future

View File

@ -4,16 +4,19 @@
use defmt::info; use defmt::info;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_nrf::interrupt;
use embassy_nrf::temp::Temp; use embassy_nrf::temp::Temp;
use embassy_nrf::{bind_interrupts, temp};
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
TEMP => temp::InterruptHandler;
});
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default()); let p = embassy_nrf::init(Default::default());
let irq = interrupt::take!(TEMP); let mut temp = Temp::new(p.TEMP, Irqs);
let mut temp = Temp::new(p.TEMP, irq);
loop { loop {
let value = temp.read().await; let value = temp.read().await;