@@ -67,7 +67,7 @@ use crate::peripherals::IEEE802154;
6767use crate :: peripherals:: WIFI ;
6868use 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" ) ]
8383pub ( 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" ) ) ) ]
10487pub 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]
215122pub 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.
0 commit comments