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 and MabezDev committed Dec 14, 2022
1 parent 39c5a04 commit 273325e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
20 changes: 11 additions & 9 deletions esp-hal-common/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ use self::{
use crate::{
clock::Clocks,
gpio::OutputPin,
system::{Peripheral, PeripheralClockControl},
peripheral::{Peripheral, PeripheralRef},
system::{Peripheral as PeripheralEnable, PeripheralClockControl},
};

pub mod channel;
Expand All @@ -84,10 +85,10 @@ pub enum LSGlobalClkSource {
}

/// LEDC (LED PWM Controller)
pub struct LEDC<'a> {
_instance: crate::pac::LEDC,
ledc: &'a crate::pac::ledc::RegisterBlock,
clock_control_config: &'a Clocks,
pub struct LEDC<'d> {
_instance: PeripheralRef<'d, crate::peripherals::LEDC>,
ledc: &'d crate::pac::ledc::RegisterBlock,
clock_control_config: &'d Clocks,
}

#[cfg(esp32)]
Expand All @@ -104,14 +105,15 @@ impl Speed for HighSpeed {}

impl Speed for LowSpeed {}

impl<'a> LEDC<'a> {
impl<'d> LEDC<'d> {
/// Return a new LEDC
pub fn new(
_instance: crate::pac::LEDC,
clock_control_config: &'a Clocks,
_instance: impl Peripheral<P = crate::peripherals::LEDC> + 'd,
clock_control_config: &'d Clocks,
system: &mut PeripheralClockControl,
) -> Self {
system.enable(Peripheral::Ledc);
crate::into_ref!(_instance);
system.enable(PeripheralEnable::Ledc);

let ledc = unsafe { &*crate::pac::LEDC::ptr() };
LEDC {
Expand Down
2 changes: 2 additions & 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 All @@ -62,5 +63,6 @@ mod peripherals {
UART1,
UART2,
DPORT,
LEDC,
}
}
2 changes: 2 additions & 0 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ mod peripherals {

crate::create_peripherals! {
I2C0,
RNG,
SPI0,
SPI1,
SPI2,
SYSTIMER,
UART0,
UART1,
SYSTEM,
LEDC,
}
}
2 changes: 2 additions & 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 All @@ -54,5 +55,6 @@ mod peripherals {
UART1,
USB_DEVICE,
SYSTEM,
LEDC,
}
}
2 changes: 2 additions & 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 All @@ -61,5 +62,6 @@ mod peripherals {
UART0,
UART1,
SYSTEM,
LEDC,
}
}
2 changes: 2 additions & 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 All @@ -73,5 +74,6 @@ mod peripherals {
UART2,
USB_DEVICE,
SYSTEM,
LEDC,
}
}
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 273325e

Please sign in to comment.