Skip to content

Commit 9b46416

Browse files
committed
Clean up passing clocks to drivers
1 parent 756e46f commit 9b46416

File tree

147 files changed

+613
-746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+613
-746
lines changed

esp-hal-embassy/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ impl<const N: usize> TimerCollection for &'static mut [Timer; N] {
140140
#[doc = esp_hal::before_snippet!()]
141141
/// use esp_hal::timg::TimerGroup;
142142
///
143-
/// let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
144-
/// esp_hal_embassy::init(&clocks, timg0.timer0);
143+
/// let timg0 = TimerGroup::new(peripherals.TIMG0);
144+
/// esp_hal_embassy::init(timg0.timer0);
145145
///
146146
/// // ... now you can spawn embassy tasks or use `Timer::after` etc.
147147
/// # }
148148
/// ```
149-
pub fn init(clocks: &Clocks, time_driver: impl TimerCollection) {
150-
EmbassyTimer::init(clocks, time_driver.timers())
149+
pub fn init(time_driver: impl TimerCollection) {
150+
EmbassyTimer::init(time_driver.timers())
151151
}

esp-hal-embassy/src/time_driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ embassy_time_driver::time_driver_impl!(static DRIVER: EmbassyTimer = EmbassyTime
4545
});
4646

4747
impl EmbassyTimer {
48-
pub(super) fn init(_clocks: &Clocks, timers: &'static mut [Timer]) {
48+
pub(super) fn init(timers: &'static mut [Timer]) {
4949
if timers.len() > MAX_SUPPORTED_ALARM_COUNT {
5050
panic!(
5151
"Maximum of {} timers can be used.",

esp-hal-smartled/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
//!
1313
//! ```rust,ignore
1414
//! let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
15-
//! let rmt = Rmt::new(peripherals.RMT, 80.MHz(), &clocks, None).unwrap();
15+
//! let rmt = Rmt::new(peripherals.RMT, 80.MHz(), None).unwrap();
1616
//!
1717
//! let rmt_buffer = smartLedBuffer!(1);
18-
//! let mut led = SmartLedsAdapter::new(rmt.channel0, io.pins.gpio2, rmt_buffer, &clocks);
18+
//! let mut led = SmartLedsAdapter::new(rmt.channel0, io.pins.gpio2, rmt_buffer);
1919
//! ```
2020
//!
2121
//! ## Feature Flags
@@ -89,7 +89,6 @@ where
8989
channel: C,
9090
pin: impl Peripheral<P = O> + 'd,
9191
rmt_buffer: [u32; BUFFER_SIZE],
92-
clocks: &Clocks,
9392
) -> SmartLedsAdapter<TX, BUFFER_SIZE>
9493
where
9594
O: OutputPin + 'd,
@@ -107,7 +106,10 @@ where
107106
let channel = channel.configure(pin, config).unwrap();
108107

109108
// Assume the RMT peripheral is set up to use the APB clock
110-
let src_clock = clocks.apb_clock.to_MHz();
109+
let src_clock = critical_section::with(|cs| {
110+
let clocks = Clocks::get(cs);
111+
clocks.apb_clock.to_MHz();
112+
});
111113

112114
Self {
113115
channel: Some(channel),

esp-hal/doc-helper/before

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
# loop {}
88
# }
99
# fn main() {
10-
# let (peripherals, clocks) = esp_hal::init(Config::default());
10+
# let peripherals = esp_hal::init(Config::default());

esp-hal/src/analog/adc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//! );
4747
//! let mut adc1 = Adc::new(peripherals.ADC1, adc1_config);
4848
//!
49-
//! let mut delay = Delay::new(&clocks);
49+
//! let mut delay = Delay::new();
5050
//!
5151
//! loop {
5252
//! let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut pin)).unwrap();

esp-hal/src/analog/dac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#![cfg_attr(esp32s2, doc = "let dac1_pin = io.pins.gpio17;")]
2828
//! let mut dac1 = Dac::new(peripherals.DAC1, dac1_pin);
2929
//!
30-
//! let mut delay = Delay::new(&clocks);
30+
//! let mut delay = Delay::new();
3131
//!
3232
//! let mut voltage_dac1 = 200u8;
3333
//!

esp-hal/src/clock/mod.rs

Lines changed: 75 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
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
@@ -63,12 +63,10 @@
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-
7270
use fugit::HertzU32;
7371
#[cfg(esp32c2)]
7472
use 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

Comments
 (0)