cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo F334R8 - basic example of a Timer TIM2 mystery

Smoun.1
Associate III

Hi guys

So I am developing a small PWM generator around the F334R8, it's overall extremely simple. All the work is done with another MCU sending data on SPI.

On the F334R8, an EXT interrupt is called when the CS line is pulled down. From there the F3 listens on its MOSI line, grabs the data, and then outputs the 3 PWM channels on its HRTIM1.

That part is working really well.

I wanted to have a failsafe timeout using TIM2 on a 1second timer.

Basically every 1s a timer interrupt is called, and the program checks for a flag. If the flag is down, then I know that it's been 1s without proper data coming in and that the PWM output should be disabled.

For some reason I can't get that simple TIM2 to work.

So I decided to pull the Nucleo board again and create a super simple project that does nothing but toggle a LED on a 1s TIM2 timer.

  • on the first try I had messed up the RCC because I selected the wrong oscillator. Fixing the oscillator does NOT fix the problem. The LED toggle is still wrong. It toggles at roughly 5Hz, regardless of the TIM2 settings. Changing the period has not impact whatsoever. Still is a slightly different behavior that I am experiencing on the custom board. So, then:
  • I created a new project, same everything, but this time I carefully selected the correct oscillator on the first try, set up TIM2, generated code, compiled. And everything works perfect. 1 s toggles. I can change the period and everything works as expected.

So I am leaving the custom board issue aside for now, and I would really try to find out what's going on.

the 2 projects are identical, and couldn't be more simple. Your typical Timer Interrupt tutorial you find online.

However, one works and one does not. I wonder if CubeIDE somehow generated code when I had the wrong RCC config and fails to update that on subsequent code generations? I really don't know and that's why I'm here.

I uploaded both of these 2 basic projects here: https://gofile.io/d/1LTvVN

test-timer is the one that is blinking at the wrong frequency

test-timer-2 is the working test, which toggles the LED correctly

both of them are very similar. I went as far as reading the main.c line by line and couldn't find anything obvious. I also generated the pdf report but they didn't help.

I would appreciate if anyone could help steer me in the right direction.

thank you

12 REPLIES 12

sure enough, it's a different issue on the actual project/board.

the 7,199,999 is correct in it.

the symptoms on it (while in debug, with a break point on the gpio toggle), it triggers once immediately, and then nothing, sometimes it will interrupt again a couple minutes later; sometimes not. I've added that project in case you wish to take a look.

Thanks for helping on this on, I never thought of checking the code automatically generated.

TDK
Guru

My guess is the system is starved of resources. Disable your other callback and/or put a PWM output on a timer channel if you want to monitor it.

You don't mention how often HAL_GPIO_EXTI_Callback is called. Flip an output pin while it's in that interrupt so you have a sense of how long it spends there.

If you feel a post has answered your question, please click "Accept as Solution".
Smoun.1
Associate III

The timer interrupt not firing is actually taking place all the time even when I am not sending data over SPI.

Just as a sanity check, in debug I set a breakpoint in both the timer interrupt callback and the EXT gpio callback.

and as expected without sending data, the EXT GPIO callback is not firing. As soon as I start sending data, the EXT GPIO callback fires. That part is working well.

Only that TIM2 interrupt not firing more than once.