nrf/ppi: implement and add example
This commit is contained in:
@ -7,19 +7,15 @@ use embassy::interrupt::InterruptExt;
|
||||
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
|
||||
use embassy::util::AtomicWaker;
|
||||
use embassy_extras::impl_unborrow;
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
|
||||
use embedded_hal::digital::v2::{InputPin, StatefulOutputPin};
|
||||
use futures::future::poll_fn;
|
||||
|
||||
use crate::gpio::sealed::Pin as _;
|
||||
use crate::gpio::{AnyPin, Input, Output, Pin as GpioPin, Port, Pull};
|
||||
use crate::gpio::{AnyPin, Input, Output, Pin as GpioPin, Port};
|
||||
use crate::pac;
|
||||
use crate::pac::generic::Reg;
|
||||
use crate::pac::gpiote::_TASKS_OUT;
|
||||
use crate::ppi::{Event, Task};
|
||||
use crate::{interrupt, peripherals};
|
||||
|
||||
#[cfg(not(feature = "51"))]
|
||||
use crate::pac::gpiote::{_TASKS_CLR, _TASKS_SET};
|
||||
|
||||
pub const CHANNEL_COUNT: usize = 8;
|
||||
|
||||
#[cfg(any(feature = "52833", feature = "52840"))]
|
||||
@ -53,7 +49,7 @@ pub struct Initialized {
|
||||
_private: (),
|
||||
}
|
||||
|
||||
pub fn initialize(gpiote: peripherals::GPIOTE, irq: interrupt::GPIOTE) -> Initialized {
|
||||
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")))]
|
||||
@ -122,6 +118,7 @@ impl Iterator for BitIter {
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIOTE channel driver in input mode
|
||||
pub struct InputChannel<'d, C: Channel, T: GpioPin> {
|
||||
ch: C,
|
||||
pin: Input<'d, T>,
|
||||
@ -185,6 +182,12 @@ impl<'d, C: Channel, T: GpioPin> InputChannel<'d, C, T> {
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Returns the IN event, for use with PPI.
|
||||
pub fn event_in(&self) -> Event {
|
||||
let g = unsafe { &*pac::GPIOTE::ptr() };
|
||||
Event::from_reg(&g.events_in[self.ch.number()])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, C: Channel, T: GpioPin> InputPin for InputChannel<'d, C, T> {
|
||||
@ -199,9 +202,10 @@ impl<'d, C: Channel, T: GpioPin> InputPin for InputChannel<'d, C, T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIOTE channel driver in output mode
|
||||
pub struct OutputChannel<'d, C: Channel, T: GpioPin> {
|
||||
ch: C,
|
||||
pin: Output<'d, T>,
|
||||
_pin: Output<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, C: Channel, T: GpioPin> Drop for OutputChannel<'d, C, T> {
|
||||
@ -242,7 +246,7 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
|
||||
unsafe { w.psel().bits(pin.pin.pin()) }
|
||||
});
|
||||
|
||||
OutputChannel { ch, pin }
|
||||
OutputChannel { ch, _pin: pin }
|
||||
}
|
||||
|
||||
/// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle).
|
||||
@ -265,28 +269,28 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> {
|
||||
g.tasks_clr[self.ch.number()].write(|w| unsafe { w.bits(1) });
|
||||
}
|
||||
|
||||
/// Returns reference to task_out endpoint for PPI.
|
||||
pub fn task_out(&self) -> &Reg<u32, _TASKS_OUT> {
|
||||
/// Returns the OUT task, for use with PPI.
|
||||
pub fn task_out(&self) -> Task {
|
||||
let g = unsafe { &*pac::GPIOTE::ptr() };
|
||||
&g.tasks_out[self.ch.number()]
|
||||
Task::from_reg(&g.tasks_out[self.ch.number()])
|
||||
}
|
||||
|
||||
/// Returns reference to task_clr endpoint for PPI.
|
||||
/// Returns the CLR task, for use with PPI.
|
||||
#[cfg(not(feature = "51"))]
|
||||
pub fn task_clr(&self) -> &Reg<u32, _TASKS_CLR> {
|
||||
pub fn task_clr(&self) -> Task {
|
||||
let g = unsafe { &*pac::GPIOTE::ptr() };
|
||||
&g.tasks_clr[self.ch.number()]
|
||||
Task::from_reg(&g.tasks_clr[self.ch.number()])
|
||||
}
|
||||
|
||||
/// Returns reference to task_set endpoint for PPI.
|
||||
/// Returns the SET task, for use with PPI.
|
||||
#[cfg(not(feature = "51"))]
|
||||
pub fn task_set(&self) -> &Reg<u32, _TASKS_SET> {
|
||||
pub fn task_set(&self) -> Task {
|
||||
let g = unsafe { &*pac::GPIOTE::ptr() };
|
||||
&g.tasks_set[self.ch.number()]
|
||||
Task::from_reg(&g.tasks_set[self.ch.number()])
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIO input driver with support
|
||||
/// GPIOTE port input driver
|
||||
pub struct PortInput<'d, T: GpioPin> {
|
||||
pin: Input<'d, T>,
|
||||
}
|
||||
|
@ -9,13 +9,106 @@
|
||||
//! On nRF52 devices, there is also a fork task endpoint, where the user can configure one more task
|
||||
//! to be triggered by the same event, even fixed PPI channels have a configurable fork task.
|
||||
|
||||
use embassy_extras::impl_unborrow;
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr::NonNull;
|
||||
use embassy::util::PeripheralBorrow;
|
||||
use embassy_extras::{impl_unborrow, unborrow};
|
||||
|
||||
use crate::peripherals;
|
||||
use crate::{pac, peripherals};
|
||||
|
||||
// ======================
|
||||
// driver
|
||||
|
||||
pub struct Ppi<'d, C: Channel> {
|
||||
ch: C,
|
||||
phantom: PhantomData<&'d mut C>,
|
||||
}
|
||||
|
||||
impl<'d, C: Channel> Ppi<'d, C> {
|
||||
pub fn new(ch: impl PeripheralBorrow<Target = C> + 'd) -> Self {
|
||||
unborrow!(ch);
|
||||
let mut this = Self {
|
||||
ch,
|
||||
phantom: PhantomData,
|
||||
};
|
||||
#[cfg(not(feature = "51"))]
|
||||
this.clear_fork_task();
|
||||
this
|
||||
}
|
||||
|
||||
/// Enables the channel.
|
||||
pub fn enable(&mut self) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.chenset
|
||||
.write(|w| unsafe { w.bits(1 << self.ch.number()) });
|
||||
}
|
||||
|
||||
/// Disables the channel.
|
||||
pub fn disable(&mut self) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.chenclr
|
||||
.write(|w| unsafe { w.bits(1 << self.ch.number()) });
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "51"))]
|
||||
/// Sets the fork task that must be triggered when the configured event occurs. The user must
|
||||
/// provide a reference to the task.
|
||||
pub fn set_fork_task(&mut self, task: Task) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.fork[self.ch.number()]
|
||||
.tep
|
||||
.write(|w| unsafe { w.bits(task.0.as_ptr() as u32) })
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "51"))]
|
||||
/// Clear the fork task endpoint. Previously set task will no longer be triggered.
|
||||
pub fn clear_fork_task(&mut self) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.fork[self.ch.number()].tep.write(|w| unsafe { w.bits(0) })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, C: Channel> Drop for Ppi<'d, C> {
|
||||
fn drop(&mut self) {
|
||||
self.disable()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, C: ConfigurableChannel> Ppi<'d, C> {
|
||||
/// Sets the task to be triggered when the configured event occurs.
|
||||
pub fn set_task(&mut self, task: Task) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.ch[self.ch.number()]
|
||||
.tep
|
||||
.write(|w| unsafe { w.bits(task.0.as_ptr() as u32) })
|
||||
}
|
||||
|
||||
/// Sets the event that will trigger the chosen task(s).
|
||||
pub fn set_event(&mut self, event: Event) {
|
||||
let r = unsafe { &*pac::PPI::ptr() };
|
||||
r.ch[self.ch.number()]
|
||||
.eep
|
||||
.write(|w| unsafe { w.bits(event.0.as_ptr() as u32) })
|
||||
}
|
||||
}
|
||||
|
||||
// ======================
|
||||
// traits
|
||||
|
||||
pub struct Task(pub NonNull<()>);
|
||||
impl Task {
|
||||
pub(crate) fn from_reg<T>(reg: &T) -> Self {
|
||||
Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut ()) })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Event(pub NonNull<()>);
|
||||
impl Event {
|
||||
pub(crate) fn from_reg<T>(reg: &T) -> Self {
|
||||
Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut ()) })
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub trait ConfigurableChannel {}
|
||||
pub trait Channel {}
|
||||
|
Reference in New Issue
Block a user