From d66b75217309b188ef7c6e33656515e78d0471a1 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 25 Apr 2024 16:52:34 -0400 Subject: [PATCH] only: migrating to Timer API --- .../microsecond_timer/microsecond_timer.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/firmware/hw_layer/microsecond_timer/microsecond_timer.cpp b/firmware/hw_layer/microsecond_timer/microsecond_timer.cpp index a7882ecdaf..cb832b3689 100644 --- a/firmware/hw_layer/microsecond_timer/microsecond_timer.cpp +++ b/firmware/hw_layer/microsecond_timer/microsecond_timer.cpp @@ -22,7 +22,7 @@ // Just in case we have a mechanism to validate that hardware timer is clocked right and all the // conversions between wall clock and hardware frequencies are done right // delay in milliseconds -#define TEST_CALLBACK_DELAY 10 +#define TEST_CALLBACK_DELAY_MS 10 // if hardware timer is 20% off we throw a critical error and call it a day // maybe this threshold should be 5%? 10%? #define TIMER_PRECISION_THRESHOLD 0.2 @@ -134,14 +134,14 @@ static void watchDogBuddyCallback(void*) { } static volatile bool testSchedulingHappened = false; -static efitimems_t testSchedulingStart; +static Timer testScheduling; static void timerValidationCallback(void*) { testSchedulingHappened = true; - efitimems_t actualTimeSinceScheduling = (getTimeNowMs() - testSchedulingStart); + efitimems_t actualTimeSinceSchedulingMs = 1e3 * testScheduling.getElapsedSeconds(); - if (absI(actualTimeSinceScheduling - TEST_CALLBACK_DELAY) > TEST_CALLBACK_DELAY * TIMER_PRECISION_THRESHOLD) { - firmwareError(ObdCode::CUSTOM_ERR_TIMER_TEST_CALLBACK_WRONG_TIME, "hwTimer broken precision: %ld ms", actualTimeSinceScheduling); + if (absI(actualTimeSinceSchedulingMs - TEST_CALLBACK_DELAY_MS) > TEST_CALLBACK_DELAY_MS * TIMER_PRECISION_THRESHOLD) { + firmwareError(ObdCode::CUSTOM_ERR_TIMER_TEST_CALLBACK_WRONG_TIME, "hwTimer broken precision: %ld ms", actualTimeSinceSchedulingMs); } } @@ -153,12 +153,12 @@ static void validateHardwareTimer() { if (hasFirmwareError()) { return; } - testSchedulingStart = getTimeNowMs(); + testScheduling.reset(); // to save RAM let's use 'watchDogBuddy' here once before we enable watchdog - engine->executor.scheduleForLater("hw-validate", &watchDogBuddy, MS2US(TEST_CALLBACK_DELAY), timerValidationCallback); + engine->executor.scheduleForLater("hw-validate", &watchDogBuddy, MS2US(TEST_CALLBACK_DELAY_MS), timerValidationCallback); - chThdSleepMilliseconds(TEST_CALLBACK_DELAY + 2); + chThdSleepMilliseconds(TEST_CALLBACK_DELAY_MS + 2); if (!testSchedulingHappened) { firmwareError(ObdCode::CUSTOM_ERR_TIMER_TEST_CALLBACK_NOT_HAPPENED, "hwTimer not alive"); }