Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we auto detect xtal reliably at runtime? #1164

Closed
MabezDev opened this issue Feb 13, 2024 · 3 comments · Fixed by #1165
Closed

Can we auto detect xtal reliably at runtime? #1164

MabezDev opened this issue Feb 13, 2024 · 3 comments · Fixed by #1165
Assignees

Comments

@MabezDev
Copy link
Member

I noticed the existence of

fn estimate_xtal_frequency() -> u32 {
// Number of 8M/256 clock cycles to use for XTAL frequency estimation.
const XTAL_FREQ_EST_CYCLES: u32 = 10;
let rtc_cntl = unsafe { &*LPWR::PTR };
let clk_8m_enabled = rtc_cntl.clk_conf().read().enb_ck8m().bit_is_clear();
let clk_8md256_enabled = rtc_cntl.clk_conf().read().enb_ck8m_div().bit_is_clear();
if !clk_8md256_enabled {
RtcClock::enable_8m(true, true);
}
let ratio = RtcClock::get_calibration_ratio(RtcCalSel::RtcCal8mD256, XTAL_FREQ_EST_CYCLES);
let freq_mhz =
((ratio as u64 * RtcFastClock::RtcFastClock8m.hz() as u64 / 1_000_000u64 / 256u64)
>> RtcClock::CAL_FRACT) as u32;
RtcClock::enable_8m(clk_8m_enabled, clk_8md256_enabled);
freq_mhz
}
}
, which we even have an example for.

If we can reliably estimate the xtal freq, we could remove the annoying features we need to supply for the HALs and all the work arounds we do in espflash.

@MabezDev
Copy link
Member Author

Note that if this does work, we'd need to impl for the esp32c6, esp32h2 and esp32p4.

@MabezDev
Copy link
Member Author

In theory, if we can detect the xtal freq, replace the cfgs here:

#[cfg(feature = "xtal-40mhz")]
let xtal_freq = XtalClock::RtcXtalFreq40M;
#[cfg(feature = "xtal-26mhz")]
let xtal_freq = XtalClock::RtcXtalFreq26M;

with autodetection code

@SergioGasquez SergioGasquez linked a pull request Feb 13, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Feb 14, 2024
@MabezDev
Copy link
Member Author

Closed via #1165

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants