diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 7c5f8d32b..d4d4ab8d8 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml @@ -48,6 +48,7 @@ embedded-dma = "0.1.2" futures = { version = "0.3.17", default-features = false } critical-section = "0.2.3" rand_core = "0.6.3" +fixed = "1.10.0" nrf52805-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } nrf52810-pac = { version = "0.10.1", optional = true, features = [ "rt" ] } diff --git a/embassy-nrf/src/chips/nrf52805.rs b/embassy-nrf/src/chips/nrf52805.rs index db1fac2ff..317e6ed66 100644 --- a/embassy-nrf/src/chips/nrf52805.rs +++ b/embassy-nrf/src/chips/nrf52805.rs @@ -114,6 +114,9 @@ embassy_hal_common::peripherals! { P0_29, P0_30, P0_31, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52810.rs b/embassy-nrf/src/chips/nrf52810.rs index 06b9bfb38..b26f30cbf 100644 --- a/embassy-nrf/src/chips/nrf52810.rs +++ b/embassy-nrf/src/chips/nrf52810.rs @@ -117,6 +117,9 @@ embassy_hal_common::peripherals! { P0_29, P0_30, P0_31, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52811.rs b/embassy-nrf/src/chips/nrf52811.rs index 2f8d98b31..9fbe3594e 100644 --- a/embassy-nrf/src/chips/nrf52811.rs +++ b/embassy-nrf/src/chips/nrf52811.rs @@ -117,6 +117,9 @@ embassy_hal_common::peripherals! { P0_29, P0_30, P0_31, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52820.rs b/embassy-nrf/src/chips/nrf52820.rs index fc7a367ef..abbdba7ac 100644 --- a/embassy-nrf/src/chips/nrf52820.rs +++ b/embassy-nrf/src/chips/nrf52820.rs @@ -112,6 +112,9 @@ embassy_hal_common::peripherals! { P0_29, P0_30, P0_31, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52832.rs b/embassy-nrf/src/chips/nrf52832.rs index 47cf27de4..de6dd7a8c 100644 --- a/embassy-nrf/src/chips/nrf52832.rs +++ b/embassy-nrf/src/chips/nrf52832.rs @@ -124,6 +124,9 @@ embassy_hal_common::peripherals! { P0_29, P0_30, P0_31, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52833.rs b/embassy-nrf/src/chips/nrf52833.rs index b3d813e24..c5e1c9d33 100644 --- a/embassy-nrf/src/chips/nrf52833.rs +++ b/embassy-nrf/src/chips/nrf52833.rs @@ -144,6 +144,9 @@ embassy_hal_common::peripherals! { P1_13, P1_14, P1_15, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index 473036f61..76cabe6f1 100644 --- a/embassy-nrf/src/chips/nrf52840.rs +++ b/embassy-nrf/src/chips/nrf52840.rs @@ -147,6 +147,9 @@ embassy_hal_common::peripherals! { P1_13, P1_14, P1_15, + + // TEMP + TEMP, } impl_uarte!(UARTE0, UARTE0, UARTE0_UART0); diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 00c719a17..961a97ce9 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs @@ -39,6 +39,8 @@ pub mod rng; #[cfg(not(feature = "nrf52820"))] pub mod saadc; pub mod spim; +#[cfg(not(feature = "nrf9160"))] +pub mod temp; pub mod timer; pub mod twim; pub mod uarte; diff --git a/embassy-nrf/src/temp.rs b/embassy-nrf/src/temp.rs new file mode 100644 index 000000000..f7c6e6609 --- /dev/null +++ b/embassy-nrf/src/temp.rs @@ -0,0 +1,86 @@ +//! Temperature sensor interface. + +use crate::interrupt; +use crate::pac; +use crate::peripherals::TEMP; + +use core::marker::PhantomData; +use core::task::Poll; +use embassy::interrupt::InterruptExt; +use embassy::util::Unborrow; +use embassy::waitqueue::AtomicWaker; +use embassy_hal_common::{drop::OnDrop, unborrow}; +use fixed::types::I30F2; +use futures::future::poll_fn; + +/// Integrated temperature sensor. +pub struct Temp<'d> { + _temp: PhantomData<&'d TEMP>, + _irq: interrupt::TEMP, +} + +static WAKER: AtomicWaker = AtomicWaker::new(); + +impl<'d> Temp<'d> { + pub fn new( + _t: impl Unborrow + 'd, + irq: impl Unborrow + 'd, + ) -> Self { + unborrow!(_t, irq); + + // 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 { + _temp: PhantomData, + _irq: irq, + } + } + + /// Perform an asynchronous temperature measurement. The returned future + /// can be awaited to obtain the measurement. + /// + /// If the future is dropped, the measurement is cancelled. + /// + /// # Example + /// + /// ```no_run + /// let mut t = Temp::new(p.TEMP, interrupt::take!(TEMP)); + /// let v: u16 = t.read().await.to_num::(); + /// ``` + pub async fn read(&mut self) -> I30F2 { + // 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_datardy.reset(); + }); + + let t = Self::regs(); + t.intenset.write(|w| w.datardy().set()); + unsafe { t.tasks_start.write(|w| w.bits(1)) }; + + let value = poll_fn(|cx| { + WAKER.register(cx.waker()); + if t.events_datardy.read().bits() == 0 { + return Poll::Pending; + } else { + t.events_datardy.reset(); + let raw = t.temp.read().bits(); + Poll::Ready(I30F2::from_bits(raw as i32)) + } + }) + .await; + on_drop.defuse(); + value + } + + fn regs() -> &'static pac::temp::RegisterBlock { + unsafe { &*pac::TEMP::ptr() } + } +} diff --git a/examples/nrf/src/bin/temp.rs b/examples/nrf/src/bin/temp.rs new file mode 100644 index 000000000..af9775f5a --- /dev/null +++ b/examples/nrf/src/bin/temp.rs @@ -0,0 +1,26 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use example_common::*; + +use defmt::panic; +use embassy::{ + executor::Spawner, + time::{Duration, Timer}, +}; +use embassy_nrf::{interrupt, temp::Temp, Peripherals}; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let irq = interrupt::take!(TEMP); + let mut temp = Temp::new(p.TEMP, irq); + + loop { + let value = temp.read().await; + info!("temperature: {}℃", value.to_num::()); + Timer::after(Duration::from_secs(1)).await; + } +}