diff --git a/firmware/controllers/algo/engine.cpp b/firmware/controllers/algo/engine.cpp index b0374b5335..adb490c8d3 100644 --- a/firmware/controllers/algo/engine.cpp +++ b/firmware/controllers/algo/engine.cpp @@ -289,6 +289,7 @@ void Engine::watchdog() { return; } isSpinning = false; + ignitionList()->isReady = false; #if EFI_PROD_CODE || EFI_SIMULATOR scheduleMsg(&logger, "engine has STOPPED"); scheduleMsg(&logger, "templog engine has STOPPED [%x][%x] [%x][%x] %d", @@ -303,6 +304,11 @@ void Engine::watchdog() { #endif } +IgnitionEventList * Engine::ignitionList() { + int revolutionIndex = rpmCalculator.getRevolutionCounter() % 2; + return &engineConfiguration2->ignitionEvents[revolutionIndex]; +} + void Engine::prepareFuelSchedule(DECLARE_ENGINE_PARAMETER_F) { int rpm = rpmCalculator.rpmValue; efiAssertVoid(ENGINE(engineConfiguration2)->injectionEvents != ENGINE(engineConfiguration2)->processing, "fuel pointers"); diff --git a/firmware/controllers/algo/engine.h b/firmware/controllers/algo/engine.h index 9d20ad1818..aa21e69aa4 100644 --- a/firmware/controllers/algo/engine.h +++ b/firmware/controllers/algo/engine.h @@ -260,6 +260,7 @@ public: Engine(persistent_config_s *config); void init(persistent_config_s *config); void prepareFuelSchedule(DECLARE_ENGINE_PARAMETER_F); + IgnitionEventList *ignitionList(); // todo: inline/rename/refactor WallFuel wallFuel; diff --git a/firmware/controllers/trigger/main_trigger_callback.cpp b/firmware/controllers/trigger/main_trigger_callback.cpp index 281716f592..6af121143f 100644 --- a/firmware/controllers/trigger/main_trigger_callback.cpp +++ b/firmware/controllers/trigger/main_trigger_callback.cpp @@ -545,8 +545,7 @@ void mainTriggerCallback(trigger_event_e ckpSignalType, uint32_t trgEventIndex D /** * For spark we schedule both start of coil charge and actual spark based on trigger angle */ - handleSpark(revolutionIndex, limitedSpark, trgEventIndex, rpm, - &engine->engineConfiguration2->ignitionEvents[revolutionIndex] PASS_ENGINE_PARAMETER); + handleSpark(limitedSpark, trgEventIndex, rpm PASS_ENGINE_PARAMETER); #if (EFI_HISTOGRAMS && EFI_PROD_CODE) || defined(__DOXYGEN__) int diff = hal_lld_get_counter_value() - beforeCallback; if (diff > 0) diff --git a/firmware/controllers/trigger/spark_logic.cpp b/firmware/controllers/trigger/spark_logic.cpp index 4ebde5d857..a2c313fee9 100644 --- a/firmware/controllers/trigger/spark_logic.cpp +++ b/firmware/controllers/trigger/spark_logic.cpp @@ -202,8 +202,7 @@ static ALWAYS_INLINE void handleSparkEvent(bool limitedSpark, uint32_t trgEventI } } -static void addIgnitionEvent(int cylinderIndex, angle_t localAdvance, angle_t dwellAngle, IgnitionEventList *list, IgnitionOutputPin *output, IgnitionOutputPin *secondOutput DECLARE_ENGINE_PARAMETER_S) { - IgnitionEvent *event = &list->elements[cylinderIndex]; +static void addIgnitionEvent(angle_t localAdvance, angle_t dwellAngle, IgnitionEvent *event, IgnitionOutputPin *output, IgnitionOutputPin *secondOutput DECLARE_ENGINE_PARAMETER_S) { if (!isPinAssigned(output)) { // todo: extract method for this index math @@ -222,12 +221,12 @@ static void addIgnitionEvent(int cylinderIndex, angle_t localAdvance, angle_t dw } -static void prepareIgnitionSchedule(int cylinderIndex, IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S) { +static void prepareIgnitionSchedule(IgnitionEvent *event DECLARE_ENGINE_PARAMETER_S) { // todo: clean up this implementation? does not look too nice as is. // change of sign here from 'before TDC' to 'after TDC' - angle_t localAdvance = -ENGINE(engineState.timingAdvance) + ENGINE(angleExtra[cylinderIndex]); - const int index = ENGINE(ignitionPin[cylinderIndex]); + angle_t localAdvance = -ENGINE(engineState.timingAdvance) + ENGINE(angleExtra[event->cylinderIndex]); + const int index = ENGINE(ignitionPin[event->cylinderIndex]); const int coilIndex = ID2INDEX(getCylinderId(CONFIG(specs.firingOrder), index)); IgnitionOutputPin *output = &enginePins.coils[coilIndex]; @@ -240,7 +239,7 @@ static void prepareIgnitionSchedule(int cylinderIndex, IgnitionEventList *list D secondOutput = NULL; } - addIgnitionEvent(cylinderIndex, localAdvance, ENGINE(engineState.dwellAngle), list, output, secondOutput PASS_ENGINE_PARAMETER); + addIgnitionEvent(localAdvance, ENGINE(engineState.dwellAngle), event, output, secondOutput PASS_ENGINE_PARAMETER); } static void initializeIgnitionActions(IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S) { @@ -248,12 +247,12 @@ static void initializeIgnitionActions(IgnitionEventList *list DECLARE_ENGINE_PAR for (int cylinderIndex = 0; cylinderIndex < CONFIG(specs.cylindersCount); cylinderIndex++) { list->elements[cylinderIndex].cylinderIndex = cylinderIndex; - prepareIgnitionSchedule(cylinderIndex, list PASS_ENGINE_PARAMETER); + prepareIgnitionSchedule(&list->elements[cylinderIndex] PASS_ENGINE_PARAMETER); } list->isReady = true; } -static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm, int revolutionIndex DECLARE_ENGINE_PARAMETER_S) { +static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm DECLARE_ENGINE_PARAMETER_S) { engine->m.beforeIgnitionSch = GET_TIMESTAMP(); /** @@ -278,7 +277,7 @@ static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm, int revolutionIndex D // todo: add some check for dwell overflow? like 4 times 6 ms while engine cycle is less then that - IgnitionEventList *list = &engine->engineConfiguration2->ignitionEvents[revolutionIndex]; + IgnitionEventList *list = engine->ignitionList(); if (cisnan(ENGINE(engineState.timingAdvance))) { // error should already be reported @@ -290,16 +289,19 @@ static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm, int revolutionIndex D engine->m.ignitionSchTime = GET_TIMESTAMP() - engine->m.beforeIgnitionSch; } -void handleSpark(int revolutionIndex, bool limitedSpark, uint32_t trgEventIndex, int rpm, - IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S) { - if (trgEventIndex == 0) { - prepareIgnitionSchedule(rpm, revolutionIndex PASS_ENGINE_PARAMETER); - } +void handleSpark(bool limitedSpark, uint32_t trgEventIndex, int rpm + DECLARE_ENGINE_PARAMETER_S) { if (!isValidRpm(rpm) || !CONFIG(isIgnitionEnabled)) { // this might happen for instance in case of a single trigger event after a pause return; } + + IgnitionEventList *list = engine->ignitionList(); + if (trgEventIndex == 0) { + prepareIgnitionSchedule(rpm PASS_ENGINE_PARAMETER); + } + /** * Ignition schedule is defined once per revolution * See initializeIgnitionActions() diff --git a/firmware/controllers/trigger/spark_logic.h b/firmware/controllers/trigger/spark_logic.h index 9c2bd88c60..6072502834 100644 --- a/firmware/controllers/trigger/spark_logic.h +++ b/firmware/controllers/trigger/spark_logic.h @@ -11,8 +11,7 @@ #include "engine.h" int isInjectionEnabled(engine_configuration_s *engineConfiguration); -void handleSpark(int revolutionIndex, bool limitedSpark, uint32_t trgEventIndex, int rpm, - IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S); +void handleSpark(bool limitedSpark, uint32_t trgEventIndex, int rpm DECLARE_ENGINE_PARAMETER_S); void initSparkLogic(Logging *sharedLogger); void turnSparkPinHigh(IgnitionEvent *event); void turnSparkPinLow(IgnitionEvent *event);