STM32F407 Timer Interrupts

Any bugs you encounter with Flowcode should be discussed here.
Post Reply
Brendan
Posts: 43
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 2:12 pm
Has thanked: 35 times
Been thanked: 10 times

Flowcode v10 STM32F407 Timer Interrupts

Post by Brendan »

I'm having issues with STM32F407VET6 and Timer Interrupts on a professional project.

So, I created very simple code leaving nothing to the imagination, using an STM32F407 board conveniently equipped with LEDs on A6 and A7.

The program has a repeating main loop with 1s delays between A7 high and A7 low, and correctly cycles a full on/off every 2s.

Additionally for compare, I added a timer interrupt, with prescaler 1:256 and rollover of 31250 to fire once every second, so the LED on A6 intentionally replicates the flash speed of the LED on A7. The panel reveals the crystal frequency as the timer source clock.

Over time, the two LEDs can naturally be expected to slowly lose sync due to the additional processing clock cycles applicable to the LED on A7. However, the LED driven by timer interrupt (port pin A6) is running at an observed 4x the speed of the LED driven by the loop (port pin A7).

The MCU's PLL multiplier/prescaler/post-scaler registers are all configured in accordance with ST's CubeMX clock configurator, and experimental changes to them indicates that register settings declared in Flowcode are indeed being set.

It would appear that the timer clocks are actually driven from one of the bus clocks instead of the crystal clock - i.e. TMR1-7 referencing APB1 (reportedly 42MHz in CubeMX) and not the crystal clock.
Tmr_Int_Scaling.png
Tmr_Int_Scaling.png (14.6 KiB) Viewed 2796 times
Thanks and best regards,
Brendan
Attachments
Heartbeat_Clock_Test.fcfx
(11.07 KiB) Downloaded 107 times

mnfisher
Valued Contributor
Posts: 961
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 510 times

Re: STM32F407 Timer Interrupts

Post by mnfisher »

I don't think this will alter things - but your code maybe wrong.

For example you have 128->A7 (a single bit) and 64->A6. The values should be 0 or 1.

Try
Heartbeat_Clock_Test.fcfx
(10.29 KiB) Downloaded 114 times
But it may just write 1 for non 0?

Martin

LeighM
Valued Contributor
Posts: 401
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 71 times
Been thanked: 217 times

Re: STM32F407 Timer Interrupts

Post by LeighM »

I think there might be threads about this on the old forum.
The STM32 devices are unique in having multiple programmable clock sources (as far as Flowcode supported devices are concerned).
The clock configurator configs are used to setup the clocks on the device and the runtime code is aware of how these relate to the base crystal "clock" frequency.
However, that generic Flowcode dialog that attempts to calculate the interrupt frequency from the timer register data is only aware of the input "clock" frequency and hence is inaccurate in this scenario.
As far as I recall, the result is that you are best working out the desired timer register value manually.

Brendan
Posts: 43
Joined: Tue Dec 08, 2020 2:12 pm
Has thanked: 35 times
Been thanked: 10 times

Re: STM32F407 Timer Interrupts

Post by Brendan »

Thank you Leigh.

I suspected this might be the case.

Perhaps FC configurations would best be contextual, based on the MCU in question, rather than to suggest user settings known to be possibly/probably incorrect? Better still, user-declared clock speeds (e.g. such as already provided in the build options for the delay component) if not just the crystal clock and subject to indeterminate user options, permitting FC components to neatly align.

Anyway, the prescaler settings at-least appear to be correct, so an easy matter to manually calculate interrupt timer roll-over values based on the bus clock frequency and chosen prescaler values.

I fear that UART baud and I2C clock settings (etc) might also be affected? I don't have the full quota of hardware quite yet to have tried them for myself, though the project in question is quite a beast from a comms perspective, with mixed hardware and software-based comms.

I'll get the calculator ready :)

Thanks and best regards,
Brendan

Post Reply