aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-09-27 21:19:13 +0200
committerDario Nieuwenhuis <[email protected]>2025-09-28 21:05:37 +0200
commit37fd0c7bce19d4618669a29bdd01945fb477eea6 (patch)
treec52824209d6792d294b682dd1b939c26b1e3b58d /embassy-nrf
parent19fdd8d96c778697610386c829f6a0a655c39940 (diff)
nrf/rng: erase instance generics
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/src/rng.rs56
1 files changed, 30 insertions, 26 deletions
diff --git a/embassy-nrf/src/rng.rs b/embassy-nrf/src/rng.rs
index 9d3130e6e..8070c1afe 100644
--- a/embassy-nrf/src/rng.rs
+++ b/embassy-nrf/src/rng.rs
@@ -56,21 +56,23 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl
56/// A wrapper around an nRF RNG peripheral. 56/// A wrapper around an nRF RNG peripheral.
57/// 57///
58/// It has a non-blocking API, and a blocking api through `rand`. 58/// It has a non-blocking API, and a blocking api through `rand`.
59pub struct Rng<'d, T: Instance, M: Mode> { 59pub struct Rng<'d, M: Mode> {
60 _peri: Peri<'d, T>, 60 r: pac::rng::Rng,
61 _phantom: PhantomData<M>, 61 state: &'static State,
62 _phantom: PhantomData<(&'d (), M)>,
62} 63}
63 64
64impl<'d, T: Instance> Rng<'d, T, Blocking> { 65impl<'d> Rng<'d, Blocking> {
65 /// Creates a new RNG driver from the `RNG` peripheral and interrupt. 66 /// Creates a new RNG driver from the `RNG` peripheral and interrupt.
66 /// 67 ///
67 /// SAFETY: The future returned from `fill_bytes` must not have its lifetime end without running its destructor, 68 /// SAFETY: The future returned from `fill_bytes` must not have its lifetime end without running its destructor,
68 /// e.g. using `mem::forget`. 69 /// e.g. using `mem::forget`.
69 /// 70 ///
70 /// The synchronous API is safe. 71 /// The synchronous API is safe.
71 pub fn new_blocking(rng: Peri<'d, T>) -> Self { 72 pub fn new_blocking<T: Instance>(_rng: Peri<'d, T>) -> Self {
72 let this = Self { 73 let this = Self {
73 _peri: rng, 74 r: T::regs(),
75 state: T::state(),
74 _phantom: PhantomData, 76 _phantom: PhantomData,
75 }; 77 };
76 78
@@ -80,19 +82,20 @@ impl<'d, T: Instance> Rng<'d, T, Blocking> {
80 } 82 }
81} 83}
82 84
83impl<'d, T: Instance> Rng<'d, T, Async> { 85impl<'d> Rng<'d, Async> {
84 /// Creates a new RNG driver from the `RNG` peripheral and interrupt. 86 /// Creates a new RNG driver from the `RNG` peripheral and interrupt.
85 /// 87 ///
86 /// SAFETY: The future returned from `fill_bytes` must not have its lifetime end without running its destructor, 88 /// SAFETY: The future returned from `fill_bytes` must not have its lifetime end without running its destructor,
87 /// e.g. using `mem::forget`. 89 /// e.g. using `mem::forget`.
88 /// 90 ///
89 /// The synchronous API is safe. 91 /// The synchronous API is safe.
90 pub fn new( 92 pub fn new<T: Instance>(
91 rng: Peri<'d, T>, 93 _rng: Peri<'d, T>,
92 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 94 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
93 ) -> Self { 95 ) -> Self {
94 let this = Self { 96 let this = Self {
95 _peri: rng, 97 r: T::regs(),
98 state: T::state(),
96 _phantom: PhantomData, 99 _phantom: PhantomData,
97 }; 100 };
98 101
@@ -106,11 +109,11 @@ impl<'d, T: Instance> Rng<'d, T, Async> {
106 } 109 }
107 110
108 fn enable_irq(&self) { 111 fn enable_irq(&self) {
109 T::regs().intenset().write(|w| w.set_valrdy(true)); 112 self.r.intenset().write(|w| w.set_valrdy(true));
110 } 113 }
111 114
112 fn disable_irq(&self) { 115 fn disable_irq(&self) {
113 T::regs().intenclr().write(|w| w.set_valrdy(true)); 116 self.r.intenclr().write(|w| w.set_valrdy(true));
114 } 117 }
115 118
116 /// Fill the buffer with random bytes. 119 /// Fill the buffer with random bytes.
@@ -120,10 +123,11 @@ impl<'d, T: Instance> Rng<'d, T, Async> {
120 } 123 }
121 124
122 let range = dest.as_mut_ptr_range(); 125 let range = dest.as_mut_ptr_range();
126 let state = self.state;
123 // Even if we've preempted the interrupt, it can't preempt us again, 127 // Even if we've preempted the interrupt, it can't preempt us again,
124 // so we don't need to worry about the order we write these in. 128 // so we don't need to worry about the order we write these in.
125 critical_section::with(|cs| { 129 critical_section::with(|cs| {
126 let mut state = T::state().borrow_mut(cs); 130 let mut state = state.borrow_mut(cs);
127 state.ptr = range.start; 131 state.ptr = range.start;
128 state.end = range.end; 132 state.end = range.end;
129 }); 133 });
@@ -136,7 +140,7 @@ impl<'d, T: Instance> Rng<'d, T, Async> {
136 self.disable_irq(); 140 self.disable_irq();
137 141
138 critical_section::with(|cs| { 142 critical_section::with(|cs| {
139 let mut state = T::state().borrow_mut(cs); 143 let mut state = state.borrow_mut(cs);
140 state.ptr = ptr::null_mut(); 144 state.ptr = ptr::null_mut();
141 state.end = ptr::null_mut(); 145 state.end = ptr::null_mut();
142 }); 146 });
@@ -144,7 +148,7 @@ impl<'d, T: Instance> Rng<'d, T, Async> {
144 148
145 poll_fn(|cx| { 149 poll_fn(|cx| {
146 critical_section::with(|cs| { 150 critical_section::with(|cs| {
147 let mut s = T::state().borrow_mut(cs); 151 let mut s = state.borrow_mut(cs);
148 s.waker.register(cx.waker()); 152 s.waker.register(cx.waker());
149 if s.ptr == s.end { 153 if s.ptr == s.end {
150 // We're done. 154 // We're done.
@@ -161,13 +165,13 @@ impl<'d, T: Instance> Rng<'d, T, Async> {
161 } 165 }
162} 166}
163 167
164impl<'d, T: Instance, M: Mode> Rng<'d, T, M> { 168impl<'d, M: Mode> Rng<'d, M> {
165 fn stop(&self) { 169 fn stop(&self) {
166 T::regs().tasks_stop().write_value(1) 170 self.r.tasks_stop().write_value(1)
167 } 171 }
168 172
169 fn start(&self) { 173 fn start(&self) {
170 T::regs().tasks_start().write_value(1) 174 self.r.tasks_start().write_value(1)
171 } 175 }
172 176
173 /// Enable or disable the RNG's bias correction. 177 /// Enable or disable the RNG's bias correction.
@@ -177,7 +181,7 @@ impl<'d, T: Instance, M: Mode> Rng<'d, T, M> {
177 /// 181 ///
178 /// Defaults to disabled. 182 /// Defaults to disabled.
179 pub fn set_bias_correction(&self, enable: bool) { 183 pub fn set_bias_correction(&self, enable: bool) {
180 T::regs().config().write(|w| w.set_dercen(enable)) 184 self.r.config().write(|w| w.set_dercen(enable))
181 } 185 }
182 186
183 /// Fill the buffer with random bytes, blocking version. 187 /// Fill the buffer with random bytes, blocking version.
@@ -185,7 +189,7 @@ impl<'d, T: Instance, M: Mode> Rng<'d, T, M> {
185 self.start(); 189 self.start();
186 190
187 for byte in dest.iter_mut() { 191 for byte in dest.iter_mut() {
188 let regs = T::regs(); 192 let regs = self.r;
189 while regs.events_valrdy().read() == 0 {} 193 while regs.events_valrdy().read() == 0 {}
190 regs.events_valrdy().write_value(0); 194 regs.events_valrdy().write_value(0);
191 *byte = regs.value().read().value(); 195 *byte = regs.value().read().value();
@@ -210,18 +214,18 @@ impl<'d, T: Instance, M: Mode> Rng<'d, T, M> {
210 } 214 }
211} 215}
212 216
213impl<'d, T: Instance, M: Mode> Drop for Rng<'d, T, M> { 217impl<'d, M: Mode> Drop for Rng<'d, M> {
214 fn drop(&mut self) { 218 fn drop(&mut self) {
215 self.stop(); 219 self.stop();
216 critical_section::with(|cs| { 220 critical_section::with(|cs| {
217 let mut state = T::state().borrow_mut(cs); 221 let mut state = self.state.borrow_mut(cs);
218 state.ptr = ptr::null_mut(); 222 state.ptr = ptr::null_mut();
219 state.end = ptr::null_mut(); 223 state.end = ptr::null_mut();
220 }); 224 });
221 } 225 }
222} 226}
223 227
224impl<'d, T: Instance, M: Mode> rand_core_06::RngCore for Rng<'d, T, M> { 228impl<'d, M: Mode> rand_core_06::RngCore for Rng<'d, M> {
225 fn fill_bytes(&mut self, dest: &mut [u8]) { 229 fn fill_bytes(&mut self, dest: &mut [u8]) {
226 self.blocking_fill_bytes(dest); 230 self.blocking_fill_bytes(dest);
227 } 231 }
@@ -237,9 +241,9 @@ impl<'d, T: Instance, M: Mode> rand_core_06::RngCore for Rng<'d, T, M> {
237 } 241 }
238} 242}
239 243
240impl<'d, T: Instance, M: Mode> rand_core_06::CryptoRng for Rng<'d, T, M> {} 244impl<'d, M: Mode> rand_core_06::CryptoRng for Rng<'d, M> {}
241 245
242impl<'d, T: Instance, M: Mode> rand_core_09::RngCore for Rng<'d, T, M> { 246impl<'d, M: Mode> rand_core_09::RngCore for Rng<'d, M> {
243 fn fill_bytes(&mut self, dest: &mut [u8]) { 247 fn fill_bytes(&mut self, dest: &mut [u8]) {
244 self.blocking_fill_bytes(dest); 248 self.blocking_fill_bytes(dest);
245 } 249 }
@@ -251,7 +255,7 @@ impl<'d, T: Instance, M: Mode> rand_core_09::RngCore for Rng<'d, T, M> {
251 } 255 }
252} 256}
253 257
254impl<'d, T: Instance, M: Mode> rand_core_09::CryptoRng for Rng<'d, T, M> {} 258impl<'d, M: Mode> rand_core_09::CryptoRng for Rng<'d, M> {}
255 259
256/// Peripheral static state 260/// Peripheral static state
257pub(crate) struct State { 261pub(crate) struct State {