Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peripheral ref/rng #306

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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