Skip to main content
NRumm.1
Associate II
Jan 16, 2024 7:59 PM UTC
Solved

Timer frequency is way off

My STM32F303RE is running with an 8MHz crystal feeding into HSE:

NRumm1_0-1705434819174.png

Using TIM4 I want to generate 700 interrupts per second, but I only get 318.

As can be seen TIM4 runs at 64MHz and the following configuration should trigger about 318 interrupts:

NRumm1_1-1705434933781.png

NRumm1_2-1705434959841.png

Using the following formula:

(edit note - had a typo in the formula)

interrupts_per_second := clock_frequency / (prescaler + 1) / (counter_period + 1)

64 MHz / 3 / 30476 = 700

The 318 interrupts are way off the expected 700 and I just can't figure why. Any ideas are highly appreciated.

Thx in advance !

Woyzeck

 

This topic has been closed for replies.
NRumm.1
NRumm.1Best answer
Associate II
Jan 20, 2024 12:18 AM UTC

*** me - it was a bug. Deep in my code I disabled the interrupt and restarted the timer.

Thx for your support, guys - I learned a lot !

13 replies

AScha.3
Super User
Jan 16, 2024 8:13 PM UTC

64M /3 / 30476 = 700 --- seem ok.

+ How you check the rate then ?

try -> set a led toggle in timer int and look with a scope at the pin.

If you feel a post has answered your question, please click on " Best Answer ".
TDK
Jan 16, 2024 8:16 PM UTC

> Using TIM4 I want to generate 700 interrupts per second, but I only get 318.

How are you monitoring this exactly?

Try clocking from internal HSI to rule out any HSE issues.

Your math (sort of) and expectations check out.

> 64 MHz / 2 / 30475 = 700

Think you made a typo here. Should be 3 instead of 2. Result is the same.

64 MHz / 3 / 30476 = 700.0043750273439 Hz

"If you feel a post has answered your question, please click ""Accept as Solution""."
NRumm.1
NRumm.1Author
Associate II
Jan 16, 2024 9:10 PM UTC

Thx for your support - I fixed the typo.

Replacing HSE with HSI for the whole board didn't fix the issue - now I get 320 interrupts per second (which is close to 318 and probably caused by the inaccuracy of HSI).

One thing that I observed is that the interrupt pulses are somehow irregular, meaning that the time between interrupts is varying by a single digit percentage, which is rather strange. Perhaps that's a hint to interrupt priority ?

NRumm.1
NRumm.1Author
Associate II
Jan 18, 2024 8:42 PM UTC

So I'm still not making any progress here.

What I tried so far:

 

  • used MCO to output the HSI and HSE clock on pin PA8 - the measured frequency is 8MHz which is exactly what it should be, so the hardware setup is correct
  • used MCO to output LSI with a measured result of 37,9 kHz - everything ok here

Interestingly I was unable to output PLLCLK and SYSCLK using MCO. Once I use HSE as the clock source for SYSCLK I measure 8MHz which is ok. In my clock setup SYSCLK is generated from PLLCLK so I wonder if there's something wrong with my setup of PLLCLK, but I can't see what it is.

NRumm1_0-1705610225785.png

 

NRumm.1
NRumm.1Author
Associate II
Jan 18, 2024 8:52 PM UTC

Some progress here - the issue with the lacking MCO signal for SYSCLK and PLLCLK was caused by the default speed setting of the MCO output pin PA8 as GPIO_SPEED_FREQ_LOW. Once set to GPIO_SPEED_FREQ_HIGH there's a signal on that pin and I can measure the expected 64 MHz.

So now I know that all clocks are working as expected, just the timer isn't.

TDK
Jan 18, 2024 9:01 PM UTC

What else is going on in your program? Feels like there is competition for cpu resources and/or an interrupt which is blocking for too long. Change frequency to say 10 Hz, do you get all of them?

"If you feel a post has answered your question, please click ""Accept as Solution""."
NRumm.1
NRumm.1Author
Associate II
Jan 19, 2024 9:37 PM UTC

I changed the timer config to get 100 Hz, but only measure 44.3 Hz. The factor between the expected and the actual frequency is constant over a wide range of timer settings - it stays around 2.2, so the expected frequency is 2.2 times the actual frequency.