2023-02-01 00:48:33 +01:00
|
|
|
//! GPIO task/event (GPIOTE) driver.
|
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
use core::convert::Infallible;
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::{poll_fn, Future};
|
2020-11-09 01:04:34 +01:00
|
|
|
use core::task::{Context, Poll};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::{impl_peripheral, into_ref, Peripheral, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2020-09-23 00:32:49 +02:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
use crate::gpio::sealed::Pin as _;
|
2022-01-13 21:15:27 +01:00
|
|
|
use crate::gpio::{AnyPin, Flex, Input, Output, Pin as GpioPin};
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::InterruptExt;
|
2021-03-27 16:13:32 +01:00
|
|
|
use crate::ppi::{Event, Task};
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::{interrupt, pac, peripherals};
|
2020-09-23 00:32:49 +02:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Amount of GPIOTE channels in the chip.
|
|
|
|
const CHANNEL_COUNT: usize = 8;
|
2020-11-08 18:59:31 +01:00
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
|
2023-02-01 00:48:33 +01:00
|
|
|
const PIN_COUNT: usize = 48;
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(not(any(feature = "nrf52833", feature = "nrf52840")))]
|
2023-02-01 00:48:33 +01:00
|
|
|
const PIN_COUNT: usize = 32;
|
2020-11-08 18:59:31 +01:00
|
|
|
|
2021-10-18 00:55:43 +02:00
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
2021-03-27 04:27:57 +01:00
|
|
|
const NEW_AW: AtomicWaker = AtomicWaker::new();
|
|
|
|
static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AW; CHANNEL_COUNT];
|
|
|
|
static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AW; PIN_COUNT];
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Polarity for listening to events for GPIOTE input channels.
|
2020-11-08 17:38:45 +01:00
|
|
|
pub enum InputChannelPolarity {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Don't listen for any pin changes.
|
2020-09-23 00:32:49 +02:00
|
|
|
None,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Listen for high to low changes.
|
2020-09-23 00:32:49 +02:00
|
|
|
HiToLo,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Listen for low to high changes.
|
2020-09-23 00:32:49 +02:00
|
|
|
LoToHi,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Listen for any change, either low to high or high to low.
|
2020-09-23 00:32:49 +02:00
|
|
|
Toggle,
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Polarity of the OUT task operation for GPIOTE output channels.
|
2020-11-08 17:38:45 +01:00
|
|
|
pub enum OutputChannelPolarity {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Set the pin high.
|
2020-09-29 19:18:52 +02:00
|
|
|
Set,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Set the pin low.
|
2020-09-29 19:18:52 +02:00
|
|
|
Clear,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Toggle the pin.
|
2020-09-29 19:18:52 +02:00
|
|
|
Toggle,
|
|
|
|
}
|
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
fn regs() -> &'static pac::gpiote::RegisterBlock {
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(any(feature="nrf5340-app-s", feature="nrf9160-s"))] {
|
|
|
|
unsafe { &*pac::GPIOTE0::ptr() }
|
|
|
|
} else if #[cfg(any(feature="nrf5340-app-ns", feature="nrf9160-ns"))] {
|
|
|
|
unsafe { &*pac::GPIOTE1::ptr() }
|
|
|
|
} else {
|
|
|
|
unsafe { &*pac::GPIOTE::ptr() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 21:27:27 +02:00
|
|
|
pub(crate) fn init(irq_prio: crate::interrupt::Priority) {
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
|
2021-10-12 11:43:57 +02:00
|
|
|
let ports = unsafe { &[&*pac::P0::ptr(), &*pac::P1::ptr()] };
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(not(any(feature = "nrf52833", feature = "nrf52840")))]
|
2021-10-12 11:43:57 +02:00
|
|
|
let ports = unsafe { &[&*pac::P0::ptr()] };
|
2021-03-20 03:09:42 +01:00
|
|
|
|
|
|
|
for &p in ports {
|
|
|
|
// Enable latched detection
|
|
|
|
p.detectmode.write(|w| w.detectmode().ldetect());
|
|
|
|
// Clear latch
|
|
|
|
p.latch.write(|w| unsafe { w.bits(0xFFFFFFFF) })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable interrupts
|
2023-06-01 02:22:46 +02:00
|
|
|
#[cfg(any(feature = "nrf5340-app-s", feature = "nrf9160-s"))]
|
2023-06-08 16:08:40 +02:00
|
|
|
let irq = interrupt::GPIOTE0;
|
2023-06-01 02:22:46 +02:00
|
|
|
#[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))]
|
2023-06-08 16:08:40 +02:00
|
|
|
let irq = interrupt::GPIOTE1;
|
2023-06-01 02:22:46 +02:00
|
|
|
#[cfg(any(feature = "_nrf52", feature = "nrf5340-net"))]
|
2023-06-08 16:08:40 +02:00
|
|
|
let irq = interrupt::GPIOTE;
|
2021-10-11 10:39:38 +02:00
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
irq.unpend();
|
|
|
|
irq.set_priority(irq_prio);
|
|
|
|
unsafe { irq.enable() };
|
2021-03-20 03:09:42 +01:00
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2021-05-12 01:01:08 +02:00
|
|
|
g.intenset.write(|w| w.port().set());
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
|
2023-06-01 02:22:46 +02:00
|
|
|
#[cfg(any(feature = "nrf5340-app-s", feature = "nrf9160-s"))]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2023-06-01 02:22:46 +02:00
|
|
|
#[interrupt]
|
|
|
|
fn GPIOTE0() {
|
|
|
|
unsafe { handle_gpiote_interrupt() };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2023-06-01 02:22:46 +02:00
|
|
|
#[interrupt]
|
|
|
|
fn GPIOTE1() {
|
|
|
|
unsafe { handle_gpiote_interrupt() };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "_nrf52", feature = "nrf5340-net"))]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2023-06-01 02:22:46 +02:00
|
|
|
#[interrupt]
|
|
|
|
fn GPIOTE() {
|
|
|
|
unsafe { handle_gpiote_interrupt() };
|
2021-10-11 10:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn handle_gpiote_interrupt() {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2020-11-08 18:59:31 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
for i in 0..CHANNEL_COUNT {
|
|
|
|
if g.events_in[i].read().bits() != 0 {
|
2022-02-16 03:54:39 +01:00
|
|
|
g.intenclr.write(|w| w.bits(1 << i));
|
2021-03-20 03:09:42 +01:00
|
|
|
CHANNEL_WAKERS[i].wake();
|
2020-11-08 18:59:31 +01:00
|
|
|
}
|
2020-09-23 00:32:49 +02:00
|
|
|
}
|
2020-12-29 01:53:17 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
if g.events_port.read().bits() != 0 {
|
|
|
|
g.events_port.write(|w| w);
|
2020-12-29 01:53:17 +01:00
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
|
2021-10-12 11:43:57 +02:00
|
|
|
let ports = &[&*pac::P0::ptr(), &*pac::P1::ptr()];
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(not(any(feature = "nrf52833", feature = "nrf52840")))]
|
2021-10-12 11:43:57 +02:00
|
|
|
let ports = &[&*pac::P0::ptr()];
|
2020-12-29 01:53:17 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
for (port, &p) in ports.iter().enumerate() {
|
|
|
|
let bits = p.latch.read().bits();
|
|
|
|
for pin in BitIter(bits) {
|
|
|
|
p.pin_cnf[pin as usize].modify(|_, w| w.sense().disabled());
|
|
|
|
PORT_WAKERS[port * 32 + pin as usize].wake();
|
2020-12-29 01:53:17 +01:00
|
|
|
}
|
2021-03-20 03:09:42 +01:00
|
|
|
p.latch.write(|w| w.bits(bits));
|
2020-12-29 01:53:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 00:32:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
struct BitIter(u32);
|
2020-11-08 18:59:31 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
impl Iterator for BitIter {
|
|
|
|
type Item = u32;
|
2020-11-08 18:59:31 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.0.trailing_zeros() {
|
|
|
|
32 => None,
|
|
|
|
b => {
|
|
|
|
self.0 &= !(1 << b);
|
|
|
|
Some(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-27 16:13:32 +01:00
|
|
|
/// GPIOTE channel driver in input mode
|
2021-03-27 03:50:18 +01:00
|
|
|
pub struct InputChannel<'d, C: Channel, T: GpioPin> {
|
2022-11-23 13:16:18 +01:00
|
|
|
ch: PeripheralRef<'d, C>,
|
|
|
|
pin: Input<'d, T>,
|
2020-09-23 00:32:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 03:50:18 +01:00
|
|
|
impl<'d, C: Channel, T: GpioPin> Drop for InputChannel<'d, C, T> {
|
2020-09-23 00:32:49 +02:00
|
|
|
fn drop(&mut self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
let num = self.ch.number();
|
2021-03-27 03:50:18 +01:00
|
|
|
g.config[num].write(|w| w.mode().disabled());
|
|
|
|
g.intenclr.write(|w| unsafe { w.bits(1 << num) });
|
2020-09-23 00:32:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:50:18 +01:00
|
|
|
impl<'d, C: Channel, T: GpioPin> InputChannel<'d, C, T> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Create a new GPIOTE input channel driver.
|
2022-11-22 16:35:38 +01:00
|
|
|
pub fn new(ch: impl Peripheral<P = C> + 'd, pin: Input<'d, T>, polarity: InputChannelPolarity) -> Self {
|
|
|
|
into_ref!(ch);
|
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2021-03-27 04:27:57 +01:00
|
|
|
let num = ch.number();
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2021-03-27 03:50:18 +01:00
|
|
|
g.config[num].write(|w| {
|
2021-02-04 23:56:17 +01:00
|
|
|
match polarity {
|
|
|
|
InputChannelPolarity::HiToLo => w.mode().event().polarity().hi_to_lo(),
|
|
|
|
InputChannelPolarity::LoToHi => w.mode().event().polarity().lo_to_hi(),
|
|
|
|
InputChannelPolarity::None => w.mode().event().polarity().none(),
|
|
|
|
InputChannelPolarity::Toggle => w.mode().event().polarity().toggle(),
|
|
|
|
};
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
|
2021-12-19 23:25:02 +01:00
|
|
|
w.port().bit(match pin.pin.pin.port() {
|
2021-07-05 02:43:34 +02:00
|
|
|
crate::gpio::Port::Port0 => false,
|
|
|
|
crate::gpio::Port::Port1 => true,
|
2021-02-04 23:56:17 +01:00
|
|
|
});
|
2021-12-19 23:25:02 +01:00
|
|
|
unsafe { w.psel().bits(pin.pin.pin.pin()) }
|
2021-02-04 23:56:17 +01:00
|
|
|
});
|
|
|
|
|
2021-03-27 03:50:18 +01:00
|
|
|
g.events_in[num].reset();
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2022-11-23 13:16:18 +01:00
|
|
|
InputChannel { ch, pin }
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Asynchronously wait for an event in this channel.
|
2021-03-27 03:50:18 +01:00
|
|
|
pub async fn wait(&self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
let num = self.ch.number();
|
2021-03-27 03:50:18 +01:00
|
|
|
|
|
|
|
// Enable interrupt
|
|
|
|
g.events_in[num].reset();
|
|
|
|
g.intenset.write(|w| unsafe { w.bits(1 << num) });
|
|
|
|
|
|
|
|
poll_fn(|cx| {
|
|
|
|
CHANNEL_WAKERS[num].register(cx.waker());
|
|
|
|
|
|
|
|
if g.events_in[num].read().bits() != 0 {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
2021-03-27 16:13:32 +01:00
|
|
|
|
|
|
|
/// Returns the IN event, for use with PPI.
|
2023-07-05 19:01:28 +02:00
|
|
|
pub fn event_in(&self) -> Event<'d> {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
Event::from_reg(&g.events_in[self.ch.number()])
|
2021-03-27 16:13:32 +01:00
|
|
|
}
|
2021-03-27 03:50:18 +01:00
|
|
|
}
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2021-03-27 16:13:32 +01:00
|
|
|
/// GPIOTE channel driver in output mode
|
2021-03-27 04:01:48 +01:00
|
|
|
pub struct OutputChannel<'d, C: Channel, T: GpioPin> {
|
2022-11-23 13:16:18 +01:00
|
|
|
ch: PeripheralRef<'d, C>,
|
2021-03-27 16:13:32 +01:00
|
|
|
_pin: Output<'d, T>,
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 04:01:48 +01:00
|
|
|
impl<'d, C: Channel, T: GpioPin> Drop for OutputChannel<'d, C, T> {
|
2020-09-29 19:18:52 +02:00
|
|
|
fn drop(&mut self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
let num = self.ch.number();
|
2021-03-27 04:01:48 +01:00
|
|
|
g.config[num].write(|w| w.mode().disabled());
|
|
|
|
g.intenclr.write(|w| unsafe { w.bits(1 << num) });
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 04:01:48 +01:00
|
|
|
impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Create a new GPIOTE output channel driver.
|
2022-11-22 16:35:38 +01:00
|
|
|
pub fn new(ch: impl Peripheral<P = C> + 'd, pin: Output<'d, T>, polarity: OutputChannelPolarity) -> Self {
|
|
|
|
into_ref!(ch);
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2021-03-27 04:27:57 +01:00
|
|
|
let num = ch.number();
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2021-03-27 04:01:48 +01:00
|
|
|
g.config[num].write(|w| {
|
2021-02-04 23:56:17 +01:00
|
|
|
w.mode().task();
|
2021-12-19 23:25:02 +01:00
|
|
|
match pin.is_set_high() {
|
2021-03-27 04:01:48 +01:00
|
|
|
true => w.outinit().high(),
|
|
|
|
false => w.outinit().low(),
|
2021-02-04 23:56:17 +01:00
|
|
|
};
|
|
|
|
match polarity {
|
|
|
|
OutputChannelPolarity::Set => w.polarity().lo_to_hi(),
|
|
|
|
OutputChannelPolarity::Clear => w.polarity().hi_to_lo(),
|
|
|
|
OutputChannelPolarity::Toggle => w.polarity().toggle(),
|
|
|
|
};
|
2021-05-11 03:04:59 +02:00
|
|
|
#[cfg(any(feature = "nrf52833", feature = "nrf52840"))]
|
2021-12-19 23:25:02 +01:00
|
|
|
w.port().bit(match pin.pin.pin.port() {
|
2021-07-05 02:43:34 +02:00
|
|
|
crate::gpio::Port::Port0 => false,
|
|
|
|
crate::gpio::Port::Port1 => true,
|
2021-02-04 23:56:17 +01:00
|
|
|
});
|
2021-12-19 23:25:02 +01:00
|
|
|
unsafe { w.psel().bits(pin.pin.pin.pin()) }
|
2021-02-04 23:56:17 +01:00
|
|
|
});
|
|
|
|
|
2022-11-23 13:16:18 +01:00
|
|
|
OutputChannel { ch, _pin: pin }
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Triggers the OUT task (does the action as configured with task_out_polarity, defaults to Toggle).
|
2020-09-29 19:18:52 +02:00
|
|
|
pub fn out(&self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
g.tasks_out[self.ch.number()].write(|w| unsafe { w.bits(1) });
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
2021-03-27 04:01:48 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Triggers the SET task (set associated pin high).
|
2021-10-11 01:09:57 +02:00
|
|
|
#[cfg(not(feature = "nrf51"))]
|
2020-09-29 19:18:52 +02:00
|
|
|
pub fn set(&self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
g.tasks_set[self.ch.number()].write(|w| unsafe { w.bits(1) });
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
2021-03-27 04:01:48 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Triggers the CLEAR task (set associated pin low).
|
2021-10-11 01:09:57 +02:00
|
|
|
#[cfg(not(feature = "nrf51"))]
|
2020-09-29 19:18:52 +02:00
|
|
|
pub fn clear(&self) {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
g.tasks_clr[self.ch.number()].write(|w| unsafe { w.bits(1) });
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 16:13:32 +01:00
|
|
|
/// Returns the OUT task, for use with PPI.
|
2023-07-05 19:01:28 +02:00
|
|
|
pub fn task_out(&self) -> Task<'d> {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
Task::from_reg(&g.tasks_out[self.ch.number()])
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 16:13:32 +01:00
|
|
|
/// Returns the CLR task, for use with PPI.
|
2021-10-11 01:09:57 +02:00
|
|
|
#[cfg(not(feature = "nrf51"))]
|
2023-07-05 19:01:28 +02:00
|
|
|
pub fn task_clr(&self) -> Task<'d> {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
Task::from_reg(&g.tasks_clr[self.ch.number()])
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 16:13:32 +01:00
|
|
|
/// Returns the SET task, for use with PPI.
|
2021-10-11 01:09:57 +02:00
|
|
|
#[cfg(not(feature = "nrf51"))]
|
2023-07-05 19:01:28 +02:00
|
|
|
pub fn task_set(&self) -> Task<'d> {
|
2021-10-28 03:07:06 +02:00
|
|
|
let g = regs();
|
2022-11-23 13:16:18 +01:00
|
|
|
Task::from_reg(&g.tasks_set[self.ch.number()])
|
2020-09-29 19:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
// =======================
|
|
|
|
|
2023-02-24 20:01:41 +01:00
|
|
|
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
2021-12-14 13:23:40 +01:00
|
|
|
pub(crate) struct PortInputFuture<'a> {
|
2022-07-23 15:13:47 +02:00
|
|
|
pin: PeripheralRef<'a, AnyPin>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PortInputFuture<'a> {
|
|
|
|
fn new(pin: impl Peripheral<P = impl GpioPin> + 'a) -> Self {
|
|
|
|
Self {
|
|
|
|
pin: pin.into_ref().map_into(),
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:23:40 +01:00
|
|
|
impl<'a> Unpin for PortInputFuture<'a> {}
|
2021-02-04 23:56:17 +01:00
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
impl<'a> Drop for PortInputFuture<'a> {
|
2021-02-04 23:56:17 +01:00
|
|
|
fn drop(&mut self) {
|
2022-07-23 15:13:47 +02:00
|
|
|
self.pin.conf().modify(|_, w| w.sense().disabled());
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 03:09:42 +01:00
|
|
|
impl<'a> Future for PortInputFuture<'a> {
|
2021-02-04 23:56:17 +01:00
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2022-07-23 15:13:47 +02:00
|
|
|
PORT_WAKERS[self.pin.pin_port() as usize].register(cx.waker());
|
2021-03-20 03:09:42 +01:00
|
|
|
|
2022-07-23 15:13:47 +02:00
|
|
|
if self.pin.conf().read().sense().is_disabled() {
|
2021-03-27 02:08:01 +01:00
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
2021-02-04 23:56:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 03:50:18 +01:00
|
|
|
|
2022-01-13 21:24:06 +01:00
|
|
|
impl<'d, T: GpioPin> Input<'d, T> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait until the pin is high. If it is already high, return immediately.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_high(&mut self) {
|
|
|
|
self.pin.wait_for_high().await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait until the pin is low. If it is already low, return immediately.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_low(&mut self) {
|
|
|
|
self.pin.wait_for_low().await
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo a transition from low to high.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_rising_edge(&mut self) {
|
|
|
|
self.pin.wait_for_rising_edge().await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo a transition from high to low.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_falling_edge(&mut self) {
|
|
|
|
self.pin.wait_for_falling_edge().await
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_any_edge(&mut self) {
|
|
|
|
self.pin.wait_for_any_edge().await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 21:24:06 +01:00
|
|
|
impl<'d, T: GpioPin> Flex<'d, T> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait until the pin is high. If it is already high, return immediately.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_high(&mut self) {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.conf().modify(|_, w| w.sense().high());
|
2022-07-23 15:13:47 +02:00
|
|
|
PortInputFuture::new(&mut self.pin).await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait until the pin is low. If it is already low, return immediately.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_low(&mut self) {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.conf().modify(|_, w| w.sense().low());
|
2022-07-23 15:13:47 +02:00
|
|
|
PortInputFuture::new(&mut self.pin).await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo a transition from low to high.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_rising_edge(&mut self) {
|
|
|
|
self.wait_for_low().await;
|
|
|
|
self.wait_for_high().await;
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo a transition from high to low.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_falling_edge(&mut self) {
|
|
|
|
self.wait_for_high().await;
|
|
|
|
self.wait_for_low().await;
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
|
2022-01-13 21:24:06 +01:00
|
|
|
pub async fn wait_for_any_edge(&mut self) {
|
2021-12-19 23:25:02 +01:00
|
|
|
if self.is_high() {
|
|
|
|
self.pin.conf().modify(|_, w| w.sense().low());
|
|
|
|
} else {
|
|
|
|
self.pin.conf().modify(|_, w| w.sense().high());
|
|
|
|
}
|
2022-07-23 15:13:47 +02:00
|
|
|
PortInputFuture::new(&mut self.pin).await
|
2021-12-19 23:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =======================
|
|
|
|
|
2021-03-27 03:50:18 +01:00
|
|
|
mod sealed {
|
2021-03-27 04:27:57 +01:00
|
|
|
pub trait Channel {}
|
2021-03-27 03:50:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// GPIOTE channel trait.
|
|
|
|
///
|
|
|
|
/// Implemented by all GPIOTE channels.
|
2021-03-27 03:50:18 +01:00
|
|
|
pub trait Channel: sealed::Channel + Sized {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Get the channel number.
|
2021-03-27 04:27:57 +01:00
|
|
|
fn number(&self) -> usize;
|
2023-02-01 00:48:33 +01:00
|
|
|
|
|
|
|
/// Convert this channel to a type-erased `AnyChannel`.
|
|
|
|
///
|
|
|
|
/// This allows using several channels in situations that might require
|
|
|
|
/// them to be the same type, like putting them in an array.
|
2021-03-27 03:50:18 +01:00
|
|
|
fn degrade(self) -> AnyChannel {
|
|
|
|
AnyChannel {
|
2021-03-27 04:27:57 +01:00
|
|
|
number: self.number() as u8,
|
2021-03-27 03:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Type-erased channel.
|
|
|
|
///
|
|
|
|
/// Obtained by calling `Channel::degrade`.
|
|
|
|
///
|
|
|
|
/// This allows using several channels in situations that might require
|
|
|
|
/// them to be the same type, like putting them in an array.
|
2021-03-27 03:50:18 +01:00
|
|
|
pub struct AnyChannel {
|
|
|
|
number: u8,
|
|
|
|
}
|
2022-07-23 14:00:19 +02:00
|
|
|
impl_peripheral!(AnyChannel);
|
2021-03-27 04:27:57 +01:00
|
|
|
impl sealed::Channel for AnyChannel {}
|
|
|
|
impl Channel for AnyChannel {
|
|
|
|
fn number(&self) -> usize {
|
|
|
|
self.number as usize
|
2021-03-27 03:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_channel {
|
|
|
|
($type:ident, $number:expr) => {
|
2021-03-27 04:27:57 +01:00
|
|
|
impl sealed::Channel for peripherals::$type {}
|
|
|
|
impl Channel for peripherals::$type {
|
|
|
|
fn number(&self) -> usize {
|
|
|
|
$number as usize
|
2021-03-27 03:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_channel!(GPIOTE_CH0, 0);
|
|
|
|
impl_channel!(GPIOTE_CH1, 1);
|
|
|
|
impl_channel!(GPIOTE_CH2, 2);
|
|
|
|
impl_channel!(GPIOTE_CH3, 3);
|
|
|
|
impl_channel!(GPIOTE_CH4, 4);
|
|
|
|
impl_channel!(GPIOTE_CH5, 5);
|
|
|
|
impl_channel!(GPIOTE_CH6, 6);
|
|
|
|
impl_channel!(GPIOTE_CH7, 7);
|
2022-01-13 23:56:25 +01:00
|
|
|
|
|
|
|
// ====================
|
|
|
|
|
|
|
|
mod eh02 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, C: Channel, T: GpioPin> embedded_hal_02::digital::v2::InputPin for InputChannel<'d, C, T> {
|
|
|
|
type Error = Infallible;
|
|
|
|
|
|
|
|
fn is_high(&self) -> Result<bool, Self::Error> {
|
2022-11-23 13:16:18 +01:00
|
|
|
Ok(self.pin.is_high())
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_low(&self) -> Result<bool, Self::Error> {
|
2022-11-23 13:16:18 +01:00
|
|
|
Ok(self.pin.is_low())
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "unstable-traits")]
|
|
|
|
mod eh1 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, C: Channel, T: GpioPin> embedded_hal_1::digital::ErrorType for InputChannel<'d, C, T> {
|
|
|
|
type Error = Infallible;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, C: Channel, T: GpioPin> embedded_hal_1::digital::InputPin for InputChannel<'d, C, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
fn is_high(&self) -> Result<bool, Self::Error> {
|
2022-11-23 13:16:18 +01:00
|
|
|
Ok(self.pin.is_high())
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_low(&self) -> Result<bool, Self::Error> {
|
2022-11-23 13:16:18 +01:00
|
|
|
Ok(self.pin.is_low())
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-12 00:24:04 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 02:46:52 +02:00
|
|
|
#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
|
|
|
|
mod eha {
|
|
|
|
use super::*;
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-08-31 02:46:52 +02:00
|
|
|
impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Input<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_high().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_low().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_rising_edge().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_falling_edge().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_any_edge().await)
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-08-31 02:46:52 +02:00
|
|
|
impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for Flex<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_high().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_low().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_rising_edge().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_falling_edge().await)
|
2022-08-31 02:46:52 +02:00
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.wait_for_any_edge().await)
|
2022-01-13 23:56:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|