diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-05-18 20:32:48 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-05-18 20:35:36 +0200 |
| commit | e4fc48764491f8981e4a145a72e9b6e72df8c546 (patch) | |
| tree | cac023f3457123f2fbc7686f2f90414987eae264 /embassy-rp | |
| parent | e8b1ea14c7fb151aa5e296ca8f9724f175bdeaef (diff) | |
Add rand-core v0.9 support.
Co-Authored-By: Aurélien Jacobs <[email protected]>
Diffstat (limited to 'embassy-rp')
| -rw-r--r-- | embassy-rp/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-rp/src/clocks.rs | 49 | ||||
| -rw-r--r-- | embassy-rp/src/trng.rs | 25 |
3 files changed, 65 insertions, 13 deletions
diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 8fb8a50fd..849cb549a 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml | |||
| @@ -157,7 +157,6 @@ embedded-io = { version = "0.6.1" } | |||
| 157 | embedded-io-async = { version = "0.6.1" } | 157 | embedded-io-async = { version = "0.6.1" } |
| 158 | embedded-storage = { version = "0.3" } | 158 | embedded-storage = { version = "0.3" } |
| 159 | embedded-storage-async = { version = "0.4.1" } | 159 | embedded-storage-async = { version = "0.4.1" } |
| 160 | rand_core = "0.6.4" | ||
| 161 | fixed = "1.28.0" | 160 | fixed = "1.28.0" |
| 162 | 161 | ||
| 163 | rp-pac = { version = "7.0.0" } | 162 | rp-pac = { version = "7.0.0" } |
| @@ -167,6 +166,9 @@ embedded-hal-1 = { package = "embedded-hal", version = "1.0" } | |||
| 167 | embedded-hal-async = { version = "1.0" } | 166 | embedded-hal-async = { version = "1.0" } |
| 168 | embedded-hal-nb = { version = "1.0" } | 167 | embedded-hal-nb = { version = "1.0" } |
| 169 | 168 | ||
| 169 | rand-core-06 = { package = "rand_core", version = "0.6" } | ||
| 170 | rand-core-09 = { package = "rand_core", version = "0.9" } | ||
| 171 | |||
| 170 | pio = { version = "0.3" } | 172 | pio = { version = "0.3" } |
| 171 | rp2040-boot2 = "0.3" | 173 | rp2040-boot2 = "0.3" |
| 172 | document-features = "0.2.10" | 174 | document-features = "0.2.10" |
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> { | |||
| 1776 | pub struct RoscRng; | 1776 | pub struct RoscRng; |
| 1777 | 1777 | ||
| 1778 | impl RoscRng { | 1778 | impl 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 | ||
| 1790 | impl rand_core::RngCore for RoscRng { | 1806 | impl 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 | |||
| 1825 | impl rand_core_06::CryptoRng for RoscRng {} | ||
| 1794 | 1826 | ||
| 1827 | impl 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 | ||
| 1841 | impl 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 | ||
| 8 | use embassy_hal_internal::{Peri, PeripheralType}; | 8 | use embassy_hal_internal::{Peri, PeripheralType}; |
| 9 | use embassy_sync::waitqueue::AtomicWaker; | 9 | use embassy_sync::waitqueue::AtomicWaker; |
| 10 | use rand_core::Error; | ||
| 11 | 10 | ||
| 12 | use crate::interrupt::typelevel::{Binding, Interrupt}; | 11 | use crate::interrupt::typelevel::{Binding, Interrupt}; |
| 13 | use crate::peripherals::TRNG; | 12 | use crate::peripherals::TRNG; |
| @@ -369,7 +368,7 @@ impl<'d, T: Instance> Trng<'d, T> { | |||
| 369 | } | 368 | } |
| 370 | } | 369 | } |
| 371 | 370 | ||
| 372 | impl<'d, T: Instance> rand_core::RngCore for Trng<'d, T> { | 371 | impl<'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 | ||
| 391 | impl<'d, T: Instance> rand_core::CryptoRng for Trng<'d, T> {} | 390 | impl<'d, T: Instance> rand_core_06::CryptoRng for Trng<'d, T> {} |
| 391 | |||
| 392 | impl<'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 | |||
| 406 | impl<'d, T: Instance> rand_core_09::CryptoRng for Trng<'d, T> {} | ||
| 392 | 407 | ||
| 393 | /// TRNG interrupt handler. | 408 | /// TRNG interrupt handler. |
| 394 | pub struct InterruptHandler<T: Instance> { | 409 | pub struct InterruptHandler<T: Instance> { |
