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

PeripheralRef for ClockControl #316

Merged
merged 1 commit 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
105 changes: 68 additions & 37 deletions esp-hal-common/src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! # Clock Control
use fugit::HertzU32;

use crate::system::SystemClockControl;
use crate::{
peripheral::{Peripheral, PeripheralRef},
system::SystemClockControl,
};

#[cfg_attr(esp32, path = "clocks_ll/esp32.rs")]
#[cfg_attr(esp32c2, path = "clocks_ll/esp32c2.rs")]
Expand Down Expand Up @@ -107,8 +110,8 @@ impl Clock for ApbClock {
///
/// The existence of this value indicates that the clock configuration can no
/// longer be changed
pub struct Clocks {
_private: (),
pub struct Clocks<'d> {
_private: PeripheralRef<'d, SystemClockControl>,
pub cpu_clock: HertzU32,
pub apb_clock: HertzU32,
pub xtal_clock: HertzU32,
Expand All @@ -121,14 +124,17 @@ pub struct Clocks {
}

#[doc(hidden)]
impl Clocks {
impl<'d> Clocks<'d> {
/// This should not be used in user code.
/// The whole point this exists is make it possible to have other crates
/// (i.e. esp-wifi) create `Clocks`
#[doc(hidden)]
pub fn from_raw_clocks(raw_clocks: RawClocks) -> Clocks {
pub fn from_raw_clocks(
system_clock_control: PeripheralRef<'d, SystemClockControl>,
raw_clocks: RawClocks,
) -> Clocks<'d> {
Self {
_private: (),
_private: system_clock_control,
cpu_clock: raw_clocks.cpu_clock,
apb_clock: raw_clocks.apb_clock,
xtal_clock: raw_clocks.xtal_clock,
Expand Down Expand Up @@ -158,27 +164,29 @@ pub struct RawClocks {
///
/// After setting all frequencies, call the freeze function to apply the
/// configuration.
pub struct ClockControl {
_private: (),
pub struct ClockControl<'d> {
_private: PeripheralRef<'d, SystemClockControl>,
desired_rates: RawClocks,
}

impl ClockControl {
impl<'d> ClockControl<'d> {
/// Applies the clock configuration and returns a Clocks struct that
/// signifies that the clocks are frozen, and contains the frequencies
/// used. After this function is called, the clocks can not change
pub fn freeze(self) -> Clocks {
Clocks::from_raw_clocks(self.desired_rates)
pub fn freeze(self) -> Clocks<'d> {
Clocks::from_raw_clocks(self._private, self.desired_rates)
}
}

#[cfg(esp32)]
impl ClockControl {
impl<'d> ClockControl<'d> {
/// Use what is considered the default settings after boot.
#[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(80),
Expand All @@ -191,7 +199,10 @@ impl ClockControl {

/// Configure the CPU clock speed.
#[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
pub fn configure(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
cpu_clock_speed: CpuClock,
) -> ClockControl<'d> {
// like NuttX use 40M hardcoded - if it turns out to be a problem
// we will take care then
let xtal_freq = XtalClock::RtcXtalFreq40M;
Expand All @@ -207,7 +218,7 @@ impl ClockControl {
clocks_ll::set_cpu_freq(cpu_clock_speed);

ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: cpu_clock_speed.frequency(),
apb_clock: HertzU32::MHz(80),
Expand All @@ -223,13 +234,15 @@ impl ClockControl {
}

#[cfg(esp32c2)]
impl ClockControl {
impl<'d> ClockControl<'d> {
/// Use what is considered the default settings after boot.
#[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
#[cfg(feature = "esp32c2_40mhz")]
return ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(40),
Expand All @@ -240,7 +253,7 @@ impl ClockControl {

#[cfg(feature = "esp32c2_26mhz")]
return ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(40),
Expand All @@ -252,7 +265,10 @@ impl ClockControl {

/// Configure the CPU clock speed.
#[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
pub fn configure(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
cpu_clock_speed: CpuClock,
) -> ClockControl<'d> {
let apb_freq;
#[cfg(feature = "esp32c2_40mhz")]
let xtal_freq = XtalClock::RtcXtalFreq40M;
Expand All @@ -273,7 +289,7 @@ impl ClockControl {
}

ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: cpu_clock_speed.frequency(),
apb_clock: apb_freq.frequency(),
Expand All @@ -285,12 +301,14 @@ impl ClockControl {
}

#[cfg(esp32c3)]
impl ClockControl {
impl<'d> ClockControl<'d> {
/// Use what is considered the default settings after boot.
#[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(80),
Expand All @@ -302,7 +320,10 @@ impl ClockControl {

/// Configure the CPU clock speed.
#[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
pub fn configure(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
cpu_clock_speed: CpuClock,
) -> ClockControl<'d> {
let apb_freq;
let xtal_freq = XtalClock::RtcXtalFreq40M;
let pll_freq = PllClock::Pll480MHz;
Expand All @@ -320,7 +341,7 @@ impl ClockControl {
}

ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: cpu_clock_speed.frequency(),
apb_clock: apb_freq.frequency(),
Expand All @@ -332,12 +353,14 @@ impl ClockControl {
}

#[cfg(esp32s2)]
impl ClockControl {
impl<'d> ClockControl<'d> {
/// Use what is considered the default settings after boot.
#[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(80),
Expand All @@ -349,11 +372,14 @@ impl ClockControl {

/// Configure the CPU clock speed.
#[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
pub fn configure(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
cpu_clock_speed: CpuClock,
) -> ClockControl<'d> {
clocks_ll::set_cpu_clock(cpu_clock_speed);

ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: cpu_clock_speed.frequency(),
apb_clock: HertzU32::MHz(80),
Expand All @@ -365,12 +391,14 @@ impl ClockControl {
}

#[cfg(esp32s3)]
impl ClockControl {
impl<'d> ClockControl<'d> {
/// Use what is considered the default settings after boot.
#[allow(unused)]
pub fn boot_defaults(clock_control: SystemClockControl) -> ClockControl {
pub fn boot_defaults(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
) -> ClockControl<'d> {
ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: HertzU32::MHz(80),
apb_clock: HertzU32::MHz(80),
Expand All @@ -383,11 +411,14 @@ impl ClockControl {

/// Configure the CPU clock speed.
#[allow(unused)]
pub fn configure(clock_control: SystemClockControl, cpu_clock_speed: CpuClock) -> ClockControl {
pub fn configure(
clock_control: impl Peripheral<P = SystemClockControl> + 'd,
cpu_clock_speed: CpuClock,
) -> ClockControl<'d> {
clocks_ll::set_cpu_clock(cpu_clock_speed);

ClockControl {
_private: (),
_private: clock_control.into_ref(),
desired_rates: RawClocks {
cpu_clock: cpu_clock_speed.frequency(),
apb_clock: HertzU32::MHz(80),
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub enum LSGlobalClkSource {
pub struct LEDC<'d> {
_instance: PeripheralRef<'d, crate::peripherals::LEDC>,
ledc: &'d crate::peripherals::ledc::RegisterBlock,
clock_control_config: &'d Clocks,
clock_control_config: &'d Clocks<'d>,
}

#[cfg(esp32)]
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/ledc/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub trait TimerHW<S: TimerSpeed> {
/// Timer struct
pub struct Timer<'a, S: TimerSpeed> {
ledc: &'a crate::peripherals::ledc::RegisterBlock,
clock_control_config: &'a Clocks,
clock_control_config: &'a Clocks<'a>,
number: Number,
duty: Option<config::Duty>,
configured: bool,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/mcpwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<PWM: PwmPeripheral> MCPWM<PWM> {
pub struct PeripheralClockConfig<'a> {
frequency: HertzU32,
prescaler: u8,
phantom: PhantomData<&'a Clocks>,
phantom: PhantomData<&'a Clocks<'a>>,
}

impl<'a> PeripheralClockConfig<'a> {
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/src/mcpwm/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub struct TimerClockConfig<'a> {
period: u16,
prescaler: u8,
mode: PwmWorkingMode,
phantom: PhantomData<&'a Clocks>,
phantom: PhantomData<&'a Clocks<'a>>,
}

impl<'a> TimerClockConfig<'a> {
Expand Down
29 changes: 29 additions & 0 deletions esp-hal-common/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,32 @@ impl<'d, T: crate::peripheral::Peripheral<P = SystemPeripheral> + 'd> SystemExt<
}
}
}

impl core::ops::Deref for SystemClockControl {
type Target = SystemClockControl;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for SystemClockControl {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}

impl crate::peripheral::Peripheral for SystemClockControl {
type P = SystemClockControl;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
SystemClockControl { _private: () }
}
}
impl crate::peripheral::Peripheral for &mut SystemClockControl {
type P = SystemClockControl;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
SystemClockControl { _private: () }
}
}