ppi: simplify driver creation.

Moving `new_*` to the version-specific mod allows doing the correct
register writes right there in `new`, without needing abstractions
like `enable_all`/`disable_all`.
This commit is contained in:
Dario Nieuwenhuis
2021-10-26 16:10:34 +02:00
parent c63d747209
commit 36d3eda2f9
3 changed files with 143 additions and 237 deletions

View File

@ -1,81 +1,87 @@
use super::{Channel, Event, Ppi, Task};
use core::marker::PhantomData;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task};
use crate::pac;
impl<'d, C: Channel + 'd, const EVENT_COUNT: usize, const TASK_COUNT: usize>
Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
{
fn set_main_task(task: Option<&Task>, channel: usize) {
impl Task {
fn reg_val(&self) -> u32 {
self.0.as_ptr() as _
}
}
impl Event {
fn reg_val(&self) -> u32 {
self.0.as_ptr() as _
}
}
#[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<Target = C> + 'd, task: Task) -> Self {
unborrow!(ch);
let r = unsafe { &*pac::PPI::ptr() };
if let Some(task) = task {
r.ch[channel]
.tep
.write(|w| unsafe { w.bits(task.0.as_ptr() as u32) })
} else {
r.ch[channel].tep.write(|w| unsafe { w.bits(0) })
}
}
let n = ch.number();
r.fork[n].tep.write(|w| unsafe { w.bits(task.reg_val()) });
#[cfg(not(feature = "nrf51"))]
fn set_fork_task(task: Option<&Task>, channel: usize) {
let r = unsafe { &*pac::PPI::ptr() };
if let Some(task) = task {
r.fork[channel]
.tep
.write(|w| unsafe { w.bits(task.0.as_ptr() as u32) })
} else {
r.fork[channel].tep.write(|w| unsafe { w.bits(0) })
}
}
fn set_event(event: Option<&Event>, channel: usize) {
let r = unsafe { &*pac::PPI::ptr() };
if let Some(event) = event {
r.ch[channel]
.eep
.write(|w| unsafe { w.bits(event.0.as_ptr() as u32) })
} else {
r.ch[channel].eep.write(|w| unsafe { w.bits(0) })
}
}
/// Enables all tasks and events
pub(super) fn enable_all(tasks: &[Task], events: &[Event], channel: &C) {
// One configurable task, no fork
if C::configurable() && TASK_COUNT == 1 {
Self::set_main_task(Some(&tasks[0]), channel.number());
}
// One configurable task, as fork
#[cfg(not(feature = "nrf51"))]
if !C::configurable() && TASK_COUNT == 1 {
Self::set_fork_task(Some(&tasks[0]), channel.number());
}
// Two configurable tasks (main + fork)
#[cfg(not(feature = "nrf51"))]
if TASK_COUNT == 2 {
Self::set_main_task(Some(&tasks[0]), channel.number());
Self::set_fork_task(Some(&tasks[1]), channel.number());
}
if EVENT_COUNT == 1 {
Self::set_event(Some(&events[0]), channel.number());
}
}
/// Disable all tasks and events
pub(super) fn disable_all(&self) {
if C::configurable() {
Self::set_main_task(None, self.ch.number());
}
#[cfg(not(feature = "nrf51"))]
if TASK_COUNT == 1 && !C::configurable() || TASK_COUNT == 2 {
Self::set_fork_task(None, self.ch.number());
}
if EVENT_COUNT == 1 {
Self::set_event(None, self.ch.number());
Self {
ch,
phantom: PhantomData,
}
}
}
impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
pub fn new_one_to_one(ch: impl Unborrow<Target = C> + 'd, event: Event, task: Task) -> Self {
unborrow!(ch);
let r = unsafe { &*pac::PPI::ptr() };
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()) });
Self {
ch,
phantom: PhantomData,
}
}
}
#[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task
impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
pub fn new_one_to_two(
ch: impl Unborrow<Target = C> + 'd,
event: Event,
task1: Task,
task2: Task,
) -> Self {
unborrow!(ch);
let r = unsafe { &*pac::PPI::ptr() };
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()) });
r.fork[n].tep.write(|w| unsafe { w.bits(task2.reg_val()) });
Self {
ch,
phantom: PhantomData,
}
}
}
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 n = self.ch.number();
r.ch[n].eep.write(|w| unsafe { w.bits(0) });
r.ch[n].tep.write(|w| unsafe { w.bits(0) });
r.fork[n].tep.write(|w| unsafe { w.bits(0) });
}
}