Struct stm32wlxx_hal::rng::Rng

source ·
pub struct Rng { /* private fields */ }
Expand description

RNG driver.

Implementations§

source§

impl Rng

source

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);
source

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();
source

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() };
source

pub unsafe fn unmask_irq()

Available on non-crate feature stm32wl5x_cm0p and crate feature rt only.

Unmask the RNG IRQ in the NVIC.

Safety

This can break mask-based critical sections.

Example
unsafe { stm32wlxx_hal::rng::Rng::unmask_irq() };
source

pub unsafe fn disable_clock(rcc: &mut RCC)

Disable the RNG clock.

Safety
  1. You are responsible for ensuring the RNG is in a state where the clock can be disabled without entering an error state.
  2. You cannot use the RNG bus while the clock is disabled.
  3. You are responsible for re-enabling the clock before resuming use of the RNG.
  4. You are responsible for setting up anything that may have lost state while the clock was disabled.
source

pub fn enable_clock(rcc: &mut RCC)

Enable the RNG clock.

source

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.

source

pub fn reset_seed_error_stat(&mut self)

Reset the correctable seed error counter to zero.

source

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)?;
source

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)?;
source

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()?;
source

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()?;
source

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()?;
source

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()?;
source

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 Debug for Rng

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl RngCore for Rng

source§

fn next_u32(&mut self) -> u32

Not recommended for use, panics upon errors.

source§

fn next_u64(&mut self) -> u64

Not recommended for use, panics upon errors.

source§

fn fill_bytes(&mut self, dest: &mut [u8])

Not recommended for use, panics upon errors.

source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>

Use this method if using the RngCore for CryptoRng traits.

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CryptoRngCore for Twhere T: CryptoRng + RngCore,

source§

fn as_rngcore(&mut self) -> &mut dyn RngCore

Upcast to an RngCore trait object.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.