aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/ppi/mod.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-10-26 16:10:34 +0200
committerDario Nieuwenhuis <[email protected]>2021-10-26 16:52:51 +0200
commit36d3eda2f9d24be5a581e95badd74bce7060572e (patch)
tree97b3b63bb6fd5d487ff14d47b2a986b836b9624a /embassy-nrf/src/ppi/mod.rs
parentc63d74720980fcf9acbf5b1d8adbb9dc2031d391 (diff)
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`.
Diffstat (limited to 'embassy-nrf/src/ppi/mod.rs')
-rw-r--r--embassy-nrf/src/ppi/mod.rs121
1 files changed, 4 insertions, 117 deletions
diff --git a/embassy-nrf/src/ppi/mod.rs b/embassy-nrf/src/ppi/mod.rs
index cea8a0e68..96f867d18 100644
--- a/embassy-nrf/src/ppi/mod.rs
+++ b/embassy-nrf/src/ppi/mod.rs
@@ -19,7 +19,7 @@ use crate::{pac, peripherals};
19use core::marker::PhantomData; 19use core::marker::PhantomData;
20use core::ptr::NonNull; 20use core::ptr::NonNull;
21use embassy::util::Unborrow; 21use embassy::util::Unborrow;
22use embassy_hal_common::{unborrow, unsafe_impl_unborrow}; 22use embassy_hal_common::unsafe_impl_unborrow;
23 23
24#[cfg(feature = "_dppi")] 24#[cfg(feature = "_dppi")]
25mod dppi; 25mod dppi;
@@ -53,106 +53,6 @@ impl<'d, C: Channel + 'd, const EVENT_COUNT: usize, const TASK_COUNT: usize>
53 } 53 }
54} 54}
55 55
56impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop
57 for Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
58{
59 fn drop(&mut self) {
60 self.disable();
61 self.disable_all();
62 }
63}
64
65#[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task
66impl<'d, C: StaticChannel> Ppi<'d, C, 0, 1> {
67 pub fn new_zero_to_one(ch: impl Unborrow<Target = C> + 'd, task: Task) -> Self {
68 unborrow!(ch);
69
70 let events = [];
71 let tasks = [task];
72
73 Self::enable_all(&tasks, &events, &ch);
74
75 Self {
76 ch,
77 #[cfg(feature = "_dppi")]
78 events,
79 #[cfg(feature = "_dppi")]
80 tasks,
81 phantom: PhantomData,
82 }
83 }
84}
85
86impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
87 pub fn new_one_to_one(ch: impl Unborrow<Target = C> + 'd, event: Event, task: Task) -> Self {
88 unborrow!(ch);
89
90 let events = [event];
91 let tasks = [task];
92
93 Self::enable_all(&tasks, &events, &ch);
94
95 Self {
96 ch,
97 #[cfg(feature = "_dppi")]
98 events,
99 #[cfg(feature = "_dppi")]
100 tasks,
101 phantom: PhantomData,
102 }
103 }
104}
105
106#[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task
107impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
108 pub fn new_one_to_two(
109 ch: impl Unborrow<Target = C> + 'd,
110 event: Event,
111 task1: Task,
112 task2: Task,
113 ) -> Self {
114 unborrow!(ch);
115
116 let events = [event];
117 let tasks = [task1, task2];
118
119 Self::enable_all(&tasks, &events, &ch);
120
121 Self {
122 ch,
123 #[cfg(feature = "_dppi")]
124 events,
125 #[cfg(feature = "_dppi")]
126 tasks,
127 phantom: PhantomData,
128 }
129 }
130}
131
132#[cfg(feature = "_dppi")]
133impl<'d, C: ConfigurableChannel, const EVENT_COUNT: usize, const TASK_COUNT: usize>
134 Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
135{
136 pub fn new_many_to_many(
137 ch: impl Unborrow<Target = C> + 'd,
138 events: [Event; EVENT_COUNT],
139 tasks: [Task; TASK_COUNT],
140 ) -> Self {
141 unborrow!(ch);
142
143 Self::enable_all(&tasks, &events, &ch);
144
145 Self {
146 ch,
147 #[cfg(feature = "_dppi")]
148 events,
149 #[cfg(feature = "_dppi")]
150 tasks,
151 phantom: PhantomData,
152 }
153 }
154}
155
156const REGISTER_DPPI_CONFIG_OFFSET: usize = 0x80 / core::mem::size_of::<u32>(); 56const REGISTER_DPPI_CONFIG_OFFSET: usize = 0x80 / core::mem::size_of::<u32>();
157 57
158/// Represents a task that a peripheral can do. 58/// Represents a task that a peripheral can do.
@@ -209,7 +109,6 @@ pub(crate) mod sealed {
209pub trait Channel: sealed::Channel + Unborrow<Target = Self> + Sized { 109pub trait Channel: sealed::Channel + Unborrow<Target = Self> + Sized {
210 /// Returns the number of the channel 110 /// Returns the number of the channel
211 fn number(&self) -> usize; 111 fn number(&self) -> usize;
212 fn configurable() -> bool;
213} 112}
214 113
215pub trait ConfigurableChannel: Channel { 114pub trait ConfigurableChannel: Channel {
@@ -243,10 +142,6 @@ impl Channel for AnyStaticChannel {
243 fn number(&self) -> usize { 142 fn number(&self) -> usize {
244 self.number as usize 143 self.number as usize
245 } 144 }
246
247 fn configurable() -> bool {
248 false
249 }
250} 145}
251impl StaticChannel for AnyStaticChannel { 146impl StaticChannel for AnyStaticChannel {
252 fn degrade(self) -> AnyStaticChannel { 147 fn degrade(self) -> AnyStaticChannel {
@@ -265,10 +160,6 @@ impl Channel for AnyConfigurableChannel {
265 fn number(&self) -> usize { 160 fn number(&self) -> usize {
266 self.number as usize 161 self.number as usize
267 } 162 }
268
269 fn configurable() -> bool {
270 true
271 }
272} 163}
273impl ConfigurableChannel for AnyConfigurableChannel { 164impl ConfigurableChannel for AnyConfigurableChannel {
274 fn degrade(self) -> AnyConfigurableChannel { 165 fn degrade(self) -> AnyConfigurableChannel {
@@ -277,20 +168,16 @@ impl ConfigurableChannel for AnyConfigurableChannel {
277} 168}
278 169
279macro_rules! impl_ppi_channel { 170macro_rules! impl_ppi_channel {
280 ($type:ident, $number:expr, $configurability:expr) => { 171 ($type:ident, $number:expr) => {
281 impl crate::ppi::sealed::Channel for peripherals::$type {} 172 impl crate::ppi::sealed::Channel for peripherals::$type {}
282 impl crate::ppi::Channel for peripherals::$type { 173 impl crate::ppi::Channel for peripherals::$type {
283 fn number(&self) -> usize { 174 fn number(&self) -> usize {
284 $number 175 $number
285 } 176 }
286
287 fn configurable() -> bool {
288 $configurability
289 }
290 } 177 }
291 }; 178 };
292 ($type:ident, $number:expr => static) => { 179 ($type:ident, $number:expr => static) => {
293 impl_ppi_channel!($type, $number, false); 180 impl_ppi_channel!($type, $number);
294 impl crate::ppi::StaticChannel for peripherals::$type { 181 impl crate::ppi::StaticChannel for peripherals::$type {
295 fn degrade(self) -> crate::ppi::AnyStaticChannel { 182 fn degrade(self) -> crate::ppi::AnyStaticChannel {
296 use crate::ppi::Channel; 183 use crate::ppi::Channel;
@@ -301,7 +188,7 @@ macro_rules! impl_ppi_channel {
301 } 188 }
302 }; 189 };
303 ($type:ident, $number:expr => configurable) => { 190 ($type:ident, $number:expr => configurable) => {
304 impl_ppi_channel!($type, $number, true); 191 impl_ppi_channel!($type, $number);
305 impl crate::ppi::ConfigurableChannel for peripherals::$type { 192 impl crate::ppi::ConfigurableChannel for peripherals::$type {
306 fn degrade(self) -> crate::ppi::AnyConfigurableChannel { 193 fn degrade(self) -> crate::ppi::AnyConfigurableChannel {
307 use crate::ppi::Channel; 194 use crate::ppi::Channel;