STM32F405RG Hardware timer period to 6h

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Mikki
Posts: 9
Joined: Sun Feb 23, 2020 12:07 pm
Location: Halle (Saale), Germany

STM32F405RG Hardware timer period to 6h

Post by Mikki » Sun Feb 23, 2020 12:25 pm

Hello,
Im using a STM32F405RG board from Aliexpress.
I set up a hardware timer with a callback function in the REPL.
And it works for timer.freq(1) to run the callback function every second.
But I need a callback every six hours.
If I call timer.freq(1/(60*60*6)), it sets the freq:=0 and manipulates the period and the prescaler, not to a total of 6h, but to a few second.
When I try to change the freq via timer.init( prescaler=168000000, period=6*60*60), it also doesn't take my values, but lower one's. Which also results in running the callback function every few second( ~5sec).

Is it possible to set up a hardware timer to callback every six hours?
Or are hardware timers only available for shorter periods?
How are max and min values for the frequency, or period and prescaler, of a hardware timer?
Last edited by Mikki on Sun Feb 23, 2020 2:52 pm, edited 2 times in total.

User avatar
Roberthh
Posts: 1889
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: STM32F405RG Hardware timer period to 3h

Post by Roberthh » Sun Feb 23, 2020 1:25 pm

What stops you from trying something like:

import machine
tim = machine.Timer(-1)
tim.init(tick_hz = 1, mode = machine.Timer.PERIODIC, period=6*3600, callback = my_callback)

Mikki
Posts: 9
Joined: Sun Feb 23, 2020 12:07 pm
Location: Halle (Saale), Germany

Re: STM32F405RG Hardware timer period to 6h

Post by Mikki » Sun Feb 23, 2020 2:44 pm

Thanks for the answer.

My board seems to have a different module structure and different attributes.
My Timer module is under pyb.Timer and it does not support a PERIODIC mode, therefore I use the UP mode. Also the tick_hz attribut is not supported.

Code: Select all

>>> dir(machine)
['__name__', 'info', 'unique_id', 'reset', 'soft_reset', 'bootloader', 'freq', 'rng', 'idle', 'sleep', 'deepsleep', 'reset_cause', 'disable_irq', 'enable_irq', 'time_pulse_us', 'mem8', 'mem16', 'mem32', 'Pin', 'Signal', 'I2C', 'SPI', 'UART', 'WDT', 'PWRON_RESET', 'HARD_RESET', 'WDT_RESET', 'DEEPSLEEP_RESET', 'SOFT_RESET']
>>> dir(pyb.Timer)
['init', 'deinit', 'channel', 'counter', 'source_freq', 'freq', 'prescaler', 'period', 'callback', 'UP', 'DOWN', 'CENTER', 'PWM', 'PWM_INVERTED', 'OC_TIMING', 'OC_ACTIVE', 'OC_INACTIVE', 'OC_TOGGLE', 'OC_FORCED_ACTIVE', 'OC_FORCED_INACTIVE', 'IC', 'ENC_A', 'ENC_B', 'ENC_AB', 'HIGH', 'LOW', 'RISING', 'FALLING', 'BOTH']
>>> timer=pyb.Timer(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Timer(-1) doesn't exist
I tried the following, but it did not accept my Values:

Code: Select all

>>> timer=pyb.Timer(1)
>>> timer.init( prescaler=168000000, period=6*3600,callback=my_callback)
>>> timer
Timer(1, freq=0, prescaler=31232, period=21600, mode=UP, div=1, deadtime=0)
>>> timer.init(freq=1/(6*3600), callback=my_callback)
>>> timer
Timer(1, freq=0, prescaler=65535, period=29530, mode=UP, div=1, deadtime=0)
>>> 


User avatar
Roberthh
Posts: 1889
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: STM32F405RG Hardware timer period to 6h

Post by Roberthh » Sun Feb 23, 2020 2:58 pm

Which version of the firmware are you using? Type Ctrl-B to get it. It looks a little bit outdated.
Also, looking at http://docs.micropython.org/en/latest/l ... ight=timer, you might better use timer 2 or 5, since these are not limited to 16 bit.

Mikki
Posts: 9
Joined: Sun Feb 23, 2020 12:07 pm
Location: Halle (Saale), Germany

Re: STM32F405RG Hardware timer period to 6h

Post by Mikki » Sun Feb 23, 2020 3:10 pm

Thanks, this solved my problem.
Altough using freq to initialise the timer is not accurrate.
I used prescaler and period instead.

Code: Select all

>>>timer2=pyb.Timer(2)
>>> timer2.source_freq()
84000000
>>> timer2.init(freq=1/(6*60*60),callback=my_callback)
>>> timer2
Timer(2, freq=0, prescaler=9999, period=181439983, mode=UP, div=1)
>>> timer2.init(prescaler=8400, period=10000*6*60*60, callback=my_callback)
>>> timer2
Timer(2, freq=0, prescaler=8400, period=216000000, mode=UP, div=1)
>>> 
My version

Code: Select all

MicroPython v1.9.3 on 2018-03-05; SM1432F405 with STM32F405RG
Type "help()" for more information.
>>> 
Should I concern a firmwareupdate?
As it is a clone, I did not find a firmware from the manufacturer. Is the original firmware for the pyboard compatible, as they both use the STM32?

User avatar
Roberthh
Posts: 1889
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: STM32F405RG Hardware timer period to 6h

Post by Roberthh » Sun Feb 23, 2020 3:40 pm

You may update. But the prebuilt images may not work for your board, unless it is a direct pyboard clone. Some members of the have prepared board definition files for other STM32F405RG boards. They may assist you finding the right one.

Mikki
Posts: 9
Joined: Sun Feb 23, 2020 12:07 pm
Location: Halle (Saale), Germany

Re: STM32F405RG Hardware timer period to 6h

Post by Mikki » Sun Feb 23, 2020 4:23 pm

Thanks again for solving my problem.
Also, looking at http://docs.micropython.org/en/latest/l ... ight=timer, you might better use timer 2 or 5, since these are not limited to 16 bit.
Having a second look at the documentation, I see what you are referring to.
I did not know the notation for the definition range.

[0-0xffff] for 0-65535
and
[0-0x3fffffff] for 0-1073741823

User avatar
Roberthh
Posts: 1889
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: STM32F405RG Hardware timer period to 6h

Post by Roberthh » Sun Feb 23, 2020 5:25 pm

Reading the docs, the prescaler should be 8399 for s division by 8400.

Mikki
Posts: 9
Joined: Sun Feb 23, 2020 12:07 pm
Location: Halle (Saale), Germany

Re: STM32F405RG Hardware timer period to 6h

Post by Mikki » Sun Feb 23, 2020 6:24 pm

You are right.
I thought it is only a small inaccuracy, but multiplying it with the period the mistake grows.

Online
User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: STM32F405RG Hardware timer period to 6h

Post by jimmo » Sun Feb 23, 2020 11:40 pm

You should definitely update the firmware -- 1.9.x is quite old (November 2017).

Do you know where the firmware came from?

Pre-built images for some boards are at http://micropython.org/download#other and all the others are built by Andrew at https://gitlab.com/alelec/micropython_ci/-/tags

But I don't see a board named SM1432F405 defined anywhere -- you might need to build your own board config. (look at ports/stm32/boards/ (there are some existing f405 boards, althoguh they're f405rg).

Post Reply