Timers (caution: long mail, abstract available)

Dear radical IoTlers,

I know the subject is neither creative nor is this a very popular topic, but since timers are critical to almost every application, I feel that there's a need for (at least) one more discussion about this issue.

Since this is gonna be a rather long mail, here's an abstract:

First, I will describe the status quo of the timer implementation. Then, I will describe some problems we're facing with this implementation and finally, I will propose a sketch for a new timer interface.

Dear Oleg,

Thank you for bringing this topic up. I would like to add a couple of comments to your email as a user or RIOT without having contributed to the development.

Firstly, I found the timer implementation in RIOT one of the most confusing part and it took me a while to figure out how to use the different timers. So your suggestion of a more straightforward timer implementation would be much easier to start using.

Secondly, there is still an issue with timers that I am unable to solve. I see that you do not want to use timers to schedule tasks because of your tickles scheduler concept. However, there are some low-level peripherals drivers that still need timers to schedule certain events. For example I am currently using the HAL library from STM and several peripherals (e.g I2C) uses timeouts from within interrupt routines to stop a peripheral if it does not get a response from a peer device. Therefore, for such peripheral drivers, it is essential to get access to a timer of some kind that has a higher interrupt priority level then all other interrupts (e.g. the SYSTICK).

Regards, Abhinav

Dear Abhinav,

Thank you for bringing this topic up. I would like to add a couple of comments to your email as a user or RIOT without having contributed to the development.

thanks for your input. Feedback from your perspective, i.e., someone who's not "living" inside RIOT for a loooong time, is highly appreciated and very valuable.

Firstly, I found the timer implementation in RIOT one of the most confusing part and it took me a while to figure out how to use the different timers. So your suggestion of a more straightforward timer implementation would be much easier to start using.

Probably even a better documentation of the current implementation could be an enhancement of the situation.

Secondly, there is still an issue with timers that I am unable to solve. I see that you do not want to use timers to schedule tasks because of your tickles scheduler concept. However, there are some low-level peripherals drivers that still need timers to schedule certain events. For example I am currently using the HAL library from STM and several peripherals (e.g I2C) uses timeouts from within interrupt routines to stop a peripheral if it does not get a response from a peer device. Therefore, for such peripheral drivers, it is essential to get access to a timer of some kind that has a higher interrupt priority level then all other interrupts (e.g. the SYSTICK).

Interestingly that's exactly what I've just discussed with a colleague some minutes ago. The problem with interrupt priority levels is that not all architectures support this. But I agree that we have to find a (generic) solution for this.

Cheers, Oleg

Dear Oleg,

I really like your new timer interface concept. However, I’d like to add that it would be a good idea to take some “inspiration” from the POSIX timer API. Especially the concept of distinguishing between different types of clocks for timers (e.g. CLOCK_REALTIME / CLOCK_MONOTONIC; see [1]) should be considered. This would allow a programmer to select a clock for a timer according to the needs of the application. This is especially important if some kind of clock synchronization protocol (wink) is involved (in this case the RTC may “jump”).

[1] http://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html

Regards, Philipp

Hi Philipp!

Especially the concept of distinguishing between different types of clocks for timers (e.g. CLOCK_REALTIME / CLOCK_MONOTONIC; see [1]) should be considered. This would allow a programmer to select a clock for a timer according to the needs of the application. This is especially important if some kind of clock synchronization protocol (*wink*) is involved (in this case the RTC may "jump").

Very good idea - I left out gtimer intensionally, because I expected it to exist somehow separately from the new timer, but you're right: maybe we could use the same interface even for this purpose.

Cheers, Oleg

Hi,

I know the subject is neither creative nor is this a very popular topic, but since timers are critical to almost every application, I feel that there's a need for (at least) one more discussion about this issue.

...

New Concept ---------- After thinking about this problem a little bit, I came to the conclusion that there's only a need for one timer interface at the upper layers (i.e., user space).

I wouldn't be that sure.

There are a number different use cases where I've seen timers/counters being used:

1. PWM and PFM generation 2. Device driver helpers 3. Protocol timeout helpers 4. Input pulse length measurement 5. Real time clock, including RTC-based events such as calendar alarms 6. Periodic or delayed tasks 7. Watchdog

The API for PWM and PFM generation is likely to be quite different from a "timer" API, and therefore they probably do not apply to this discussion. Ditto for watchdog.

The device driver helper case can be divided further into the timeout case, which is pretty similar to the protocol timeouts (see below), and other needs. Some of the other needs have hard real time requirements, sometimes to a nanosecond-level accuracy, therefore usually requiring interrupts and sometimes counting the clock cycles for the interrupt delay, too.

Protocol timeouts have varying real-time requirements, but most of the time the accuracy is in the millisecond-scale or coarser. (Of course, if you need to implement a (slow) serial interface completely in software, that is a different case.) Hence, most probably most of them can be folded to the "delayed task" category, and should be implemented with the help of the scheduler (see below).

Input pulse measurement usually requires accuracy, but when exactly the value is then read from the counter may be delayed, depending on the reaction time requirements. Furthermore, the API is likely to be different from a timer API.

RTC is a chapter of its own, and usually requires its own API.

Periodic or delayed tasks usually need to be implemented in the scheduler API, the scheduler using an underlying timer.

So, at least to me emerges the question of whether RIoT needs a "timer API" or a "timeout API" for those device drivers, protocols, and other modules that need timeouts.

Furthermore, I am not really understanding what the "user space" is in the RIoT case. I could imagine an "application" needing periodic tasks, real time clock, and real-time events, at least. But "timers" as such, hmm.

So, to me, the question seems to boil down in a different way, whether there is any real need for a "generic timer" API to the user space at all. Maybe there is, but I wasn't able to come up with any good examples that wouldn't fall into a slightly different category when thinking more closely.

(A hardware-abstraction level API is a completely different story, of course.)

--Pekka Nikander

Hi Oleg, hi everyone,

I tried to analyze the use cases for timers in RIOT and put the needed functions into groups. I further put in some ideas about possible implementations.

...

To conclude: I think we should be able to create a generic timer infrastructure with 3 submodules: - directly used low-level timer drivers for high-res, short periods - something like our vtimer for medium to long-term waiting - a central timer thread based on the above for sending messages on timeouts and for periodic triggers

Looks reasonable to me. I think the latter two should be relatively simple and straightforward to implement given the current structure.

The low-level interface may require more thinking, and may be quite tricky given the desire to support quite different chips.

--Pekka

PS. PFM stands for Pulse Frequency Modulation. PWM, PFM, and them combined are used in modern SMPS (switching mode power supply) controllers. The frequency range is from tens of kHz to a few MHz, in experimental systems tens or hundreds of MHz. PFM is usually not directly implementable with just a timer, but fixed PWM (without a feedback loop) can be implemented relatively easily with a timer and a DMA. If feedback is needed, then then typically CPU needs to be involved.

Hi!

- a central timer thread based on the above for sending messages on timeouts and for periodic triggers

Yes, something like this would be very helpful, but as René and Kaspar pointed out in vtimer: Callback timer · Issue #860 · RIOT-OS/RIOT · GitHub this is not a trivial problem.

Cheers, Oleg

Hi Oleg, hi everyone,

first of all +1 for rethinking the current timer implementation. I was also having some thoughts over the last weeks on how we could

1. Hardware peripherals: This groups contains all functions that are covered directly by hardware peripherals. These should in my opinion be covered by low-level drivers. In this group I see - PWM (already in RIOT) - PFM (I don't know this, can it done by HW directly?) - Input pulse lenth measurement (I have an interface started, but no PR for it yet) - Watchdog (interface needed)

+1

The accuracy of these modules depends heavily on the underlying hardware and should be controlled by the low-level driver interface (at it is done for the PWM so far).

2. Waiting/Sleeping

+1

- High resolution: waiting for for a (precise) short amount of time. Typically in the order of micro or even nano seconds.

In these cases, I'd just suggest using a blocking/wait as implemented to just chew cycles.

- Low resolution: waiting for for a mid-to-long term of time (speaking from a millisecond to many seconds) and used in thread-context. This I would imagine can be implemented by putting the calling thread to sleep. For a wider time span this behavior could be based on the low-level timer and rtt/rtc peripherals (basically what the vtimer is doing at the moment).

3. Timeouts Some threads (e.g. protocol implementations) need to react to timeouts, while doing other tasks when waiting. The resolution is typically rather low(?), in the order of mili to more seconds. ..

4. Periodic triggers I think we can all agree, that RIOT to its tickless scheduling paradigm is not very friendly to periodic tasks at the moment. In this

Sure. Well I'd just suggest something not done in other systems and just implement low-resolution timer with a default value of 1 second and optionally to support 100 miliseconds (once again - in low-resolution applications).

As an application program, I can't see why they (the applications) can't count their own 'seconds'. There are hardware limits to timers (that we all know about) in terms of how long they can count until overflow. That varies from processor to processor - and is therefore confusing, to remember specific timer overflows between uC's.

With 'int' types and 'long' types being so darn-big these days, a somewhat accurate "second" timer is pretty much all applications need in the use-case mentioned.

Embedded programmers should be able to deal with the simplicity that having "second" and optionally 1/10th second would bring.

These are just my opinions. It would reduce work-load, not increase the work-load.

If anyone wishes to discuss why a "one-second" timer wouldn't work (for low-res) I'd be happy for follow on discussion.

Regards

David

Hello everyone,

First, please excuse me for reacting quit lately to this important message, I have been quite busy with publication-related work these last weeks.

I agree with Oleg on the development of a new upper-layer API for timers.

I beg to differ, however, about the inability to work with 16-bit timers on Cortex-M : the latter architecture comes with an advanced interrupt mechanism (NVIC), that allows to selectively enable or disable interrupts by type, and -- even more interesting -- to choose the priority of these various types of interrupts. I think it may allow us to extend by software the hardware 16-bit timers quite efficiently and reliably: to achieve this goal, we basically need the timer-counter overflow interrupt to have a greater priority than any other timer-related priority. This is absolutely not possible on MSP430 architecture, which only has a (vary) basic interrupt management design to offer (this is why I eventually came to propose PR #1619 to uphold robustness). While architectures like MSP430 are effectively unable to provide hwtimer accuracy for a long period without the addition of an RTC/RTT, I'm not sure we can't make it using only core Cortex-M architecture.

Anyway, I also want to say that we have, with our 'hwtimer' mechanism, a very valuable asset: thanks to it, RIOT OS is able to work 'naturally' in a completely timeless manner (and thus, I'm conviced, in a very efficient way energy-wise) by design, while offering very advanced real-time features. On the MSP430 architecture I work with, even FreeRTOS (a reference in the real-time domain) is not able to provide the simultaneous use of many hardware timers -- only one hardware timer is used to provide a master tick, the timers provided by the kernel being comparable to RIOT's 'vtimers'. That's why, besides any IoT consideration, I consider RIOT to be a very promising real-time software platform. I strongly believe we should be proud of this 'design win', and build on it.

Finally, since I have spend quite some time on the subjects last months, I hope to be able to provide help in designing and building this new high-level timer API. I will certainly do as much as I can to this end.

Best regards,

Le 16/09/2014 23:04, Oleg Hahm a �crit :