STM32G070RB HSI16 Clock and clock calibration issues. Is there a clock calibration example without the need to use HSE or LSE clocks?
I am trying to acquire the amount of ticks a timer counts using a 50Hz signal (acquired from the mains using hardware). For this I use a EXTI8 interrupt where the 50Hz signal is sampled. I can see on the scope that this interrupts works perfectly (by measuring another pin where I toggle the output during the interrupt).
Using a timer I read the counter every interrupt using: LL_TIM_GetCounter(TIMx). And then proceed to reset the counter using LL_TIM_SetCounter(TIMx, 0). The timer is set to 500kHz using a prescaler that is a multiply of 16 (maximizing the accuracy).
However, the counter value I read every time is about 9700 ticks. Which is about 19.4ms = 51.55Hz. Which is way more than the datasheet provided accuracy of +/-0.5% of the 16MHz HSI.
After that. I proceed to use the HSI TRIM register and tried to calibrate the timer using my own mains frequency which was a perfect 50Hz signal. It resulted in having to increment the default 64 in the RCC->ICSCR register with 11 (which is about 11 * 0.3% = 3.3%!!) to acquire the desired amount of 10000 ticks to recreate the 50Hz.
What's more, when I proceeded to check all the timers on the STM32G070RB by generating 50Hz interrupts (Setting 160 as prescaler, and 2000 as the auto-reload value), I measured 51.25Hz on the oscilloscope on ALL timers.
So I needed to speed up the timer using the TRIM register to calibrate the Timer Counter, but then the timer interrupts are all messed up?
Am I doing something wrong?
