Skip to content

Commit

Permalink
feat: update TRNG utils with optional RngCore impl (#4)
Browse files Browse the repository at this point in the history
* replace trng utils with optional rand impl

* fix: rand_core version

* feat: `rt` flag is now default
docs: improve TRNG docs

---------

Co-authored-by: henopied <[email protected]>
Co-authored-by: Minh Duong <[email protected]>
  • Loading branch information
3 people authored Jan 22, 2025
1 parent 074c0a6 commit 52fc813
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ embedded-hal-nb = "1.0.0"
embedded-io = "0.6.1"
max78000-pac = "0.3.0"
paste = "1.0.15"
rand = { version = "0.8.5", default-features = false, optional = true }
rand_core = { version = "0.6.4", default-features = false, optional = true }

[features]
default = ["rand", "rt"]
rand = ["dep:rand", "dep:rand_core"]
rt = ["max78000-pac/critical-section", "max78000-pac/rt"]
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! # Hardware Abstraction Layer for MAX7800x Microcontrollers
#![no_std]
#![feature(doc_cfg)]

/// Re-export of the Peripheral Access Crate (PAC) for the MAX78000.
pub use max78000_pac as pac;
Expand Down
56 changes: 42 additions & 14 deletions src/trng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//!
//! The TRNG is a hardware module that generates random numbers using
//! physical entropy sources.
use rand::Error;
#[cfg(feature = "rand")]
use rand::RngCore;
#[cfg(feature = "rand")]
use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};

/// # True Random Number Generator (TRNG) Peripheral
///
Expand All @@ -10,10 +15,7 @@
/// // Create a new TRNG peripheral instance
/// let trng = Trng::new(p.trng, &mut gcr.reg);
/// // Generate a random 32-bit number
/// let random_number = trng.next_u32();
/// // Fill an array with random bytes
/// let mut buffer = [0u8; 64];
/// trng.fill_bytes(&mut buffer);
/// let random_u32 = trng.gen_u32();
/// ```
pub struct Trng {
trng: crate::pac::Trng,
Expand All @@ -36,23 +38,49 @@ impl Trng {

/// Generate a random 32-bit number.
#[inline(always)]
pub fn next_u32(&self) -> u32 {
pub fn gen_u32(&self) -> u32 {
while !self._is_ready() {}
self.trng.data().read().bits() as u32
}
}

/// Enhanced functionality for the TRNG peripheral using the [`rand`] crate.
/// This trait implementation can be disabled by removing the `rand` feature
/// flag since you may want to implement your own [`RngCore`].
///
/// Example:
/// ```
/// // Create a new TRNG peripheral instance
/// let trng = Trng::new(p.trng, &mut gcr.reg);
/// // Generate a random 32-bit number
/// let random_u32 = trng.next_u32(); // Equivalent to trng.gen_u32()
/// // Generate a random 64-bit number
/// let random_u64 = trng.next_u64();
/// // Fill a buffer with random bytes
/// let mut buffer = [0u8; 16];
/// trng.fill_bytes(&mut buffer);
/// ```
#[doc(cfg(feature = "rand"))]
#[cfg(feature = "rand")]
impl RngCore for Trng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
self.gen_u32()
}

#[inline(always)]
fn next_u64(&mut self) -> u64 {
next_u64_via_u32(self)
}

/// Generate a random 8-bit number.
#[inline(always)]
pub fn next_u8(&self) -> u8 {
self.next_u32() as u8
fn fill_bytes(&mut self, dest: &mut [u8]) {
fill_bytes_via_next(self, dest);
}

/// Fill a buffer with random bytes.
#[inline(always)]
pub fn fill_bytes(&self, buffer: &mut [u8]) {
for word in buffer.chunks_mut(size_of::<u32>()) {
let random = self.next_u32();
word.copy_from_slice(&random.to_le_bytes()[..word.len()]);
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}

0 comments on commit 52fc813

Please sign in to comment.