Support for use of LPTIMx as system timer in freerunning mode

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Support for use of LPTIMx as system timer in freerunning mode

Postby iggarpe » Tue May 05, 2020 6:40 pm

See this thread for reference: viewtopic.php?f=16&t=3749

Focuses a bit too much in soft 32 bit counter emulation which I don't think is worth the trouble.

I've made progress implementing use of LPTIMx as the system timer, and while I haven't it working completely, I've come to the conclusion that it is not possible without some changes to the ChibiOS core.

The two main problems are:

1- The CMP register must be strictly smaller than the ARR value. That means we cannot use CMP = 0xFFFF.

2- The IER register cannot be modified when the timer is enabled, this disabling the interrupts to implement the alarm stop function would result in timing inaccuracies.

Workarounds:

1- Whenever we are about to set the alarm at CMP = 0xFFFF, make it CMP = 0x0000. Would cause timing jitter in that particular case.

2- Use ARR = 0xFFFE, thus the count will never reach 0xFFFF, and we can use CMP = 0xFFFF to effectively disable the compare match interrupt.

For the latter, I haven't fully wrapped my head around how ChibiOS internals work in freerunning mode so I'm not sure what changes would be needed to allow for timer counts different than full 16 and 32 bits.

Any ideas/hints?

P.S: before you ask why am I using a full blown RTOS for a super low-power device that sleeps most of the time, the answer is: (a) I hate ST HAL from the bottom of my heart (b) did I say I hate ST HAL? (c) I love ChibiOS HAL (d) while it's awake my system has to do some complex stuff for which threads really help.

User avatar
Giovanni
Site Admin
Posts: 12962
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 735 times
Been thanked: 610 times
Contact:

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby Giovanni » Tue May 05, 2020 7:13 pm

Hi,

It is required to be able to count to full 16 or 32 bits or the overflow would lead to inaccurate results. See the function that add intervals or check intervals range.

Giovanni

geoffrey.brown
Posts: 68
Joined: Thu May 07, 2015 9:47 pm
Has thanked: 1 time
Been thanked: 11 times

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby geoffrey.brown » Wed May 06, 2020 4:27 pm

I'm somewhat curious what the use case is for your work. If it's just a matter of waking a sleeping device, there are other options. With the stm32l432 I can get comfortably below 1uA using the RTC alarms to wake from standby.

Geoffrey

alexblack
Posts: 241
Joined: Mon Sep 24, 2012 3:52 pm
Location: Donetsk
Been thanked: 27 times
Contact:

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby alexblack » Wed May 06, 2020 9:26 pm

Hi.
I am sorry that fit into the conversation. The main reason is convenience and versatility: you use standard multitasking application, which will sleep (and draw minimum current) every moment it can without special dances with RTC or other non standard techiquies. For example, how to use sdGetTimeout() and sleep? Main feature is to use sleep timer for system timer and this would possible. ChibiOS have no this useful feature yet. I wrote ST driver for sleep timer some years ago and it works. I wrote about this.

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby iggarpe » Thu May 07, 2020 8:34 pm

geoffrey.brown wrote:I'm somewhat curious what the use case is for your work. If it's just a matter of waking a sleeping device, there are other options. With the stm32l432 I can get comfortably below 1uA using the RTC alarms to wake from standby.

Geoffrey


It's a LoRaWAN irrigation controller. It spends most of the time in standby mode and is woken up by the RTC as you describe, in order to (a) do LoRaWAN communication and (b) run the irrigation program.

The problem with standby is that the uC comes out of reset every time, so I have to save the state somewhere and it is a royal PITA (and anyway I need continuous use of the RTC alarms for other stuff), so I figured I'd use the stop mode. I do not care about interrupt latency so the increased wake up time is no problem for me, while having all clocks stopped when ChibiOS is idle saves a lot of power.

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby iggarpe » Thu May 07, 2020 8:41 pm

alexblack wrote:Hi.
I am sorry that fit into the conversation. The main reason is convenience and versatility: you use standard multitasking application, which will sleep (and draw minimum current) every moment it can without special dances with RTC or other non standard techiquies. For example, how to use sdGetTimeout() and sleep? Main feature is to use sleep timer for system timer and this would possible. ChibiOS have no this useful feature yet. I wrote ST driver for sleep timer some years ago and it works. I wrote about this.


I put the link to that thread of yours in my first message. Your code and ideas were super helpful, but I would several problems (maybe specific to my STM32L476?):

1- The RM states that CMP must be strictly lower than ARR.
2- The RM states that IER cannot be modified with the timer enabled.

I have something else wrong since sometimes the alarms are missed and the next wakeup takes a full timer rollover, but I think I'll figure it out when i have some time.

The problem with (1) above is that in theory you just can't use 0xFFFF value as alarm. I proposed the workaround of just setting it to the next value (0x0000) and live with the inaccuracy of that corner case.

The problem with (2) is that interrupts cannot be disabled (without introducing the timing error corresponding to the time the timer is disabled).
Last edited by iggarpe on Thu May 07, 2020 8:43 pm, edited 1 time in total.

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby iggarpe » Thu May 07, 2020 8:43 pm

Giovanni wrote:Hi,

It is required to be able to count to full 16 or 32 bits or the overflow would lead to inaccurate results. See the function that add intervals or check intervals range.

Giovanni


Quick question: what happens if the ST alarm is stopped but still an interrupt is triggered? will ChibiOS just ignore that interrupt with the only side effect of an extra unecessary wakeup?

Because I'm thinking of using LPTIMx->CNT = LPTIMx->CNT - 1 to "disable" the alarm.

User avatar
Giovanni
Site Admin
Posts: 12962
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 735 times
Been thanked: 610 times
Contact:

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby Giovanni » Thu May 07, 2020 9:09 pm

That is not a planned situation, there is not a specific behaviour defined for that, it is to be verified.

In general it is responsibility of the timer driver to behave as expected by upper layers.

Giovanni

alexblack
Posts: 241
Joined: Mon Sep 24, 2012 3:52 pm
Location: Donetsk
Been thanked: 27 times
Contact:

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby alexblack » Thu May 07, 2020 9:19 pm

iggarpe wrote:
alexblack wrote:I put the link to that thread of yours in my first message. Your code and ideas were super helpful, but I would several problems (maybe specific to my STM32L476?):
1- The RM states that CMP must be strictly lower than ARR.
2- The RM states that IER cannot be modified with the timer enabled.

Thats why I prefer the EFM32 for such devices. I'm using EFM32 and STM32 in parallel several years for battery powered devices and EFM32 are wonderfull, especially since the price for it is close now to the STM.

iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: Support for use of LPTIMx as system timer in freerunning mode

Postby iggarpe » Fri May 08, 2020 10:10 am

alexblack wrote:
iggarpe wrote:
alexblack wrote:I put the link to that thread of yours in my first message. Your code and ideas were super helpful, but I would several problems (maybe specific to my STM32L476?):
1- The RM states that CMP must be strictly lower than ARR.
2- The RM states that IER cannot be modified with the timer enabled.

Thats why I prefer the EFM32 for such devices. I'm using EFM32 and STM32 in parallel several years for battery powered devices and EFM32 are wonderfull, especially since the price for it is close now to the STM.


I'll keep that in mind for low power applications, but as of now (and for the time being), I'm not considering any uC that is not supported by ChibiOS, decision that is reinforced every time 10 minutes into working with some other vendor SDK/HAL/RTOS.


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 5 guests