Skip to main content
nNg.1
Associate II
December 14, 2021
Question

Why toggling of Port E inside the timer 2 interrupt handler only work if the APB1 prescaler is set to /2 or /4 only?

  • December 14, 2021
  • 5 replies
  • 1159 views

I have a Timer 2 interrupt handler  which will toggle the Port E output. My MCU is STM32F407 running at 168 Mhz. I found that the toggle code inside the interrupt handler only work if the APB1 prescaler is set to /2 or /4 only. Slower ABB1 frequency won't work.

The Timer 2 is set to toggle with the timer prescaler = 4000 & autoreload = 10000. This gives roughly 1Hz toggle frequency.

How does this happen?

This topic has been closed for replies.

5 replies

waclawek.jan
Super User
December 14, 2021

What hardware? Which "port E output"? How do you observe it?

With increasing APB divider, TIM2 runs proportionally slower. 1Hz is very slow output toggle; if you increased divider, the output wouldn't change for several seconds. Did you wait that long?

Prepare a minimal but complete compilable code exhibiting the problem and post.

JW

TDK
December 15, 2021

Toggling functionality is not dependent on the APB1 prescaler.

Perhaps the system is overloaded with interrupts in one case while it makes progress in the other. How do you know the code is actually getting ran?

"If you feel a post has answered your question, please click ""Accept as Solution""."
nNg.1
nNg.1Author
Associate II
December 15, 2021

I used a scope to monitor the PortE output and measure the frequency. By trial and error, I found out the problem. Yes, indeed somehow there is racing of interrupt. I have to add a DisableInterrupts at the beginning of the interrupt handler and then a EnableInterrupt before returning. Then lowering the APB1 clock has no effect now. I expect the C compiler to do this ( disable and enable of interrupt) for me. Anyway, I start with an example code and that example did not advice this insertion of DisableInterrupts & EnableInterrupt.

Thanks for yours advice anyway.

TDK
December 15, 2021

> I expect the C compiler to do this ( disable and enable of interrupt) for me.

Why would you expect the C compiler to disable/enable interrupts for you? The compiler does what you tell it to do.

"If you feel a post has answered your question, please click ""Accept as Solution""."
nNg.1
nNg.1Author
Associate II
December 15, 2021

This is like saving of register contents on the stack, something that shall be taken care of by the compiler. Of course, there might be occasion where the programming prefer doing optimization of the code himself and would like to do it himself. But that shall be exception.