diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-04-05 00:20:22 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-04-05 00:48:46 +0200 |
| commit | ab85eb4b60cd49ebcd43d2305f42327685f5e5a6 (patch) | |
| tree | 3c385a5703edcd1e791ec1934d3232dc4084ab2b /embassy-nrf/src/rng.rs | |
| parent | 0e1208947e89ea60bd1b5c85e4deb79efb94d89a (diff) | |
nrf: remove mod sealed.
Diffstat (limited to 'embassy-nrf/src/rng.rs')
| -rw-r--r-- | embassy-nrf/src/rng.rs | 81 |
1 files changed, 36 insertions, 45 deletions
diff --git a/embassy-nrf/src/rng.rs b/embassy-nrf/src/rng.rs index 1c463fb7c..ff61e08f3 100644 --- a/embassy-nrf/src/rng.rs +++ b/embassy-nrf/src/rng.rs | |||
| @@ -2,13 +2,16 @@ | |||
| 2 | 2 | ||
| 3 | #![macro_use] | 3 | #![macro_use] |
| 4 | 4 | ||
| 5 | use core::cell::{RefCell, RefMut}; | ||
| 5 | use core::future::poll_fn; | 6 | use core::future::poll_fn; |
| 6 | use core::marker::PhantomData; | 7 | use core::marker::PhantomData; |
| 7 | use core::ptr; | 8 | use core::ptr; |
| 8 | use core::task::Poll; | 9 | use core::task::Poll; |
| 9 | 10 | ||
| 11 | use critical_section::{CriticalSection, Mutex}; | ||
| 10 | use embassy_hal_internal::drop::OnDrop; | 12 | use embassy_hal_internal::drop::OnDrop; |
| 11 | use embassy_hal_internal::{into_ref, PeripheralRef}; | 13 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 14 | use embassy_sync::waitqueue::WakerRegistration; | ||
| 12 | 15 | ||
| 13 | use crate::interrupt::typelevel::Interrupt; | 16 | use crate::interrupt::typelevel::Interrupt; |
| 14 | use crate::{interrupt, Peripheral}; | 17 | use crate::{interrupt, Peripheral}; |
| @@ -205,73 +208,61 @@ impl<'d, T: Instance> rand_core::RngCore for Rng<'d, T> { | |||
| 205 | 208 | ||
| 206 | impl<'d, T: Instance> rand_core::CryptoRng for Rng<'d, T> {} | 209 | impl<'d, T: Instance> rand_core::CryptoRng for Rng<'d, T> {} |
| 207 | 210 | ||
| 208 | pub(crate) mod sealed { | 211 | /// Peripheral static state |
| 209 | use core::cell::{Ref, RefCell, RefMut}; | 212 | pub(crate) struct State { |
| 210 | 213 | inner: Mutex<RefCell<InnerState>>, | |
| 211 | use critical_section::{CriticalSection, Mutex}; | 214 | } |
| 212 | use embassy_sync::waitqueue::WakerRegistration; | ||
| 213 | |||
| 214 | use super::*; | ||
| 215 | |||
| 216 | /// Peripheral static state | ||
| 217 | pub struct State { | ||
| 218 | inner: Mutex<RefCell<InnerState>>, | ||
| 219 | } | ||
| 220 | |||
| 221 | pub struct InnerState { | ||
| 222 | pub ptr: *mut u8, | ||
| 223 | pub end: *mut u8, | ||
| 224 | pub waker: WakerRegistration, | ||
| 225 | } | ||
| 226 | 215 | ||
| 227 | unsafe impl Send for InnerState {} | 216 | struct InnerState { |
| 217 | ptr: *mut u8, | ||
| 218 | end: *mut u8, | ||
| 219 | waker: WakerRegistration, | ||
| 220 | } | ||
| 228 | 221 | ||
| 229 | impl State { | 222 | unsafe impl Send for InnerState {} |
| 230 | pub const fn new() -> Self { | ||
| 231 | Self { | ||
| 232 | inner: Mutex::new(RefCell::new(InnerState::new())), | ||
| 233 | } | ||
| 234 | } | ||
| 235 | 223 | ||
| 236 | pub fn borrow<'cs>(&'cs self, cs: CriticalSection<'cs>) -> Ref<'cs, InnerState> { | 224 | impl State { |
| 237 | self.inner.borrow(cs).borrow() | 225 | pub(crate) const fn new() -> Self { |
| 226 | Self { | ||
| 227 | inner: Mutex::new(RefCell::new(InnerState::new())), | ||
| 238 | } | 228 | } |
| 229 | } | ||
| 239 | 230 | ||
| 240 | pub fn borrow_mut<'cs>(&'cs self, cs: CriticalSection<'cs>) -> RefMut<'cs, InnerState> { | 231 | fn borrow_mut<'cs>(&'cs self, cs: CriticalSection<'cs>) -> RefMut<'cs, InnerState> { |
| 241 | self.inner.borrow(cs).borrow_mut() | 232 | self.inner.borrow(cs).borrow_mut() |
| 242 | } | ||
| 243 | } | 233 | } |
| 234 | } | ||
| 244 | 235 | ||
| 245 | impl InnerState { | 236 | impl InnerState { |
| 246 | pub const fn new() -> Self { | 237 | const fn new() -> Self { |
| 247 | Self { | 238 | Self { |
| 248 | ptr: ptr::null_mut(), | 239 | ptr: ptr::null_mut(), |
| 249 | end: ptr::null_mut(), | 240 | end: ptr::null_mut(), |
| 250 | waker: WakerRegistration::new(), | 241 | waker: WakerRegistration::new(), |
| 251 | } | ||
| 252 | } | 242 | } |
| 253 | } | 243 | } |
| 244 | } | ||
| 254 | 245 | ||
| 255 | pub trait Instance { | 246 | pub(crate) trait SealedInstance { |
| 256 | fn regs() -> &'static crate::pac::rng::RegisterBlock; | 247 | fn regs() -> &'static crate::pac::rng::RegisterBlock; |
| 257 | fn state() -> &'static State; | 248 | fn state() -> &'static State; |
| 258 | } | ||
| 259 | } | 249 | } |
| 260 | 250 | ||
| 261 | /// RNG peripheral instance. | 251 | /// RNG peripheral instance. |
| 262 | pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send { | 252 | #[allow(private_bounds)] |
| 253 | pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send { | ||
| 263 | /// Interrupt for this peripheral. | 254 | /// Interrupt for this peripheral. |
| 264 | type Interrupt: interrupt::typelevel::Interrupt; | 255 | type Interrupt: interrupt::typelevel::Interrupt; |
| 265 | } | 256 | } |
| 266 | 257 | ||
| 267 | macro_rules! impl_rng { | 258 | macro_rules! impl_rng { |
| 268 | ($type:ident, $pac_type:ident, $irq:ident) => { | 259 | ($type:ident, $pac_type:ident, $irq:ident) => { |
| 269 | impl crate::rng::sealed::Instance for peripherals::$type { | 260 | impl crate::rng::SealedInstance for peripherals::$type { |
| 270 | fn regs() -> &'static crate::pac::rng::RegisterBlock { | 261 | fn regs() -> &'static crate::pac::rng::RegisterBlock { |
| 271 | unsafe { &*pac::$pac_type::ptr() } | 262 | unsafe { &*pac::$pac_type::ptr() } |
| 272 | } | 263 | } |
| 273 | fn state() -> &'static crate::rng::sealed::State { | 264 | fn state() -> &'static crate::rng::State { |
| 274 | static STATE: crate::rng::sealed::State = crate::rng::sealed::State::new(); | 265 | static STATE: crate::rng::State = crate::rng::State::new(); |
| 275 | &STATE | 266 | &STATE |
| 276 | } | 267 | } |
| 277 | } | 268 | } |
