2022-07-23 14:00:19 +02:00
|
|
|
use embassy_hal_common::into_ref;
|
2021-10-26 16:10:34 +02:00
|
|
|
|
|
|
|
use super::{Channel, ConfigurableChannel, Event, Ppi, Task};
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{pac, Peripheral};
|
2021-10-19 10:13:08 +02:00
|
|
|
|
|
|
|
const DPPI_ENABLE_BIT: u32 = 0x8000_0000;
|
|
|
|
const DPPI_CHANNEL_MASK: u32 = 0x0000_00FF;
|
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
fn regs() -> &'static pac::dppic::RegisterBlock {
|
|
|
|
unsafe { &*pac::DPPIC::ptr() }
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:10:34 +02:00
|
|
|
impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new_one_to_one(ch: impl Peripheral<P = C> + 'd, event: Event, task: Task) -> Self {
|
2021-10-26 16:10:34 +02:00
|
|
|
Ppi::new_many_to_many(ch, [event], [task])
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
}
|
2021-10-19 10:13:08 +02:00
|
|
|
|
2021-10-26 16:10:34 +02:00
|
|
|
impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new_one_to_two(ch: impl Peripheral<P = C> + 'd, event: Event, task1: Task, task2: Task) -> Self {
|
2021-10-26 16:10:34 +02:00
|
|
|
Ppi::new_many_to_many(ch, [event], [task1, task2])
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
}
|
2021-10-19 10:13:08 +02:00
|
|
|
|
2021-10-26 16:10:34 +02:00
|
|
|
impl<'d, C: ConfigurableChannel, const EVENT_COUNT: usize, const TASK_COUNT: usize>
|
|
|
|
Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
|
|
|
|
{
|
|
|
|
pub fn new_many_to_many(
|
2022-07-23 14:00:19 +02:00
|
|
|
ch: impl Peripheral<P = C> + 'd,
|
2021-10-26 16:10:34 +02:00
|
|
|
events: [Event; EVENT_COUNT],
|
|
|
|
tasks: [Task; TASK_COUNT],
|
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(ch);
|
2021-10-26 16:10:34 +02:00
|
|
|
|
|
|
|
let val = DPPI_ENABLE_BIT | (ch.number() as u32 & DPPI_CHANNEL_MASK);
|
|
|
|
for task in tasks {
|
|
|
|
if unsafe { task.subscribe_reg().read_volatile() } != 0 {
|
2021-10-19 10:13:08 +02:00
|
|
|
panic!("Task is already in use");
|
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
unsafe { task.subscribe_reg().write_volatile(val) }
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
for event in events {
|
|
|
|
if unsafe { event.publish_reg().read_volatile() } != 0 {
|
|
|
|
panic!("Event is already in use");
|
|
|
|
}
|
|
|
|
unsafe { event.publish_reg().write_volatile(val) }
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 01:43:41 +02:00
|
|
|
Self { ch, events, tasks }
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Ppi<'d, C, EVENT_COUNT, TASK_COUNT> {
|
2021-10-28 03:07:06 +02:00
|
|
|
/// 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) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop for Ppi<'d, C, EVENT_COUNT, TASK_COUNT> {
|
2021-10-26 16:10:34 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.disable();
|
2021-10-19 10:13:08 +02:00
|
|
|
|
2021-10-26 16:10:34 +02:00
|
|
|
for task in self.tasks {
|
|
|
|
unsafe { task.subscribe_reg().write_volatile(0) }
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
2021-10-26 16:10:34 +02:00
|
|
|
for event in self.events {
|
|
|
|
unsafe { event.publish_reg().write_volatile(0) }
|
2021-10-19 10:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|