Add a 64-bit millis64() function to avoid rollover issues

This commit is contained in:
Jonny 2025-07-24 08:34:01 +01:00
parent 6f83a1c905
commit 4ad621f6c3
6 changed files with 26 additions and 26 deletions

View file

@ -47,7 +47,6 @@ const char* version_number = "9.0.experimental";
volatile unsigned long currentMillis = 0; volatile unsigned long currentMillis = 0;
unsigned long previousMillis10ms = 0; unsigned long previousMillis10ms = 0;
unsigned long previousMillisUpdateVal = 0; unsigned long previousMillisUpdateVal = 0;
unsigned long lastMillisOverflowCheck = 0;
#ifdef FUNCTION_TIME_MEASUREMENT #ifdef FUNCTION_TIME_MEASUREMENT
// Task time measurement for debugging // Task time measurement for debugging
MyTimer core_task_timer_10s(INTERVAL_10_S); MyTimer core_task_timer_10s(INTERVAL_10_S);
@ -347,15 +346,6 @@ void init_serial() {
#endif // DEBUG_VIA_USB #endif // DEBUG_VIA_USB
} }
void update_overflow(unsigned long currentMillis) {
// Check if millis overflowed
if (currentMillis < lastMillisOverflowCheck) {
// We have overflowed, increase rollover count
datalayer.system.status.millisrolloverCount++;
}
lastMillisOverflowCheck = currentMillis;
}
void check_interconnect_available() { void check_interconnect_available() {
if (datalayer.battery.status.voltage_dV == 0 || datalayer.battery2.status.voltage_dV == 0) { if (datalayer.battery.status.voltage_dV == 0 || datalayer.battery2.status.voltage_dV == 0) {
return; // Both voltage values need to be available to start check return; // Both voltage values need to be available to start check
@ -543,8 +533,6 @@ void update_calculated_values() {
datalayer.battery.status.reported_remaining_capacity_Wh = datalayer.battery2.status.remaining_capacity_Wh; datalayer.battery.status.reported_remaining_capacity_Wh = datalayer.battery2.status.remaining_capacity_Wh;
} }
} }
update_overflow(currentMillis); // Update millis rollover count
} }
void check_reset_reason() { void check_reset_reason() {
@ -602,9 +590,3 @@ void check_reset_reason() {
break; break;
} }
} }
uint64_t get_timestamp(unsigned long currentMillis) {
update_overflow(currentMillis);
return (uint64_t)datalayer.system.status.millisrolloverCount * (uint64_t)std::numeric_limits<uint32_t>::max() +
(uint64_t)currentMillis;
}

View file

@ -253,8 +253,6 @@ typedef struct {
} DATALAYER_SYSTEM_INFO_TYPE; } DATALAYER_SYSTEM_INFO_TYPE;
typedef struct { typedef struct {
/** Millis rollover count. Increments every 49.7 days. Used for keeping track on events */
uint8_t millisrolloverCount = 0;
#ifdef FUNCTION_TIME_MEASUREMENT #ifdef FUNCTION_TIME_MEASUREMENT
/** Core task measurement variable */ /** Core task measurement variable */
int64_t core_task_max_us = 0; int64_t core_task_max_us = 0;

View file

@ -397,8 +397,6 @@ EVENTS_LEVEL_TYPE get_event_level(void) {
return events.level; return events.level;
} }
uint64_t get_timestamp(unsigned long currentMillis);
/* Local functions */ /* Local functions */
static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) { static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) {
@ -417,7 +415,7 @@ static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) {
} }
// We should set the event, update event info // We should set the event, update event info
events.entries[event].timestamp = get_timestamp(millis()); events.entries[event].timestamp = millis64();
events.entries[event].data = data; events.entries[event].data = data;
// Check if the event is latching // Check if the event is latching
events.entries[event].state = latched ? EVENT_STATE_ACTIVE_LATCHED : EVENT_STATE_ACTIVE; events.entries[event].state = latched ? EVENT_STATE_ACTIVE_LATCHED : EVENT_STATE_ACTIVE;

View file

@ -0,0 +1,12 @@
#include <Arduino.h>
#include "esp_timer.h"
uint64_t ARDUINO_ISR_ATTR millis64() {
// ESP32's esp_timer_get_time() returns time in microseconds, we convert to
// milliseconds by dividing by 1000.
// This is almost identical to the existing Arduino millis() function, except
// we return a 64-bit value which won't roll over for 600k years.
return esp_timer_get_time() / 1000ULL;
}

View file

@ -0,0 +1,11 @@
#include <stdint.h>
/**
* @brief Return ESP32's high-resolution timer in milliseconds, as a 64 bit value.
*
* @param[in] void
*
* @return uint64_t Timestamp in milliseconds
*
*/
extern uint64_t millis64(void);

View file

@ -2,6 +2,7 @@
#include <limits> #include <limits>
#include "../../datalayer/datalayer.h" #include "../../datalayer/datalayer.h"
#include "../../devboard/utils/logging.h" #include "../../devboard/utils/logging.h"
#include "../../devboard/utils/millis64.h"
const char EVENTS_HTML_START[] = R"=====( const char EVENTS_HTML_START[] = R"=====(
<style>body{background-color:#000;color:#fff}.event-log{display:flex;flex-direction:column}.event{display:flex;flex-wrap:wrap;border:1px solid #fff;padding:10px}.event>div{flex:1;min-width:100px;max-width:90%;word-break:break-word}</style><div style="background-color:#303e47;padding:10px;margin-bottom:10px;border-radius:25px"><div class="event-log"><div class="event" style="background-color:#1e2c33;font-weight:700"><div>Event Type</div><div>Severity</div><div>Last Event</div><div>Count</div><div>Data</div><div>Message</div></div> <style>body{background-color:#000;color:#fff}.event-log{display:flex;flex-direction:column}.event{display:flex;flex-wrap:wrap;border:1px solid #fff;padding:10px}.event>div{flex:1;min-width:100px;max-width:90%;word-break:break-word}</style><div style="background-color:#303e47;padding:10px;margin-bottom:10px;border-radius:25px"><div class="event-log"><div class="event" style="background-color:#1e2c33;font-weight:700"><div>Event Type</div><div>Severity</div><div>Last Event</div><div>Count</div><div>Data</div><div>Message</div></div>
@ -17,8 +18,6 @@ button:hover { background-color: #3A4A52; }</style>
</script> </script>
)====="; )=====";
uint64_t get_timestamp(unsigned long currentMillis);
static std::vector<EventData> order_events; static std::vector<EventData> order_events;
String events_processor(const String& var) { String events_processor(const String& var) {
@ -40,7 +39,7 @@ String events_processor(const String& var) {
} }
// Sort events by timestamp // Sort events by timestamp
std::sort(order_events.begin(), order_events.end(), compareEventsByTimestampDesc); std::sort(order_events.begin(), order_events.end(), compareEventsByTimestampDesc);
uint64_t current_timestamp = get_timestamp(millis()); uint64_t current_timestamp = millis64();
// Generate HTML and debug output // Generate HTML and debug output
for (const auto& event : order_events) { for (const auto& event : order_events) {