diff options
Diffstat (limited to 'embassy-rp/src/critical_section_impl.rs')
| -rw-r--r-- | embassy-rp/src/critical_section_impl.rs | 43 |
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 @@ | |||
| 1 | use core::sync::atomic::{AtomicU8, Ordering}; | 1 | use core::sync::atomic::{AtomicU8, Ordering}; |
| 2 | 2 | ||
| 3 | use crate::pac; | 3 | use crate::pac; |
| 4 | use crate::spinlock::Spinlock; | ||
| 4 | 5 | ||
| 5 | struct RpSpinlockCs; | 6 | struct RpSpinlockCs; |
| 6 | critical_section::set_impl!(RpSpinlockCs); | 7 | critical_section::set_impl!(RpSpinlockCs); |
| @@ -92,46 +93,4 @@ impl RpSpinlockCs { | |||
| 92 | } | 93 | } |
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | pub struct Spinlock<const N: usize>(core::marker::PhantomData<()>) | ||
| 96 | where | ||
| 97 | Spinlock<N>: SpinlockValid; | ||
| 98 | |||
| 99 | impl<const N: usize> Spinlock<N> | ||
| 100 | where | ||
| 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 | |||
| 125 | impl<const N: usize> Drop for Spinlock<N> | ||
| 126 | where | ||
| 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 | |||
| 135 | pub(crate) type Spinlock31 = Spinlock<31>; | 96 | pub(crate) type Spinlock31 = Spinlock<31>; |
| 136 | pub trait SpinlockValid {} | ||
| 137 | impl SpinlockValid for Spinlock<31> {} | ||
