STM32: Detect Crystal freqency - Page 1
Forum section: electronics / general

author: avst
author_rank: Contributor
timestamp: August 13, 2021, 01:54:54 pm
content: Hi,in a already shiping product, we are not able to source a secific crystal (this is an automotive grade 16MHz crystal). As we are stuck to the existing footprint, no redesign is possible. But i noticed, there is enough stock for the same series with other frequencies. I was wondering, if by detecting the crystal frequency at startup, and configuring the clocks according to the detected frequency, we would be able to achive greater flexibility when sourcing components. The frequencies of the crystal would be limited to a predefined set, e.g. 12MHz, 16MHz, 20MHz and 24MHz, and spread far enough, so the measured frequency wouldn't need to be very accurate. This should be possible with the internal RC oscillators and timers.Any suggestions and examples would be greytly appreciated.

author: Gribo
author_rank: Frequent Contributor
timestamp: August 13, 2021, 02:12:20 pm
content: You can set one timer to use the LF clock (RC, 32768Hz or other source) and another timer to use the HF clock.Count how many overflows the 2nd timer had for each of the 1st timer overflow and you will have your frequency.Accuracy is terrible, but should be good enough for detecting 4MHz differences.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 13, 2021, 04:41:02 pm
content: "STM32" is pretty vague. It covers a lot of different families. Which one is it going to be?As suggested above, just use an internal oscillator as a timer source. Now I don't know about all STM32 families. I have worked with the L4 and F7. The L4 series have an internal high speed RC oscillator which is +/-1%. Doesn't seem too shabby for this job. Look at your datasheet.

author: avst
author_rank: Contributor
timestamp: August 13, 2021, 06:01:53 pm
content: In this special case it is a STM32F746, but we do use others to ( e.g. L1, L4 ).

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 13, 2021, 06:26:03 pm
content: Quote from: avst on August 13, 2021, 06:01:53 pmThe F7 series (like the L4) also have an "Internal 16 MHz factory-trimmed RC (1% accuracy)", so you can use this as a reference clock for determining the frequency of your crystal.The L1 series, though, also have internal oscillators, but neither the 16 MHz nor the 32 kHz one are factory-trimmed on those AFAIK - so this would be much less accurate on the L1. Not sure this approach would work on the L1.

author: newbrain
author_rank: Super Contributor
timestamp: August 13, 2021, 06:33:04 pm
content: Both the STM32F746 and all the L4 should have an LPTIM (low power timer) among their peripherals.By looking at the clock trees, LPTIM are the only timers that can take a internal clock not derived from the APB (together with the RTC) so they are the most practical choice for the reference value.There are several strategies, a simple one might be, once the HSE is set up and working, simply measuring how many counts the LPTIM, clocked by the 16 MHz HSI oscillator, gets between two systick interrupts.The accuracy of HSI at room temperature are enough to guarantee a good discrimination.On the L1 that, AFAIK, lacks LPTIM peripherals, using the RTC fed by the LSI is probably the only option.

author: Jeroen3
author_rank: Super Contributor
timestamp: August 13, 2021, 08:15:08 pm
content: Most STM32 timers have an option to measure the LSI clock as capture on a timer input for internal testing.For example, on the F1: TIM5CH4_IREMAP - LSI internal clock is connected to TIM5_CH4 input for calibration purpose.On the F4: TI4_RMP - the LSI internal clock is connected to the TIM5_CH4 input for calibration purposes.You run the chip on the HSE, without any PLL, the CSS will make sure it will run. Then you measure against the LSI, of which you know the speed.In the F407 reference manual there is a chapter for it:7.2.11 Internal/external clock measurement using TIM5/TIM11

author: mino-fm
author_rank: Regular Contributor
timestamp: August 14, 2021, 08:25:27 am
content: HSE detection for F7xx. I haven't used this code for longer timer. It should work, but you have to control correct calculation of pll_m.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 14, 2021, 05:24:38 pm
content: I was suggesting using the 16 MHz internal oscillator (usually called HSI?) because it's more accurate (on the families I mentioned, it's factory trimmed to +/- 1%)You may not be able to use input capture with that scheme - using HSI as a reference clock and HSE as an external clock, but my idea was to use HSI for one timer and HSE for another. That should be doable since there are two APBs. Not using input capture at all.So you could start a timer from HSI with a certain duration (long enough to make any code latency negligible), and start another timer from HSE. When the first is done, just read the timer count for the second and compare it to bounds determined for possible values for each possible crystal. Done. Again if the duration is long enough, any latency due to code execution will be completely negligible.Anyone sees a problem with this approach? Is it not possible to clock one timer from HSI and another from HSE?

author: Jeroen3
author_rank: Super Contributor
timestamp: August 14, 2021, 07:38:51 pm
content: I don’t think those connections are possible without using the mco pins and routing them externally.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 14, 2021, 11:48:56 pm
content: I checked to see if what I suggested was indeed possible or not.Turns out this is probably not. As far as I've gotten, both APBs are clocked from the same clock source, with a different prescaler, but that's it. (Probably to avoid having to deal with clock domain crossing.)So that's why the LSI capture is suggested.The alternative way I see otherwise would be to use the RTC as a timer, or the low-power timer LPTIM on devices supporting it, since both can be clocked from either LSI or LSE while the rest of the device operates on another clock. As we saw though, the LSI is usually less accurate than the HSI, but in this particular application, it should fit the bill.If you want something more accurate, you could use the LSE, but that would require adding an external 32k crystal. If you're using the RTC in your design though, chances are you'll be using a 32kHz crystal already - so this would be a good alternative IMO. A lot more accurate. You could then use the scheme I suggested: enable HSE as the system clock source, configure a general-purpose timer, and then use the RTC or LPTIM as the secondary timer, clocked from LSE.Note: after checking further, LPTIM timer(s) also support being clocked from HSI16, so you can implement the scheme I initially suggested with a general purpose timer, with a sysclk from HSE, and a LPTIM timer clocked from HSI16.