From 70e4ccc2efaffa36c9e4394931bcb31f74fe2fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 24 Nov 2025 13:25:59 +0100 Subject: [PATCH] Take peripherals into account when computing clock dependencies (#4540) --- .../src/_generated_esp32.rs | 34 ++++++++++++++----- esp-metadata/src/cfg/soc.rs | 10 +++--- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/esp-metadata-generated/src/_generated_esp32.rs b/esp-metadata-generated/src/_generated_esp32.rs index eeea94aa6..9b890a244 100644 --- a/esp-metadata-generated/src/_generated_esp32.rs +++ b/esp-metadata-generated/src/_generated_esp32.rs @@ -679,6 +679,9 @@ macro_rules! define_clock_tree_types { rc_fast_clk_refcount: u32, apb_clk_refcount: u32, ref_tick_refcount: u32, + xtal32k_clk_refcount: u32, + rc_slow_clk_refcount: u32, + rc_fast_div_clk_refcount: u32, rtc_slow_clk_refcount: u32, rtc_fast_clk_refcount: u32, timg0_calibration_clock_refcount: u32, @@ -714,6 +717,9 @@ macro_rules! define_clock_tree_types { rc_fast_clk_refcount: 0, apb_clk_refcount: 0, ref_tick_refcount: 0, + xtal32k_clk_refcount: 0, + rc_slow_clk_refcount: 0, + rc_fast_div_clk_refcount: 0, rtc_slow_clk_refcount: 0, rtc_fast_clk_refcount: 0, timg0_calibration_clock_refcount: 0, @@ -1140,30 +1146,42 @@ macro_rules! define_clock_tree_types { 80000000 } pub fn request_xtal32k_clk(clocks: &mut ClockTree) { - enable_xtal32k_clk_impl(clocks, true); + if increment_reference_count(&mut clocks.xtal32k_clk_refcount) { + enable_xtal32k_clk_impl(clocks, true); + } } pub fn release_xtal32k_clk(clocks: &mut ClockTree) { - enable_xtal32k_clk_impl(clocks, false); + if decrement_reference_count(&mut clocks.xtal32k_clk_refcount) { + enable_xtal32k_clk_impl(clocks, false); + } } pub fn xtal32k_clk_frequency(clocks: &mut ClockTree) -> u32 { 32768 } pub fn request_rc_slow_clk(clocks: &mut ClockTree) { - enable_rc_slow_clk_impl(clocks, true); + if increment_reference_count(&mut clocks.rc_slow_clk_refcount) { + enable_rc_slow_clk_impl(clocks, true); + } } pub fn release_rc_slow_clk(clocks: &mut ClockTree) { - enable_rc_slow_clk_impl(clocks, false); + if decrement_reference_count(&mut clocks.rc_slow_clk_refcount) { + enable_rc_slow_clk_impl(clocks, false); + } } pub fn rc_slow_clk_frequency(clocks: &mut ClockTree) -> u32 { 150000 } pub fn request_rc_fast_div_clk(clocks: &mut ClockTree) { - request_rc_fast_clk(clocks); - enable_rc_fast_div_clk_impl(clocks, true); + if increment_reference_count(&mut clocks.rc_fast_div_clk_refcount) { + request_rc_fast_clk(clocks); + enable_rc_fast_div_clk_impl(clocks, true); + } } pub fn release_rc_fast_div_clk(clocks: &mut ClockTree) { - enable_rc_fast_div_clk_impl(clocks, false); - release_rc_fast_clk(clocks); + if decrement_reference_count(&mut clocks.rc_fast_div_clk_refcount) { + enable_rc_fast_div_clk_impl(clocks, false); + release_rc_fast_clk(clocks); + } } pub fn rc_fast_div_clk_frequency(clocks: &mut ClockTree) -> u32 { (rc_fast_clk_frequency(clocks) / 256) diff --git a/esp-metadata/src/cfg/soc.rs b/esp-metadata/src/cfg/soc.rs index 57a3918a9..f915d9554 100644 --- a/esp-metadata/src/cfg/soc.rs +++ b/esp-metadata/src/cfg/soc.rs @@ -602,11 +602,6 @@ impl DeviceClocks { }) .collect::>(); - // To compute refcount requirement and correct initialization order, we need to be able to - // access direct dependencies (downstream clocks). As it is simpler to define - // dependents (inputs), we have to do a bit of maths. - let dependency_graph = DependencyGraph::build_from(&clock_tree); - let validation_context = ValidationContext { tree: self.system_clocks.clock_tree.as_slice(), }; @@ -657,6 +652,11 @@ impl DeviceClocks { } } + // To compute refcount requirement and correct initialization order, we need to be able to + // access direct dependencies (downstream clocks). As it is simpler to define + // dependents (inputs), we have to do a bit of maths. + let dependency_graph = DependencyGraph::build_from(&clock_tree); + // Classify clock tree items for node in clock_tree.iter() { // If item A configures item B, then item B is a dependent clock tree item.