From fcf0d68849475acae3cbd9cc7cd597423e9bca23 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 14 Nov 2021 14:53:44 -0500 Subject: [PATCH] more API --- firmware/util/timer.cpp | 14 +++++++++----- firmware/util/timer.h | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/firmware/util/timer.cpp b/firmware/util/timer.cpp index 8056dd744e..831d6dc83f 100644 --- a/firmware/util/timer.cpp +++ b/firmware/util/timer.cpp @@ -39,21 +39,25 @@ float Timer::getElapsedSeconds(efitick_t nowNt) const { return 1 / US_PER_SECOND_F * getElapsedUs(nowNt); } +float Timer::getElapsedUs() const { + return getElapsedUs(getTimeNowNt()); +} + float Timer::getElapsedUs(efitick_t nowNt) const { - auto delta = nowNt - m_lastReset; + auto deltaNt = nowNt - m_lastReset; // Yes, things can happen slightly in the future if we get a lucky interrupt between // the timestamp and this subtraction, that updates m_lastReset to what's now "the future", // resulting in a negative delta. - if (delta < 0) { + if (deltaNt < 0) { return 0; } - if (delta > UINT32_MAX - 1) { - delta = UINT32_MAX - 1; + if (deltaNt > UINT32_MAX - 1) { + deltaNt = UINT32_MAX - 1; } - auto delta32 = (uint32_t)delta; + auto delta32 = (uint32_t)deltaNt; return NT2US(delta32); } diff --git a/firmware/util/timer.h b/firmware/util/timer.h index 04e1fd399d..228175d1d1 100644 --- a/firmware/util/timer.h +++ b/firmware/util/timer.h @@ -22,6 +22,8 @@ public: // then a time period representing 2^32 counts will be returned. float getElapsedSeconds() const; float getElapsedSeconds(efitick_t nowNt) const; + float getElapsedUs() const; + // WOW yes returns US while parameter is NT float getElapsedUs(efitick_t nowNt) const; // Perform an atomic update event based on the passed timestamp,