diff options
| -rw-r--r-- | embassy-rp/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-rp/src/clocks.rs | 39 | ||||
| -rw-r--r-- | embassy-rp/src/lib.rs | 2 |
3 files changed, 40 insertions, 2 deletions
diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index daa60f9c5..284d458c6 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml | |||
| @@ -55,6 +55,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 55 | chrono = { version = "0.4", default-features = false, optional = true } | 55 | chrono = { version = "0.4", default-features = false, optional = true } |
| 56 | embedded-io = { version = "0.4.0", features = ["async"], optional = true } | 56 | embedded-io = { version = "0.4.0", features = ["async"], optional = true } |
| 57 | embedded-storage = { version = "0.3" } | 57 | embedded-storage = { version = "0.3" } |
| 58 | rand_core = "0.6.4" | ||
| 58 | 59 | ||
| 59 | rp2040-pac2 = { git = "https://github.com/embassy-rs/rp2040-pac2", rev="017e3c9007b2d3b6965f0d85b5bf8ce3fa6d7364", features = ["rt"] } | 60 | rp2040-pac2 = { git = "https://github.com/embassy-rs/rp2040-pac2", rev="017e3c9007b2d3b6965f0d85b5bf8ce3fa6d7364", features = ["rt"] } |
| 60 | #rp2040-pac2 = { path = "../../rp2040-pac2", features = ["rt"] } | 61 | #rp2040-pac2 = { path = "../../rp2040-pac2", features = ["rt"] } |
diff --git a/embassy-rp/src/clocks.rs b/embassy-rp/src/clocks.rs index 1c446f389..85c9bbb7a 100644 --- a/embassy-rp/src/clocks.rs +++ b/embassy-rp/src/clocks.rs | |||
| @@ -5,7 +5,7 @@ use crate::{pac, reset}; | |||
| 5 | const XOSC_MHZ: u32 = 12; | 5 | const XOSC_MHZ: u32 = 12; |
| 6 | 6 | ||
| 7 | /// safety: must be called exactly once at bootup | 7 | /// safety: must be called exactly once at bootup |
| 8 | pub unsafe fn init() { | 8 | pub(crate) unsafe fn init() { |
| 9 | // Reset everything except: | 9 | // Reset everything except: |
| 10 | // - QSPI (we're using it to run this code!) | 10 | // - QSPI (we're using it to run this code!) |
| 11 | // - PLLs (it may be suicide if that's what's clocking us) | 11 | // - PLLs (it may be suicide if that's what's clocking us) |
| @@ -196,3 +196,40 @@ unsafe fn configure_pll(p: pac::pll::Pll, refdiv: u32, vco_freq: u32, post_div1: | |||
| 196 | // Turn on post divider | 196 | // Turn on post divider |
| 197 | p.pwr().modify(|w| w.set_postdivpd(false)); | 197 | p.pwr().modify(|w| w.set_postdivpd(false)); |
| 198 | } | 198 | } |
| 199 | |||
| 200 | /// Random number generator based on the ROSC RANDOMBIT register. | ||
| 201 | /// | ||
| 202 | /// This will not produce random values if the ROSC is stopped or run at some | ||
| 203 | /// harmonic of the bus frequency. With default clock settings these are not | ||
| 204 | /// issues. | ||
| 205 | pub struct RoscRng; | ||
| 206 | |||
| 207 | impl RoscRng { | ||
| 208 | fn next_u8() -> u8 { | ||
| 209 | let random_reg = pac::ROSC.randombit(); | ||
| 210 | let mut acc = 0; | ||
| 211 | for _ in 0..u8::BITS { | ||
| 212 | acc <<= 1; | ||
| 213 | acc |= unsafe { random_reg.read().randombit() as u8 }; | ||
| 214 | } | ||
| 215 | acc | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | impl rand_core::RngCore for RoscRng { | ||
| 220 | fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { | ||
| 221 | Ok(self.fill_bytes(dest)) | ||
| 222 | } | ||
| 223 | |||
| 224 | fn next_u32(&mut self) -> u32 { | ||
| 225 | rand_core::impls::next_u32_via_fill(self) | ||
| 226 | } | ||
| 227 | |||
| 228 | fn next_u64(&mut self) -> u64 { | ||
| 229 | rand_core::impls::next_u64_via_fill(self) | ||
| 230 | } | ||
| 231 | |||
| 232 | fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
| 233 | dest.fill_with(Self::next_u8) | ||
| 234 | } | ||
| 235 | } | ||
diff --git a/embassy-rp/src/lib.rs b/embassy-rp/src/lib.rs index e5b07c903..d21b5f7b0 100644 --- a/embassy-rp/src/lib.rs +++ b/embassy-rp/src/lib.rs | |||
| @@ -21,7 +21,7 @@ pub mod uart; | |||
| 21 | #[cfg(feature = "nightly")] | 21 | #[cfg(feature = "nightly")] |
| 22 | pub mod usb; | 22 | pub mod usb; |
| 23 | 23 | ||
| 24 | mod clocks; | 24 | pub mod clocks; |
| 25 | pub mod flash; | 25 | pub mod flash; |
| 26 | mod reset; | 26 | mod reset; |
| 27 | 27 | ||
