embassy/embassy-nrf/src/gpiote.rs

456 lines
13 KiB
Rust
Raw Normal View History

2020-11-08 17:59:31 +00:00
use anyfmt::{panic, *};
2020-09-22 22:32:49 +00:00
use core::cell::Cell;
use core::ptr;
use embassy::util::Signal;
2020-10-31 22:03:09 +00:00
use crate::hal::gpio::{Input, Level, Output, Pin, Port};
2020-09-22 22:32:49 +00:00
use crate::interrupt;
2020-09-29 17:18:52 +00:00
use crate::pac::generic::Reg;
use crate::pac::gpiote::_TASKS_OUT;
2020-11-08 18:05:34 +00:00
#[cfg(any(feature = "52833", feature = "52840"))]
use crate::pac::P1;
use crate::pac::{p0 as gpio, GPIOTE, P0};
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;
2020-09-22 22:32:49 +00:00
pub struct Gpiote {
inner: GPIOTE,
free_channels: Cell<u8>, // 0 = used, 1 = free. 8 bits for 8 channelself.
2020-11-08 17:59:31 +00:00
channel_signals: [Signal<()>; CHANNEL_COUNT],
port_signals: [Signal<()>; PIN_COUNT],
2020-09-22 22:32:49 +00:00
}
static mut INSTANCE: *const Gpiote = ptr::null_mut();
2020-11-08 17:59:31 +00:00
pub enum PortInputPolarity {
High,
Low,
}
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,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2020-09-22 22:32:49 +00:00
pub enum NewChannelError {
NoFreeChannels,
}
impl Gpiote {
pub fn new(gpiote: GPIOTE) -> Self {
2020-11-08 17:59:31 +00:00
#[cfg(any(feature = "52833", feature = "52840"))]
let ports = unsafe { &[&*P0::ptr(), &*P1::ptr()] };
#[cfg(not(any(feature = "52833", feature = "52840")))]
let ports = unsafe { &[&*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
gpiote.events_port.write(|w| w);
gpiote.intenset.write(|w| w.port().set());
2020-09-22 22:32:49 +00:00
interrupt::unpend(interrupt::GPIOTE);
interrupt::enable(interrupt::GPIOTE);
Self {
inner: gpiote,
free_channels: Cell::new(0xFF), // all 8 channels free
2020-11-08 17:59:31 +00:00
channel_signals: [
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
],
// This is just horrible
#[cfg(any(feature = "52833", feature = "52840"))]
port_signals: [
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
],
#[cfg(not(any(feature = "52833", feature = "52840")))]
port_signals: [
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
2020-09-22 22:32:49 +00:00
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
Signal::new(),
],
}
}
2020-09-29 17:18:52 +00:00
fn allocate_channel(&self) -> Result<u8, NewChannelError> {
2020-09-22 22:32:49 +00:00
interrupt::free(|_| {
let chs = self.free_channels.get();
let index = chs.trailing_zeros() as usize;
if index == 8 {
return Err(NewChannelError::NoFreeChannels);
}
self.free_channels.set(chs & !(1 << index));
2020-09-29 17:18:52 +00:00
Ok(index as u8)
})
}
fn free_channel(&self, index: u8) {
interrupt::free(|_| {
self.inner.config[index as usize].write(|w| w.mode().disabled());
self.inner.intenclr.write(|w| unsafe { w.bits(1 << index) });
self.free_channels
.set(self.free_channels.get() | 1 << index);
2020-11-08 18:00:23 +00:00
trace!("freed ch {:?}", index);
2020-09-29 17:18:52 +00:00
})
}
2020-09-22 22:32:49 +00:00
2020-11-08 17:59:31 +00:00
pub fn new_port_input<'a, T>(&'a self, pin: Pin<Input<T>>) -> PortInput<'a, T> {
interrupt::free(|_| {
unsafe { INSTANCE = self };
PortInput { gpiote: self, pin }
})
}
2020-09-29 17:18:52 +00:00
pub fn new_input_channel<'a, T>(
&'a self,
pin: Pin<Input<T>>,
2020-11-08 16:38:45 +00:00
polarity: InputChannelPolarity,
) -> Result<InputChannel<'a, T>, NewChannelError> {
2020-09-29 17:18:52 +00:00
interrupt::free(|_| {
unsafe { INSTANCE = self };
let index = self.allocate_channel()?;
2020-11-08 18:00:23 +00:00
trace!("allocated in ch {:?}", index as u8);
2020-09-22 22:32:49 +00:00
2020-09-29 17:18:52 +00:00
self.inner.config[index as usize].write(|w| {
2020-11-08 16:38:45 +00: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(),
2020-09-22 22:32:49 +00:00
};
2020-10-31 22:03:09 +00:00
#[cfg(any(feature = "52833", feature = "52840"))]
2020-09-22 22:32:49 +00:00
w.port().bit(match pin.port() {
Port::Port0 => false,
Port::Port1 => true,
});
unsafe { w.psel().bits(pin.pin()) }
});
// Enable interrupt
self.inner.intenset.write(|w| unsafe { w.bits(1 << index) });
2020-09-29 17:18:52 +00:00
Ok(InputChannel {
gpiote: self,
index,
pin,
2020-09-29 17:18:52 +00:00
})
})
}
pub fn new_output_channel<'a, T>(
&'a self,
pin: Pin<Output<T>>,
level: Level,
2020-11-08 16:38:45 +00:00
polarity: OutputChannelPolarity,
2020-09-29 17:18:52 +00:00
) -> Result<OutputChannel<'a>, NewChannelError> {
interrupt::free(|_| {
unsafe { INSTANCE = self };
let index = self.allocate_channel()?;
2020-11-08 18:00:23 +00:00
trace!("allocated out ch {:?}", index);
2020-09-29 17:18:52 +00:00
self.inner.config[index as usize].write(|w| {
w.mode().task();
match level {
Level::High => w.outinit().high(),
Level::Low => w.outinit().low(),
};
2020-11-08 16:38:45 +00:00
match polarity {
OutputChannelPolarity::Set => w.polarity().lo_to_hi(),
OutputChannelPolarity::Clear => w.polarity().hi_to_lo(),
OutputChannelPolarity::Toggle => w.polarity().toggle(),
2020-09-29 17:18:52 +00:00
};
2020-10-31 22:03:09 +00:00
#[cfg(any(feature = "52833", feature = "52840"))]
2020-09-29 17:18:52 +00:00
w.port().bit(match pin.port() {
Port::Port0 => false,
Port::Port1 => true,
});
unsafe { w.psel().bits(pin.pin()) }
});
// Enable interrupt
self.inner.intenset.write(|w| unsafe { w.bits(1 << index) });
Ok(OutputChannel {
2020-09-22 22:32:49 +00:00
gpiote: self,
2020-09-29 17:18:52 +00:00
index,
2020-09-22 22:32:49 +00:00
})
})
}
}
2020-11-08 17:59:31 +00:00
pub struct PortInput<'a, T> {
gpiote: &'a Gpiote,
pin: Pin<Input<T>>,
}
impl<'a, T> Drop for PortInput<'a, T> {
fn drop(&mut self) {
pin_conf(&self.pin).modify(|_, w| w.sense().disabled());
self.gpiote.port_signals[pin_num(&self.pin)].reset();
}
}
impl<'a, T> PortInput<'a, T> {
pub async fn wait(&self, polarity: PortInputPolarity) {
pin_conf(&self.pin).modify(|_, w| match polarity {
PortInputPolarity::Low => w.sense().low(),
PortInputPolarity::High => w.sense().high(),
});
self.gpiote.port_signals[pin_num(&self.pin)].wait().await;
}
pub fn pin(&self) -> &Pin<Input<T>> {
&self.pin
}
}
fn pin_num<T>(pin: &Pin<T>) -> usize {
let port = match pin.port() {
Port::Port0 => 0,
#[cfg(any(feature = "52833", feature = "52840"))]
Port::Port1 => 32,
};
port + pin.pin() as usize
}
fn pin_block<T>(pin: &Pin<T>) -> &gpio::RegisterBlock {
let ptr = match pin.port() {
Port::Port0 => P0::ptr(),
#[cfg(any(feature = "52833", feature = "52840"))]
Port::Port1 => P1::ptr(),
};
unsafe { &*ptr }
}
fn pin_conf<T>(pin: &Pin<T>) -> &gpio::PIN_CNF {
&pin_block(pin).pin_cnf[pin.pin() as usize]
}
pub struct InputChannel<'a, T> {
2020-09-22 22:32:49 +00:00
gpiote: &'a Gpiote,
pin: Pin<Input<T>>,
2020-09-22 22:32:49 +00:00
index: u8,
}
impl<'a, T> Drop for InputChannel<'a, T> {
2020-09-22 22:32:49 +00:00
fn drop(&mut self) {
2020-09-29 17:18:52 +00:00
self.gpiote.free_channel(self.index);
2020-09-22 22:32:49 +00:00
}
}
impl<'a, T> InputChannel<'a, T> {
2020-11-08 17:59:31 +00:00
pub async fn wait(&self) {
self.gpiote.channel_signals[self.index as usize]
.wait()
.await;
2020-09-22 22:32:49 +00:00
}
pub fn pin(&self) -> &Pin<Input<T>> {
&self.pin
}
2020-09-22 22:32:49 +00:00
}
2020-09-29 17:18:52 +00:00
pub struct OutputChannel<'a> {
gpiote: &'a Gpiote,
index: u8,
}
impl<'a> Drop for OutputChannel<'a> {
fn drop(&mut self) {
self.gpiote.free_channel(self.index);
}
}
impl<'a> OutputChannel<'a> {
/// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle).
pub fn out(&self) {
self.gpiote.inner.tasks_out[self.index as usize].write(|w| unsafe { w.bits(1) });
}
/// Triggers `task set` (set associated pin high).
#[cfg(not(feature = "51"))]
pub fn set(&self) {
self.gpiote.inner.tasks_set[self.index as usize].write(|w| unsafe { w.bits(1) });
}
/// Triggers `task clear` (set associated pin low).
#[cfg(not(feature = "51"))]
pub fn clear(&self) {
self.gpiote.inner.tasks_clr[self.index as usize].write(|w| unsafe { w.bits(1) });
}
/// Returns reference to task_out endpoint for PPI.
pub fn task_out(&self) -> &Reg<u32, _TASKS_OUT> {
&self.gpiote.inner.tasks_out[self.index as usize]
}
/// Returns reference to task_clr endpoint for PPI.
#[cfg(not(feature = "51"))]
pub fn task_clr(&self) -> &Reg<u32, _TASKS_CLR> {
&self.gpiote.inner.tasks_clr[self.index as usize]
}
/// Returns reference to task_set endpoint for PPI.
#[cfg(not(feature = "51"))]
pub fn task_set(&self) -> &Reg<u32, _TASKS_SET> {
&self.gpiote.inner.tasks_set[self.index as usize]
}
}
2020-09-22 22:32:49 +00:00
#[interrupt]
unsafe fn GPIOTE() {
let s = &(*INSTANCE);
for i in 0..8 {
if s.inner.events_in[i].read().bits() != 0 {
s.inner.events_in[i].write(|w| w);
2020-11-08 17:59:31 +00:00
s.channel_signals[i].signal(());
}
}
if s.inner.events_port.read().bits() != 0 {
s.inner.events_port.write(|w| w);
#[cfg(any(feature = "52833", feature = "52840"))]
let ports = &[&*P0::ptr(), &*P1::ptr()];
#[cfg(not(any(feature = "52833", feature = "52840")))]
let ports = &[&*P0::ptr()];
let mut work = true;
while work {
work = false;
for (port, &p) in ports.iter().enumerate() {
for pin in BitIter(p.latch.read().bits()) {
work = true;
p.pin_cnf[pin as usize].modify(|_, w| w.sense().disabled());
p.latch.write(|w| w.bits(1 << pin));
s.port_signals[port * 32 + pin as usize].signal(());
}
}
}
}
}
struct BitIter(u32);
impl Iterator for BitIter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
match self.0.trailing_zeros() {
32 => None,
b => {
self.0 &= !(1 << b);
Some(b)
}
2020-09-22 22:32:49 +00:00
}
}
}