clock: slew backward time sets

This commit is contained in:
liquidraver committed 2026-11-24 13:55:24 +01:00
1 parent 50a28614f7
commit 6ddfb65b61
17 files changed
+346 -71

No files matched your search

+40 -9
View File
@@ -21,6 +21,17 @@ static void rtc_save_work_fn(struct k_work *work)
}
static K_WORK_DEFINE(s_rtc_save_work, rtc_save_work_fn);
static void rtc_save(ZephyrRTCClock *clock)
{
s_saved_clock = clock;
k_work_submit(&s_rtc_save_work);
}
#else
static void rtc_save(ZephyrRTCClock *clock)
{
ARG_UNUSED(clock);
}
#endif
/* The last time read, kept across a reset that keeps RAM (reboot, crash,
@@ -40,40 +51,60 @@ static RTC_BACKUP_ATTR uint32_t _rtc_backup_magic;
ZephyrRTCClock::ZephyrRTCClock()
{
if (_rtc_backup_magic == RTC_BACKUP_MAGIC && _rtc_backup_time > RTC_TIME_MIN) {
epoch_offset = _rtc_backup_time;
slew.step((int64_t)_rtc_backup_time * 1000, 0);
}
}
uint32_t ZephyrRTCClock::getCurrentTime()
{
uint32_t uptime_sec = (uint32_t)(k_uptime_get() / 1000);
uint32_t now = epoch_offset + uptime_sec;
k_spinlock_key_t key = k_spin_lock(&lock);
int64_t up = k_uptime_get();
bool settled = slew.settle(up);
uint32_t now = (uint32_t)(slew.nowMs(up) / 1000);
/* upstream does this in tick(), which nothing calls here */
if (now > RTC_TIME_MIN && now != _rtc_backup_time) {
_rtc_backup_time = now;
_rtc_backup_magic = RTC_BACKUP_MAGIC;
}
k_spin_unlock(&lock, key);
if (settled) {
rtc_save(this);
}
return now;
}
void ZephyrRTCClock::seedCurrentTime(uint32_t time)
{
uint32_t uptime_sec = (uint32_t)(k_uptime_get() / 1000);
epoch_offset = time - uptime_sec;
k_spinlock_key_t key = k_spin_lock(&lock);
slew.step((int64_t)time * 1000, k_uptime_get());
/* As upstream's setCurrentTime(): unconditional, so a set below
* RTC_TIME_MIN (clkreboot) replaces the backup and is not restored. */
_rtc_backup_time = time;
_rtc_backup_magic = RTC_BACKUP_MAGIC;
k_spin_unlock(&lock, key);
}
void ZephyrRTCClock::setCurrentTime(uint32_t time)
{
k_spinlock_key_t key = k_spin_lock(&lock);
bool slewing = slew.set(time, k_uptime_get());
if (!slewing) {
_rtc_backup_time = time;
_rtc_backup_magic = RTC_BACKUP_MAGIC;
}
k_spin_unlock(&lock, key);
if (!slewing) {
rtc_save(this);
}
}
void ZephyrRTCClock::resetCurrentTime(uint32_t time)
{
seedCurrentTime(time);
#if DT_HAS_COMPAT_STATUS_OKAY(zephcore_rtc_i2c)
s_saved_clock = this;
k_work_submit(&s_rtc_save_work);
#endif
rtc_save(this);
}
} /* namespace mesh */