pub struct Trng<'d, L: SecurityMarker> { /* private fields */ }Expand description
True Random Number Generator (TRNG) Driver for MSPM0 series.
The driver provides blocking random numbers with TryRngCore methods and asynchronous counterparts in Trng::async_read_u32, Trng::async_read_u64, and Trng::async_read_bytes.
The TRNG can be configured with different decimation rates. See DecimRate, FastDecimRate, and CryptoDecimRate.
The TRNG can be instantiated with Trng::new, Trng::new_fast, or Trng::new_secure.
Usage example:
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_mspm0::Config;
use embassy_mspm0::trng::Trng;
use rand_core::TryRngCore;
use {defmt_rtt as _, panic_halt as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let p = embassy_mspm0::init(Config::default());
let mut trng = Trng::new(p.TRNG).expect("Failed to initialize TRNG");
let mut randomness = [0u8; 16];
loop {
trng.fill_bytes(&mut randomness).unwrap();
assert_ne!(randomness, [0u8; 16]);
}
}Implementations§
Source§impl<'d> Trng<'d, Fast>
impl<'d> Trng<'d, Fast>
Sourcepub fn new_fast(
peripheral: Peri<'d, TRNG>,
rate: FastDecimRate,
) -> Result<Self, Error>
pub fn new_fast( peripheral: Peri<'d, TRNG>, rate: FastDecimRate, ) -> Result<Self, Error>
Setup a TRNG driver with a specific fast decimation rate.
Trng::new_secure instead. Source§impl<'d> Trng<'d, Crypto>
impl<'d> Trng<'d, Crypto>
Sourcepub fn new_secure(
peripheral: Peri<'d, TRNG>,
rate: CryptoDecimRate,
) -> Result<Self, Error>
pub fn new_secure( peripheral: Peri<'d, TRNG>, rate: CryptoDecimRate, ) -> Result<Self, Error>
Setup a TRNG driver with a specific cryptographic decimation rate.
Source§impl<'d, D: SecurityMarker> Trng<'d, D>
impl<'d, D: SecurityMarker> Trng<'d, D>
Sourcepub fn fail_reset(&mut self) -> Result<(), Error>
pub fn fail_reset(&mut self) -> Result<(), Error>
L-series TRM 13.2.5.2(10): procedure for recovering from a failed read.
This method reinitializes the TRNG which may take some time to complete.
Sourcepub async fn async_read_u32(&mut self) -> Result<u32, Error>
pub async fn async_read_u32(&mut self) -> Result<u32, Error>
Asynchronously read a 32-bit random value from the TRNG.
The synchronous counterpart is given by TryRngCore::try_next_u32.
As with the synchronous methods, an Err may be retried up to two times after calling Trng::fail_reset.
Sourcepub async fn async_read_u64(&mut self) -> Result<u64, Error>
pub async fn async_read_u64(&mut self) -> Result<u64, Error>
Asynchronously read a 64-bit random value from the TRNG.
The synchronous counterpart is given by TryRngCore::try_next_u64.
As with the synchronous methods, an Err may be retried up to two times after calling Trng::fail_reset.
Sourcepub async fn async_read_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
pub async fn async_read_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
Asynchronously fill dest with random bytes from the TRNG.
The synchronous counterpart is given by TryRngCore::try_fill_bytes.
As with the synchronous methods, an Err may be retried up to two times after calling Trng::fail_reset.
Note When an error condition occurs, the buffer may be partially filled.
Trait Implementations§
Source§impl<D: SecurityMarker> TryRngCore for Trng<'_, D>
Implements the fallible TryRngCore.
impl<D: SecurityMarker> TryRngCore for Trng<'_, D>
Implements the fallible TryRngCore.
If any of the methods give an Err, the operation may be retried up to two times after calling Trng::fail_reset.