Skip to content

Commit

Permalink
Peripheral ref/rng (#306)
Browse files Browse the repository at this point in the history
* Add RNG to list of peripherals to be created

* Refactor RNG driver to use PeripheralRef
  • Loading branch information
jessebraham authored Dec 14, 2022
1 parent 39c5a04 commit f2b5953
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod peripherals {
crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod peripherals {

crate::create_peripherals! {
I2C0,
RNG,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod peripherals {

crate::create_peripherals! {
I2C0,
RNG,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod peripherals {
crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod peripherals {
crate::create_peripherals! {
I2C0,
I2C1,
RNG,
SPI0,
SPI1,
SPI2,
Expand Down
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 f2b5953

Please sign in to comment.