aboutsummaryrefslogtreecommitdiff
path: root/embassy-hal-common
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-06-11 05:08:57 +0200
committerDario Nieuwenhuis <[email protected]>2022-06-12 21:45:38 +0200
commit5085100df2845745f13715669c18a785a374a879 (patch)
treed24d264b23753d628e58fa3b92da77a78e28ce35 /embassy-hal-common
parentdb344c2bda55bd0352a43720788185cc4d3a420e (diff)
Add embassy-cortex-m crate.
- Move Interrupt and InterruptExecutor from `embassy` to `embassy-cortex-m`. - Move Unborrow from `embassy` to `embassy-hal-common` (nothing in `embassy` requires it anymore) - Move PeripheralMutex from `embassy-hal-common` to `embassy-cortex-m`.
Diffstat (limited to 'embassy-hal-common')
-rw-r--r--embassy-hal-common/src/interrupt.rs571
-rw-r--r--embassy-hal-common/src/lib.rs4
-rw-r--r--embassy-hal-common/src/macros.rs4
-rw-r--r--embassy-hal-common/src/peripheral.rs134
-rw-r--r--embassy-hal-common/src/unborrow.rs60
5 files changed, 64 insertions, 709 deletions
diff --git a/embassy-hal-common/src/interrupt.rs b/embassy-hal-common/src/interrupt.rs
deleted file mode 100644
index 80b2cad5d..000000000
--- a/embassy-hal-common/src/interrupt.rs
+++ /dev/null
@@ -1,571 +0,0 @@
1use core::mem;
2
3macro_rules! prio {
4 ($name:ident, $mask:expr, ($($k:ident = $v:expr,)*)) => {
5 #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
6 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
7 #[repr(u8)]
8 pub enum $name {
9 $($k = $v),*
10 }
11
12 impl From<u8> for $name {
13 fn from(priority: u8) -> Self {
14 unsafe { mem::transmute(priority & $mask) }
15 }
16 }
17
18 impl From<$name> for u8 {
19 fn from(p: $name) -> Self {
20 p as u8
21 }
22 }
23 };
24}
25
26#[rustfmt::skip]
27prio!(Priority0, 0x00, (
28 P0 = 0x0,
29));
30
31#[rustfmt::skip]
32prio!(Priority1, 0x80, (
33 P0 = 0x0,
34 P1 = 0x80,
35));
36
37#[rustfmt::skip]
38prio!(Priority2, 0xc0, (
39 P0 = 0x0,
40 P1 = 0x40,
41 P2 = 0x80,
42 P3 = 0xc0,
43));
44
45#[rustfmt::skip]
46prio!(Priority3, 0xe0, (
47 P0 = 0x0,
48 P1 = 0x20,
49 P2 = 0x40,
50 P3 = 0x60,
51 P4 = 0x80,
52 P5 = 0xa0,
53 P6 = 0xc0,
54 P7 = 0xe0,
55));
56
57#[rustfmt::skip]
58prio!(Priority4, 0xf0, (
59 P0 = 0x0,
60 P1 = 0x10,
61 P2 = 0x20,
62 P3 = 0x30,
63 P4 = 0x40,
64 P5 = 0x50,
65 P6 = 0x60,
66 P7 = 0x70,
67 P8 = 0x80,
68 P9 = 0x90,
69 P10 = 0xa0,
70 P11 = 0xb0,
71 P12 = 0xc0,
72 P13 = 0xd0,
73 P14 = 0xe0,
74 P15 = 0xf0,
75));
76
77#[rustfmt::skip]
78prio!(Priority5, 0xf8, (
79 P0 = 0x0,
80 P1 = 0x8,
81 P2 = 0x10,
82 P3 = 0x18,
83 P4 = 0x20,
84 P5 = 0x28,
85 P6 = 0x30,
86 P7 = 0x38,
87 P8 = 0x40,
88 P9 = 0x48,
89 P10 = 0x50,
90 P11 = 0x58,
91 P12 = 0x60,
92 P13 = 0x68,
93 P14 = 0x70,
94 P15 = 0x78,
95 P16 = 0x80,
96 P17 = 0x88,
97 P18 = 0x90,
98 P19 = 0x98,
99 P20 = 0xa0,
100 P21 = 0xa8,
101 P22 = 0xb0,
102 P23 = 0xb8,
103 P24 = 0xc0,
104 P25 = 0xc8,
105 P26 = 0xd0,
106 P27 = 0xd8,
107 P28 = 0xe0,
108 P29 = 0xe8,
109 P30 = 0xf0,
110 P31 = 0xf8,
111));
112
113#[rustfmt::skip]
114prio!(Priority6, 0xfc, (
115 P0 = 0x0,
116 P1 = 0x4,
117 P2 = 0x8,
118 P3 = 0xc,
119 P4 = 0x10,
120 P5 = 0x14,
121 P6 = 0x18,
122 P7 = 0x1c,
123 P8 = 0x20,
124 P9 = 0x24,
125 P10 = 0x28,
126 P11 = 0x2c,
127 P12 = 0x30,
128 P13 = 0x34,
129 P14 = 0x38,
130 P15 = 0x3c,
131 P16 = 0x40,
132 P17 = 0x44,
133 P18 = 0x48,
134 P19 = 0x4c,
135 P20 = 0x50,
136 P21 = 0x54,
137 P22 = 0x58,
138 P23 = 0x5c,
139 P24 = 0x60,
140 P25 = 0x64,
141 P26 = 0x68,
142 P27 = 0x6c,
143 P28 = 0x70,
144 P29 = 0x74,
145 P30 = 0x78,
146 P31 = 0x7c,
147 P32 = 0x80,
148 P33 = 0x84,
149 P34 = 0x88,
150 P35 = 0x8c,
151 P36 = 0x90,
152 P37 = 0x94,
153 P38 = 0x98,
154 P39 = 0x9c,
155 P40 = 0xa0,
156 P41 = 0xa4,
157 P42 = 0xa8,
158 P43 = 0xac,
159 P44 = 0xb0,
160 P45 = 0xb4,
161 P46 = 0xb8,
162 P47 = 0xbc,
163 P48 = 0xc0,
164 P49 = 0xc4,
165 P50 = 0xc8,
166 P51 = 0xcc,
167 P52 = 0xd0,
168 P53 = 0xd4,
169 P54 = 0xd8,
170 P55 = 0xdc,
171 P56 = 0xe0,
172 P57 = 0xe4,
173 P58 = 0xe8,
174 P59 = 0xec,
175 P60 = 0xf0,
176 P61 = 0xf4,
177 P62 = 0xf8,
178 P63 = 0xfc,
179));
180
181#[rustfmt::skip]
182prio!(Priority7, 0xfe, (
183 P0 = 0x0,
184 P1 = 0x2,
185 P2 = 0x4,
186 P3 = 0x6,
187 P4 = 0x8,
188 P5 = 0xa,
189 P6 = 0xc,
190 P7 = 0xe,
191 P8 = 0x10,
192 P9 = 0x12,
193 P10 = 0x14,
194 P11 = 0x16,
195 P12 = 0x18,
196 P13 = 0x1a,
197 P14 = 0x1c,
198 P15 = 0x1e,
199 P16 = 0x20,
200 P17 = 0x22,
201 P18 = 0x24,
202 P19 = 0x26,
203 P20 = 0x28,
204 P21 = 0x2a,
205 P22 = 0x2c,
206 P23 = 0x2e,
207 P24 = 0x30,
208 P25 = 0x32,
209 P26 = 0x34,
210 P27 = 0x36,
211 P28 = 0x38,
212 P29 = 0x3a,
213 P30 = 0x3c,
214 P31 = 0x3e,
215 P32 = 0x40,
216 P33 = 0x42,
217 P34 = 0x44,
218 P35 = 0x46,
219 P36 = 0x48,
220 P37 = 0x4a,
221 P38 = 0x4c,
222 P39 = 0x4e,
223 P40 = 0x50,
224 P41 = 0x52,
225 P42 = 0x54,
226 P43 = 0x56,
227 P44 = 0x58,
228 P45 = 0x5a,
229 P46 = 0x5c,
230 P47 = 0x5e,
231 P48 = 0x60,
232 P49 = 0x62,
233 P50 = 0x64,
234 P51 = 0x66,
235 P52 = 0x68,
236 P53 = 0x6a,
237 P54 = 0x6c,
238 P55 = 0x6e,
239 P56 = 0x70,
240 P57 = 0x72,
241 P58 = 0x74,
242 P59 = 0x76,
243 P60 = 0x78,
244 P61 = 0x7a,
245 P62 = 0x7c,
246 P63 = 0x7e,
247 P64 = 0x80,
248 P65 = 0x82,
249 P66 = 0x84,
250 P67 = 0x86,
251 P68 = 0x88,
252 P69 = 0x8a,
253 P70 = 0x8c,
254 P71 = 0x8e,
255 P72 = 0x90,
256 P73 = 0x92,
257 P74 = 0x94,
258 P75 = 0x96,
259 P76 = 0x98,
260 P77 = 0x9a,
261 P78 = 0x9c,
262 P79 = 0x9e,
263 P80 = 0xa0,
264 P81 = 0xa2,
265 P82 = 0xa4,
266 P83 = 0xa6,
267 P84 = 0xa8,
268 P85 = 0xaa,
269 P86 = 0xac,
270 P87 = 0xae,
271 P88 = 0xb0,
272 P89 = 0xb2,
273 P90 = 0xb4,
274 P91 = 0xb6,
275 P92 = 0xb8,
276 P93 = 0xba,
277 P94 = 0xbc,
278 P95 = 0xbe,
279 P96 = 0xc0,
280 P97 = 0xc2,
281 P98 = 0xc4,
282 P99 = 0xc6,
283 P100 = 0xc8,
284 P101 = 0xca,
285 P102 = 0xcc,
286 P103 = 0xce,
287 P104 = 0xd0,
288 P105 = 0xd2,
289 P106 = 0xd4,
290 P107 = 0xd6,
291 P108 = 0xd8,
292 P109 = 0xda,
293 P110 = 0xdc,
294 P111 = 0xde,
295 P112 = 0xe0,
296 P113 = 0xe2,
297 P114 = 0xe4,
298 P115 = 0xe6,
299 P116 = 0xe8,
300 P117 = 0xea,
301 P118 = 0xec,
302 P119 = 0xee,
303 P120 = 0xf0,
304 P121 = 0xf2,
305 P122 = 0xf4,
306 P123 = 0xf6,
307 P124 = 0xf8,
308 P125 = 0xfa,
309 P126 = 0xfc,
310 P127 = 0xfe,
311));
312
313#[rustfmt::skip]
314prio!(Priority8, 0xff, (
315 P0 = 0x0,
316 P1 = 0x1,
317 P2 = 0x2,
318 P3 = 0x3,
319 P4 = 0x4,
320 P5 = 0x5,
321 P6 = 0x6,
322 P7 = 0x7,
323 P8 = 0x8,
324 P9 = 0x9,
325 P10 = 0xa,
326 P11 = 0xb,
327 P12 = 0xc,
328 P13 = 0xd,
329 P14 = 0xe,
330 P15 = 0xf,
331 P16 = 0x10,
332 P17 = 0x11,
333 P18 = 0x12,
334 P19 = 0x13,
335 P20 = 0x14,
336 P21 = 0x15,
337 P22 = 0x16,
338 P23 = 0x17,
339 P24 = 0x18,
340 P25 = 0x19,
341 P26 = 0x1a,
342 P27 = 0x1b,
343 P28 = 0x1c,
344 P29 = 0x1d,
345 P30 = 0x1e,
346 P31 = 0x1f,
347 P32 = 0x20,
348 P33 = 0x21,
349 P34 = 0x22,
350 P35 = 0x23,
351 P36 = 0x24,
352 P37 = 0x25,
353 P38 = 0x26,
354 P39 = 0x27,
355 P40 = 0x28,
356 P41 = 0x29,
357 P42 = 0x2a,
358 P43 = 0x2b,
359 P44 = 0x2c,
360 P45 = 0x2d,
361 P46 = 0x2e,
362 P47 = 0x2f,
363 P48 = 0x30,
364 P49 = 0x31,
365 P50 = 0x32,
366 P51 = 0x33,
367 P52 = 0x34,
368 P53 = 0x35,
369 P54 = 0x36,
370 P55 = 0x37,
371 P56 = 0x38,
372 P57 = 0x39,
373 P58 = 0x3a,
374 P59 = 0x3b,
375 P60 = 0x3c,
376 P61 = 0x3d,
377 P62 = 0x3e,
378 P63 = 0x3f,
379 P64 = 0x40,
380 P65 = 0x41,
381 P66 = 0x42,
382 P67 = 0x43,
383 P68 = 0x44,
384 P69 = 0x45,
385 P70 = 0x46,
386 P71 = 0x47,
387 P72 = 0x48,
388 P73 = 0x49,
389 P74 = 0x4a,
390 P75 = 0x4b,
391 P76 = 0x4c,
392 P77 = 0x4d,
393 P78 = 0x4e,
394 P79 = 0x4f,
395 P80 = 0x50,
396 P81 = 0x51,
397 P82 = 0x52,
398 P83 = 0x53,
399 P84 = 0x54,
400 P85 = 0x55,
401 P86 = 0x56,
402 P87 = 0x57,
403 P88 = 0x58,
404 P89 = 0x59,
405 P90 = 0x5a,
406 P91 = 0x5b,
407 P92 = 0x5c,
408 P93 = 0x5d,
409 P94 = 0x5e,
410 P95 = 0x5f,
411 P96 = 0x60,
412 P97 = 0x61,
413 P98 = 0x62,
414 P99 = 0x63,
415 P100 = 0x64,
416 P101 = 0x65,
417 P102 = 0x66,
418 P103 = 0x67,
419 P104 = 0x68,
420 P105 = 0x69,
421 P106 = 0x6a,
422 P107 = 0x6b,
423 P108 = 0x6c,
424 P109 = 0x6d,
425 P110 = 0x6e,
426 P111 = 0x6f,
427 P112 = 0x70,
428 P113 = 0x71,
429 P114 = 0x72,
430 P115 = 0x73,
431 P116 = 0x74,
432 P117 = 0x75,
433 P118 = 0x76,
434 P119 = 0x77,
435 P120 = 0x78,
436 P121 = 0x79,
437 P122 = 0x7a,
438 P123 = 0x7b,
439 P124 = 0x7c,
440 P125 = 0x7d,
441 P126 = 0x7e,
442 P127 = 0x7f,
443 P128 = 0x80,
444 P129 = 0x81,
445 P130 = 0x82,
446 P131 = 0x83,
447 P132 = 0x84,
448 P133 = 0x85,
449 P134 = 0x86,
450 P135 = 0x87,
451 P136 = 0x88,
452 P137 = 0x89,
453 P138 = 0x8a,
454 P139 = 0x8b,
455 P140 = 0x8c,
456 P141 = 0x8d,
457 P142 = 0x8e,
458 P143 = 0x8f,
459 P144 = 0x90,
460 P145 = 0x91,
461 P146 = 0x92,
462 P147 = 0x93,
463 P148 = 0x94,
464 P149 = 0x95,
465 P150 = 0x96,
466 P151 = 0x97,
467 P152 = 0x98,
468 P153 = 0x99,
469 P154 = 0x9a,
470 P155 = 0x9b,
471 P156 = 0x9c,
472 P157 = 0x9d,
473 P158 = 0x9e,
474 P159 = 0x9f,
475 P160 = 0xa0,
476 P161 = 0xa1,
477 P162 = 0xa2,
478 P163 = 0xa3,
479 P164 = 0xa4,
480 P165 = 0xa5,
481 P166 = 0xa6,
482 P167 = 0xa7,
483 P168 = 0xa8,
484 P169 = 0xa9,
485 P170 = 0xaa,
486 P171 = 0xab,
487 P172 = 0xac,
488 P173 = 0xad,
489 P174 = 0xae,
490 P175 = 0xaf,
491 P176 = 0xb0,
492 P177 = 0xb1,
493 P178 = 0xb2,
494 P179 = 0xb3,
495 P180 = 0xb4,
496 P181 = 0xb5,
497 P182 = 0xb6,
498 P183 = 0xb7,
499 P184 = 0xb8,
500 P185 = 0xb9,
501 P186 = 0xba,
502 P187 = 0xbb,
503 P188 = 0xbc,
504 P189 = 0xbd,
505 P190 = 0xbe,
506 P191 = 0xbf,
507 P192 = 0xc0,
508 P193 = 0xc1,
509 P194 = 0xc2,
510 P195 = 0xc3,
511 P196 = 0xc4,
512 P197 = 0xc5,
513 P198 = 0xc6,
514 P199 = 0xc7,
515 P200 = 0xc8,
516 P201 = 0xc9,
517 P202 = 0xca,
518 P203 = 0xcb,
519 P204 = 0xcc,
520 P205 = 0xcd,
521 P206 = 0xce,
522 P207 = 0xcf,
523 P208 = 0xd0,
524 P209 = 0xd1,
525 P210 = 0xd2,
526 P211 = 0xd3,
527 P212 = 0xd4,
528 P213 = 0xd5,
529 P214 = 0xd6,
530 P215 = 0xd7,
531 P216 = 0xd8,
532 P217 = 0xd9,
533 P218 = 0xda,
534 P219 = 0xdb,
535 P220 = 0xdc,
536 P221 = 0xdd,
537 P222 = 0xde,
538 P223 = 0xdf,
539 P224 = 0xe0,
540 P225 = 0xe1,
541 P226 = 0xe2,
542 P227 = 0xe3,
543 P228 = 0xe4,
544 P229 = 0xe5,
545 P230 = 0xe6,
546 P231 = 0xe7,
547 P232 = 0xe8,
548 P233 = 0xe9,
549 P234 = 0xea,
550 P235 = 0xeb,
551 P236 = 0xec,
552 P237 = 0xed,
553 P238 = 0xee,
554 P239 = 0xef,
555 P240 = 0xf0,
556 P241 = 0xf1,
557 P242 = 0xf2,
558 P243 = 0xf3,
559 P244 = 0xf4,
560 P245 = 0xf5,
561 P246 = 0xf6,
562 P247 = 0xf7,
563 P248 = 0xf8,
564 P249 = 0xf9,
565 P250 = 0xfa,
566 P251 = 0xfb,
567 P252 = 0xfc,
568 P253 = 0xfd,
569 P254 = 0xfe,
570 P255 = 0xff,
571));
diff --git a/embassy-hal-common/src/lib.rs b/embassy-hal-common/src/lib.rs
index 6ee2ccd59..c8cf1c4cd 100644
--- a/embassy-hal-common/src/lib.rs
+++ b/embassy-hal-common/src/lib.rs
@@ -5,11 +5,11 @@
5pub(crate) mod fmt; 5pub(crate) mod fmt;
6 6
7pub mod drop; 7pub mod drop;
8pub mod interrupt;
9mod macros; 8mod macros;
10pub mod peripheral;
11pub mod ratio; 9pub mod ratio;
12pub mod ring_buffer; 10pub mod ring_buffer;
11mod unborrow;
12pub use unborrow::Unborrow;
13 13
14/// Low power blocking wait loop using WFE/SEV. 14/// Low power blocking wait loop using WFE/SEV.
15pub fn low_power_wait_until(mut condition: impl FnMut() -> bool) { 15pub fn low_power_wait_until(mut condition: impl FnMut() -> bool) {
diff --git a/embassy-hal-common/src/macros.rs b/embassy-hal-common/src/macros.rs
index 771db40f6..c054a87c4 100644
--- a/embassy-hal-common/src/macros.rs
+++ b/embassy-hal-common/src/macros.rs
@@ -16,7 +16,7 @@ macro_rules! peripherals {
16 } 16 }
17 17
18 $(#[$cfg])? 18 $(#[$cfg])?
19 unsafe impl embassy::util::Unborrow for $name { 19 unsafe impl $crate::Unborrow for $name {
20 type Target = $name; 20 type Target = $name;
21 #[inline] 21 #[inline]
22 unsafe fn unborrow(self) -> $name { 22 unsafe fn unborrow(self) -> $name {
@@ -80,7 +80,7 @@ macro_rules! unborrow {
80#[macro_export] 80#[macro_export]
81macro_rules! unsafe_impl_unborrow { 81macro_rules! unsafe_impl_unborrow {
82 ($type:ident) => { 82 ($type:ident) => {
83 unsafe impl ::embassy::util::Unborrow for $type { 83 unsafe impl $crate::Unborrow for $type {
84 type Target = $type; 84 type Target = $type;
85 #[inline] 85 #[inline]
86 unsafe fn unborrow(self) -> Self::Target { 86 unsafe fn unborrow(self) -> Self::Target {
diff --git a/embassy-hal-common/src/peripheral.rs b/embassy-hal-common/src/peripheral.rs
deleted file mode 100644
index db2bc7888..000000000
--- a/embassy-hal-common/src/peripheral.rs
+++ /dev/null
@@ -1,134 +0,0 @@
1use core::marker::PhantomData;
2use core::mem::MaybeUninit;
3
4use cortex_m::peripheral::scb::VectActive;
5use cortex_m::peripheral::{NVIC, SCB};
6use embassy::interrupt::{Interrupt, InterruptExt};
7
8/// A type which can be used as state with `PeripheralMutex`.
9///
10/// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt,
11/// and `&mut T` is only `Send` where `T: Send`.
12pub trait PeripheralState: Send {
13 type Interrupt: Interrupt;
14 fn on_interrupt(&mut self);
15}
16
17pub struct StateStorage<S>(MaybeUninit<S>);
18
19impl<S> StateStorage<S> {
20 pub const fn new() -> Self {
21 Self(MaybeUninit::uninit())
22 }
23}
24
25pub struct PeripheralMutex<'a, S: PeripheralState> {
26 state: *mut S,
27 _phantom: PhantomData<&'a mut S>,
28 irq: S::Interrupt,
29}
30
31/// Whether `irq` can be preempted by the current interrupt.
32pub(crate) fn can_be_preempted(irq: &impl Interrupt) -> bool {
33 match SCB::vect_active() {
34 // Thread mode can't preempt anything.
35 VectActive::ThreadMode => false,
36 // Exceptions don't always preempt interrupts,
37 // but there isn't much of a good reason to be keeping a `PeripheralMutex` in an exception anyway.
38 VectActive::Exception(_) => true,
39 VectActive::Interrupt { irqn } => {
40 #[derive(Clone, Copy)]
41 struct NrWrap(u16);
42 unsafe impl cortex_m::interrupt::InterruptNumber for NrWrap {
43 fn number(self) -> u16 {
44 self.0
45 }
46 }
47 NVIC::get_priority(NrWrap(irqn.into())) < irq.get_priority().into()
48 }
49 }
50}
51
52impl<'a, S: PeripheralState> PeripheralMutex<'a, S> {
53 /// Create a new `PeripheralMutex` wrapping `irq`, with `init` initializing the initial state.
54 ///
55 /// Registers `on_interrupt` as the `irq`'s handler, and enables it.
56 pub fn new(
57 irq: S::Interrupt,
58 storage: &'a mut StateStorage<S>,
59 init: impl FnOnce() -> S,
60 ) -> Self {
61 if can_be_preempted(&irq) {
62 panic!("`PeripheralMutex` cannot be created in an interrupt with higher priority than the interrupt it wraps");
63 }
64
65 let state_ptr = storage.0.as_mut_ptr();
66
67 // Safety: The pointer is valid and not used by anyone else
68 // because we have the `&mut StateStorage`.
69 unsafe { state_ptr.write(init()) };
70
71 irq.disable();
72 irq.set_handler(|p| unsafe {
73 // Safety: it's OK to get a &mut to the state, since
74 // - We checked that the thread owning the `PeripheralMutex` can't preempt us in `new`.
75 // Interrupts' priorities can only be changed with raw embassy `Interrupts`,
76 // which can't safely store a `PeripheralMutex` across invocations.
77 // - We can't have preempted a with() call because the irq is disabled during it.
78 let state = &mut *(p as *mut S);
79 state.on_interrupt();
80 });
81 irq.set_handler_context(state_ptr as *mut ());
82 irq.enable();
83
84 Self {
85 irq,
86 state: state_ptr,
87 _phantom: PhantomData,
88 }
89 }
90
91 pub fn with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R {
92 self.irq.disable();
93
94 // Safety: it's OK to get a &mut to the state, since the irq is disabled.
95 let state = unsafe { &mut *self.state };
96 let r = f(state);
97
98 self.irq.enable();
99
100 r
101 }
102
103 /// Returns whether the wrapped interrupt is currently in a pending state.
104 pub fn is_pending(&self) -> bool {
105 self.irq.is_pending()
106 }
107
108 /// Forces the wrapped interrupt into a pending state.
109 pub fn pend(&self) {
110 self.irq.pend()
111 }
112
113 /// Forces the wrapped interrupt out of a pending state.
114 pub fn unpend(&self) {
115 self.irq.unpend()
116 }
117
118 /// Gets the priority of the wrapped interrupt.
119 pub fn priority(&self) -> <S::Interrupt as Interrupt>::Priority {
120 self.irq.get_priority()
121 }
122}
123
124impl<'a, S: PeripheralState> Drop for PeripheralMutex<'a, S> {
125 fn drop(&mut self) {
126 self.irq.disable();
127 self.irq.remove_handler();
128
129 // safety:
130 // - we initialized the state in `new`, so we know it's initialized.
131 // - the irq is disabled, so it won't preempt us while dropping.
132 unsafe { self.state.drop_in_place() }
133 }
134}
diff --git a/embassy-hal-common/src/unborrow.rs b/embassy-hal-common/src/unborrow.rs
new file mode 100644
index 000000000..dacfa3d42
--- /dev/null
+++ b/embassy-hal-common/src/unborrow.rs
@@ -0,0 +1,60 @@
1/// Unsafely unborrow an owned singleton out of a `&mut`.
2///
3/// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`.
4/// Unborrowing an owned `T` yields the same `T`. Unborrowing a `&mut T` yields a copy of the T.
5///
6/// This allows writing HAL drivers that either own or borrow their peripherals, but that don't have
7/// to store pointers in the borrowed case.
8///
9/// Safety: this trait can be used to copy non-Copy types. Implementors must not cause
10/// immediate UB when copied, and must not cause UB when copies are later used, provided they
11/// are only used according the [`Self::unborrow`] safety contract.
12///
13pub unsafe trait Unborrow {
14 /// Unborrow result type
15 type Target;
16
17 /// Unborrow a value.
18 ///
19 /// Safety: This returns a copy of a singleton that's normally not
20 /// copiable. The returned copy must ONLY be used while the lifetime of `self` is
21 /// valid, as if it were accessed through `self` every time.
22 unsafe fn unborrow(self) -> Self::Target;
23}
24
25unsafe impl<'a, T: Unborrow> Unborrow for &'a mut T {
26 type Target = T::Target;
27 unsafe fn unborrow(self) -> Self::Target {
28 T::unborrow(core::ptr::read(self))
29 }
30}
31
32macro_rules! unsafe_impl_unborrow_tuples {
33 ($($t:ident),+) => {
34 unsafe impl<$($t),+> Unborrow for ($($t),+)
35 where
36 $(
37 $t: Unborrow<Target = $t>
38 ),+
39 {
40 type Target = ($($t),+);
41 unsafe fn unborrow(self) -> Self::Target {
42 self
43 }
44 }
45
46
47 };
48}
49
50unsafe_impl_unborrow_tuples!(A, B);
51unsafe_impl_unborrow_tuples!(A, B, C);
52unsafe_impl_unborrow_tuples!(A, B, C, D);
53unsafe_impl_unborrow_tuples!(A, B, C, D, E);
54unsafe_impl_unborrow_tuples!(A, B, C, D, E, F);
55unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G);
56unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G, H);
57unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G, H, I);
58unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G, H, I, J);
59unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G, H, I, J, K);
60unsafe_impl_unborrow_tuples!(A, B, C, D, E, F, G, H, I, J, K, L);