2023-02-01 00:48:33 +01:00
|
|
|
//! Builtin temperature sensor driver.
|
2021-10-18 15:24:31 +02:00
|
|
|
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2021-10-19 15:31:28 +02:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::drop::OnDrop;
|
|
|
|
use embassy_hal_internal::{into_ref, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2021-10-18 15:24:31 +02:00
|
|
|
use fixed::types::I30F2;
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::InterruptExt;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::peripherals::TEMP;
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{interrupt, pac, Peripheral};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-03-05 22:09:54 +01:00
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
impl interrupt::typelevel::Handler<interrupt::typelevel::TEMP> for InterruptHandler {
|
2023-03-05 22:09:54 +01:00
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
let r = unsafe { &*pac::TEMP::PTR };
|
|
|
|
r.intenclr.write(|w| w.datardy().clear());
|
|
|
|
WAKER.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Builtin temperature sensor driver.
|
2021-10-18 15:24:31 +02:00
|
|
|
pub struct Temp<'d> {
|
2023-03-05 22:09:54 +01:00
|
|
|
_peri: PeripheralRef<'d, TEMP>,
|
2021-10-18 15:24:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 15:31:28 +02:00
|
|
|
static WAKER: AtomicWaker = AtomicWaker::new();
|
2021-10-18 15:24:31 +02:00
|
|
|
|
|
|
|
impl<'d> Temp<'d> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Create a new temperature sensor driver.
|
2023-03-05 22:09:54 +01:00
|
|
|
pub fn new(
|
|
|
|
_peri: impl Peripheral<P = TEMP> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<interrupt::typelevel::TEMP, InterruptHandler> + 'd,
|
2023-03-05 22:09:54 +01:00
|
|
|
) -> Self {
|
|
|
|
into_ref!(_peri);
|
2021-10-18 15:24:31 +02:00
|
|
|
|
|
|
|
// Enable interrupt that signals temperature values
|
2023-06-08 16:08:40 +02:00
|
|
|
interrupt::TEMP.unpend();
|
|
|
|
unsafe { interrupt::TEMP.enable() };
|
2023-03-05 22:09:54 +01:00
|
|
|
|
|
|
|
Self { _peri }
|
2021-10-18 15:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2023-05-29 21:30:28 +02:00
|
|
|
/// use embassy_nrf::{bind_interrupts, temp};
|
|
|
|
/// use embassy_nrf::temp::Temp;
|
|
|
|
/// use embassy_time::{Duration, Timer};
|
|
|
|
///
|
|
|
|
/// bind_interrupts!(struct Irqs {
|
|
|
|
/// TEMP => temp::InterruptHandler;
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// # async {
|
|
|
|
/// # let p: embassy_nrf::Peripherals = todo!();
|
|
|
|
/// let mut t = Temp::new(p.TEMP, Irqs);
|
2021-10-18 15:24:31 +02:00
|
|
|
/// let v: u16 = t.read().await.to_num::<u16>();
|
2023-05-29 21:30:28 +02:00
|
|
|
/// # };
|
2021-10-18 15:24:31 +02:00
|
|
|
/// ```
|
2021-10-19 20:48:46 +02:00
|
|
|
pub async fn read(&mut self) -> I30F2 {
|
2021-10-18 15:24:31 +02:00
|
|
|
// In case the future is dropped, stop the task and reset events.
|
|
|
|
let on_drop = OnDrop::new(|| {
|
|
|
|
let t = Self::regs();
|
2021-10-19 15:31:28 +02:00
|
|
|
t.tasks_stop.write(|w| unsafe { w.bits(1) });
|
2021-10-18 15:24:31 +02:00
|
|
|
t.events_datardy.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
let t = Self::regs();
|
2021-10-19 15:31:28 +02:00
|
|
|
t.intenset.write(|w| w.datardy().set());
|
2021-10-18 15:24:31 +02:00
|
|
|
unsafe { t.tasks_start.write(|w| w.bits(1)) };
|
|
|
|
|
2021-10-19 20:48:46 +02:00
|
|
|
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
|
2021-10-18 15:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn regs() -> &'static pac::temp::RegisterBlock {
|
|
|
|
unsafe { &*pac::TEMP::ptr() }
|
|
|
|
}
|
|
|
|
}
|