4848//! # }
4949//! # fn main() {
5050//! // Initialize with the highest possible frequency for this chip
51- //! let ( peripherals, clocks) = esp_hal::init({
51+ //! let peripherals = esp_hal::init({
5252//! let mut config = Config::default();
5353//! config.cpu_clock = CpuClock::max();
5454//! config
6363#![ cfg_attr( esp32h2, doc = "// let system = esp_hal::init(CpuClock::Clock96MHz);" ) ]
6464//! //
6565//! // Initialize with default clock frequency for this chip
66- //! // let ( peripherals, clocks) = esp_hal::init(Config::default());
66+ //! // let peripherals = esp_hal::init(Config::default());
6767//! # }
6868//! ```
6969
70- use core:: marker:: PhantomData ;
71-
7270use fugit:: HertzU32 ;
7371#[ cfg( esp32c2) ]
7472use portable_atomic:: { AtomicU32 , Ordering } ;
@@ -271,38 +269,11 @@ impl Clock for ApbClock {
271269 }
272270}
273271
274- /// Frozen clock frequencies
275- ///
276- /// The instantiation of this type indicates that the clock configuration can no
277- /// longer be changed.
278- pub struct Clocks < ' a > {
279- _private : PhantomData < & ' a ( ) > ,
280- rates : RawClocks ,
281- }
282-
283- impl < ' a > Clocks < ' a > {
284- /// This should not be used in user code.
285- /// The whole point this exists is make it possible to have other crates
286- /// (i.e. esp-wifi) create `Clocks`
287- #[ doc( hidden) ]
288- pub ( crate ) fn from_raw_clocks ( raw_clocks : RawClocks ) -> Clocks < ' a > {
289- Self {
290- _private : PhantomData ,
291- rates : raw_clocks,
292- }
293- }
294- }
295-
296- impl core:: ops:: Deref for Clocks < ' _ > {
297- type Target = RawClocks ;
298-
299- fn deref ( & self ) -> & RawClocks {
300- & self . rates
301- }
302- }
303-
304- /// The list of the clock frequencies that are used in the system.
305- pub struct RawClocks {
272+ /// Clock frequencies.
273+ #[ derive( Debug , Clone , Copy ) ]
274+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
275+ #[ non_exhaustive]
276+ pub struct Clocks {
306277 /// CPU clock frequency
307278 pub cpu_clock : HertzU32 ,
308279
@@ -357,31 +328,33 @@ cfg_if::cfg_if! {
357328 }
358329}
359330
360- /// Used to configure the frequencies of the clocks present in the chip.
361- ///
362- /// After setting all frequencies, call the freeze function to apply the
363- /// configuration.
364- pub struct ClockControl {
365- desired_rates : RawClocks ,
366- }
331+ use core:: cell:: { Ref , RefCell } ;
332+
333+ use critical_section:: { CriticalSection , Mutex } ;
367334
368- impl ClockControl {
369- pub ( crate ) fn new ( clock : CpuClock ) -> Self {
370- Self :: configure ( clock)
335+ static ACTIVE_CLOCKS : Mutex < RefCell < Option < Clocks > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
336+
337+ impl Clocks {
338+ pub ( crate ) fn init ( cpu_clock_speed : CpuClock ) {
339+ critical_section:: with ( |cs| {
340+ ACTIVE_CLOCKS
341+ . borrow ( cs)
342+ . replace ( Some ( Self :: configure ( cpu_clock_speed) ) ) ;
343+ } ) ;
371344 }
372345
373- /// Applies the clock configuration and returns a Clocks struct that
374- /// signifies that the clocks are frozen, and contains the frequencies
375- /// used. After this function is called, the clocks can not change
376- pub fn freeze ( self ) -> Clocks < ' static > {
377- Clocks :: from_raw_clocks ( self . desired_rates )
346+ /// Get the active clock configuration.
347+ pub fn get < ' a > ( cs : CriticalSection < ' a > ) -> Ref < ' a , Self > {
348+ Ref :: map ( ACTIVE_CLOCKS . borrow_ref ( cs ) , |active_clocks| {
349+ unwrap ! ( active_clocks . as_ref ( ) )
350+ } )
378351 }
379352}
380353
381354#[ cfg( esp32) ]
382- impl ClockControl {
355+ impl Clocks {
383356 /// Configure the CPU clock speed.
384- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
357+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
385358 let xtal_freq = if RtcClock :: estimate_xtal_frequency ( ) > 33 {
386359 XtalClock :: RtcXtalFreq40M
387360 } else {
@@ -401,25 +374,23 @@ impl ClockControl {
401374 clocks_ll:: set_cpu_freq ( cpu_clock_speed) ;
402375 }
403376
404- ClockControl {
405- desired_rates : RawClocks {
406- cpu_clock : cpu_clock_speed. frequency ( ) ,
407- apb_clock : HertzU32 :: MHz ( 80 ) ,
408- xtal_clock : HertzU32 :: MHz ( xtal_freq. mhz ( ) ) ,
409- i2c_clock : HertzU32 :: MHz ( 80 ) ,
410- // The docs are unclear here. pwm_clock seems to be tied to clocks.apb_clock
411- // while simultaneously being fixed at 160 MHz.
412- // Testing showed 160 MHz to be correct for current clock configurations.
413- pwm_clock : HertzU32 :: MHz ( 160 ) ,
414- } ,
377+ Self {
378+ cpu_clock : cpu_clock_speed. frequency ( ) ,
379+ apb_clock : HertzU32 :: MHz ( 80 ) ,
380+ xtal_clock : HertzU32 :: MHz ( xtal_freq. mhz ( ) ) ,
381+ i2c_clock : HertzU32 :: MHz ( 80 ) ,
382+ // The docs are unclear here. pwm_clock seems to be tied to clocks.apb_clock
383+ // while simultaneously being fixed at 160 MHz.
384+ // Testing showed 160 MHz to be correct for current clock configurations.
385+ pwm_clock : HertzU32 :: MHz ( 160 ) ,
415386 }
416387 }
417388}
418389
419390#[ cfg( esp32c2) ]
420- impl ClockControl {
391+ impl Clocks {
421392 /// Configure the CPU clock speed.
422- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
393+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
423394 let xtal_freq = if RtcClock :: estimate_xtal_frequency ( ) > 33 {
424395 XtalClock :: RtcXtalFreq40M
425396 } else {
@@ -446,20 +417,18 @@ impl ClockControl {
446417 apb_freq = ApbClock :: ApbFreq40MHz ;
447418 }
448419
449- ClockControl {
450- desired_rates : RawClocks {
451- cpu_clock : cpu_clock_speed. frequency ( ) ,
452- apb_clock : apb_freq. frequency ( ) ,
453- xtal_clock : xtal_freq. frequency ( ) ,
454- } ,
420+ Self {
421+ cpu_clock : cpu_clock_speed. frequency ( ) ,
422+ apb_clock : apb_freq. frequency ( ) ,
423+ xtal_clock : xtal_freq. frequency ( ) ,
455424 }
456425 }
457426}
458427
459428#[ cfg( esp32c3) ]
460- impl ClockControl {
429+ impl Clocks {
461430 /// Configure the CPU clock speed.
462- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
431+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
463432 let xtal_freq = XtalClock :: RtcXtalFreq40M ;
464433
465434 let apb_freq;
@@ -480,20 +449,18 @@ impl ClockControl {
480449 apb_freq = ApbClock :: ApbFreq80MHz ;
481450 }
482451
483- ClockControl {
484- desired_rates : RawClocks {
485- cpu_clock : cpu_clock_speed. frequency ( ) ,
486- apb_clock : apb_freq. frequency ( ) ,
487- xtal_clock : xtal_freq. frequency ( ) ,
488- } ,
452+ Self {
453+ cpu_clock : cpu_clock_speed. frequency ( ) ,
454+ apb_clock : apb_freq. frequency ( ) ,
455+ xtal_clock : xtal_freq. frequency ( ) ,
489456 }
490457 }
491458}
492459
493460#[ cfg( esp32c6) ]
494- impl ClockControl {
461+ impl Clocks {
495462 /// Configure the CPU clock speed.
496- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
463+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
497464 let xtal_freq = XtalClock :: RtcXtalFreq40M ;
498465
499466 let apb_freq;
@@ -514,21 +481,19 @@ impl ClockControl {
514481 apb_freq = ApbClock :: ApbFreq80MHz ;
515482 }
516483
517- ClockControl {
518- desired_rates : RawClocks {
519- cpu_clock : cpu_clock_speed. frequency ( ) ,
520- apb_clock : apb_freq. frequency ( ) ,
521- xtal_clock : xtal_freq. frequency ( ) ,
522- crypto_clock : HertzU32 :: MHz ( 160 ) ,
523- } ,
484+ Self {
485+ cpu_clock : cpu_clock_speed. frequency ( ) ,
486+ apb_clock : apb_freq. frequency ( ) ,
487+ xtal_clock : xtal_freq. frequency ( ) ,
488+ crypto_clock : HertzU32 :: MHz ( 160 ) ,
524489 }
525490 }
526491}
527492
528493#[ cfg( esp32h2) ]
529- impl ClockControl {
494+ impl Clocks {
530495 /// Configure the CPU clock speed.
531- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
496+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
532497 let xtal_freq = XtalClock :: RtcXtalFreq32M ;
533498
534499 let apb_freq;
@@ -550,51 +515,45 @@ impl ClockControl {
550515 }
551516
552517 ClockControl {
553- desired_rates : RawClocks {
554- cpu_clock : cpu_clock_speed. frequency ( ) ,
555- apb_clock : apb_freq. frequency ( ) ,
556- xtal_clock : xtal_freq. frequency ( ) ,
557- pll_48m_clock : HertzU32 :: MHz ( 48 ) ,
558- crypto_clock : HertzU32 :: MHz ( 96 ) ,
559- pll_96m_clock : HertzU32 :: MHz ( 96 ) ,
560- } ,
518+ cpu_clock : cpu_clock_speed. frequency ( ) ,
519+ apb_clock : apb_freq. frequency ( ) ,
520+ xtal_clock : xtal_freq. frequency ( ) ,
521+ pll_48m_clock : HertzU32 :: MHz ( 48 ) ,
522+ crypto_clock : HertzU32 :: MHz ( 96 ) ,
523+ pll_96m_clock : HertzU32 :: MHz ( 96 ) ,
561524 }
562525 }
563526}
564527
565528#[ cfg( esp32s2) ]
566- impl ClockControl {
529+ impl Clocks {
567530 /// Configure the CPU clock speed.
568- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
531+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
569532 if cpu_clock_speed != CpuClock :: default ( ) {
570533 clocks_ll:: set_cpu_clock ( cpu_clock_speed) ;
571534 }
572535
573- ClockControl {
574- desired_rates : RawClocks {
575- cpu_clock : cpu_clock_speed. frequency ( ) ,
576- apb_clock : HertzU32 :: MHz ( 80 ) ,
577- xtal_clock : HertzU32 :: MHz ( 40 ) ,
578- } ,
536+ Self {
537+ cpu_clock : cpu_clock_speed. frequency ( ) ,
538+ apb_clock : HertzU32 :: MHz ( 80 ) ,
539+ xtal_clock : HertzU32 :: MHz ( 40 ) ,
579540 }
580541 }
581542}
582543
583544#[ cfg( esp32s3) ]
584- impl ClockControl {
545+ impl Clocks {
585546 /// Configure the CPU clock speed.
586- pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> ClockControl {
547+ pub ( crate ) fn configure ( cpu_clock_speed : CpuClock ) -> Self {
587548 if cpu_clock_speed != CpuClock :: default ( ) {
588549 clocks_ll:: set_cpu_clock ( cpu_clock_speed) ;
589550 }
590551
591- ClockControl {
592- desired_rates : RawClocks {
593- cpu_clock : cpu_clock_speed. frequency ( ) ,
594- apb_clock : HertzU32 :: MHz ( 80 ) ,
595- xtal_clock : HertzU32 :: MHz ( 40 ) ,
596- crypto_pwm_clock : HertzU32 :: MHz ( 160 ) ,
597- } ,
552+ Self {
553+ cpu_clock : cpu_clock_speed. frequency ( ) ,
554+ apb_clock : HertzU32 :: MHz ( 80 ) ,
555+ xtal_clock : HertzU32 :: MHz ( 40 ) ,
556+ crypto_pwm_clock : HertzU32 :: MHz ( 160 ) ,
598557 }
599558 }
600559}
0 commit comments