From 0ee45c012f0535225bf6777bf003ed7b2343ab75 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Tue, 11 Nov 2014 14:03:07 -0600 Subject: [PATCH] auto-sync --- .../controllers/algo/main_trigger_callback.h | 2 +- firmware/controllers/algo/signal_executor.h | 2 +- firmware/controllers/map_averaging.cpp | 8 ++++---- .../trigger/main_trigger_callback.cpp | 5 ++--- .../controllers/trigger/rpm_calculator.cpp | 19 ++++++++++--------- firmware/controllers/trigger/rpm_calculator.h | 2 +- .../controllers/trigger/trigger_central.cpp | 8 ++++---- .../controllers/trigger/trigger_central.h | 7 ++++--- firmware/emulation/wave_analyzer.cpp | 4 ++-- unit_tests/engine_test_helper.cpp | 4 +++- unit_tests/test_trigger_decoder.cpp | 4 +--- 11 files changed, 33 insertions(+), 32 deletions(-) diff --git a/firmware/controllers/algo/main_trigger_callback.h b/firmware/controllers/algo/main_trigger_callback.h index 18c29ba827..4de23348a7 100644 --- a/firmware/controllers/algo/main_trigger_callback.h +++ b/firmware/controllers/algo/main_trigger_callback.h @@ -29,7 +29,7 @@ public: }; void initMainEventListener(Engine *engine, engine_configuration2_s *engineConfiguration2); -void onTriggerEvent(trigger_event_e ckpSignalType, uint32_t eventIndex, MainTriggerCallback *mainTriggerCallback); +void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t eventIndex, Engine *engine); #endif int isIgnitionTimingError(void); diff --git a/firmware/controllers/algo/signal_executor.h b/firmware/controllers/algo/signal_executor.h index 6ec98b4e5b..722523cde0 100644 --- a/firmware/controllers/algo/signal_executor.h +++ b/firmware/controllers/algo/signal_executor.h @@ -53,7 +53,7 @@ void turnPinLow(io_pin_e pin); void initSignalExecutor(void); void initSignalExecutorImpl(void); -void scheduleByAngle(scheduling_s *timer, float angle, schfunc_t callback, void *param); +void scheduleByAngle(int rpm, scheduling_s *timer, float angle, schfunc_t callback, void *param); #ifdef __cplusplus } diff --git a/firmware/controllers/map_averaging.cpp b/firmware/controllers/map_averaging.cpp index f1ded4ddf9..7cd119c3b3 100644 --- a/firmware/controllers/map_averaging.cpp +++ b/firmware/controllers/map_averaging.cpp @@ -126,13 +126,13 @@ static void endAveraging(void *arg) { /** * Shaft Position callback used to schedule start and end of MAP averaging */ -static void shaftPositionCallback(trigger_event_e ckpEventType, uint32_t index, void *arg) { +static void shaftPositionCallback(trigger_event_e ckpEventType, uint32_t index, Engine *arg) { // this callback is invoked on interrupt thread if (index != 0) return; - int rpm = getRpm(); + int rpm = getRpmE(engine); if (!isValidRpm(rpm)) return; @@ -150,8 +150,8 @@ static void shaftPositionCallback(trigger_event_e ckpEventType, uint32_t index, int structIndex = getRevolutionCounter() % 2; // todo: schedule this based on closest trigger event, same as ignition works - scheduleByAngle(&startTimer[structIndex], startAngle, startAveraging, NULL); - scheduleByAngle(&endTimer[structIndex], startAngle + windowAngle, endAveraging, NULL); + scheduleByAngle(rpm, &startTimer[structIndex], startAngle, startAveraging, NULL); + scheduleByAngle(rpm, &endTimer[structIndex], startAngle + windowAngle, endAveraging, NULL); } static void showMapStats(void) { diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index f81f1f7f72..8061cac87a 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -243,7 +243,7 @@ void showMainHistogram(void) { * This is the main trigger event handler. * Both injection and ignition are controlled from this method. */ -void onTriggerEvent(trigger_event_e ckpSignalType, uint32_t eventIndex, MainTriggerCallback *mtc) { +void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t eventIndex, Engine *engine) { if (hasFirmwareError()) { /** * In case on a major error we should not process any more events. @@ -252,7 +252,6 @@ void onTriggerEvent(trigger_event_e ckpSignalType, uint32_t eventIndex, MainTrig return; } - Engine *engine = mtc->engine; (void) ckpSignalType; efiAssertVoid(eventIndex < 2 * engine->engineConfiguration2->triggerShape.shaftPositionEventCount, "event index"); efiAssertVoid(getRemainingStack(chThdSelf()) > 256, "lowstck#2"); @@ -370,7 +369,7 @@ void initMainEventListener(Engine *engine, engine_configuration2_s *engineConfig initHistogram(&mainLoopHisto, "main callback"); #endif /* EFI_HISTOGRAMS */ - addTriggerEventListener((ShaftPositionListener) &onTriggerEvent, "main loop", &mainTriggerCallbackInstance); + addTriggerEventListener(mainTriggerCallback, "main loop", engine); } int isIgnitionTimingError(void) { diff --git a/firmware/controllers/trigger/rpm_calculator.cpp b/firmware/controllers/trigger/rpm_calculator.cpp index bb7956fa52..cca9e35a53 100644 --- a/firmware/controllers/trigger/rpm_calculator.cpp +++ b/firmware/controllers/trigger/rpm_calculator.cpp @@ -136,7 +136,9 @@ bool isCranking(void) { * updated here. * This callback is invoked on interrupt thread. */ -void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, RpmCalculator *rpmState) { +void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Engine *engine) { + RpmCalculator *rpmState = engine->rpmCalculator; + efiAssertVoid(rpmState!=NULL, "NULL rpmState"); uint64_t nowNt = getTimeNowNt(); #if EFI_PROD_CODE efiAssertVoid(getRemainingStack(chThdSelf()) > 256, "lowstck#2z"); @@ -149,7 +151,6 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Rpm #endif return; } - rpmState->onNewEngineCycle(); bool hadRpmRecently = rpmState->isRunning(); @@ -170,6 +171,7 @@ void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Rpm rpmState->setRpmValue(rpm > UNREALISTIC_RPM ? NOISY_RPM : rpm); } } + rpmState->onNewEngineCycle(); rpmState->lastRpmEventTimeNt = nowNt; #if EFI_ANALOG_CHART || defined(__DOXYGEN__) if (engineConfiguration->analogChartMode == AC_TRIGGER) @@ -194,14 +196,14 @@ static void onTdcCallback(void) { /** * This trigger callback schedules the actual physical TDC callback in relation to trigger synchronization point. */ -static void tdcMarkCallback(trigger_event_e ckpSignalType, uint32_t index0, void *arg) { - (void) arg; +static void tdcMarkCallback(trigger_event_e ckpSignalType, uint32_t index0, Engine *engine) { (void) ckpSignalType; bool isTriggerSynchronizationPoint = index0 == 0; if (isTriggerSynchronizationPoint) { int revIndex2 = getRevolutionCounter() % 2; + int rpm = getRpm(); // todo: use event-based scheduling, not just time-based scheduling - scheduleByAngle(&tdcScheduler[revIndex2], engineConfiguration->globalTriggerAngleOffset, + scheduleByAngle(rpm, &tdcScheduler[revIndex2], engineConfiguration->globalTriggerAngleOffset, (schfunc_t) onTdcCallback, NULL); } } @@ -238,10 +240,10 @@ void initRpmCalculator(Engine *engine) { tdcScheduler[0].name = "tdc0"; tdcScheduler[1].name = "tdc1"; - addTriggerEventListener(&tdcMarkCallback, "chart TDC mark", NULL); + addTriggerEventListener(tdcMarkCallback, "chart TDC mark", engine); #endif - addTriggerEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", &rpmState); + addTriggerEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", engine); } #if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__) @@ -250,8 +252,7 @@ void initRpmCalculator(Engine *engine) { * The callback would be executed once after the duration of time which * it takes the crankshaft to rotate to the specified angle. */ -void scheduleByAngle(scheduling_s *timer, float angle, schfunc_t callback, void *param) { - int rpm = getRpm(); +void scheduleByAngle(int rpm, scheduling_s *timer, float angle, schfunc_t callback, void *param) { if (!isValidRpm(rpm)) { /** * this might happen in case of a single trigger event after a pause - this is normal, so no diff --git a/firmware/controllers/trigger/rpm_calculator.h b/firmware/controllers/trigger/rpm_calculator.h index 62a0117452..ef67084ce3 100644 --- a/firmware/controllers/trigger/rpm_calculator.h +++ b/firmware/controllers/trigger/rpm_calculator.h @@ -59,7 +59,7 @@ private: */ int getRpmE(Engine *engine); bool isCrankingE(Engine *engine); -void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, RpmCalculator *rpmState); +void rpmShaftPositionCallback(trigger_event_e ckpSignalType, uint32_t index, Engine *engine); /** * @brief Initialize RPM calculator */ diff --git a/firmware/controllers/trigger/trigger_central.cpp b/firmware/controllers/trigger/trigger_central.cpp index 0212ca1774..772b6c8b0a 100644 --- a/firmware/controllers/trigger/trigger_central.cpp +++ b/firmware/controllers/trigger/trigger_central.cpp @@ -43,9 +43,9 @@ uint64_t getStartOfRevolutionIndex() { return triggerCentral.triggerState.getStartOfRevolutionIndex(); } -void TriggerCentral::addEventListener(ShaftPositionListener listener, const char *name, void *arg) { +void TriggerCentral::addEventListener(ShaftPositionListener listener, const char *name, Engine *engine) { print("registerCkpListener: %s\r\n", name); - registerCallback(&triggerListeneres, (IntListener) listener, arg); + registerCallback(&triggerListeneres, (IntListener) listener, engine); } /** @@ -54,8 +54,8 @@ void TriggerCentral::addEventListener(ShaftPositionListener listener, const char * Trigger event listener would be invoked on each trigger event. For example, for a 60/2 wheel * that would be 116 events: 58 SHAFT_PRIMARY_UP and 58 SHAFT_PRIMARY_DOWN events. */ -void addTriggerEventListener(ShaftPositionListener listener, const char *name, void *arg) { - triggerCentral.addEventListener(listener, name, arg); +void addTriggerEventListener(ShaftPositionListener listener, const char *name, Engine *engine) { + triggerCentral.addEventListener(listener, name, engine); } #if (EFI_PROD_CODE || EFI_SIMULATOR) || defined(__DOXYGEN__) diff --git a/firmware/controllers/trigger/trigger_central.h b/firmware/controllers/trigger/trigger_central.h index 420ccc7594..7864c91212 100644 --- a/firmware/controllers/trigger/trigger_central.h +++ b/firmware/controllers/trigger/trigger_central.h @@ -12,7 +12,8 @@ #include "listener_array.h" #include "trigger_decoder.h" -typedef void (*ShaftPositionListener)(trigger_event_e signal, uint32_t index, void *arg); +class Engine; +typedef void (*ShaftPositionListener)(trigger_event_e signal, uint32_t index, Engine *engine); #ifdef __cplusplus #include "ec2.h" @@ -23,7 +24,7 @@ typedef void (*ShaftPositionListener)(trigger_event_e signal, uint32_t index, vo class TriggerCentral { public: TriggerCentral(); - void addEventListener(ShaftPositionListener handler, const char *name, void *arg); + void addEventListener(ShaftPositionListener handler, const char *name, Engine *engine); void handleShaftSignal(Engine *engine, trigger_event_e signal); int getHwEventCounter(int index); TriggerState triggerState; @@ -44,7 +45,7 @@ void printAllCallbacksHistogram(void); extern "C" { #endif /* __cplusplus */ -void addTriggerEventListener(ShaftPositionListener handler, const char *name, void *arg); +void addTriggerEventListener(ShaftPositionListener handler, const char *name, Engine *engine); int isSignalDecoderError(void); #ifdef __cplusplus diff --git a/firmware/emulation/wave_analyzer.cpp b/firmware/emulation/wave_analyzer.cpp index a866e95874..ec2281b7df 100644 --- a/firmware/emulation/wave_analyzer.cpp +++ b/firmware/emulation/wave_analyzer.cpp @@ -137,7 +137,7 @@ static void initWave(const char *name, int index) { } #endif -static void waTriggerEventListener(trigger_event_e ckpSignalType, uint32_t index, void *arg) { +static void waTriggerEventListener(trigger_event_e ckpSignalType, uint32_t index, Engine *arg) { (void)ckpSignalType; (void)arg; if (index != 0) { @@ -251,7 +251,7 @@ void initWaveAnalyzer(void) { initWave(WA_CHANNEL_1, 0); initWave(WA_CHANNEL_2, 1); - addTriggerEventListener(&waTriggerEventListener, "wave analyzer", (void*) NULL); + addTriggerEventListener(waTriggerEventListener, "wave analyzer", NULL); addConsoleActionII("set_logic_input_mode", setWaveModeSilent); diff --git a/unit_tests/engine_test_helper.cpp b/unit_tests/engine_test_helper.cpp index cac5900e2e..b894bf40a1 100644 --- a/unit_tests/engine_test_helper.cpp +++ b/unit_tests/engine_test_helper.cpp @@ -20,6 +20,8 @@ EngineTestHelper::EngineTestHelper(engine_type_e engineType) { engine.engineConfiguration2 = &ec2; + engine.rpmCalculator = &rpmState; + prepareFuelMap(engine.engineConfiguration); @@ -43,6 +45,6 @@ void EngineTestHelper::initTriggerShapeAndRpmCalculator() { initializeTriggerShape(NULL, ec, &ec2); incrementGlobalConfigurationVersion(); - triggerCentral.addEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", &rpmState); + triggerCentral.addEventListener((ShaftPositionListener) &rpmShaftPositionCallback, "rpm reporter", &engine); } diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index 5de1d77538..c4040c26a0 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -403,9 +403,7 @@ static void testRpmCalculator(void) { static MainTriggerCallback triggerCallbackInstance; triggerCallbackInstance.init(ð.engine, ec2); - eth.triggerCentral.addEventListener((ShaftPositionListener)&onTriggerEvent, "main loop", &triggerCallbackInstance); - - eth.engine.rpmCalculator = ð.rpmState; + eth.triggerCentral.addEventListener((ShaftPositionListener)&mainTriggerCallback, "main loop", ð.engine); // engine.rpmCalculator = ð.rpmState; prepareTimingMap();