From 663141b4e456bbfacaaff8decdba6840c76a136b Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 28 Oct 2021 03:07:06 +0200 Subject: nrf: add initial nrf5340 support --- embassy-nrf/src/ppi/dppi.rs | 22 ++++++++++++++++++++++ embassy-nrf/src/ppi/mod.rs | 20 +------------------- embassy-nrf/src/ppi/ppi.rs | 28 ++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 23 deletions(-) (limited to 'embassy-nrf/src/ppi') diff --git a/embassy-nrf/src/ppi/dppi.rs b/embassy-nrf/src/ppi/dppi.rs index b3676fca4..1842590b4 100644 --- a/embassy-nrf/src/ppi/dppi.rs +++ b/embassy-nrf/src/ppi/dppi.rs @@ -3,11 +3,17 @@ use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::unborrow; +use crate::pac; + use super::{Channel, ConfigurableChannel, Event, Ppi, Task}; const DPPI_ENABLE_BIT: u32 = 0x8000_0000; const DPPI_CHANNEL_MASK: u32 = 0x0000_00FF; +fn regs() -> &'static pac::dppic::RegisterBlock { + unsafe { &*pac::DPPIC::ptr() } +} + impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> { pub fn new_one_to_one(ch: impl Unborrow + 'd, event: Event, task: Task) -> Self { Ppi::new_many_to_many(ch, [event], [task]) @@ -58,6 +64,22 @@ impl<'d, C: ConfigurableChannel, const EVENT_COUNT: usize, const TASK_COUNT: usi } } +impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> + Ppi<'d, C, EVENT_COUNT, TASK_COUNT> +{ + /// Enables the channel. + pub fn enable(&mut self) { + let n = self.ch.number(); + regs().chenset.write(|w| unsafe { w.bits(1 << n) }); + } + + /// Disables the channel. + pub fn disable(&mut self) { + let n = self.ch.number(); + regs().chenclr.write(|w| unsafe { w.bits(1 << n) }); + } +} + impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop for Ppi<'d, C, EVENT_COUNT, TASK_COUNT> { diff --git a/embassy-nrf/src/ppi/mod.rs b/embassy-nrf/src/ppi/mod.rs index 96f867d18..aeccb154b 100644 --- a/embassy-nrf/src/ppi/mod.rs +++ b/embassy-nrf/src/ppi/mod.rs @@ -15,7 +15,7 @@ //! many tasks and events, but any single task or event can only be coupled with one channel. //! -use crate::{pac, peripherals}; +use crate::peripherals; use core::marker::PhantomData; use core::ptr::NonNull; use embassy::util::Unborrow; @@ -35,24 +35,6 @@ pub struct Ppi<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize phantom: PhantomData<&'d mut C>, } -impl<'d, C: Channel + 'd, const EVENT_COUNT: usize, const TASK_COUNT: usize> - Ppi<'d, C, EVENT_COUNT, TASK_COUNT> -{ - /// 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()) }); - } -} - const REGISTER_DPPI_CONFIG_OFFSET: usize = 0x80 / core::mem::size_of::(); /// Represents a task that a peripheral can do. diff --git a/embassy-nrf/src/ppi/ppi.rs b/embassy-nrf/src/ppi/ppi.rs index c1d9794c9..cdbe046f8 100644 --- a/embassy-nrf/src/ppi/ppi.rs +++ b/embassy-nrf/src/ppi/ppi.rs @@ -17,12 +17,16 @@ impl Event { } } +fn regs() -> &'static pac::ppi::RegisterBlock { + unsafe { &*pac::PPI::ptr() } +} + #[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task impl<'d, C: StaticChannel> Ppi<'d, C, 0, 1> { pub fn new_zero_to_one(ch: impl Unborrow + 'd, task: Task) -> Self { unborrow!(ch); - let r = unsafe { &*pac::PPI::ptr() }; + let r = regs(); let n = ch.number(); r.fork[n].tep.write(|w| unsafe { w.bits(task.reg_val()) }); @@ -37,7 +41,7 @@ impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> { pub fn new_one_to_one(ch: impl Unborrow + 'd, event: Event, task: Task) -> Self { unborrow!(ch); - let r = unsafe { &*pac::PPI::ptr() }; + let r = regs(); let n = ch.number(); r.ch[n].eep.write(|w| unsafe { w.bits(event.reg_val()) }); r.ch[n].tep.write(|w| unsafe { w.bits(task.reg_val()) }); @@ -59,7 +63,7 @@ impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> { ) -> Self { unborrow!(ch); - let r = unsafe { &*pac::PPI::ptr() }; + let r = regs(); let n = ch.number(); r.ch[n].eep.write(|w| unsafe { w.bits(event.reg_val()) }); r.ch[n].tep.write(|w| unsafe { w.bits(task1.reg_val()) }); @@ -72,13 +76,29 @@ impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> { } } +impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> + Ppi<'d, C, EVENT_COUNT, TASK_COUNT> +{ + /// Enables the channel. + pub fn enable(&mut self) { + let n = self.ch.number(); + regs().chenset.write(|w| unsafe { w.bits(1 << n) }); + } + + /// Disables the channel. + pub fn disable(&mut self) { + let n = self.ch.number(); + regs().chenclr.write(|w| unsafe { w.bits(1 << n) }); + } +} + impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop for Ppi<'d, C, EVENT_COUNT, TASK_COUNT> { fn drop(&mut self) { self.disable(); - let r = unsafe { &*pac::PPI::ptr() }; + let r = regs(); let n = self.ch.number(); r.ch[n].eep.write(|w| unsafe { w.bits(0) }); r.ch[n].tep.write(|w| unsafe { w.bits(0) }); -- cgit