Using HW RTC as an RTOS Timer

by JP Norair on 15 Apr 2015

in Digress, Zen of Bit Twiddling

Let’s say you are writing a low-power embedded RTOS (maybe: OpenTag) and you need a tickless timer that can operate through the lowest power sleep stages of your MCU.  Some MCU designers were nice enough to provide a special timer for this purpose (e.g. STM32L0, STM32L4, EFM32).  Others did not —  the STM32L1 was the original ultra-low-power ARM, coming to us way back in 2011, so I can forgive them for overlooking one important feature.  MSP432, on the other hand, should have a low power timer.  It doesn’t.  It’s not the end of the world, though; it’s possible to make the RTC your bitch and use it as a tickless timer source.

The example below is for the STM32L1 RTC, which is has a hardware calendar.  In other words, it is the worst possible situation: no low power timer and an RTC that implements features you don’t even want (seriously, I know you STM32 guys are Alpine-French and like clocks, but just give us a damn binary timer and we’ll do the clock in software).  Nonetheless, we can turn it into a tickless timer with virtually no overhead.  Keep in mind the “virtually no overhead” statement has a lot to do with the fact the Cortex M3 in the STM32L1 has a hardware divider and a very fast Multiply-Accumulate ALU instruction.  These are both exploited.  On M0 or M0+ units, there will be plenty of overhead.

I’m hoping the MSP432 users of the world, who are going to emerge soon as the chip is new, might be able to take this general idea and port it to MSP432.

You can find the entire file in the OpenTag repo.  Again, you’ll probably want to refer to the RTC section in the STM32L1 Reference Manual (13MB PDF) to know how the register bit fields are organized, but I think it should be a handy reference.

 

Previous post:

Next post: