From 36319fc121f19f86dded45b6fb93aed7c3f4ae33 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sun, 5 Mar 2023 22:09:54 +0100 Subject: [PATCH] nrf/temp: switch to new interrupt binding. --- embassy-nrf/src/temp.rs | 35 +++++++++++++++++++++---------- examples/nrf52840/src/bin/temp.rs | 9 +++++--- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/embassy-nrf/src/temp.rs b/embassy-nrf/src/temp.rs index 5298faabc..3a75ec629 100644 --- a/embassy-nrf/src/temp.rs +++ b/embassy-nrf/src/temp.rs @@ -3,6 +3,7 @@ use core::future::poll_fn; use core::task::Poll; +use embassy_cortex_m::interrupt::Interrupt; use embassy_hal_common::drop::OnDrop; use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_sync::waitqueue::AtomicWaker; @@ -12,27 +13,39 @@ use crate::interrupt::InterruptExt; use crate::peripherals::TEMP; use crate::{interrupt, pac, Peripheral}; +/// Interrupt handler. +pub struct InterruptHandler { + _private: (), +} + +impl interrupt::Handler 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. pub struct Temp<'d> { - _irq: PeripheralRef<'d, interrupt::TEMP>, + _peri: PeripheralRef<'d, TEMP>, } static WAKER: AtomicWaker = AtomicWaker::new(); impl<'d> Temp<'d> { /// Create a new temperature sensor driver. - pub fn new(_t: impl Peripheral

+ 'd, irq: impl Peripheral

+ 'd) -> Self { - into_ref!(_t, irq); + pub fn new( + _peri: impl Peripheral

+ 'd, + _irq: impl interrupt::Binding + 'd, + ) -> Self { + into_ref!(_peri); // Enable interrupt that signals temperature values - irq.disable(); - irq.set_handler(|_| { - let t = Self::regs(); - t.intenclr.write(|w| w.datardy().clear()); - WAKER.wake(); - }); - irq.enable(); - Self { _irq: irq } + unsafe { interrupt::TEMP::steal() }.unpend(); + unsafe { interrupt::TEMP::steal() }.enable(); + + Self { _peri } } /// Perform an asynchronous temperature measurement. The returned future diff --git a/examples/nrf52840/src/bin/temp.rs b/examples/nrf52840/src/bin/temp.rs index b06ac709e..70957548f 100644 --- a/examples/nrf52840/src/bin/temp.rs +++ b/examples/nrf52840/src/bin/temp.rs @@ -4,16 +4,19 @@ use defmt::info; use embassy_executor::Spawner; -use embassy_nrf::interrupt; use embassy_nrf::temp::Temp; +use embassy_nrf::{bind_interrupts, temp}; use embassy_time::{Duration, Timer}; use {defmt_rtt as _, panic_probe as _}; +bind_interrupts!(struct Irqs { + TEMP => temp::InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) { let p = embassy_nrf::init(Default::default()); - let irq = interrupt::take!(TEMP); - let mut temp = Temp::new(p.TEMP, irq); + let mut temp = Temp::new(p.TEMP, Irqs); loop { let value = temp.read().await;