aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src
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
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')
-rw-r--r--embassy-nrf/src/ppi/dppi.rs95
-rw-r--r--embassy-nrf/src/ppi/mod.rs121
-rw-r--r--embassy-nrf/src/ppi/ppi.rs128
3 files changed, 125 insertions, 219 deletions
diff --git a/embassy-nrf/src/ppi/dppi.rs b/embassy-nrf/src/ppi/dppi.rs
index 083ec858a..b3676fca4 100644
--- a/embassy-nrf/src/ppi/dppi.rs
+++ b/embassy-nrf/src/ppi/dppi.rs
@@ -1,61 +1,74 @@
1use super::{Channel, Event, Ppi, Task}; 1use core::marker::PhantomData;
2
3use embassy::util::Unborrow;
4use embassy_hal_common::unborrow;
5
6use super::{Channel, ConfigurableChannel, Event, Ppi, Task};
2 7
3const DPPI_ENABLE_BIT: u32 = 0x8000_0000; 8const DPPI_ENABLE_BIT: u32 = 0x8000_0000;
4const DPPI_CHANNEL_MASK: u32 = 0x0000_00FF; 9const DPPI_CHANNEL_MASK: u32 = 0x0000_00FF;
5 10
6impl<'d, C: Channel + 'd, const EVENT_COUNT: usize, const TASK_COUNT: usize> 11impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
7 Ppi<'d, C, EVENT_COUNT, TASK_COUNT> 12 pub fn new_one_to_one(ch: impl Unborrow<Target = C> + 'd, event: Event, task: Task) -> Self {
8{ 13 Ppi::new_many_to_many(ch, [event], [task])
9 pub(super) fn enable_task(task: &Task, channel: &C, _index: usize) {
10 unsafe {
11 if task.subscribe_reg().read_volatile() != 0 {
12 panic!("Task is already in use");
13 }
14 task.subscribe_reg()
15 .write_volatile(DPPI_ENABLE_BIT | (channel.number() as u32 & DPPI_CHANNEL_MASK));
16 }
17 } 14 }
15}
18 16
19 pub(super) fn disable_task(task: &Task, _channel: &C, _index: usize) { 17impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
20 unsafe { 18 pub fn new_one_to_two(
21 task.subscribe_reg().write_volatile(0); 19 ch: impl Unborrow<Target = C> + 'd,
22 } 20 event: Event,
21 task1: Task,
22 task2: Task,
23 ) -> Self {
24 Ppi::new_many_to_many(ch, [event], [task1, task2])
23 } 25 }
26}
24 27
25 pub(super) fn enable_event(event: &Event, channel: &C, _index: usize) { 28impl<'d, C: ConfigurableChannel, const EVENT_COUNT: usize, const TASK_COUNT: usize>
26 unsafe { 29 Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
27 if event.publish_reg().read_volatile() != 0 { 30{
31 pub fn new_many_to_many(
32 ch: impl Unborrow<Target = C> + 'd,
33 events: [Event; EVENT_COUNT],
34 tasks: [Task; TASK_COUNT],
35 ) -> Self {
36 unborrow!(ch);
37
38 let val = DPPI_ENABLE_BIT | (ch.number() as u32 & DPPI_CHANNEL_MASK);
39 for task in tasks {
40 if unsafe { task.subscribe_reg().read_volatile() } != 0 {
28 panic!("Task is already in use"); 41 panic!("Task is already in use");
29 } 42 }
30 event 43 unsafe { task.subscribe_reg().write_volatile(val) }
31 .publish_reg()
32 .write_volatile(DPPI_ENABLE_BIT | (channel.number() as u32 & DPPI_CHANNEL_MASK));
33 } 44 }
34 } 45 for event in events {
35 46 if unsafe { event.publish_reg().read_volatile() } != 0 {
36 pub(super) fn disable_event(event: &Event, _channel: &C, _index: usize) { 47 panic!("Event is already in use");
37 unsafe { 48 }
38 event.publish_reg().write_volatile(0); 49 unsafe { event.publish_reg().write_volatile(val) }
39 } 50 }
40 }
41 51
42 /// Enables all tasks and events 52 Self {
43 pub(super) fn enable_all(tasks: &[Task], events: &[Event], channel: &C) { 53 ch,
44 for (index, task) in tasks.iter().enumerate() { 54 events,
45 Self::enable_task(task, channel, index); 55 tasks,
46 } 56 phantom: PhantomData,
47 for (index, event) in events.iter().enumerate() {
48 Self::enable_event(event, channel, index);
49 } 57 }
50 } 58 }
59}
60
61impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop
62 for Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
63{
64 fn drop(&mut self) {
65 self.disable();
51 66
52 /// Disable all tasks and events 67 for task in self.tasks {
53 pub(super) fn disable_all(&self) { 68 unsafe { task.subscribe_reg().write_volatile(0) }
54 for (index, task) in self.tasks.iter().enumerate() {
55 Self::disable_task(task, &self.ch, index);
56 } 69 }
57 for (index, event) in self.events.iter().enumerate() { 70 for event in self.events {
58 Self::disable_event(event, &self.ch, index); 71 unsafe { event.publish_reg().write_volatile(0) }
59 } 72 }
60 } 73 }
61} 74}
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;
diff --git a/embassy-nrf/src/ppi/ppi.rs b/embassy-nrf/src/ppi/ppi.rs
index 0bcb3a791..c1d9794c9 100644
--- a/embassy-nrf/src/ppi/ppi.rs
+++ b/embassy-nrf/src/ppi/ppi.rs
@@ -1,81 +1,87 @@
1use super::{Channel, Event, Ppi, Task}; 1use core::marker::PhantomData;
2
3use embassy::util::Unborrow;
4use embassy_hal_common::unborrow;
5
6use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task};
2use crate::pac; 7use crate::pac;
3 8
4impl<'d, C: Channel + 'd, const EVENT_COUNT: usize, const TASK_COUNT: usize> 9impl Task {
5 Ppi<'d, C, EVENT_COUNT, TASK_COUNT> 10 fn reg_val(&self) -> u32 {
6{ 11 self.0.as_ptr() as _
7 fn set_main_task(task: Option<&Task>, channel: usize) { 12 }
8 let r = unsafe { &*pac::PPI::ptr() }; 13}
9 if let Some(task) = task { 14impl Event {
10 r.ch[channel] 15 fn reg_val(&self) -> u32 {
11 .tep 16 self.0.as_ptr() as _
12 .write(|w| unsafe { w.bits(task.0.as_ptr() as u32) })
13 } else {
14 r.ch[channel].tep.write(|w| unsafe { w.bits(0) })
15 }
16 } 17 }
18}
19
20#[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task
21impl<'d, C: StaticChannel> Ppi<'d, C, 0, 1> {
22 pub fn new_zero_to_one(ch: impl Unborrow<Target = C> + 'd, task: Task) -> Self {
23 unborrow!(ch);
17 24
18 #[cfg(not(feature = "nrf51"))]
19 fn set_fork_task(task: Option<&Task>, channel: usize) {
20 let r = unsafe { &*pac::PPI::ptr() }; 25 let r = unsafe { &*pac::PPI::ptr() };
21 if let Some(task) = task { 26 let n = ch.number();
22 r.fork[channel] 27 r.fork[n].tep.write(|w| unsafe { w.bits(task.reg_val()) });
23 .tep 28
24 .write(|w| unsafe { w.bits(task.0.as_ptr() as u32) }) 29 Self {
25 } else { 30 ch,
26 r.fork[channel].tep.write(|w| unsafe { w.bits(0) }) 31 phantom: PhantomData,
27 } 32 }
28 } 33 }
34}
35
36impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 1> {
37 pub fn new_one_to_one(ch: impl Unborrow<Target = C> + 'd, event: Event, task: Task) -> Self {
38 unborrow!(ch);
29 39
30 fn set_event(event: Option<&Event>, channel: usize) {
31 let r = unsafe { &*pac::PPI::ptr() }; 40 let r = unsafe { &*pac::PPI::ptr() };
32 if let Some(event) = event { 41 let n = ch.number();
33 r.ch[channel] 42 r.ch[n].eep.write(|w| unsafe { w.bits(event.reg_val()) });
34 .eep 43 r.ch[n].tep.write(|w| unsafe { w.bits(task.reg_val()) });
35 .write(|w| unsafe { w.bits(event.0.as_ptr() as u32) })
36 } else {
37 r.ch[channel].eep.write(|w| unsafe { w.bits(0) })
38 }
39 }
40 44
41 /// Enables all tasks and events 45 Self {
42 pub(super) fn enable_all(tasks: &[Task], events: &[Event], channel: &C) { 46 ch,
43 // One configurable task, no fork 47 phantom: PhantomData,
44 if C::configurable() && TASK_COUNT == 1 {
45 Self::set_main_task(Some(&tasks[0]), channel.number());
46 } 48 }
49 }
50}
47 51
48 // One configurable task, as fork 52#[cfg(not(feature = "nrf51"))] // Not for nrf51 because of the fork task
49 #[cfg(not(feature = "nrf51"))] 53impl<'d, C: ConfigurableChannel> Ppi<'d, C, 1, 2> {
50 if !C::configurable() && TASK_COUNT == 1 { 54 pub fn new_one_to_two(
51 Self::set_fork_task(Some(&tasks[0]), channel.number()); 55 ch: impl Unborrow<Target = C> + 'd,
52 } 56 event: Event,
57 task1: Task,
58 task2: Task,
59 ) -> Self {
60 unborrow!(ch);
53 61
54 // Two configurable tasks (main + fork) 62 let r = unsafe { &*pac::PPI::ptr() };
55 #[cfg(not(feature = "nrf51"))] 63 let n = ch.number();
56 if TASK_COUNT == 2 { 64 r.ch[n].eep.write(|w| unsafe { w.bits(event.reg_val()) });
57 Self::set_main_task(Some(&tasks[0]), channel.number()); 65 r.ch[n].tep.write(|w| unsafe { w.bits(task1.reg_val()) });
58 Self::set_fork_task(Some(&tasks[1]), channel.number()); 66 r.fork[n].tep.write(|w| unsafe { w.bits(task2.reg_val()) });
59 }
60 67
61 if EVENT_COUNT == 1 { 68 Self {
62 Self::set_event(Some(&events[0]), channel.number()); 69 ch,
70 phantom: PhantomData,
63 } 71 }
64 } 72 }
73}
65 74
66 /// Disable all tasks and events 75impl<'d, C: Channel, const EVENT_COUNT: usize, const TASK_COUNT: usize> Drop
67 pub(super) fn disable_all(&self) { 76 for Ppi<'d, C, EVENT_COUNT, TASK_COUNT>
68 if C::configurable() { 77{
69 Self::set_main_task(None, self.ch.number()); 78 fn drop(&mut self) {
70 } 79 self.disable();
71
72 #[cfg(not(feature = "nrf51"))]
73 if TASK_COUNT == 1 && !C::configurable() || TASK_COUNT == 2 {
74 Self::set_fork_task(None, self.ch.number());
75 }
76 80
77 if EVENT_COUNT == 1 { 81 let r = unsafe { &*pac::PPI::ptr() };
78 Self::set_event(None, self.ch.number()); 82 let n = self.ch.number();
79 } 83 r.ch[n].eep.write(|w| unsafe { w.bits(0) });
84 r.ch[n].tep.write(|w| unsafe { w.bits(0) });
85 r.fork[n].tep.write(|w| unsafe { w.bits(0) });
80 } 86 }
81} 87}