From ba0c48e0ac47a781e6b1b2626362feedffce6ae0 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 19 Jan 2020 19:23:41 -0800 Subject: [PATCH] use NT_PER_SECOND instead of convert from microseconds (#1107) * add NT_PER_SECOND * missed a few * inject tooth logger timestamp * inject --- firmware/console/tooth_logger.cpp | 6 +++--- firmware/console/tooth_logger.h | 3 ++- firmware/controllers/algo/engine.cpp | 2 +- firmware/controllers/algo/engine2.cpp | 4 ++-- firmware/controllers/engine_cycle/rpm_calculator.cpp | 8 ++++---- firmware/controllers/trigger/trigger_central.cpp | 6 +++--- firmware/controllers/trigger/trigger_decoder.cpp | 4 ++-- firmware/hw_layer/sensors/cj125.cpp | 4 ++-- firmware/hw_layer/vehicle_speed.cpp | 4 ++-- firmware/util/efitime.h | 1 + 10 files changed, 22 insertions(+), 20 deletions(-) diff --git a/firmware/console/tooth_logger.cpp b/firmware/console/tooth_logger.cpp index 4fd0eddfb7..f8978d6a8f 100644 --- a/firmware/console/tooth_logger.cpp +++ b/firmware/console/tooth_logger.cpp @@ -33,7 +33,7 @@ void SetNextEntry(uint16_t entry) { } } -void LogTriggerTooth(trigger_event_e tooth) { +void LogTriggerTooth(trigger_event_e tooth, efitick_t timestamp) { // bail if we aren't enabled if (!ToothLoggerEnabled) { return; @@ -46,7 +46,7 @@ void LogTriggerTooth(trigger_event_e tooth) { return; } - uint32_t nowUs = NT2US(getTimeNowNt()); + uint32_t nowUs = NT2US(timestamp); // 10us per LSB - this gives plenty of accuracy, yet fits 655.35 ms in to a uint16 uint16_t delta = static_cast((nowUs - lastEdgeTimestamp) / 10); lastEdgeTimestamp = nowUs; @@ -59,7 +59,7 @@ void EnableToothLogger() { memset(buffer, 0, sizeof(buffer)); // Reset the last edge to now - this prevents the first edge logged from being bogus - lastEdgeTimestamp = NT2US(getTimeNowNt()); + lastEdgeTimestamp = getTimeNowUs(); // Reset write index NextIdx = 0; diff --git a/firmware/console/tooth_logger.h b/firmware/console/tooth_logger.h index 8e33112f38..b1cf3469bf 100644 --- a/firmware/console/tooth_logger.h +++ b/firmware/console/tooth_logger.h @@ -9,6 +9,7 @@ #include #include +#include "efitime.h" #include "rusefi_enums.h" // Enable the tooth logger - this clears the buffer starts logging @@ -18,7 +19,7 @@ void EnableToothLogger(); void DisableToothLogger(); // A new tooth has arrived! Log to the buffer if enabled. -void LogTriggerTooth(trigger_event_e tooth); +void LogTriggerTooth(trigger_event_e tooth, efitick_t timestamp); struct ToothLoggerBuffer { diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index 31c3f0e7e3..6d9f9833e0 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -213,7 +213,7 @@ Engine::Engine(persistent_config_s *config) { */ bool Engine::needToStopEngine(efitick_t nowNt) const { return stopEngineRequestTimeNt != 0 && - nowNt - stopEngineRequestTimeNt < 3 * US2NT(US_PER_SECOND_LL); + nowNt - stopEngineRequestTimeNt < 3 * NT_PER_SECOND; } int Engine::getGlobalConfigurationVersion(void) const { diff --git a/firmware/controllers/algo/engine2.cpp b/firmware/controllers/algo/engine2.cpp index 44810e969c..33d4464cc0 100644 --- a/firmware/controllers/algo/engine2.cpp +++ b/firmware/controllers/algo/engine2.cpp @@ -88,14 +88,14 @@ void FuelConsumptionState::addData(float durationMs) { void FuelConsumptionState::update(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUFFIX) { efitick_t deltaNt = nowNt - accumulatedSecondPrevNt; - if (deltaNt >= US2NT(US_PER_SECOND_LL)) { + if (deltaNt >= NT_PER_SECOND) { perSecondConsumption = getFuelRate(perSecondAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX); perSecondAccumulator = 0; accumulatedSecondPrevNt = nowNt; } deltaNt = nowNt - accumulatedMinutePrevNt; - if (deltaNt >= US2NT(US_PER_SECOND_LL * 60)) { + if (deltaNt >= NT_PER_SECOND * 60) { perMinuteConsumption = getFuelRate(perMinuteAccumulator, deltaNt PASS_ENGINE_PARAMETER_SUFFIX); perMinuteAccumulator = 0; accumulatedMinutePrevNt = nowNt; diff --git a/firmware/controllers/engine_cycle/rpm_calculator.cpp b/firmware/controllers/engine_cycle/rpm_calculator.cpp index 29a616718c..5c0e0470f3 100644 --- a/firmware/controllers/engine_cycle/rpm_calculator.cpp +++ b/firmware/controllers/engine_cycle/rpm_calculator.cpp @@ -96,7 +96,7 @@ RpmCalculator::RpmCalculator() { // which we cannot provide inside this parameter-less constructor. need a solution for this minor mess // we need this initial to have not_running at first invocation - lastRpmEventTimeNt = (efitick_t) -10 * US2NT(US_PER_SECOND_LL); + lastRpmEventTimeNt = (efitick_t) -10 * NT_PER_SECOND; } /** @@ -118,11 +118,11 @@ bool RpmCalculator::checkIfSpinning(efitick_t nowNt DECLARE_ENGINE_PARAMETER_SUF * note that the result of this subtraction could be negative, that would happen if * we have a trigger event between the time we've invoked 'getTimeNow' and here */ - bool noRpmEventsForTooLong = nowNt - lastRpmEventTimeNt >= US2NT(NO_RPM_EVENTS_TIMEOUT_SECS * US_PER_SECOND_LL); // Anything below 60 rpm is not running + bool noRpmEventsForTooLong = nowNt - lastRpmEventTimeNt >= NT_PER_SECOND * NO_RPM_EVENTS_TIMEOUT_SECS; // Anything below 60 rpm is not running /** * Also check if there were no trigger events */ - bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.previousShaftEventTimeNt >= US2NT(US_PER_SECOND_LL); + bool noTriggerEventsForTooLong = nowNt - engine->triggerCentral.previousShaftEventTimeNt >= NT_PER_SECOND; if (noRpmEventsForTooLong || noTriggerEventsForTooLong) { return false; } @@ -250,7 +250,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, rpmState->setRpmValue(NOISY_RPM PASS_ENGINE_PARAMETER_SUFFIX); } else { int mult = (int)getEngineCycle(engine->getOperationMode(PASS_ENGINE_PARAMETER_SIGNATURE)) / 360; - float rpm = 60.0 * US2NT(US_PER_SECOND_LL) * mult / diffNt; + float rpm = 60.0 * NT_PER_SECOND * mult / diffNt; rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm PASS_ENGINE_PARAMETER_SUFFIX); } } diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 364f0cbf9f..2bcd91187d 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -46,7 +46,7 @@ trigger_central_s::trigger_central_s() : hwEventCounters() { TriggerCentral::TriggerCentral() : trigger_central_s() { // we need this initial to have not_running at first invocation - previousShaftEventTimeNt = (efitimems_t) -10 * US2NT(US_PER_SECOND_LL); + previousShaftEventTimeNt = (efitimems_t) -10 * NT_PER_SECOND; clearCallbacks(&triggerListeneres); triggerState.resetTriggerState(); @@ -198,7 +198,7 @@ void hwHandleShaftSignal(trigger_event_e signal, efitick_t timestamp) { // Log to the Tunerstudio tooth logger // We want to do this before anything else as we // actually want to capture any noise/jitter that may be occurring - LogTriggerTooth(signal); + LogTriggerTooth(signal, timestamp); #endif /* EFI_TOOTH_LOGGER */ // for effective noise filtering, we need both signal edges, @@ -340,7 +340,7 @@ void TriggerCentral::handleShaftSignal(trigger_event_e signal, efitick_t timesta efiAssertVoid(CUSTOM_ERR_6638, eventIndex >= 0 && eventIndex < HW_EVENT_TYPES, "signal type"); hwEventCounters[eventIndex]++; - if (timestamp - previousShaftEventTimeNt > US2NT(US_PER_SECOND_LL)) { + if (timestamp - previousShaftEventTimeNt > NT_PER_SECOND) { /** * We are here if there is a time gap between now and previous shaft event - that means the engine is not running. * That means we have lost synchronization since the engine is not running :) diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index fdd9d6e2cf..a042a52469 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -448,7 +448,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal * 10 seconds since previous trigger event we do not really care. */ toothDurations[0] = - currentDurationLong > 10 * US2NT(US_PER_SECOND_LL) ? 10 * US2NT(US_PER_SECOND_LL) : currentDurationLong; + currentDurationLong > 10 * NT_PER_SECOND ? 10 * NT_PER_SECOND : currentDurationLong; bool haveListener = triggerStateListener != NULL; bool isPrimary = triggerWheel == T_PRIMARY; @@ -693,7 +693,7 @@ void TriggerState::decodeTriggerEvent(const TriggerStateCallback triggerCycleCal } } if (someSortOfTriggerError) { - if (getTimeNowNt() - lastDecodingErrorTime > US2NT(US_PER_SECOND_LL)) { + if (getTimeNowNt() - lastDecodingErrorTime > NT_PER_SECOND) { someSortOfTriggerError = false; } } diff --git a/firmware/hw_layer/sensors/cj125.cpp b/firmware/hw_layer/sensors/cj125.cpp index 53709e7a23..63b0b55927 100644 --- a/firmware/hw_layer/sensors/cj125.cpp +++ b/firmware/hw_layer/sensors/cj125.cpp @@ -415,12 +415,12 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) { case CJ125_PREHEAT: // use constant-speed startup heat-up if (nowNt - instance->prevNt >= CJ125_HEATER_PREHEAT_PERIOD) { - float periodSecs = (float)(nowNt - instance->prevNt) / US2NT(US_PER_SECOND_LL); + float periodSecs = (float)(nowNt - instance->prevNt) / NT_PER_SECOND; // maintain speed at ~0.4V/sec float preheatDuty = instance->heaterDuty + periodSecs * CJ125_HEATER_PREHEAT_RATE; instance->SetHeater(preheatDuty PASS_ENGINE_PARAMETER_SUFFIX); // If we are heating too long, and there's still no result, then something is wrong... - if (nowNt - instance->startHeatingNt > US2NT(US_PER_SECOND_LL) * CJ125_PREHEAT_TIMEOUT) { + if (nowNt - instance->startHeatingNt > NT_PER_SECOND * CJ125_PREHEAT_TIMEOUT) { instance->setError(CJ125_ERROR_HEATER_MALFUNCTION PASS_ENGINE_PARAMETER_SUFFIX); } cjPrintData(); diff --git a/firmware/hw_layer/vehicle_speed.cpp b/firmware/hw_layer/vehicle_speed.cpp index c641d18d4b..9cbc7d364e 100644 --- a/firmware/hw_layer/vehicle_speed.cpp +++ b/firmware/hw_layer/vehicle_speed.cpp @@ -37,10 +37,10 @@ float getVehicleSpeed(void) { if (!hasVehicleSpeedSensor()) return 0; efitick_t nowNt = getTimeNowNt(); - if (nowNt - lastSignalTimeNt > US2NT(US_PER_SECOND_LL)) + if (nowNt - lastSignalTimeNt > NT_PER_SECOND) return 0; // previous signal time is too long ago - we are stopped - return engineConfiguration->vehicleSpeedCoef * US2NT(US_PER_SECOND_LL) / vssDiff; + return engineConfiguration->vehicleSpeedCoef * NT_PER_SECOND / vssDiff; } static void vsAnaWidthCallback(void) { diff --git a/firmware/util/efitime.h b/firmware/util/efitime.h index c64599b9bc..e44761d950 100644 --- a/firmware/util/efitime.h +++ b/firmware/util/efitime.h @@ -16,6 +16,7 @@ #define US_PER_SECOND 1000000 #define US_PER_SECOND_F 1000000.0 #define US_PER_SECOND_LL 1000000LL +#define NT_PER_SECOND (US2NT(US_PER_SECOND_LL)) #define MS2US(MS_TIME) ((MS_TIME) * 1000)