aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/critical_section_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-rp/src/critical_section_impl.rs')
-rw-r--r--embassy-rp/src/critical_section_impl.rs43
1 files changed, 1 insertions, 42 deletions
diff --git a/embassy-rp/src/critical_section_impl.rs b/embassy-rp/src/critical_section_impl.rs
index d233e6fab..2e4e8f716 100644
--- a/embassy-rp/src/critical_section_impl.rs
+++ b/embassy-rp/src/critical_section_impl.rs
@@ -1,6 +1,7 @@
1use core::sync::atomic::{AtomicU8, Ordering}; 1use core::sync::atomic::{AtomicU8, Ordering};
2 2
3use crate::pac; 3use crate::pac;
4use crate::spinlock::Spinlock;
4 5
5struct RpSpinlockCs; 6struct RpSpinlockCs;
6critical_section::set_impl!(RpSpinlockCs); 7critical_section::set_impl!(RpSpinlockCs);
@@ -92,46 +93,4 @@ impl RpSpinlockCs {
92 } 93 }
93} 94}
94 95
95pub struct Spinlock<const N: usize>(core::marker::PhantomData<()>)
96where
97 Spinlock<N>: SpinlockValid;
98
99impl<const N: usize> Spinlock<N>
100where
101 Spinlock<N>: SpinlockValid,
102{
103 /// Try to claim the spinlock. Will return `Some(Self)` if the lock is obtained, and `None` if the lock is
104 /// already in use somewhere else.
105 pub fn try_claim() -> Option<Self> {
106 let lock = pac::SIO.spinlock(N).read();
107 if lock > 0 {
108 Some(Self(core::marker::PhantomData))
109 } else {
110 None
111 }
112 }
113
114 /// Clear a locked spin-lock.
115 ///
116 /// # Safety
117 ///
118 /// Only call this function if you hold the spin-lock.
119 pub unsafe fn release() {
120 // Write (any value): release the lock
121 pac::SIO.spinlock(N).write_value(1);
122 }
123}
124
125impl<const N: usize> Drop for Spinlock<N>
126where
127 Spinlock<N>: SpinlockValid,
128{
129 fn drop(&mut self) {
130 // This is safe because we own the object, and hence hold the lock.
131 unsafe { Self::release() }
132 }
133}
134
135pub(crate) type Spinlock31 = Spinlock<31>; 96pub(crate) type Spinlock31 = Spinlock<31>;
136pub trait SpinlockValid {}
137impl SpinlockValid for Spinlock<31> {}