From f28d1902a000a787d36b852a7aeb91491cc6b2a7 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 14 Dec 2022 09:16:23 +0100 Subject: [PATCH 1/2] Add RNG to list of peripherals to be created --- esp-hal-common/src/peripherals/esp32.rs | 1 + esp-hal-common/src/peripherals/esp32c2.rs | 1 + esp-hal-common/src/peripherals/esp32c3.rs | 1 + esp-hal-common/src/peripherals/esp32s2.rs | 1 + esp-hal-common/src/peripherals/esp32s3.rs | 1 + 5 files changed, 5 insertions(+) diff --git a/esp-hal-common/src/peripherals/esp32.rs b/esp-hal-common/src/peripherals/esp32.rs index e7cf346bfe7..13f44785748 100644 --- a/esp-hal-common/src/peripherals/esp32.rs +++ b/esp-hal-common/src/peripherals/esp32.rs @@ -54,6 +54,7 @@ mod peripherals { crate::create_peripherals! { I2C0, I2C1, + RNG, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32c2.rs b/esp-hal-common/src/peripherals/esp32c2.rs index 6c96fccab3d..e9217fc8a46 100644 --- a/esp-hal-common/src/peripherals/esp32c2.rs +++ b/esp-hal-common/src/peripherals/esp32c2.rs @@ -35,6 +35,7 @@ mod peripherals { crate::create_peripherals! { I2C0, + RNG, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32c3.rs b/esp-hal-common/src/peripherals/esp32c3.rs index cf96540353a..bd9100b4c18 100644 --- a/esp-hal-common/src/peripherals/esp32c3.rs +++ b/esp-hal-common/src/peripherals/esp32c3.rs @@ -46,6 +46,7 @@ mod peripherals { crate::create_peripherals! { I2C0, + RNG, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32s2.rs b/esp-hal-common/src/peripherals/esp32s2.rs index 3befeb7e410..9b92f145b5a 100644 --- a/esp-hal-common/src/peripherals/esp32s2.rs +++ b/esp-hal-common/src/peripherals/esp32s2.rs @@ -52,6 +52,7 @@ mod peripherals { crate::create_peripherals! { I2C0, I2C1, + RNG, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32s3.rs b/esp-hal-common/src/peripherals/esp32s3.rs index 0fa2dd501d8..d7b934d2794 100644 --- a/esp-hal-common/src/peripherals/esp32s3.rs +++ b/esp-hal-common/src/peripherals/esp32s3.rs @@ -63,6 +63,7 @@ mod peripherals { crate::create_peripherals! { I2C0, I2C1, + RNG, SPI0, SPI1, SPI2, From f728ddbd4f1aa17001f11488c931d38f9d40e38b Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 14 Dec 2022 09:16:34 +0100 Subject: [PATCH 2/2] Refactor RNG driver to use PeripheralRef --- esp-hal-common/src/rng.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/esp-hal-common/src/rng.rs b/esp-hal-common/src/rng.rs index a99d28e4b9f..64e2eeb9c31 100644 --- a/esp-hal-common/src/rng.rs +++ b/esp-hal-common/src/rng.rs @@ -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 /// @@ -28,14 +31,15 @@ use crate::pac::RNG; /// /// For more information, please refer to the ESP-IDF documentation: /// -#[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

+ 'd) -> Self { + crate::into_ref!(rng); + Self { rng } } @@ -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> {