Skip to content

Commit 7a701f8

Browse files
committed
Remove more legacy clock code
1 parent c87ff31 commit 7a701f8

File tree

11 files changed

+32
-199
lines changed

11 files changed

+32
-199
lines changed

esp-hal/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6060
- The `ESP_HAL_CONFIG_XTAL_FREQUENCY` configuration option has been removed (#4517)
6161
- `Clocks::{i2c_clock, pwm_clock, crypto_clock}` fields (#4636, #4647)
6262
- `RtcClock::xtal_freq()` and the `XtalClock` enum (#4724)
63+
- `Rtc::estimate_xtal_frequency()` (#4851)
64+
- `RtcFastClock`, `RtcSlowClock` (#4851)
6365

6466
## [v1.0.0] - 2025-10-30
6567

esp-hal/src/clock/mod.rs

Lines changed: 9 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::peripherals::IEEE802154;
6767
use crate::peripherals::WIFI;
6868
use crate::{
6969
ESP_HAL_LOCK,
70-
peripherals::{LPWR, TIMG0},
70+
peripherals::TIMG0,
7171
private::Sealed,
7272
soc::clocks::{self, ClockTree, Timg0CalibrationClockConfig},
7373
time::Rate,
@@ -82,23 +82,6 @@ use crate::{
8282
#[cfg_attr(esp32s3, path = "clocks_ll/esp32s3.rs")]
8383
pub(crate) mod clocks_ll;
8484

85-
/// Clock properties
86-
#[doc(hidden)]
87-
pub trait Clock {
88-
/// Frequency of the clock in [Rate].
89-
fn frequency(&self) -> Rate;
90-
91-
/// Frequency of the clock in Megahertz
92-
fn mhz(&self) -> u32 {
93-
self.frequency().as_mhz()
94-
}
95-
96-
/// Frequency of the clock in Hertz
97-
fn hz(&self) -> u32 {
98-
self.frequency().as_hz()
99-
}
100-
}
101-
10285
#[cfg(feature = "unstable")]
10386
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
10487
pub use crate::soc::clocks::ClockConfig;
@@ -134,82 +117,6 @@ impl CpuClock {
134117
}
135118
}
136119

137-
/// RTC FAST_CLK frequency values
138-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
139-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
140-
#[instability::unstable]
141-
pub enum RtcFastClock {
142-
/// Main XTAL, divided by 4
143-
#[cfg(not(any(esp32c6, esp32h2)))]
144-
XtalD4,
145-
146-
/// Select XTAL_D2_CLK as RTC_FAST_CLK source
147-
#[cfg(any(esp32c6, esp32h2))]
148-
XtalD2,
149-
150-
/// Internal fast RC oscillator
151-
RcFast,
152-
}
153-
154-
impl Clock for RtcFastClock {
155-
fn frequency(&self) -> Rate {
156-
match self {
157-
#[cfg(not(any(esp32c6, esp32h2)))]
158-
RtcFastClock::XtalD4 => Clocks::get().xtal_clock / 4,
159-
#[cfg(any(esp32c6, esp32h2))]
160-
RtcFastClock::XtalD2 => Clocks::get().xtal_clock / 2,
161-
162-
RtcFastClock::RcFast => Rate::from_hz(property!("soc.rc_fast_clk_default")),
163-
}
164-
}
165-
}
166-
167-
/// RTC SLOW_CLK frequency values
168-
#[cfg(not(any(esp32c6, esp32h2)))]
169-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
170-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
171-
#[non_exhaustive]
172-
#[instability::unstable]
173-
pub enum RtcSlowClock {
174-
/// Internal slow RC oscillator
175-
RcSlow = 0,
176-
/// External 32 KHz XTAL
177-
_32kXtal = 1,
178-
/// Internal fast RC oscillator, divided by 256
179-
_8mD256 = 2,
180-
}
181-
182-
/// RTC SLOW_CLK frequency values
183-
#[cfg(any(esp32c6, esp32h2))]
184-
#[derive(Debug, Clone, Copy)]
185-
#[non_exhaustive]
186-
#[instability::unstable]
187-
pub enum RtcSlowClock {
188-
/// Select RC_SLOW_CLK as RTC_SLOW_CLK source
189-
RcSlow = 0,
190-
/// Select XTAL32K_CLK as RTC_SLOW_CLK source
191-
_32kXtal = 1,
192-
/// Select RC32K_CLK as RTC_SLOW_CLK source
193-
_32kRc = 2,
194-
/// Select OSC_SLOW_CLK (external slow clock) as RTC_SLOW_CLK source
195-
OscSlow = 3,
196-
}
197-
198-
impl Clock for RtcSlowClock {
199-
fn frequency(&self) -> Rate {
200-
match self {
201-
RtcSlowClock::RcSlow => Rate::from_hz(property!("soc.rc_slow_clock")),
202-
RtcSlowClock::_32kXtal => Rate::from_hz(32_768),
203-
#[cfg(any(esp32c6, esp32h2))]
204-
RtcSlowClock::_32kRc => Rate::from_hz(32_768),
205-
#[cfg(not(any(esp32c6, esp32h2)))]
206-
RtcSlowClock::_8mD256 => RtcFastClock::RcFast.frequency() / 256,
207-
#[cfg(any(esp32c6, esp32h2))]
208-
RtcSlowClock::OscSlow => Rate::from_hz(32_768),
209-
}
210-
}
211-
}
212-
213120
/// RTC Clocks.
214121
#[instability::unstable]
215122
pub struct RtcClock;
@@ -219,26 +126,15 @@ impl RtcClock {
219126
const CAL_FRACT: u32 = 19;
220127

221128
/// Get the RTC_SLOW_CLK source.
222-
#[cfg(not(any(esp32c6, esp32h2)))]
223-
pub fn slow_freq() -> RtcSlowClock {
224-
match LPWR::regs().clk_conf().read().ana_clk_rtc_sel().bits() {
225-
0 => RtcSlowClock::RcSlow,
226-
1 => RtcSlowClock::_32kXtal,
227-
2 => RtcSlowClock::_8mD256,
228-
_ => unreachable!(),
229-
}
230-
}
231-
232-
/// Get the RTC_SLOW_CLK source
233-
#[cfg(any(esp32h2, esp32c6))]
234-
pub fn slow_freq() -> RtcSlowClock {
235-
match LPWR::regs().lp_clk_conf().read().slow_clk_sel().bits() {
236-
0 => RtcSlowClock::RcSlow,
237-
1 => RtcSlowClock::_32kXtal,
238-
2 => RtcSlowClock::_32kRc,
239-
3 => RtcSlowClock::OscSlow,
240-
_ => unreachable!(),
129+
pub fn slow_freq() -> Rate {
130+
cfg_if::cfg_if! {
131+
if #[cfg(soc_has_clock_node_rtc_slow_clk)] {
132+
let getter = clocks::rtc_slow_clk_frequency;
133+
} else {
134+
let getter = clocks::lp_slow_clk_frequency;
135+
}
241136
}
137+
Rate::from_hz(ClockTree::with(getter))
242138
}
243139

244140
/// Measure the frequency of one of the TIMG0 calibration clocks,
@@ -299,48 +195,6 @@ impl RtcClock {
299195

300196
(100_000_000 * 1000 / period) as u16
301197
}
302-
303-
/// Return estimated XTAL frequency in MHz.
304-
pub(crate) fn estimate_xtal_frequency() -> u32 {
305-
const SLOW_CLOCK_CYCLES: u32 = 100;
306-
307-
let calibration_clock = RtcSlowClock::RcSlow;
308-
309-
// Make sure the process doesn't time out due to some spooky configuration.
310-
#[cfg(not(esp32))]
311-
TIMG0::regs().rtccalicfg2().reset();
312-
313-
TIMG0::regs()
314-
.rtccalicfg()
315-
.modify(|_, w| w.rtc_cali_start().clear_bit());
316-
317-
TIMG0::regs().rtccalicfg().write(|w| unsafe {
318-
w.rtc_cali_clk_sel().bits(calibration_clock as u8);
319-
w.rtc_cali_max().bits(SLOW_CLOCK_CYCLES as u16);
320-
w.rtc_cali_start_cycling().clear_bit();
321-
w.rtc_cali_start().set_bit()
322-
});
323-
324-
// Delay, otherwise the CPU may read back the previous state of the completion flag and skip
325-
// waiting.
326-
ets_delay_us(SLOW_CLOCK_CYCLES * 1_000_000 / calibration_clock.frequency().as_hz());
327-
328-
// Wait for the calibration to finish
329-
while TIMG0::regs()
330-
.rtccalicfg()
331-
.read()
332-
.rtc_cali_rdy()
333-
.bit_is_clear()
334-
{}
335-
336-
let cali_value = TIMG0::regs().rtccalicfg1().read().rtc_cali_value().bits();
337-
338-
TIMG0::regs()
339-
.rtccalicfg()
340-
.modify(|_, w| w.rtc_cali_start().clear_bit());
341-
342-
(cali_value * (calibration_clock.frequency().as_hz() / SLOW_CLOCK_CYCLES)) / 1_000_000
343-
}
344198
}
345199

346200
/// Clock frequencies.

esp-hal/src/rtc_cntl/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ use crate::efuse::Efuse;
116116
#[cfg(sleep)]
117117
use crate::rtc_cntl::sleep::{RtcSleepConfig, WakeSource, WakeTriggers};
118118
use crate::{
119-
clock::{Clock, RtcClock},
119+
clock::RtcClock,
120120
interrupt::{self, InterruptHandler},
121121
peripherals::{Interrupt, LPWR},
122122
system::{Cpu, SleepSource},
@@ -211,11 +211,6 @@ impl<'d> Rtc<'d> {
211211
}
212212
}
213213

214-
/// Return estimated XTAL frequency in MHz.
215-
pub fn estimate_xtal_frequency(&mut self) -> u32 {
216-
RtcClock::estimate_xtal_frequency()
217-
}
218-
219214
/// Get the time since boot in the raw register units.
220215
fn time_since_boot_raw(&self) -> u64 {
221216
let rtc_cntl = LP_TIMER::regs();
@@ -256,8 +251,7 @@ impl<'d> Rtc<'d> {
256251
/// reset the RTC timer.
257252
pub fn time_since_power_up(&self) -> Duration {
258253
Duration::from_micros(
259-
self.time_since_boot_raw() * 1_000_000
260-
/ RtcClock::slow_freq().frequency().as_hz() as u64,
254+
self.time_since_boot_raw() * 1_000_000 / RtcClock::slow_freq().as_hz() as u64,
261255
)
262256
}
263257

esp-hal/src/rtc_cntl/sleep/esp32.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{Ext0WakeupSource, Ext1WakeupSource, TimerWakeupSource, WakeSource, W
22
use crate::{
33
gpio::{RtcFunction, RtcPin},
44
peripherals::{BB, DPORT, I2S0, LPWR, NRX, RTC_IO},
5-
rtc_cntl::{Clock, Rtc, RtcClock, sleep::WakeupLevel},
5+
rtc_cntl::{Rtc, RtcClock, sleep::WakeupLevel},
66
};
77

88
// Approximate mapping of voltages to RTC_CNTL_DBIAS_WAK, RTC_CNTL_DBIAS_SLP,
@@ -79,7 +79,7 @@ impl WakeSource for TimerWakeupSource {
7979
let clock_freq = RtcClock::slow_freq();
8080
// TODO: maybe add sleep time adjustlemnt like idf
8181
// TODO: maybe add check to prevent overflow?
82-
let clock_hz = clock_freq.frequency().as_hz() as u64;
82+
let clock_hz = clock_freq.as_hz() as u64;
8383
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
8484
// "alarm" time in slow rtc ticks
8585
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32c2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{TimerWakeupSource, WakeSource, WakeTriggers, WakeupLevel};
22
use crate::{
33
gpio::{RtcFunction, RtcPinWithResistors},
44
peripherals::{APB_CTRL, BB, EXTMEM, GPIO, IO_MUX, LPWR, SPI0, SPI1, SYSTEM},
5-
rtc_cntl::{Clock, Rtc, RtcClock, sleep::RtcioWakeupSource},
5+
rtc_cntl::{Rtc, RtcClock, sleep::RtcioWakeupSource},
66
soc::regi2c,
77
};
88

@@ -100,7 +100,7 @@ impl WakeSource for TimerWakeupSource {
100100
let clock_freq = RtcClock::slow_freq();
101101
// TODO: maybe add sleep time adjustlemnt like idf
102102
// TODO: maybe add check to prevent overflow?
103-
let clock_hz = clock_freq.frequency().as_hz() as u64;
103+
let clock_hz = clock_freq.as_hz() as u64;
104104
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
105105
// "alarm" time in slow rtc ticks
106106
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32c3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{TimerWakeupSource, WakeSource, WakeTriggers, WakeupLevel};
22
use crate::{
33
gpio::{RtcFunction, RtcPinWithResistors},
44
peripherals::{APB_CTRL, BB, EXTMEM, FE, FE2, GPIO, IO_MUX, LPWR, NRX, SPI0, SPI1, SYSTEM},
5-
rtc_cntl::{Clock, Rtc, RtcClock, sleep::RtcioWakeupSource},
5+
rtc_cntl::{Rtc, RtcClock, sleep::RtcioWakeupSource},
66
soc::regi2c,
77
};
88

@@ -99,7 +99,7 @@ impl WakeSource for TimerWakeupSource {
9999
let clock_freq = RtcClock::slow_freq();
100100
// TODO: maybe add sleep time adjustlemnt like idf
101101
// TODO: maybe add check to prevent overflow?
102-
let clock_hz = clock_freq.frequency().as_hz() as u64;
102+
let clock_hz = clock_freq.as_hz() as u64;
103103
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
104104
// "alarm" time in slow rtc ticks
105105
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32c6.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::ops::Not;
22

33
use crate::{
4-
clock::Clock,
54
gpio::RtcFunction,
65
rtc_cntl::{
76
Rtc,
@@ -32,7 +31,7 @@ impl WakeSource for TimerWakeupSource {
3231
let clock_freq = RtcClock::slow_freq();
3332
// TODO: maybe add sleep time adjustment like idf
3433
// TODO: maybe add check to prevent overflow?
35-
let clock_hz = clock_freq.frequency().as_hz() as u64;
34+
let clock_hz = clock_freq.as_hz() as u64;
3635
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
3736
// "alarm" time in slow rtc ticks
3837
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32h2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::ops::Not;
22

33
use crate::{
4-
clock::Clock,
54
gpio::{AnyPin, Input, InputConfig, Pull, RtcPin},
65
peripherals::APB_SARADC,
76
rtc_cntl::{
@@ -26,7 +25,7 @@ impl WakeSource for TimerWakeupSource {
2625
let clock_freq = RtcClock::slow_freq();
2726
// TODO: maybe add sleep time adjustment like idf
2827
// TODO: maybe add check to prevent overflow?
29-
let clock_hz = clock_freq.frequency().as_hz() as u64;
28+
let clock_hz = clock_freq.as_hz() as u64;
3029
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
3130
// "alarm" time in slow rtc ticks
3231
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32s2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
use crate::{
1010
gpio::{RtcFunction, RtcPin},
1111
peripherals::{EXTMEM, LPWR, RTC_IO, SENS, SPI0, SPI1, SYSTEM},
12-
rtc_cntl::{Clock, Rtc, RtcClock, sleep::RtcioWakeupSource},
12+
rtc_cntl::{Rtc, RtcClock, sleep::RtcioWakeupSource},
1313
soc::regi2c,
1414
};
1515

@@ -85,7 +85,7 @@ impl WakeSource for TimerWakeupSource {
8585
let clock_freq = RtcClock::slow_freq();
8686
// TODO: maybe add sleep time adjustlemnt like idf
8787
// TODO: maybe add check to prevent overflow?
88-
let clock_hz = clock_freq.frequency().as_hz() as u64;
88+
let clock_hz = clock_freq.as_hz() as u64;
8989
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
9090
// "alarm" time in slow rtc ticks
9191
let now = rtc.time_since_boot_raw();

esp-hal/src/rtc_cntl/sleep/esp32s3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
use crate::{
1010
gpio::{RtcFunction, RtcPin},
1111
peripherals::{APB_CTRL, EXTMEM, LPWR, RTC_IO, SPI0, SPI1, SYSTEM},
12-
rtc_cntl::{Clock, Rtc, RtcClock, sleep::RtcioWakeupSource},
12+
rtc_cntl::{Rtc, RtcClock, sleep::RtcioWakeupSource},
1313
soc::regi2c,
1414
};
1515

@@ -93,7 +93,7 @@ impl WakeSource for TimerWakeupSource {
9393
let clock_freq = RtcClock::slow_freq();
9494
// TODO: maybe add sleep time adjustlemnt like idf
9595
// TODO: maybe add check to prevent overflow?
96-
let clock_hz = clock_freq.frequency().as_hz() as u64;
96+
let clock_hz = clock_freq.as_hz() as u64;
9797
let ticks = self.duration.as_micros() as u64 * clock_hz / 1_000_000u64;
9898
// "alarm" time in slow rtc ticks
9999
let now = rtc.time_since_boot_raw();

0 commit comments

Comments
 (0)