cancel
Showing results for 
Search instead for 
Did you mean: 

STM32-L4 RTC Alarms in Fractional Second Intervals

Scott Bennett
Associate II
Posted on January 31, 2018 at 15:23

I'm trying to use AN4759 to understand how to configure the RTC alarm for fractional seconds.  As an example, I want to trigger an alarm every, say, 10.5 seconds.

Pages 13 and 14 make reference to something that appears to be reference code, but neither the Cube code nor the  X-CUBE-RTC code seem to have examples that look like what the application note is asking me to do.

I have tried to make the Cube code here:

\STM32Cube_FW_L4_V1.11.0\Projects\NUCLEO-L476RG\Examples\RTC\RTC_Alarm

do what the app note is asking (I've attached my main.c for reference - it's the only code from the example I've changed other than to use the LSE instead of LSI), but I only appear to be getting integer-second triggers of the alarm.  As an example, if I do set my alarm for 10.5 seconds, I appear to be getting alternating 10- and 11-second triggers.  It's like on average my trigger is every 10.5 seconds, not individual events.  So something is happening, but not what I think should be.

You'll note that I've set salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL, which doesn't seem right, but even if I set it to 

RTC_ALARMSUBSECONDMASK_NONE it makes no difference - the behavior appears to be the same.  Given my prescaler setting of 255 for the LSE, I'd expect to set this to RTC_ALARMSUBSECONDMASK_SS14_8, but that doesn't do anything different either.

Any help would be appreciated.

Thanks,

Scott

6 REPLIES 6
Posted on January 31, 2018 at 16:29

Read back and check/post the RTC registers content after being set, for several successive instances.

JW

Scott Bennett
Associate II
Posted on January 31, 2018 at 20:18

OK, so I did that, and I discovered one thing.

In incrementing my alarm, I used HAL_RTC_GetAlarm() to increment the time for the next alarm.  What I didn't realize is that 'under the hood' the sub-second mask is being manipulated.  So I need to reset it using the appropriate macro.

Now my alarm seems to be going off at the right time in software, but when I measure time on an o-scope I'm still seeing alternating 10- and 11-second triggers.

One thing I just tried... instead of resetting the alarm, I tried resetting the time back to 0.  That actually seems to make this work.  I could possibly work around my issue using this technique, but in my application I am using actual time, so I'd have to think about how to make that work.

henry.dick
Senior II
Posted on January 31, 2018 at 20:36

There are sub second registers, in multiples of the prescaker, for the rtc and alarms.

Posted on January 31, 2018 at 20:43

'under the hood' the sub-second mask is being manipulated

Consider avoiding the hood, then.

Now my alarm seems to be going off at the right time in software, but when I measure time on an o-scope I'm still seeing alternating 10- and 11-second triggers.

I'd take a snapshot (copy content of RTC registers into an array in memory) just after being set for the next alarm, and then take another snapshot when the alarm happens before touching the registers, and compare the two with regard with the observed time.

Post here those snapshots if you want to discuss it further.

Posted on February 01, 2018 at 14:44

So I think I figured out what’s going on… as a newbie to this feature I didn’t realize that the subsecond counter is a decrementing counter. I was incrementing my subsecond value instead of decrementing it. When I fixed that in my code it seems to make it work like I expect.

Posted on February 01, 2018 at 15:04

Nice catch!

How did you get to it?

JW