Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPI channel group tasks #212

Merged
merged 4 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion nrf-hal-common/src/ppi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//! 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 crate::pac::generic::Reg;
use crate::pac::ppi::tasks_chg::{_DIS, _EN};
use crate::pac::PPI;
use cfg_if::cfg_if;

Expand Down Expand Up @@ -52,8 +54,12 @@ mod sealed {
}

pub trait NotFixed {}

pub trait ChannelGroup {
const CHG: usize;
}
}
use sealed::{Channel, Event, NotFixed, Task};
use sealed::{Channel, ChannelGroup, Event, NotFixed, Task};

pub struct TaskAddr(pub(crate) u32);
pub struct EventAddr(pub(crate) u32);
Expand Down Expand Up @@ -83,6 +89,20 @@ pub trait ConfigurablePpi {
fn set_event_endpoint<E: Event>(&mut self, event: &E);
}

/// Trait for a PPI channel group.
pub trait PpiChannelGroup {
/// Returns reference to tasks_chg[x].en endpoint for enabling channel group.
jackscan marked this conversation as resolved.
Show resolved Hide resolved
fn task_enable(&self) -> &Reg<u32, _EN>;
/// Returns reference to tasks_chg[x].dis endpoint for disabling channel group.
jackscan marked this conversation as resolved.
Show resolved Hide resolved
fn task_disable(&self) -> &Reg<u32, _DIS>;
/// Sets bitmask for PPI channels which shall be included in this channel group.
fn set_channels(&self, mask: u32);
/// Enables this channel group.
fn enable(&self);
/// Disables this channel group.
fn disable(&self);
}

// All unsafe `ptr` calls only uses registers atomically, and only changes the resources owned by
// the type (guaranteed by the abstraction).
impl<P: Channel> Ppi for P {
Expand Down Expand Up @@ -128,13 +148,42 @@ impl<P: Channel + NotFixed> ConfigurablePpi for P {
}
}

impl<G: ChannelGroup> PpiChannelGroup for G {
#[inline(always)]
fn task_enable(&self) -> &Reg<u32, _EN> {
let regs = unsafe { &*PPI::ptr() };
&regs.tasks_chg[Self::CHG].en
}
#[inline(always)]
fn task_disable(&self) -> &Reg<u32, _DIS> {
let regs = unsafe { &*PPI::ptr() };
&regs.tasks_chg[Self::CHG].dis
}
#[inline(always)]
fn set_channels(&self, mask: u32) {
let regs = unsafe { &*PPI::ptr() };
regs.chg[Self::CHG].write(|w| unsafe { w.bits(mask) });
}
#[inline(always)]
fn enable(&self) {
self.task_enable().write(|w| unsafe { w.bits(1) });
}
#[inline(always)]
fn disable(&self) {
self.task_disable().write(|w| unsafe { w.bits(1) });
}
}

macro_rules! ppi {
(
not_fixed: [ $(
$(#[$attr:meta])*
($ppix:ident, $PpixType:ident, $ch:expr),)+
],
fixed: [$(($ppix_fixed:ident, $PpixTypeFixed:ident, $ch_fixed:expr),)+],
groups: [$(
$(#[$chgattr:meta])*
($chgx:ident, $ChgxType:ident, $chg:expr),)+],
) => {

$(
Expand Down Expand Up @@ -164,6 +213,19 @@ macro_rules! ppi {
}
)+

$(
/// Channel groups.
$(#[$chgattr])*
pub struct $ChgxType {
_private: (),
}

$(#[$chgattr])*
impl ChannelGroup for $ChgxType {
const CHG: usize = $chg;
}
)*

/// Type that abstracts all the PPI channels.
pub struct Parts {
$(
Expand All @@ -173,6 +235,10 @@ macro_rules! ppi {
$(
pub $ppix_fixed: $PpixTypeFixed,
)+
$(
$(#[$chgattr])*
pub $chgx: $ChgxType,
)*
}

impl Parts {
Expand All @@ -191,6 +257,12 @@ macro_rules! ppi {
_private: (),
},
)+
$(
$(#[$chgattr])*
$chgx: $ChgxType {
_private: (),
},
)*
}
}
}
Expand Down Expand Up @@ -238,4 +310,14 @@ ppi!(
(ppi30, Ppi30, 30),
(ppi31, Ppi31, 31),
],
groups: [
(chg0, Chg0, 0),
(chg1, Chg1, 1),
(chg2, Chg2, 2),
(chg3, Chg3, 3),
#[cfg(not(feature = "51"))]
(chg4, Chg4, 4),
#[cfg(not(feature = "51"))]
(chg5, Chg5, 5),
],
);
4 changes: 4 additions & 0 deletions nrf-hal-common/src/ppi/task_nrf52832.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::ppi::Task;

// Task Impls for PPI channel groups
impl Task for crate::pac::ppi::tasks_chg::EN {}
impl Task for crate::pac::ppi::tasks_chg::DIS {}

// Task Impls
//
// To reproduce, in the pac crate, search
Expand Down