aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/ppi/ppi.rs
blob: 18bc8b8db39d55a619f37d18a6b1ed9d5ad428ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::{Channel, ConfigurableChannel, Event, Ppi, Task};
use crate::{Peri, pac};

impl<'d> Task<'d> {
    fn reg_val(&self) -> u32 {
        self.0.as_ptr() as _
    }
}
impl<'d> Event<'d> {
    fn reg_val(&self) -> u32 {
        self.0.as_ptr() as _
    }
}

pub(crate) fn regs() -> pac::ppi::Ppi {
    pac::PPI
}

#[cfg(not(feature = "_nrf51"))] // Not for nrf51 because of the fork task
impl<'d, C: super::StaticChannel> Ppi<'d, C, 0, 1> {
    /// Configure PPI channel to trigger `task`.
    pub fn new_zero_to_one(ch: Peri<'d, C>, task: Task) -> Self {
        let r = regs();
        let n = ch.number();
        r.fork(n).tep().write_value(task.reg_val());

        Self { ch }
    }
}

impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
    /// Configure PPI channel to trigger `task` on `event`.
    pub fn new_one_to_one(ch: Peri<'d, C>, event: Event<'d>, task: Task<'d>) -> Self {
        let r = regs();
        let n = ch.number();
        r.ch(n).eep().write_value(event.reg_val());
        r.ch(n).tep().write_value(task.reg_val());

        Self { ch }
    }
}

#[cfg(not(feature = "_nrf51"))] // Not for nrf51 because of the fork task
impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
    /// Configure PPI channel to trigger both `task1` and `task2` on `event`.
    pub fn new_one_to_two(ch: Peri<'d, C>, event: Event<'d>, task1: Task<'d>, task2: Task<'d>) -> Self {
        let r = regs();
        let n = ch.number();
        r.ch(n).eep().write_value(event.reg_val());
        r.ch(n).tep().write_value(task1.reg_val());
        r.fork(n).tep().write_value(task2.reg_val());

        Self { ch }
    }
}

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| w.set_ch(n, true));
    }

    /// Disables the channel.
    pub fn disable(&mut self) {
        let n = self.ch.number();
        regs().chenclr().write(|w| w.set_ch(n, true));
    }
}

impl<C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Ppi<'static, C, EVENT_COUNT, TASK_COUNT> {
    /// Persist the channel's configuration for the rest of the program's lifetime. This method
    /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents
    /// accidental reuse of the underlying peripheral.
    pub fn persist(self) {
        core::mem::forget(self);
    }
}

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 = regs();
        let n = self.ch.number();
        r.ch(n).eep().write_value(0);
        r.ch(n).tep().write_value(0);
        #[cfg(not(feature = "_nrf51"))]
        r.fork(n).tep().write_value(0);
    }
}