Skip to content

Commit

Permalink
Refactor RNG driver to use PeripheralRef
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Dec 14, 2022
1 parent f28d190 commit f728ddb
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions esp-hal-common/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use core::convert::Infallible;

use embedded_hal::blocking::rng::Read;

use crate::pac::RNG;
use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::RNG,
};

/// Random Number Generator
///
Expand All @@ -28,14 +31,15 @@ use crate::pac::RNG;
///
/// For more information, please refer to the ESP-IDF documentation:
/// <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html>
#[derive(Debug)]
pub struct Rng {
rng: RNG,
pub struct Rng<'d> {
rng: PeripheralRef<'d, RNG>,
}

impl Rng {
impl<'d> Rng<'d> {
/// Create a new random number generator instance
pub fn new(rng: RNG) -> Self {
pub fn new(rng: impl Peripheral<P = RNG> + 'd) -> Self {
crate::into_ref!(rng);

Self { rng }
}

Expand All @@ -44,14 +48,9 @@ impl Rng {
pub fn random(&mut self) -> u32 {
self.rng.data.read().bits()
}

/// Return the raw interface to the underlying `Rng` instance
pub fn free(self) -> RNG {
self.rng
}
}

impl Read for Rng {
impl Read for Rng<'_> {
type Error = Infallible;

fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
Expand Down

0 comments on commit f728ddb

Please sign in to comment.