Struct stm32wlxx_hal::rng::Rng
source · pub struct Rng { /* private fields */ }
Expand description
RNG driver.
Implementations§
source§impl Rng
impl Rng
sourcepub fn new(rng: RNG, clk: Clk, rcc: &mut RCC) -> Rng
pub fn new(rng: RNG, clk: Clk, rcc: &mut RCC) -> Rng
Create a new Rng
driver from a RNG peripheral.
This will select the clock source, enable clocks, and reset the RNG peripheral.
This will NOT enable the selected clock source, when in doubt use MSI because it is enabled with power-on-reset.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
sourcepub fn free(self) -> RNG
pub fn free(self) -> RNG
Free the RNG peripheral from the driver.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rcc: pac::RCC = dp.RCC;
let rng_dp: pac::RNG = dp.RNG;
let mut rng = Rng::new(rng_dp, Clk::Msi, &mut rcc);
// ... use rng
let rng_dp: pac::RNG = rng.free();
sourcepub unsafe fn steal() -> Rng
pub unsafe fn steal() -> Rng
Steal the RNG peripheral from whatever is currently using it.
This will not initialize the RNG peripheral (unlike new
).
Safety
This will create a new RNG peripheral, bypassing the singleton checks that normally occur. You are responsible for ensuring that the driver has exclusive access to the RNG peripheral. You are also responsible for ensuring the RNG peripheral has been setup correctly.
This will also reset the correctable noise error counter.
Example
use stm32wlxx_hal::rng::Rng;
// ... setup happens here
let rng = unsafe { Rng::steal() };
sourcepub unsafe fn unmask_irq()
Available on non-crate feature stm32wl5x_cm0p
and crate feature rt
only.
pub unsafe fn unmask_irq()
stm32wl5x_cm0p
and crate feature rt
only.sourcepub unsafe fn disable_clock(rcc: &mut RCC)
pub unsafe fn disable_clock(rcc: &mut RCC)
Disable the RNG clock.
Safety
- You are responsible for ensuring the RNG is in a state where the clock can be disabled without entering an error state.
- You cannot use the RNG bus while the clock is disabled.
- You are responsible for re-enabling the clock before resuming use of the RNG.
- You are responsible for setting up anything that may have lost state while the clock was disabled.
sourcepub fn enable_clock(rcc: &mut RCC)
pub fn enable_clock(rcc: &mut RCC)
Enable the RNG clock.
sourcepub fn seed_error_stat(&self) -> u32
pub fn seed_error_stat(&self) -> u32
Returns the number of correctable seed errors that have occurred.
This counter will saturate when it hits the maximum value.
sourcepub fn reset_seed_error_stat(&mut self)
pub fn reset_seed_error_stat(&mut self)
Reset the correctable seed error counter to zero.
sourcepub fn try_fill_u32(&mut self, dest: &mut [u32]) -> Result<(), Error>
pub fn try_fill_u32(&mut self, dest: &mut [u32]) -> Result<(), Error>
Try to fill the destination buffer with random data.
This is the native data size for the RNG.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let mut nonce: [u32; 4] = [0; 4];
rng.try_fill_u32(&mut nonce)?;
sourcepub fn try_fill_u8(&mut self, dest: &mut [u8]) -> Result<(), Error>
pub fn try_fill_u8(&mut self, dest: &mut [u8]) -> Result<(), Error>
Try to fill the destination buffer with random data.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let mut nonce: [u8; 16] = [0; 16];
rng.try_fill_u8(&mut nonce)?;
sourcepub fn try_u8(&mut self) -> Result<u8, Error>
pub fn try_u8(&mut self) -> Result<u8, Error>
Try to generate a random u8
.
This is not efficient if you need to generate a lot of entropy, this is
provided as a convenience function that wraps
try_fill_u8
for testing and prototyping.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let rand_value: u8 = rng.try_u8()?;
sourcepub fn try_u16(&mut self) -> Result<u16, Error>
pub fn try_u16(&mut self) -> Result<u16, Error>
Try to generate a random u16
.
This is not efficient if you need to generate a lot of entropy, this is
provided as a convenience function that wraps
try_fill_u8
for testing and prototyping.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let rand_value: u16 = rng.try_u16()?;
sourcepub fn try_u32(&mut self) -> Result<u32, Error>
pub fn try_u32(&mut self) -> Result<u32, Error>
Try to generate a random u32
.
This is not efficient if you need to generate a lot of entropy, this is
provided as a convenience function that wraps
try_fill_u32
for testing and prototyping.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let rand_value: u32 = rng.try_u32()?;
sourcepub fn try_u64(&mut self) -> Result<u64, Error>
pub fn try_u64(&mut self) -> Result<u64, Error>
Try to generate a random u64
.
This is not efficient if you need to generate a lot of entropy, this is
provided as a convenience function that wraps
try_fill_u8
for testing and prototyping.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let rand_value: u64 = rng.try_u64()?;
sourcepub fn try_u128(&mut self) -> Result<u128, Error>
pub fn try_u128(&mut self) -> Result<u128, Error>
Try to generate a random u128
.
This is not efficient if you need to generate a lot of entropy, this is
provided as a convenience function that wraps
try_fill_u8
for testing and prototyping.
Example
use stm32wlxx_hal::{
pac,
rng::{Clk, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut rng = Rng::new(dp.RNG, Clk::Msi, &mut dp.RCC);
let rand_value: u128 = rng.try_u128()?;
Trait Implementations§
source§impl RngCore for Rng
impl RngCore for Rng
source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
Not recommended for use, panics upon errors.
impl CryptoRng for Rng
Auto Trait Implementations§
impl RefUnwindSafe for Rng
impl Send for Rng
impl !Sync for Rng
impl Unpin for Rng
impl UnwindSafe for Rng
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CryptoRngCore for Twhere
T: CryptoRng + RngCore,
impl<T> CryptoRngCore for Twhere T: CryptoRng + RngCore,
source§fn as_rngcore(&mut self) -> &mut dyn RngCore
fn as_rngcore(&mut self) -> &mut dyn RngCore
RngCore
trait object.