This commit is contained in:
rusefi 2021-11-14 14:53:44 -05:00
parent cdeeb6f966
commit fcf0d68849
2 changed files with 11 additions and 5 deletions

View File

@ -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);
}

View File

@ -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,