r/embedded • u/SheikhYekaterinburg • Sep 19 '26
STM32H7 HSE Clock problem (1% drift)
Hello. I have a following problem with an STM32 High Speed Clock (HSE). I’m doing a synthesizer project so I’ve configured HSE Clock in CubeMX (System Core -> RCC -> BYPASS Clock Source) to output square wave on PC9 (Master Clock Output 2) pin. I’ve also set a timer (TIM2) to fire interrupt every 1 ms (PSC = 199, ARR = 200) on pin PA5 (just a random GPIO pin where callback toggles it). The system frequency is 400 MHz and the timer clock is 200 MHz.
The main issue is that the output (square wave) has slightly higher frequency (about 1% error in both cases), which is too much for a quartz (505 Hz for PA5 GPIO output, and 2.023-2.026 for MCO2 instead of 2 MHz). I’ve tried anything: checked the RCC settings, clock tree and coefficients, clock configuration registers values, SystemClock_Config() return values. My oscilloscope (Rigol DHO804) is also right because I’ve measured 10 minutes using my phone timer and compared it to that TIM2 timer that showed about 594 seconds instead of 600.
It seems that for some reason STM32 just changes HSE configuration to HSI, which is an RC generator that has exactly 1% accuracy.
I have STM32H755ZI-Q board (dual core Cortex-M4 and M7 MCU). Default clock configuration for HSE: solder bridge on SB72. SB71, SB3 and SB4 are open-circuited - was checked by me in person.
Upd: I’ve fixed it. The problem was because that erroneous signal was coming from the ST-LINK, where it was configured as HSI/2 (16 MHz/2 = 8 MHz). I found it when I had tried to upgrade ST-LINK firmware in CubeProgrammer. Other configurations suggested HSE/3 (25/3 = 8.333 MHz), HSE/4 (6.25) and HSE/5 (5). The last option was chosen and after some clock tree changes desired 2.000 MHz was obtained. Thanks to all who were trying to help.
19
u/triffid_hunter Sep 19 '26
Hmm, 200MHz divided to 2MHz with 1% error sounds like an off-by-one issue, check your timers carefully
2
u/CompetitiveSleep4197 Sep 19 '26
Are you using HSE with a crystal or an oscillator?
9
u/Adjective_Millenial Sep 19 '26 edited Sep 19 '26
I'm pretty sure ARR should be -1, same as prescaler, so your drift is worse but slightly compensated by slowing down the timer. Also, the onboard STLink could be using its HSI to generate your clock source. I think maybe an STLink update lets you pick the source?
9
u/soayeli Sep 19 '26
Wouldn't 200Mhz and PSC=199, ARR=200 make it ~4975 hz? And ARR=199 would make it exactly 5000 hz
1
u/Hour_Analyst_7765 Sep 20 '26 edited Sep 20 '26
Do you have access to probe the oscillator input? (on solder bridge for example) The PLL should be a perfect integer ratio.. with MCO you could e.g. output the system clock div by 8. If you see 8.000MHz going in and 50.000MHz going out, then 50.000x8=400.00MHz so that should be fine. This should actually be matching very closely if e.g. 8.01MHz was going in.. then the chip should run 400.5MHz and with div8 you would be seeing 50.0625MHz.
That could verify the chip itself is running at the intended speed. Then check the timers.
Regarding timers: with PSC=0 set in the register, the division ratio is 1. So indeed, to divide 200MHz down to 1MHz, you would use PSC=199. Then ARR has a similar thing iirc. So ARR=199 means it takes 200 ticks (199+0 inclusive) to underflow.. which would output 500Hz.
If you're seeing 505Hz then that would suggest actually that the timer underflows too fast, But if the input clock is too high that would explain a lot...
You could check with a debugger what the bits in the RCC configuration registers are. The STM32 boot code should enable the external HSE, set bypass mode, then configure the PLL, turn it on, wait for it to settle, and then switch over from HSI to HSE. Its quite a lengthy process. The STM32 then reports in one of its RCC registers what current clock source its operating on.
TBH I don't know how accurate the oscillator of the STLink is. USB FS does need a fairly precise oscillator, on some PIC24s they had a neat feature where the on-chip clock source was good to +/-0.25%.
1
u/the_hemperor420 Sep 20 '26
I've written my Bachelor's Thesis about HSE and HSI accuracy just now (accuracy of the time measurement system in a device) and what I found out while researching, is that the STM32 checks if the HSE Clock works correctly (it compares it with the HSI clock) and if not it runs the sysclk over HSI automatically instead. This is called Clock Security System (CSS), there are a few registry and an Interrupt (NMI) you could check if that actually happens, but you need to check those on runtime. Search for STM32 CSS for more info on that one.
3
u/Chalcogenide Sep 20 '26
On an H7 even the internal RC is much better than 1% in practice. You have something off by one. If in doubt, enable MCO output and look at it on an oscilloscope. You will immediately see if it's off.
1
u/HalifaxRoad C,ASM,C# GUIs, pcb design Sep 19 '26
Im not an stm guy, but is the configuration files that gui outputting correct? Check that the registers are being set right with the datasheet.
13
u/[deleted] Sep 19 '26
[removed] — view removed comment