aboutsummaryrefslogtreecommitdiff
path: root/embassy-cortex-m/src/peripheral.rs
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2022-06-23 12:59:18 +0200
committerUlf Lilleengen <[email protected]>2022-06-23 13:17:56 +0200
commitca59c1ff3570474dc819c2d759c69c3a186ca5bc (patch)
tree22f96a5a03cdb43366e8bedf954cad13566328ca /embassy-cortex-m/src/peripheral.rs
parent6d3a652026d222bb0191c77406e1f4145a64c5f9 (diff)
Add more API docs for embassy-cortex-m and embassy-nrf
Diffstat (limited to 'embassy-cortex-m/src/peripheral.rs')
-rw-r--r--embassy-cortex-m/src/peripheral.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/embassy-cortex-m/src/peripheral.rs b/embassy-cortex-m/src/peripheral.rs
index 5ff690831..6a03bfb9f 100644
--- a/embassy-cortex-m/src/peripheral.rs
+++ b/embassy-cortex-m/src/peripheral.rs
@@ -1,3 +1,4 @@
1//! Peripheral interrupt handling specific to cortex-m devices.
1use core::marker::PhantomData; 2use core::marker::PhantomData;
2use core::mem::MaybeUninit; 3use core::mem::MaybeUninit;
3 4
@@ -11,18 +12,25 @@ use crate::interrupt::{Interrupt, InterruptExt, Priority};
11/// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt, 12/// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt,
12/// and `&mut T` is only `Send` where `T: Send`. 13/// and `&mut T` is only `Send` where `T: Send`.
13pub trait PeripheralState: Send { 14pub trait PeripheralState: Send {
15 /// The interrupt that is used for this peripheral.
14 type Interrupt: Interrupt; 16 type Interrupt: Interrupt;
17
18 /// The interrupt handler that should be invoked for the peripheral. Implementations need to clear the appropriate interrupt flags to ensure the handle will not be called again.
15 fn on_interrupt(&mut self); 19 fn on_interrupt(&mut self);
16} 20}
17 21
22/// A type for storing the state of a peripheral that can be stored in a static.
18pub struct StateStorage<S>(MaybeUninit<S>); 23pub struct StateStorage<S>(MaybeUninit<S>);
19 24
20impl<S> StateStorage<S> { 25impl<S> StateStorage<S> {
26 /// Create a new instance for storing peripheral state.
21 pub const fn new() -> Self { 27 pub const fn new() -> Self {
22 Self(MaybeUninit::uninit()) 28 Self(MaybeUninit::uninit())
23 } 29 }
24} 30}
25 31
32/// A type for a peripheral that keeps the state of a peripheral that can be accessed from thread mode and an interrupt handler in
33/// a safe way.
26pub struct PeripheralMutex<'a, S: PeripheralState> { 34pub struct PeripheralMutex<'a, S: PeripheralState> {
27 state: *mut S, 35 state: *mut S,
28 _phantom: PhantomData<&'a mut S>, 36 _phantom: PhantomData<&'a mut S>,
@@ -87,6 +95,8 @@ impl<'a, S: PeripheralState> PeripheralMutex<'a, S> {
87 } 95 }
88 } 96 }
89 97
98 /// Access the peripheral state ensuring interrupts are disabled so that the state can be
99 /// safely accessed.
90 pub fn with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R { 100 pub fn with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R {
91 self.irq.disable(); 101 self.irq.disable();
92 102