I have an Argon / Gen3 application running deviceOS@6.1.0 where most of the time I cannot connect to the network, so it is important that the RTC (counter) time is preserved over System or Watchdog resets from when it last successfully synced to the Cloud.
According to the docs, this should not be a problem:
- Retained memory is enabled by default for Argon / Gen3
- The RTC should behave the same as retained memory:
https://docs.particle.io/reference/device-os/firmware/#retained-memory
https://community.particle.io/t/boron-rtc-not-retaining-time-after-system-reset/60933/3
“there is no need for special treatment to keep the RTC (on Gen3 devices rather a RealTimeCounter than a RealTimeClock) persisting its count across a mere reset. … RTC is part of the backup domain and retains its counters under the same conditions as Backup RAM. … This only means the same rules apply, not that the user needs to store the timestamp in retained memory”.
BUT
- A simple test shows that retained memory works fine across reboots, but RTC time does not! (see my code + results below).
- [I did also try to save 'Time.now()' to retained memory using the Watchdog.onExpired handler, but a) this shouldn’t be necessary and b) it didn’t work anyway]
There was a similar issue raised back in 2021, but it looks like the problem still persists: [Boron RTC not retaining time after System.reset]
This is a really major problem for me since Cloud sync is not readily available.
Any thoughts / advice gratefully received!
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
SerialLogHandler logHandler(LOG_LEVEL_WARN, { { "app", LOG_LEVEL_INFO } });
retained int savedValue = 0;
void setup() {
Serial.begin();
waitFor(Serial.isConnected, 10000);
if (!Time.isValid()) {
Log.info("Invalid time: " + Time.timeStr());
Particle.connect();
waitUntil(Time.isValid);
}
Watchdog.init(WatchdogConfiguration().timeout(40s));
Watchdog.start();
}
void loop() {
Log.info("Saved value= " + String(savedValue++) + "; Current time= " + Time.timeStr());
delay(10000);
// Should reset every 40s since we don't call Watchdog.refresh()
}
Results in...
0000001036 [app] INFO: Invalid time: Sat Jan 1 00:00:01 2000
*** As expected, the system will need to connect / sync on initial power-up
0000010206 [system] WARN: Failed to load session data from persistent storage
0000011947 [app] INFO: Saved value= 0; Current time= Thu Jun 6 20:22:44 2024
0000021948 [app] INFO: Saved value= 1; Current time= Thu Jun 6 20:22:54 2024
0000031949 [app] INFO: Saved value= 2; Current time= Thu Jun 6 20:23:04 2024
0000041950 [app] INFO: Saved value= 3; Current time= Thu Jun 6 20:23:14 2024
0000000901 [app] INFO: Invalid time: Sat Jan 1 00:00:00 2000
*** The system had to re-connect at this point (which it should not need to do)
*** If the time were valid then since SEMI_AUTOMATIC, we would not connect at all on reboot
0000030376 [app] INFO: Saved value= 4; Current time= Thu Jun 6 20:23:55 2024
0000040378 [app] INFO: Saved value= 5; Current time= Thu Jun 6 20:24:05 2024
0000050379 [app] INFO: Saved value= 6; Current time= Thu Jun 6 20:24:15 2024