wrap times in simple structs

This commit is contained in:
Matthew Kennedy 2024-04-25 15:46:53 -07:00
parent ba613dbf15
commit 79dfb0f2c4
7 changed files with 43 additions and 13 deletions

View File

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

View File

@ -178,7 +178,7 @@ expected<efitick_t> 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;
}

View File

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

View File

@ -415,7 +415,7 @@ expected<TriggerDecodeResult> 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

View File

@ -5,11 +5,11 @@
/**
* Stores the recent peak value, preventing loss of intermittent peaks in a signal.
*/
template <typename TValue, efidur_t TTimeoutPeriod>
template <typename TValue, int64_t TTimeoutPeriod>
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;

View File

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

View File

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