aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-05-18 20:32:48 +0200
committerDario Nieuwenhuis <[email protected]>2025-05-18 20:35:36 +0200
commite4fc48764491f8981e4a145a72e9b6e72df8c546 (patch)
treecac023f3457123f2fbc7686f2f90414987eae264 /embassy-rp/src
parente8b1ea14c7fb151aa5e296ca8f9724f175bdeaef (diff)
Add rand-core v0.9 support.
Co-Authored-By: AurĂ©lien Jacobs <[email protected]>
Diffstat (limited to 'embassy-rp/src')
-rw-r--r--embassy-rp/src/clocks.rs49
-rw-r--r--embassy-rp/src/trng.rs25
2 files changed, 62 insertions, 12 deletions
diff --git a/embassy-rp/src/clocks.rs b/embassy-rp/src/clocks.rs
index 857877680..d79bffab3 100644
--- a/embassy-rp/src/clocks.rs
+++ b/embassy-rp/src/clocks.rs
@@ -1776,7 +1776,8 @@ impl<'d, T: GpoutPin> Drop for Gpout<'d, T> {
1776pub struct RoscRng; 1776pub struct RoscRng;
1777 1777
1778impl RoscRng { 1778impl RoscRng {
1779 fn next_u8() -> u8 { 1779 /// Get a random u8
1780 pub fn next_u8() -> u8 {
1780 let random_reg = pac::ROSC.randombit(); 1781 let random_reg = pac::ROSC.randombit();
1781 let mut acc = 0; 1782 let mut acc = 0;
1782 for _ in 0..u8::BITS { 1783 for _ in 0..u8::BITS {
@@ -1785,26 +1786,60 @@ impl RoscRng {
1785 } 1786 }
1786 acc 1787 acc
1787 } 1788 }
1789
1790 /// Get a random u32
1791 pub fn next_u32(&mut self) -> u32 {
1792 rand_core_09::impls::next_u32_via_fill(self)
1793 }
1794
1795 /// Get a random u64
1796 pub fn next_u64(&mut self) -> u64 {
1797 rand_core_09::impls::next_u64_via_fill(self)
1798 }
1799
1800 /// Fill a slice with random bytes
1801 pub fn fill_bytes(&mut self, dest: &mut [u8]) {
1802 dest.fill_with(Self::next_u8)
1803 }
1788} 1804}
1789 1805
1790impl rand_core::RngCore for RoscRng { 1806impl rand_core_06::RngCore for RoscRng {
1791 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { 1807 fn next_u32(&mut self) -> u32 {
1792 Ok(self.fill_bytes(dest)) 1808 self.next_u32()
1809 }
1810
1811 fn next_u64(&mut self) -> u64 {
1812 self.next_u64()
1813 }
1814
1815 fn fill_bytes(&mut self, dest: &mut [u8]) {
1816 self.fill_bytes(dest);
1817 }
1818
1819 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
1820 self.fill_bytes(dest);
1821 Ok(())
1793 } 1822 }
1823}
1824
1825impl rand_core_06::CryptoRng for RoscRng {}
1794 1826
1827impl rand_core_09::RngCore for RoscRng {
1795 fn next_u32(&mut self) -> u32 { 1828 fn next_u32(&mut self) -> u32 {
1796 rand_core::impls::next_u32_via_fill(self) 1829 self.next_u32()
1797 } 1830 }
1798 1831
1799 fn next_u64(&mut self) -> u64 { 1832 fn next_u64(&mut self) -> u64 {
1800 rand_core::impls::next_u64_via_fill(self) 1833 self.next_u64()
1801 } 1834 }
1802 1835
1803 fn fill_bytes(&mut self, dest: &mut [u8]) { 1836 fn fill_bytes(&mut self, dest: &mut [u8]) {
1804 dest.fill_with(Self::next_u8) 1837 self.fill_bytes(dest);
1805 } 1838 }
1806} 1839}
1807 1840
1841impl rand_core_09::CryptoRng for RoscRng {}
1842
1808/// Enter the `DORMANT` sleep state. This will stop *all* internal clocks 1843/// Enter the `DORMANT` sleep state. This will stop *all* internal clocks
1809/// and can only be exited through resets, dormant-wake GPIO interrupts, 1844/// and can only be exited through resets, dormant-wake GPIO interrupts,
1810/// and RTC interrupts. If RTC is clocked from an internal clock source 1845/// and RTC interrupts. If RTC is clocked from an internal clock source
diff --git a/embassy-rp/src/trng.rs b/embassy-rp/src/trng.rs
index a8a0172be..a3f23c1f2 100644
--- a/embassy-rp/src/trng.rs
+++ b/embassy-rp/src/trng.rs
@@ -7,7 +7,6 @@ use core::task::Poll;
7 7
8use embassy_hal_internal::{Peri, PeripheralType}; 8use embassy_hal_internal::{Peri, PeripheralType};
9use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
10use rand_core::Error;
11 10
12use crate::interrupt::typelevel::{Binding, Interrupt}; 11use crate::interrupt::typelevel::{Binding, Interrupt};
13use crate::peripherals::TRNG; 12use crate::peripherals::TRNG;
@@ -369,7 +368,7 @@ impl<'d, T: Instance> Trng<'d, T> {
369 } 368 }
370} 369}
371 370
372impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> { 371impl<'d, T: Instance> rand_core_06::RngCore for Trng<'d, T> {
373 fn next_u32(&mut self) -> u32 { 372 fn next_u32(&mut self) -> u32 {
374 self.blocking_next_u32() 373 self.blocking_next_u32()
375 } 374 }
@@ -379,16 +378,32 @@ impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> {
379 } 378 }
380 379
381 fn fill_bytes(&mut self, dest: &mut [u8]) { 380 fn fill_bytes(&mut self, dest: &mut [u8]) {
382 self.blocking_fill_bytes(dest) 381 self.blocking_fill_bytes(dest);
383 } 382 }
384 383
385 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { 384 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
386 self.blocking_fill_bytes(dest); 385 self.blocking_fill_bytes(dest);
387 Ok(()) 386 Ok(())
388 } 387 }
389} 388}
390 389
391impl<'d, T: Instance> rand_core::CryptoRng for Trng<'d, T> {} 390impl<'d, T: Instance> rand_core_06::CryptoRng for Trng<'d, T> {}
391
392impl<'d, T: Instance> rand_core_09::RngCore for Trng<'d, T> {
393 fn next_u32(&mut self) -> u32 {
394 self.blocking_next_u32()
395 }
396
397 fn next_u64(&mut self) -> u64 {
398 self.blocking_next_u64()
399 }
400
401 fn fill_bytes(&mut self, dest: &mut [u8]) {
402 self.blocking_fill_bytes(dest);
403 }
404}
405
406impl<'d, T: Instance> rand_core_09::CryptoRng for Trng<'d, T> {}
392 407
393/// TRNG interrupt handler. 408/// TRNG interrupt handler.
394pub struct InterruptHandler<T: Instance> { 409pub struct InterruptHandler<T: Instance> {