diff options
| author | Sebastian Quilitz <[email protected]> | 2025-03-27 16:50:26 +0000 |
|---|---|---|
| committer | Sebastian Quilitz <[email protected]> | 2025-03-27 20:10:34 +0100 |
| commit | d17d43735f46fe808e7c9756d7192222b7d2ee2a (patch) | |
| tree | f283a688f2e5de31f18471c6b6caee604a028777 /embassy-rp | |
| parent | 502c188cf4c08a1acf9d7095e790e0b5d77d3702 (diff) | |
embassy-rp: Move Spinlock implementation out of critical_section_impl
Diffstat (limited to 'embassy-rp')
| -rw-r--r-- | embassy-rp/src/critical_section_impl.rs | 43 | ||||
| -rw-r--r-- | embassy-rp/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-rp/src/spinlock.rs | 75 |
3 files changed, 77 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> {} | ||
diff --git a/embassy-rp/src/lib.rs b/embassy-rp/src/lib.rs index 35099d07b..01d26b8e9 100644 --- a/embassy-rp/src/lib.rs +++ b/embassy-rp/src/lib.rs | |||
| @@ -41,6 +41,7 @@ pub mod rom_data; | |||
| 41 | #[cfg(feature = "rp2040")] | 41 | #[cfg(feature = "rp2040")] |
| 42 | pub mod rtc; | 42 | pub mod rtc; |
| 43 | pub mod spi; | 43 | pub mod spi; |
| 44 | mod spinlock; | ||
| 44 | #[cfg(feature = "time-driver")] | 45 | #[cfg(feature = "time-driver")] |
| 45 | pub mod time_driver; | 46 | pub mod time_driver; |
| 46 | #[cfg(feature = "_rp235x")] | 47 | #[cfg(feature = "_rp235x")] |
diff --git a/embassy-rp/src/spinlock.rs b/embassy-rp/src/spinlock.rs new file mode 100644 index 000000000..7effd2ae0 --- /dev/null +++ b/embassy-rp/src/spinlock.rs | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | use crate::pac; | ||
| 2 | |||
| 3 | pub struct Spinlock<const N: usize>(core::marker::PhantomData<()>) | ||
| 4 | where | ||
| 5 | Spinlock<N>: SpinlockValid; | ||
| 6 | |||
| 7 | impl<const N: usize> Spinlock<N> | ||
| 8 | where | ||
| 9 | Spinlock<N>: SpinlockValid, | ||
| 10 | { | ||
| 11 | /// Try to claim the spinlock. Will return `Some(Self)` if the lock is obtained, and `None` if the lock is | ||
| 12 | /// already in use somewhere else. | ||
| 13 | pub fn try_claim() -> Option<Self> { | ||
| 14 | let lock = pac::SIO.spinlock(N).read(); | ||
| 15 | if lock > 0 { | ||
| 16 | Some(Self(core::marker::PhantomData)) | ||
| 17 | } else { | ||
| 18 | None | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | /// Clear a locked spin-lock. | ||
| 23 | /// | ||
| 24 | /// # Safety | ||
| 25 | /// | ||
| 26 | /// Only call this function if you hold the spin-lock. | ||
| 27 | pub unsafe fn release() { | ||
| 28 | // Write (any value): release the lock | ||
| 29 | pac::SIO.spinlock(N).write_value(1); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl<const N: usize> Drop for Spinlock<N> | ||
| 34 | where | ||
| 35 | Spinlock<N>: SpinlockValid, | ||
| 36 | { | ||
| 37 | fn drop(&mut self) { | ||
| 38 | // This is safe because we own the object, and hence hold the lock. | ||
| 39 | unsafe { Self::release() } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | pub trait SpinlockValid {} | ||
| 44 | impl SpinlockValid for Spinlock<0> {} | ||
| 45 | impl SpinlockValid for Spinlock<1> {} | ||
| 46 | impl SpinlockValid for Spinlock<2> {} | ||
| 47 | impl SpinlockValid for Spinlock<3> {} | ||
| 48 | impl SpinlockValid for Spinlock<4> {} | ||
| 49 | impl SpinlockValid for Spinlock<5> {} | ||
| 50 | impl SpinlockValid for Spinlock<6> {} | ||
| 51 | impl SpinlockValid for Spinlock<7> {} | ||
| 52 | impl SpinlockValid for Spinlock<8> {} | ||
| 53 | impl SpinlockValid for Spinlock<9> {} | ||
| 54 | impl SpinlockValid for Spinlock<10> {} | ||
| 55 | impl SpinlockValid for Spinlock<11> {} | ||
| 56 | impl SpinlockValid for Spinlock<12> {} | ||
| 57 | impl SpinlockValid for Spinlock<13> {} | ||
| 58 | impl SpinlockValid for Spinlock<14> {} | ||
| 59 | impl SpinlockValid for Spinlock<15> {} | ||
| 60 | impl SpinlockValid for Spinlock<16> {} | ||
| 61 | impl SpinlockValid for Spinlock<17> {} | ||
| 62 | impl SpinlockValid for Spinlock<18> {} | ||
| 63 | impl SpinlockValid for Spinlock<19> {} | ||
| 64 | impl SpinlockValid for Spinlock<20> {} | ||
| 65 | impl SpinlockValid for Spinlock<21> {} | ||
| 66 | impl SpinlockValid for Spinlock<22> {} | ||
| 67 | impl SpinlockValid for Spinlock<23> {} | ||
| 68 | impl SpinlockValid for Spinlock<24> {} | ||
| 69 | impl SpinlockValid for Spinlock<25> {} | ||
| 70 | impl SpinlockValid for Spinlock<26> {} | ||
| 71 | impl SpinlockValid for Spinlock<27> {} | ||
| 72 | impl SpinlockValid for Spinlock<28> {} | ||
| 73 | impl SpinlockValid for Spinlock<29> {} | ||
| 74 | impl SpinlockValid for Spinlock<30> {} | ||
| 75 | impl SpinlockValid for Spinlock<31> {} | ||
