Skip to content

Commit f728ddb

Browse files
committed
Refactor RNG driver to use PeripheralRef
1 parent f28d190 commit f728ddb

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

esp-hal-common/src/rng.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use core::convert::Infallible;
44

55
use embedded_hal::blocking::rng::Read;
66

7-
use crate::pac::RNG;
7+
use crate::{
8+
peripheral::{Peripheral, PeripheralRef},
9+
peripherals::RNG,
10+
};
811

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

36-
impl Rng {
38+
impl<'d> Rng<'d> {
3739
/// Create a new random number generator instance
38-
pub fn new(rng: RNG) -> Self {
40+
pub fn new(rng: impl Peripheral<P = RNG> + 'd) -> Self {
41+
crate::into_ref!(rng);
42+
3943
Self { rng }
4044
}
4145

@@ -44,14 +48,9 @@ impl Rng {
4448
pub fn random(&mut self) -> u32 {
4549
self.rng.data.read().bits()
4650
}
47-
48-
/// Return the raw interface to the underlying `Rng` instance
49-
pub fn free(self) -> RNG {
50-
self.rng
51-
}
5251
}
5352

54-
impl Read for Rng {
53+
impl Read for Rng<'_> {
5554
type Error = Infallible;
5655

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

0 commit comments

Comments
 (0)