From b6496a85d89beb0f9bc4d3db2f39d82e8768e5da Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 27 Mar 2021 16:13:32 +0100 Subject: [PATCH] nrf/ppi: implement and add example --- embassy-nrf-examples/src/bin/ppi.rs | 112 ++++++++++++++++++++++++++++ embassy-nrf/src/gpiote.rs | 44 ++++++----- embassy-nrf/src/ppi.rs | 97 +++++++++++++++++++++++- 3 files changed, 231 insertions(+), 22 deletions(-) create mode 100644 embassy-nrf-examples/src/bin/ppi.rs diff --git a/embassy-nrf-examples/src/bin/ppi.rs b/embassy-nrf-examples/src/bin/ppi.rs new file mode 100644 index 00000000..87854fa5 --- /dev/null +++ b/embassy-nrf-examples/src/bin/ppi.rs @@ -0,0 +1,112 @@ +#![no_std] +#![no_main] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use core::future::pending; + +use example_common::*; + +use cortex_m_rt::entry; +use defmt::panic; + +use embassy::executor::{task, Executor}; +use embassy::util::Forever; +use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull}; +use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity}; +use embassy_nrf::ppi::Ppi; +use embassy_nrf::{interrupt, Peripherals}; +use futures::future; +use gpiote::{OutputChannel, OutputChannelPolarity}; + +#[task] +async fn run() { + let p = Peripherals::take().unwrap(); + let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE)); + + info!("Starting!"); + + let button1 = InputChannel::new( + g, + p.GPIOTE_CH0, + Input::new(p.P0_11, Pull::Up), + InputChannelPolarity::HiToLo, + ); + let button2 = InputChannel::new( + g, + p.GPIOTE_CH1, + Input::new(p.P0_12, Pull::Up), + InputChannelPolarity::HiToLo, + ); + let button3 = InputChannel::new( + g, + p.GPIOTE_CH2, + Input::new(p.P0_24, Pull::Up), + InputChannelPolarity::HiToLo, + ); + let button4 = InputChannel::new( + g, + p.GPIOTE_CH3, + Input::new(p.P0_25, Pull::Up), + InputChannelPolarity::HiToLo, + ); + + let led1 = OutputChannel::new( + g, + p.GPIOTE_CH4, + Output::new(p.P0_13, Level::Low, OutputDrive::Standard), + OutputChannelPolarity::Toggle, + ); + + let led2 = OutputChannel::new( + g, + p.GPIOTE_CH5, + Output::new(p.P0_14, Level::Low, OutputDrive::Standard), + OutputChannelPolarity::Toggle, + ); + + let mut ppi = Ppi::new(p.PPI_CH0); + ppi.set_event(button1.event_in()); + ppi.set_task(led1.task_out()); + ppi.enable(); + + let mut ppi = Ppi::new(p.PPI_CH1); + ppi.set_event(button2.event_in()); + ppi.set_task(led1.task_clr()); + ppi.enable(); + + let mut ppi = Ppi::new(p.PPI_CH2); + ppi.set_event(button3.event_in()); + ppi.set_task(led1.task_set()); + ppi.enable(); + + let mut ppi = Ppi::new(p.PPI_CH3); + ppi.set_event(button4.event_in()); + ppi.set_task(led1.task_out()); + ppi.set_fork_task(led2.task_out()); + ppi.enable(); + + info!("PPI setup!"); + info!("Press button 1 to toggle LED 1"); + info!("Press button 2 to turn on LED 1"); + info!("Press button 3 to turn off LED 1"); + info!("Press button 4 to toggle LEDs 1 and 2"); + // Block forever so the above drivers don't get dropped + pending::<()>().await; +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let executor = EXECUTOR.put(Executor::new()); + executor.run(|spawner| { + unwrap!(spawner.spawn(run())); + }); +} diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index c321c0fd..412eef1b 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -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 { + /// 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 { + 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 { + 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>, } diff --git a/embassy-nrf/src/ppi.rs b/embassy-nrf/src/ppi.rs index c06b212c..0129a2a4 100644 --- a/embassy-nrf/src/ppi.rs +++ b/embassy-nrf/src/ppi.rs @@ -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 + '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(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(reg: &T) -> Self { + Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut ()) }) + } +} + mod sealed { pub trait ConfigurableChannel {} pub trait Channel {}