Skip to content

Commit 3def49d

Browse files
committed
Describe C2 clock tree, remove TIMG APB clock gate node
1 parent b19a036 commit 3def49d

File tree

15 files changed

+1850
-533
lines changed

15 files changed

+1850
-533
lines changed

esp-hal/src/clock/mod.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,32 +1201,23 @@ impl Clocks {
12011201
impl Clocks {
12021202
/// Configure the CPU clock speed.
12031203
pub(crate) fn configure(cpu_clock_speed: CpuClock) -> Self {
1204-
let xtal_freq = Self::measure_xtal_frequency();
1205-
1206-
let apb_freq;
1207-
if cpu_clock_speed != CpuClock::default() {
1208-
let pll_freq = PllClock::Pll480MHz;
1204+
use crate::soc::clocks::{ClockTree, request_cpu_clk};
12091205

1210-
if cpu_clock_speed.mhz() <= xtal_freq.mhz() {
1211-
apb_freq = ApbClock::ApbFreqOther(cpu_clock_speed.mhz());
1212-
clocks_ll::esp32c2_rtc_update_to_xtal(xtal_freq, 1);
1213-
clocks_ll::esp32c2_rtc_apb_freq_update(apb_freq);
1214-
} else {
1215-
apb_freq = ApbClock::ApbFreq40MHz;
1216-
clocks_ll::esp32c2_rtc_bbpll_enable();
1217-
clocks_ll::esp32c2_rtc_bbpll_configure(xtal_freq, pll_freq);
1218-
clocks_ll::esp32c2_rtc_freq_to_pll_mhz(cpu_clock_speed);
1219-
clocks_ll::esp32c2_rtc_apb_freq_update(apb_freq);
1220-
}
1221-
} else {
1222-
apb_freq = ApbClock::ApbFreq40MHz;
1206+
// TODO: expose the whole new enum for custom options
1207+
match cpu_clock_speed {
1208+
CpuClock::_80MHz => crate::soc::clocks::CpuClock::_80MHz,
1209+
CpuClock::_120MHz => crate::soc::clocks::CpuClock::_120MHz,
12231210
}
1211+
.configure();
12241212

1225-
Self {
1226-
cpu_clock: cpu_clock_speed.frequency(),
1227-
apb_clock: apb_freq.frequency(),
1228-
xtal_clock: xtal_freq.frequency(),
1229-
}
1213+
ClockTree::with(|clocks| {
1214+
request_cpu_clk(clocks);
1215+
Self {
1216+
cpu_clock: Rate::from_hz(crate::soc::clocks::cpu_clk_frequency(clocks)),
1217+
apb_clock: Rate::from_hz(crate::soc::clocks::apb_clk_frequency(clocks)),
1218+
xtal_clock: Rate::from_hz(crate::soc::clocks::xtl_clk_frequency(clocks)),
1219+
}
1220+
})
12301221
}
12311222
}
12321223

esp-hal/src/soc/esp32/clocks.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(crate) enum CpuClock {
4545
impl CpuClock {
4646
pub(crate) fn configure(self) {
4747
// Resolve presets
48+
// TODO: set some defaults to RTC clocks
4849
let mut config = match self {
4950
CpuClock::_80MHz => ClockConfig {
5051
xtl_clk: None,
@@ -102,12 +103,12 @@ fn detect_xtal_freq(clocks: &mut ClockTree) -> XtlClkConfig {
102103

103104
// Just an assumption for things to not panic.
104105
configure_xtl_clk(clocks, XtlClkConfig::_40);
106+
// TIMG0 counter is clocked from the APB clock, make sure APB_CLK == XTL_CLK
105107
configure_syscon_pre_div(clocks, SysconPreDivConfig(0));
106108
configure_cpu_clk(clocks, CpuClkConfig::Xtal);
107109

108-
// Without the peripheral clock, we can't write peripheral registers.
109-
configure_timg0_peripheral_clock(clocks, Timg0PeripheralClockConfig::ApbClk);
110-
request_timg0_peripheral_clock(clocks);
110+
// By default the TIMG0 bus clock is running. Do not create a peripehral guard as dropping it
111+
// would reset the timer, and it would enable its WDT.
111112

112113
// Make sure the process doesn't time out due to some spooky configuration.
113114
#[cfg(not(esp32))]
@@ -146,7 +147,6 @@ fn detect_xtal_freq(clocks: &mut ClockTree) -> XtlClkConfig {
146147
.rtccalicfg()
147148
.modify(|_, w| w.rtc_cali_start().clear_bit());
148149
release_timg0_calibration_clock(clocks);
149-
release_timg0_peripheral_clock(clocks);
150150

151151
let mhz = (cali_value * (calibration_clock_frequency / SLOW_CLOCK_CYCLES)) / 1_000_000;
152152
if mhz.abs_diff(40) < mhz.abs_diff(26) {
@@ -462,10 +462,10 @@ fn configure_cpu_clk_impl(
462462
ets_update_cpu_frequency_rom(cpu_freq.as_mhz());
463463

464464
let apb_freq = Rate::from_hz(apb_clk_frequency(clocks));
465-
update_cpu_frequency(apb_freq);
465+
update_apb_frequency(apb_freq);
466466
}
467467

468-
fn update_cpu_frequency(freq: Rate) {
468+
fn update_apb_frequency(freq: Rate) {
469469
let freq_shifted = (freq.as_hz() >> 12) & 0xFFFF;
470470
let value = freq_shifted | (freq_shifted << 16);
471471
LPWR::regs()
@@ -567,19 +567,6 @@ fn configure_rtc_fast_clk_impl(
567567
});
568568
}
569569

570-
fn enable_timg0_peripheral_clock_impl(_clocks: &mut ClockTree, _en: bool) {
571-
// TODO: currently managed by PeripheralClockControl. Perhaps these function impls should be
572-
// generated?
573-
}
574-
575-
fn configure_timg0_peripheral_clock_impl(
576-
_clocks: &mut ClockTree,
577-
_old_selector: Option<Timg0PeripheralClockConfig>,
578-
_new_selector: Timg0PeripheralClockConfig,
579-
) {
580-
// TIMG on ESP32 can only use a single clock source, it doesn't need to be configured.
581-
}
582-
583570
fn enable_timg0_calibration_clock_impl(_clocks: &mut ClockTree, _en: bool) {
584571
// Nothing to do, calibration clocks can only be selected. They are gated by the CALI_START bit,
585572
// which is managed by the calibration process.
@@ -605,19 +592,6 @@ fn configure_timg0_calibration_clock_impl(
605592
.modify(|_, w| unsafe { w.rtc_cali_clk_sel().bits(new_selector.cali_clk_sel_bits()) });
606593
}
607594

608-
fn enable_timg1_peripheral_clock_impl(_clocks: &mut ClockTree, _en: bool) {
609-
// TODO: currently managed by PeripheralClockControl. Perhaps these function impls should be
610-
// generated?
611-
}
612-
613-
fn configure_timg1_peripheral_clock_impl(
614-
_clocks: &mut ClockTree,
615-
_old_selector: Option<Timg0PeripheralClockConfig>,
616-
_new_selector: Timg0PeripheralClockConfig,
617-
) {
618-
// TIMG on ESP32 can only use a single clock source, it doesn't need to be configured.
619-
}
620-
621595
fn enable_timg1_calibration_clock_impl(_clocks: &mut ClockTree, _en: bool) {
622596
// Nothing to do, calibration clocks can only be selected. They are gated by the CALI_START bit,
623597
// which is managed by the calibration process.

0 commit comments

Comments
 (0)