Skip to content

Use RTC to generate tick events#1083

Draft
gmarull wants to merge 2 commits intocoredevices:mainfrom
teslabs:tick-timer-rtc
Draft

Use RTC to generate tick events#1083
gmarull wants to merge 2 commits intocoredevices:mainfrom
teslabs:tick-timer-rtc

Conversation

@gmarull
Copy link
Copy Markdown
Member

@gmarull gmarull commented Apr 9, 2026

Note that RTC is not used as an RTOS "wake-up alarm" because some issues are observed like longer than expected wake-up (in discussion with SiFli). Because of that, RTC/RTOS are not in sync, causing issues. However, it is a good idea to have a clear split between RTC-based timers (like, e.g., CLOCK_REALTIME in POSIX) and others that just follow OS tick rate (regular timer API does not offer any guarantees about RTC time...)

Fixes FIRM-1377

@gmarull gmarull requested a review from jplexer April 9, 2026 11:53
Copy link
Copy Markdown
Member

@jplexer jplexer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peak

@gmarull gmarull force-pushed the tick-timer-rtc branch 3 times, most recently from ee402c7 to 691af11 Compare April 9, 2026 16:05
@gmarull gmarull changed the title Use RTC to generate Use RTC to generate tick events Apr 9, 2026
@gmarull gmarull force-pushed the tick-timer-rtc branch 3 times, most recently from cfab0df to b59e974 Compare April 9, 2026 17:53
@gmarull gmarull marked this pull request as draft April 9, 2026 17:53
gmarull added 2 commits April 9, 2026 21:08
A new API to configure RTC alarm aligned with second change, with an
optional callback. Useful for systems where RTC is not used to configure
RTOS wake-ups, meaning RTOS and RTC are not in sync.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
Time in PebbleOS is a bit of a mess (lol). Regular timer, derived from
New Timer, together with RTC comparison logic is used to trigger "tick
events", however, this is fragile. First, if RTOS is not clocked with
the same timer as RTC, there is a chance both drift over time. In
PT2/PR2 (SiFli with RC10K) this is partially true, RC10K is used for RTC
and for LPTIM when system is in deepsleep (not while ON, though, where
HXT48/RC48 is used). In old devices, things likely worked because RTC
was clocked with a precise crystal, so, the hacks in regular timer init
(first NT fire to align, then setup a repeating one) likely stayed valid
for a long time and corrections/hacks in there did the job. This is not
the case for SiFli, RC10K is not precisely precise.

In this patch, we modify tick timer to use second-aligned IRQs from RTC,
so that we make sure that the tick event is posted after RTC time
changes. This way RTC missed updates should be rare, unless IRQ is
served in > 1s (which means we may have a problem elsewhere!).

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
@ericmigi
Copy link
Copy Markdown
Collaborator

ready to merge?

@gmarull
Copy link
Copy Markdown
Member Author

gmarull commented Apr 13, 2026

ready to merge?

The main issue was actually due to #1084. With this PR, what you get is perfectly aligned tick events and an actual RTC clock, but in reality we do not need that. That also comes at a cost of extra RTC wake-ups (because we're no longer relying on OS ticks). I'm waiting for SiFli feedback as well because it seems the need for other PR is not well understood/documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants