From 79dfb0f2c4c22e48158e56ede42ccab8937f5dfa Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 25 Apr 2024 15:46:53 -0700 Subject: [PATCH] wrap times in simple structs --- firmware/controllers/algo/rusefi_types.h | 29 +++++++++++++++++-- .../controllers/system/timer/event_queue.cpp | 2 +- firmware/controllers/system/timer/scheduler.h | 2 +- .../controllers/trigger/trigger_decoder.cpp | 2 +- firmware/util/peak_detect.h | 4 +-- unit_tests/tests/test_can_rx.cpp | 2 +- .../trigger/test_injection_scheduling.cpp | 15 ++++++---- 7 files changed, 43 insertions(+), 13 deletions(-) diff --git a/firmware/controllers/algo/rusefi_types.h b/firmware/controllers/algo/rusefi_types.h index 58a3cadb5a..d2d7e8186b 100644 --- a/firmware/controllers/algo/rusefi_types.h +++ b/firmware/controllers/algo/rusefi_types.h @@ -52,8 +52,33 @@ using time_t = uint32_t; * platform-dependent tick since boot * in case of stm32f4 that's 32-bit timer ticks (SCHEDULER_TIMER_DEVICE == TIM5) extended to 64 bits */ -using efitick_t = int64_t; -using efidur_t = efitick_t; + +struct efidur_t { + constexpr efidur_t() = default; + constexpr efidur_t(int64_t c) : count(c) { } + + constexpr operator int64_t() const { + return count; + } + + int64_t count = 0; +}; + +struct efitick_t { + constexpr efitick_t() = default; + constexpr efitick_t(int64_t c) : count(c) { } + + constexpr operator int64_t() const { + return count; + } + + efitick_t& operator+=(const efidur_t &s) { + count += s.count; + return *this; + } + + int64_t count = 0; +}; /** * 64 bit time in microseconds (1/1_000_000 of a second), since boot diff --git a/firmware/controllers/system/timer/event_queue.cpp b/firmware/controllers/system/timer/event_queue.cpp index ac7f9f2391..fbb82bd835 100644 --- a/firmware/controllers/system/timer/event_queue.cpp +++ b/firmware/controllers/system/timer/event_queue.cpp @@ -178,7 +178,7 @@ expected EventQueue::getNextEventTime(efitick_t nowX) const { * looks like we end up here after 'writeconfig' (which freezes the firmware) - we are late * for the next scheduled event */ - return nowX + m_lateDelay; + return efitick_t{nowX + m_lateDelay}; } else { return m_head->momentX; } diff --git a/firmware/controllers/system/timer/scheduler.h b/firmware/controllers/system/timer/scheduler.h index 5936dfc9ba..efd54d86c6 100644 --- a/firmware/controllers/system/timer/scheduler.h +++ b/firmware/controllers/system/timer/scheduler.h @@ -68,7 +68,7 @@ struct scheduling_s { /** * timestamp represented as 64-bit value of ticks since MCU start */ - volatile efitick_t momentX = 0; + efitick_t momentX = 0; /** * Scheduler implementation uses a sorted linked list of these scheduling records. diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index bd77b655d3..33d7ed2c65 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -415,7 +415,7 @@ expected TriggerDecoderBase::decodeTriggerEvent( * 10 seconds since previous trigger event we do not really care. */ toothDurations[0] = - currentDurationLong > 10 * NT_PER_SECOND ? 10 * NT_PER_SECOND : currentDurationLong; + currentDurationLong > 10 * NT_PER_SECOND ? efidur_t{10 * NT_PER_SECOND} : currentDurationLong; if (!shouldConsiderEdge(triggerShape, triggerWheel, isRising)) { #if EFI_UNIT_TEST diff --git a/firmware/util/peak_detect.h b/firmware/util/peak_detect.h index d50d57f8a1..b4a628fec1 100644 --- a/firmware/util/peak_detect.h +++ b/firmware/util/peak_detect.h @@ -5,11 +5,11 @@ /** * Stores the recent peak value, preventing loss of intermittent peaks in a signal. */ -template +template class PeakDetect { public: TValue detect(TValue currentValue, efitick_t nowNt) { - if ((nowNt > m_lastPeakTime + TTimeoutPeriod) || // if timed out + if ((nowNt > m_lastPeakTime + efidur_t{TTimeoutPeriod}) || // if timed out (currentValue > m_peak)) { // or current is higher than the previous peak // store new peak and time m_peak = currentValue; diff --git a/unit_tests/tests/test_can_rx.cpp b/unit_tests/tests/test_can_rx.cpp index 021a9f93b4..aaae120ae3 100644 --- a/unit_tests/tests/test_can_rx.cpp +++ b/unit_tests/tests/test_can_rx.cpp @@ -22,7 +22,7 @@ TEST(CanListener, FrameAccepted) { EXPECT_CALL(dut, acceptFrame(_)).WillOnce(Return(true)); // Because accept returns true, decode is called - EXPECT_CALL(dut, decodeFrame(_, 1234)); + EXPECT_CALL(dut, decodeFrame(_, efitick_t{1234})); dut.processFrame(frame, 1234); } diff --git a/unit_tests/tests/trigger/test_injection_scheduling.cpp b/unit_tests/tests/trigger/test_injection_scheduling.cpp index 1c002a1098..97fcf5aab3 100644 --- a/unit_tests/tests/trigger/test_injection_scheduling.cpp +++ b/unit_tests/tests/trigger/test_injection_scheduling.cpp @@ -44,7 +44,8 @@ TEST(injectionScheduling, InjectionIsScheduled) { efitick_t startTime = nowNt + nt5deg; EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime, Not(Truly(ActionArgumentHasLowBitSet)))); // falling edge 20ms later - EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime + MS2NT(20), Property(&action_s::getArgument, Eq(&event)))); + efitick_t endTime = startTime + MS2NT(20); + EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, endTime, Property(&action_s::getArgument, Eq(&event)))); } // Event scheduled at 125 degrees @@ -93,9 +94,11 @@ TEST(injectionScheduling, InjectionIsScheduledDualStage) { efitick_t startTime = nowNt + nt5deg; EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime, Truly(ActionArgumentHasLowBitSet))); // falling edge (primary) 20ms later - EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime + MS2NT(20), Truly(ActionArgumentHasLowBitSet))); + efitick_t endTime1 = startTime + MS2NT(20); + EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, endTime1, Truly(ActionArgumentHasLowBitSet))); // falling edge (secondary) 10ms later - EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime + MS2NT(10), Property(&action_s::getArgument, Eq(&event)))); + efitick_t endTime2 = startTime + MS2NT(10); + EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, endTime2, Property(&action_s::getArgument, Eq(&event)))); } // Event scheduled at 125 degrees @@ -134,7 +137,8 @@ TEST(injectionScheduling, InjectionIsScheduledBeforeWraparound) { efitick_t startTime = nowNt + nt5deg; EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime, Not(Truly(ActionArgumentHasLowBitSet)))); // falling edge 20ms later - EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime + MS2NT(20), Property(&action_s::getArgument, Eq(&event)))); + efitick_t endTime = startTime + MS2NT(20); + EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, endTime, Property(&action_s::getArgument, Eq(&event)))); } // Event scheduled at 715 degrees @@ -173,7 +177,8 @@ TEST(injectionScheduling, InjectionIsScheduledAfterWraparound) { efitick_t startTime = nowNt + nt5deg; EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime, Not(Truly(ActionArgumentHasLowBitSet)))); // falling edge 20ms later - EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, startTime + MS2NT(20), Property(&action_s::getArgument, Eq(&event)))); + efitick_t endTime = startTime + MS2NT(20); + EXPECT_CALL(mockExec, scheduleByTimestampNt(testing::NotNull(), _, endTime, Property(&action_s::getArgument, Eq(&event)))); } // Event scheduled at 5 degrees