embassy/embassy-nrf/src/gpiote.rs

423 lines
11 KiB
Rust
Raw Normal View History

2021-03-20 02:09:42 +00:00
use core::convert::Infallible;
use core::future::Future;
2021-03-20 02:09:42 +00:00
use core::intrinsics::transmute;
use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop};
use core::ops::Deref;
use core::pin::Pin;
2020-09-22 22:32:49 +00:00
use core::ptr;
use core::task::{Context, Poll};
use embassy::interrupt::InterruptExt;
2021-03-02 20:14:58 +00:00
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
use embassy::util::{AtomicWaker, PeripheralBorrow, Signal};
2021-03-20 02:09:42 +00:00
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
2020-09-22 22:32:49 +00:00
2021-03-20 02:09:42 +00:00
use crate::gpio::sealed::Pin as _;
use crate::gpio::{AnyPin, Input, Pin as GpioPin, Pull};
use crate::pac;
2020-09-29 17:18:52 +00:00
use crate::pac::generic::Reg;
use crate::pac::gpiote::_TASKS_OUT;
2021-03-20 02:09:42 +00:00
use crate::pac::p0 as pac_gpio;
use crate::{interrupt, peripherals};
2020-09-22 22:32:49 +00:00
2020-09-29 17:18:52 +00:00
#[cfg(not(feature = "51"))]
use crate::pac::gpiote::{_TASKS_CLR, _TASKS_SET};
2020-11-08 17:59:31 +00:00
pub const CHANNEL_COUNT: usize = 8;
#[cfg(any(feature = "52833", feature = "52840"))]
pub const PIN_COUNT: usize = 48;
#[cfg(not(any(feature = "52833", feature = "52840")))]
pub const PIN_COUNT: usize = 32;
pub trait ChannelID {
fn number(&self) -> usize;
2020-09-22 22:32:49 +00:00
}
macro_rules! impl_channel {
($ChX:ident, $n:expr) => {
pub struct $ChX(());
impl $ChX {
pub fn degrade(self) -> ChAny {
ChAny($n)
}
}
2020-09-22 22:32:49 +00:00
impl ChannelID for $ChX {
fn number(&self) -> usize {
$n
}
}
};
2020-11-08 17:59:31 +00:00
}
impl_channel!(Ch0, 0);
impl_channel!(Ch1, 1);
impl_channel!(Ch2, 2);
impl_channel!(Ch3, 3);
impl_channel!(Ch4, 4);
impl_channel!(Ch5, 5);
impl_channel!(Ch6, 6);
impl_channel!(Ch7, 7);
pub struct ChAny(u8);
impl ChannelID for ChAny {
fn number(&self) -> usize {
self.0 as usize
}
}
const NEW_AWR: AtomicWaker = AtomicWaker::new();
static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AWR; CHANNEL_COUNT];
static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AWR; PIN_COUNT];
2020-11-08 16:38:45 +00:00
pub enum InputChannelPolarity {
2020-09-22 22:32:49 +00:00
None,
HiToLo,
LoToHi,
Toggle,
}
2020-09-29 17:18:52 +00:00
/// Polarity of the `task out` operation.
2020-11-08 16:38:45 +00:00
pub enum OutputChannelPolarity {
2020-09-29 17:18:52 +00:00
Set,
Clear,
Toggle,
}
2021-03-20 02:09:42 +00:00
/// Token indicating GPIOTE has been correctly initialized.
///
/// This is not an owned singleton, it is Copy. Drivers that make use of GPIOTE require it.
#[derive(Clone, Copy)]
pub struct Initialized {
_private: (),
2020-09-22 22:32:49 +00:00
}
2021-03-20 02:09:42 +00:00
pub fn initialize(gpiote: peripherals::GPIOTE, irq: interrupt::GPIOTE) -> Initialized {
#[cfg(any(feature = "52833", feature = "52840"))]
let ports = unsafe { &[&*pac::P0::ptr(), &*pac::P1::ptr()] };
#[cfg(not(any(feature = "52833", feature = "52840")))]
let ports = unsafe { &[&*pac::P0::ptr()] };
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
let g = unsafe { &*pac::GPIOTE::ptr() };
g.events_port.write(|w| w);
g.intenset.write(|w| w.port().set());
irq.set_handler(on_irq);
irq.unpend();
irq.enable();
Initialized { _private: () }
}
2021-03-20 02:09:42 +00:00
unsafe fn on_irq(_ctx: *mut ()) {
let g = &*pac::GPIOTE::ptr();
2020-11-08 17:59:31 +00:00
2021-03-20 02:09:42 +00:00
for i in 0..CHANNEL_COUNT {
if g.events_in[i].read().bits() != 0 {
g.events_in[i].write(|w| w);
CHANNEL_WAKERS[i].wake();
2020-11-08 17:59:31 +00:00
}
2020-09-22 22:32:49 +00:00
}
2020-12-29 00:53:17 +00:00
2021-03-20 02:09:42 +00:00
if g.events_port.read().bits() != 0 {
g.events_port.write(|w| w);
2020-12-29 00:53:17 +00:00
2021-03-20 02:09:42 +00:00
#[cfg(any(feature = "52833", feature = "52840"))]
let ports = &[&*pac::P0::ptr(), &*pac::P1::ptr()];
#[cfg(not(any(feature = "52833", feature = "52840")))]
let ports = &[&*pac::P0::ptr()];
2020-12-29 00:53:17 +00:00
2021-03-20 02:09:42 +00: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 00:53:17 +00:00
}
2021-03-20 02:09:42 +00:00
p.latch.write(|w| w.bits(bits));
2020-12-29 00:53:17 +00:00
}
}
2020-09-22 22:32:49 +00:00
}
2021-03-20 02:09:42 +00:00
struct BitIter(u32);
2020-11-08 17:59:31 +00:00
2021-03-20 02:09:42 +00:00
impl Iterator for BitIter {
type Item = u32;
2020-11-08 17:59:31 +00:00
2021-03-20 02:09:42 +00: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 17:59:31 +00:00
}
2021-03-20 02:09:42 +00:00
/*
pub struct InputChannel<C: ChannelID, T> {
ch: C,
pin: GpioPin<Input<T>>,
2020-09-22 22:32:49 +00:00
}
impl<C: ChannelID, T> Drop for InputChannel<C, T> {
2020-09-22 22:32:49 +00:00
fn drop(&mut self) {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
g.config[index].write(|w| w.mode().disabled());
g.intenclr.write(|w| unsafe { w.bits(1 << index) });
2020-09-22 22:32:49 +00:00
}
}
impl<C: ChannelID, T> InputChannel<C, T> {
pub fn new(
2021-03-20 02:09:42 +00:00
_init: Initialized,
ch: C,
pin: GpioPin<Input<T>>,
polarity: InputChannelPolarity,
) -> Self {
let g = unsafe { &*GPIOTE::ptr() };
let index = ch.number();
g.config[index].write(|w| {
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(),
};
#[cfg(any(feature = "52833", feature = "52840"))]
w.port().bit(match pin.port() {
Port::Port0 => false,
Port::Port1 => true,
});
unsafe { w.psel().bits(pin.pin()) }
});
2021-03-20 02:09:42 +00:00
CHANNEL_WAKERS[index].reset();
// Enable interrupt
g.intenset.write(|w| unsafe { w.bits(1 << index) });
InputChannel { ch, pin }
}
pub fn free(self) -> (C, GpioPin<Input<T>>) {
let m = ManuallyDrop::new(self);
let ch = unsafe { ptr::read(&m.ch) };
let pin = unsafe { ptr::read(&m.pin) };
(ch, pin)
}
2020-11-08 17:59:31 +00:00
pub async fn wait(&self) {
let index = self.ch.number();
2021-03-20 02:09:42 +00:00
CHANNEL_WAKERS[index].wait().await;
2020-09-22 22:32:49 +00:00
}
pub fn pin(&self) -> &GpioPin<Input<T>> {
&self.pin
}
2020-09-22 22:32:49 +00:00
}
pub struct OutputChannel<C: ChannelID, T> {
ch: C,
pin: GpioPin<Output<T>>,
2020-09-29 17:18:52 +00:00
}
impl<C: ChannelID, T> Drop for OutputChannel<C, T> {
2020-09-29 17:18:52 +00:00
fn drop(&mut self) {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
g.config[index].write(|w| w.mode().disabled());
g.intenclr.write(|w| unsafe { w.bits(1 << index) });
2020-09-29 17:18:52 +00:00
}
}
impl<C: ChannelID, T> OutputChannel<C, T> {
pub fn new(
_gpiote: Gpiote,
ch: C,
pin: GpioPin<Output<T>>,
level: Level,
polarity: OutputChannelPolarity,
) -> Self {
let g = unsafe { &*GPIOTE::ptr() };
let index = ch.number();
g.config[index].write(|w| {
w.mode().task();
match level {
Level::High => w.outinit().high(),
Level::Low => w.outinit().low(),
};
match polarity {
OutputChannelPolarity::Set => w.polarity().lo_to_hi(),
OutputChannelPolarity::Clear => w.polarity().hi_to_lo(),
OutputChannelPolarity::Toggle => w.polarity().toggle(),
};
#[cfg(any(feature = "52833", feature = "52840"))]
w.port().bit(match pin.port() {
Port::Port0 => false,
Port::Port1 => true,
});
unsafe { w.psel().bits(pin.pin()) }
});
// Enable interrupt
g.intenset.write(|w| unsafe { w.bits(1 << index) });
OutputChannel { ch, pin }
}
pub fn free(self) -> (C, GpioPin<Output<T>>) {
let m = ManuallyDrop::new(self);
let ch = unsafe { ptr::read(&m.ch) };
let pin = unsafe { ptr::read(&m.pin) };
(ch, pin)
}
2020-09-29 17:18:52 +00:00
/// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle).
pub fn out(&self) {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
g.tasks_out[index].write(|w| unsafe { w.bits(1) });
2020-09-29 17:18:52 +00:00
}
/// Triggers `task set` (set associated pin high).
#[cfg(not(feature = "51"))]
pub fn set(&self) {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
g.tasks_set[index].write(|w| unsafe { w.bits(1) });
2020-09-29 17:18:52 +00:00
}
/// Triggers `task clear` (set associated pin low).
#[cfg(not(feature = "51"))]
pub fn clear(&self) {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
g.tasks_clr[index].write(|w| unsafe { w.bits(1) });
2020-09-29 17:18:52 +00:00
}
/// Returns reference to task_out endpoint for PPI.
pub fn task_out(&self) -> &Reg<u32, _TASKS_OUT> {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
&g.tasks_out[index]
2020-09-29 17:18:52 +00:00
}
/// Returns reference to task_clr endpoint for PPI.
#[cfg(not(feature = "51"))]
pub fn task_clr(&self) -> &Reg<u32, _TASKS_CLR> {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
&g.tasks_clr[index]
2020-09-29 17:18:52 +00:00
}
/// Returns reference to task_set endpoint for PPI.
#[cfg(not(feature = "51"))]
pub fn task_set(&self) -> &Reg<u32, _TASKS_SET> {
let g = unsafe { &*GPIOTE::ptr() };
let index = self.ch.number();
&g.tasks_set[index]
2020-09-29 17:18:52 +00:00
}
}
2021-03-20 02:09:42 +00:00
*/
2020-09-29 17:18:52 +00:00
2021-03-20 02:09:42 +00:00
/// GPIO input driver with support
2021-03-24 17:31:11 +00:00
pub struct PortInput<'d, T: GpioPin> {
pin: Input<'d, T>,
2021-03-20 02:09:42 +00:00
}
2020-11-08 17:59:31 +00:00
2021-03-24 17:31:11 +00:00
impl<'d, T: GpioPin> Unpin for PortInput<'d, T> {}
impl<'d, T: GpioPin> PortInput<'d, T> {
pub fn new(_init: Initialized, pin: Input<'d, T>) -> Self {
2021-03-20 02:09:42 +00:00
Self { pin }
2020-09-22 22:32:49 +00:00
}
}
2021-03-24 17:31:11 +00:00
impl<'d, T: GpioPin> InputPin for PortInput<'d, T> {
2021-03-20 02:09:42 +00:00
type Error = Infallible;
2021-03-20 02:09:42 +00:00
fn is_high(&self) -> Result<bool, Self::Error> {
self.pin.is_high()
}
2021-03-20 02:09:42 +00:00
fn is_low(&self) -> Result<bool, Self::Error> {
self.pin.is_low()
}
}
2021-03-24 17:31:11 +00:00
impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> {
2021-03-20 02:09:42 +00:00
type Future<'a> = PortInputFuture<'a>;
fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> {
2021-03-20 02:09:42 +00:00
self.pin.pin.conf().modify(|_, w| w.sense().high());
PortInputFuture {
2021-03-20 02:09:42 +00:00
pin_port: self.pin.pin.pin_port(),
phantom: PhantomData,
}
}
}
2021-03-24 17:31:11 +00:00
impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> {
2021-03-20 02:09:42 +00:00
type Future<'a> = PortInputFuture<'a>;
fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> {
2021-03-20 02:09:42 +00:00
self.pin.pin.conf().modify(|_, w| w.sense().low());
PortInputFuture {
2021-03-20 02:09:42 +00:00
pin_port: self.pin.pin.pin_port(),
phantom: PhantomData,
}
}
}
2021-03-20 02:09:42 +00:00
pub struct PortInputFuture<'a> {
pin_port: u8,
phantom: PhantomData<&'a mut AnyPin>,
}
2021-03-20 02:09:42 +00:00
impl<'a> Drop for PortInputFuture<'a> {
fn drop(&mut self) {
2021-03-20 02:09:42 +00:00
unsafe { AnyPin::steal(self.pin_port) }
.conf()
.modify(|_, w| w.sense().disabled());
}
}
2021-03-20 02:09:42 +00:00
impl<'a> Future for PortInputFuture<'a> {
type Output = ();
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2021-03-20 02:09:42 +00:00
let dis = unsafe { AnyPin::steal(self.pin_port) }
.conf()
.read()
.sense()
.is_disabled();
if dis {
return Poll::Ready(());
}
PORT_WAKERS[self.pin_port as usize].register(cx.waker());
Poll::Pending
}
}