pub struct Trng<'d> {
pub rng: Rng,
/* private fields */
}Expand description
True Random Number Generator (TRNG) driver
The Trng struct represents a true random number generator that combines
the randomness from the hardware RNG and an ADC. This struct provides
methods to generate random numbers and fill buffers with random bytes.
Due to pulling the entropy source from the ADC, it uses the associated
registers, so to use TRNG we need to “occupy” the ADC peripheral.
let mut buf = [0u8; 16];
// ADC is not available from now
let mut trng = Trng::new(peripherals.RNG, &mut peripherals.ADC1);
trng.read(&mut buf);
let mut true_rand = trng.random();
let mut rng = trng.downgrade();
// ADC is available now
let analog_pin = peripherals.GPIO3;
let mut adc1_config = AdcConfig::new();
let mut adc1_pin = adc1_config.enable_pin(
analog_pin,
Attenuation::Attenuation11dB
);
let mut adc1 = Adc::<ADC1>::new(peripherals.ADC1, adc1_config);
let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap();
rng.read(&mut buf);
true_rand = rng.random();
let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap();Fields§
§rng: RngThe hardware random number generator instance.
Implementations§
Source§impl<'d> Trng<'d>
impl<'d> Trng<'d>
Sourcepub fn new(
rng: impl Peripheral<P = RNG>,
adc: impl Peripheral<P = ADC1> + 'd,
) -> Self
pub fn new( rng: impl Peripheral<P = RNG>, adc: impl Peripheral<P = ADC1> + 'd, ) -> Self
Trait Implementations§
Source§impl RngCore for Trng<'_>
impl RngCore for Trng<'_>
Implementing RngCore trait from rand_core for Trng structure
Source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
Fill
dest with random data. Read moreimpl CryptoRng for Trng<'_>
Implementing a CryptoRng marker trait that indicates that the generator is cryptographically secure.
Auto Trait Implementations§
impl<'d> Freeze for Trng<'d>
impl<'d> RefUnwindSafe for Trng<'d>
impl<'d> Send for Trng<'d>
impl<'d> Sync for Trng<'d>
impl<'d> Unpin for Trng<'d>
impl<'d> !UnwindSafe for Trng<'d>
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
Mutably borrows from an owned value. Read more
Source§impl<T> CryptoRngCore for T
impl<T> CryptoRngCore for T
Source§fn as_rngcore(&mut self) -> &mut dyn RngCore
fn as_rngcore(&mut self) -> &mut dyn RngCore
Upcast to an
RngCore trait object.