Allow setting RTC clock backwards and fix elapsed-time underflow#1349
Allow setting RTC clock backwards and fix elapsed-time underflow#1349weebl2000 wants to merge 1 commit intomeshcore-dev:devfrom
Conversation
4d9e093 to
1cc133b
Compare
6ffa6c9 to
31e5e57
Compare
31e5e57 to
e518469
Compare
|
@weebl2000 does this resolve the year 1970 issue on the v4 or is that a separate issue? |
Not sure what issue that is. Can you link me to more info? |
|
@AI7NC I think it's unrelated. https://github.com/meshcore-dev/MeshCore/blob/main/src/helpers/ESP32Board.h#L136 This resets clock to 2024 for clean power on. If the board resets for any other reason than the regular power on path, the clock isn't set so it can go to 0. To troubleshoot further would be good to have some serial logging. |
this should return it to 2024 not 1969/1970 no?
|
It only resets to 2024 on a clean power on, not if the power on was caused by something else. |
Brownout / watchdog timer resets. Is it battery powered, what is tx power set to? |
|
Brownout I think is unlikely as we're using >3ah battery packs and have good state of charge and reported voltages on these nodes. Deathball is using an 11v pack stepped down to USB-C 5v/3A with a converter. Bear is using at least a 5ah battery and is showing a charged battery. @towerviewcams any ideas? Edit: We're using software 22dBm TX which is 28dBm on the V4. On power supplies we show this pulling <1A at 4v. |
All my nodes are 8000mah batts and kick but. I've been testing voltage drop (brown out) and i can take my V4 down to 3.1 and it works fine in my lab. watching it on 3 different batteries and all ok. not even 0.15 change at most. pretty solid. I truly do think we have something where we have reboots or the clock just dies and its always 31 December 1969....This has showed up in 1.13.......I've also been testing setting changes and also change to very low TX power setting and still does it. |
I think this is the culprit. Try setting TX to 17 dBm. If your antenna has a higher SWR too much power simply comes back at the board and it can cause resets. I've seen this happening with bad antennas and high tx power. |
Remove the "clock cannot go backwards" restriction from all set-time paths (CLI `time`, `clock sync`, companion radio CMD_SET_DEVICE_TIME, and simple_secure_chat). The ESP32-S3 RTC drifts 5-10% during deep sleep, making backwards correction necessary after even a few days. Add safeElapsedSecs() helper in ArduinoHelpers.h that clamps elapsed time to 0 when a stored timestamp appears to be in the future after a clock correction. Applied to: - Neighbor "heard X ago" displays in simple_repeater - UI time displays in companion_radio - TimeSeriesData calculations in simple_sensor Switch BaseChatMesh connection expiry from RTC timestamps to monotonic millis(), making it immune to RTC adjustments from GPS, NTP, or manual sync. Rename last_activity to last_activity_ms to reflect the change.
e518469 to
2db37f6
Compare




Build firmware: Build from this branch
Problem
The ESP32-S3 RTC drifts 5-10% during deep sleep. After a few days with powersaving ON, the clock can run hours ahead. Previously it was impossible to correct this — all set-time paths rejected timestamps older than the current time with "clock cannot go backwards".
Additionally, when the clock is corrected backwards (e.g. via GPS sync), stored timestamps appear to be in the future, causing unsigned subtraction underflow (wrapping to ~4 billion seconds / 136 years).
Changes
1. Remove forward-only clock restriction
All set-time paths now accept any timestamp:
time <epoch>andclock synccommands (CommonCLI.cpp)CMD_SET_DEVICE_TIME(companion_radio/MyMesh.cpp)simple_secure_chatsetClock()Only admins/owners can set the time, so replay risk is minimal.
2. Add
safeElapsedSecs()helperNew inline function in
ArduinoHelpers.hthat clamps elapsed time to 0 when a stored timestamp is in the future (i.e. clock was corrected backwards). Applied to:simple_repeatercompanion_radioTimeSeriesDataage calculations insimple_sensor3. Switch connection expiry to monotonic time
BaseChatMeshconnection expiry now usesmillis()instead of RTC timestamps, making it immune to clock adjustments from GPS, NTP, or manual sync. Field renamed fromlast_activitytolast_activity_msto reflect the semantic change.This assumes light sleep mode where
millis()continues to increment (deep sleep resets it, butBaseChatMeshis only used bycompanion_radiowhich uses light sleep).Trade-offs
clock sync. However, only authenticated admins/owners can issue these commands.safeElapsedSecs()clamps to 0 ("just now") rather than preserving relative ordering — acceptable for display purposes.