16
16
#include "memfault/ports/freertos.h"
17
17
#include "timers.h"
18
18
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
+
19
28
static MemfaultPlatformTimerCallback * s_metric_timer_cb = NULL ;
20
29
static void prv_metric_timer_callback (MEMFAULT_UNUSED TimerHandle_t handle ) {
21
30
s_metric_timer_cb ();
@@ -37,7 +46,13 @@ static TimerHandle_t prv_metric_timer_init(const char *const pcTimerName,
37
46
38
47
bool memfault_platform_metrics_timer_boot (uint32_t period_sec ,
39
48
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 ),
41
56
pdTRUE , /* auto reload */
42
57
(void * )NULL , prv_metric_timer_callback );
43
58
if (timer == 0 ) {
0 commit comments