1616#include "memfault/ports/freertos.h"
1717#include "timers.h"
1818
19+ // Define portTICK_PERIOD_MS is using an older version of FreeRTOS for compatibility
20+ // portTICK_PERIOD_MS was added in FreeRTOS v8.0.0
21+ #ifndef portTICK_PERIOD_MS
22+ #define portTICK_PERIOD_MS portTICK_RATE_MS
23+ #endif
24+
25+ #define SEC_TO_FREERTOS_TICKS (period_sec ) \
26+ ((uint64_t)(((uint64_t)period_sec * 1000ULL) / (uint64_t)portTICK_PERIOD_MS))
27+
1928static MemfaultPlatformTimerCallback * s_metric_timer_cb = NULL ;
2029static void prv_metric_timer_callback (MEMFAULT_UNUSED TimerHandle_t handle ) {
2130 s_metric_timer_cb ();
@@ -37,7 +46,13 @@ static TimerHandle_t prv_metric_timer_init(const char *const pcTimerName,
3746
3847bool memfault_platform_metrics_timer_boot (uint32_t period_sec ,
3948 MemfaultPlatformTimerCallback callback ) {
40- TimerHandle_t timer = prv_metric_timer_init ("metric_timer" , pdMS_TO_TICKS (period_sec * 1000 ),
49+ // Validate MEMFAULT_METRICS_HEARTBEAT_INTERVAL_SECS does not overflow when converting to ticks
50+ // Assumes a tick rate <= 1000 Hz and MEMFAULT_METRICS_HEARTBEAT_INTERVAL_SECS used as period
51+ MEMFAULT_STATIC_ASSERT (
52+ MEMFAULT_METRICS_HEARTBEAT_INTERVAL_SECS <= (uint32_t )(UINT32_MAX / 1000UL ),
53+ "Period too large and will cause overflow" );
54+
55+ TimerHandle_t timer = prv_metric_timer_init ("metric_timer" , SEC_TO_FREERTOS_TICKS (period_sec ),
4156 pdTRUE , /* auto reload */
4257 (void * )NULL , prv_metric_timer_callback );
4358 if (timer == 0 ) {
0 commit comments