diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 09f5450863..529b7d3f83 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -478,7 +478,7 @@ static int getIgnitionPinForIndex(int i DECLARE_ENGINE_PARAMETER_SUFFIX) { break; default: - warning(CUSTOM_OBD_IGNITION_MODE, "unsupported ignitionMode %d in initializeIgnitionActions()", engineConfiguration->ignitionMode); + warning(CUSTOM_OBD_IGNITION_MODE, "unsupported ignitionMode %d in getIgnitionPinForIndex()", engineConfiguration->ignitionMode); return 0; } } diff --git a/firmware/controllers/trigger/spark_logic.cpp b/firmware/controllers/trigger/spark_logic.cpp index ed3f97b686..1c2fa9ae87 100644 --- a/firmware/controllers/trigger/spark_logic.cpp +++ b/firmware/controllers/trigger/spark_logic.cpp @@ -30,8 +30,6 @@ int isIgnitionTimingError(void) { return ignitionErrorDetection.sum(6) > 4; } -void prepareCylinderIgnitionSchedule(IgnitionEvent *event DECLARE_ENGINE_PARAMETER_SUFFIX); - static void turnSparkPinLow2(IgnitionEvent *event, IgnitionOutputPin *output) { #if SPARK_EXTREME_LOGGING || defined(__DOXYGEN__) scheduleMsg(logger, "spark goes low %d %s %d current=%d cnt=%d id=%d", getRevolutionCounter(), output->name, (int)getTimeNowUs(), @@ -61,6 +59,51 @@ static void turnSparkPinLow2(IgnitionEvent *event, IgnitionOutputPin *output) { #endif /* EFI_PROD_CODE */ } +// todo: make this a class method? +#define assertPinAssigned(output) { \ + if (!output->isInitialized()) { \ + warning(CUSTOM_OBD_COIL_PIN_NOT_ASSIGNED, "no_pin_cl #%s", (output)->name); \ + } \ +} + +void prepareCylinderIgnitionSchedule(IgnitionEvent *event DECLARE_ENGINE_PARAMETER_SUFFIX) { + // todo: clean up this implementation? does not look too nice as is. + + // change of sign here from 'before TDC' to 'after TDC' + const angle_t localAdvance = -ENGINE(engineState.timingAdvance) + ENGINE(ignitionPositionWithEngineCycle[event->cylinderIndex]) + CONFIG(timing_offset_cylinder[event->cylinderIndex]); + efiAssertVoid(!cisnan(localAdvance), "localAdvance#1"); + const int index = ENGINE(ignitionPin[event->cylinderIndex]); + const int coilIndex = ID2INDEX(getCylinderId(index PASS_ENGINE_PARAMETER_SUFFIX)); + IgnitionOutputPin *output = &enginePins.coils[coilIndex]; + + IgnitionOutputPin *secondOutput; + if (getIgnitionMode(PASS_ENGINE_PARAMETER_SIGNATURE) == IM_WASTED_SPARK && CONFIG(twoWireBatchIgnition)) { + int secondIndex = index + CONFIG(specs.cylindersCount) / 2; + int secondCoilIndex = ID2INDEX(getCylinderId(secondIndex PASS_ENGINE_PARAMETER_SUFFIX)); + secondOutput = &enginePins.coils[secondCoilIndex]; + assertPinAssigned(secondOutput); + } else { + secondOutput = NULL; + } + angle_t dwellAngle = ENGINE(engineState.dwellAngle); + + assertPinAssigned(output); + + event->outputs[0] = output; + event->outputs[1] = secondOutput; + event->advance = localAdvance; + + angle_t a = localAdvance - dwellAngle; + efiAssertVoid(!cisnan(a), "findAngle#5"); + assertAngleRange(a, "findAngle#a6", CUSTOM_ERR_6550); + TRIGGER_SHAPE(findTriggerPosition(&event->dwellPosition, a PASS_ENGINE_PARAMETER_SUFFIX)); + +#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__) + printf("addIgnitionEvent %s ind=%d\n", output->name, event->dwellPosition.eventIndex); + // scheduleMsg(logger, "addIgnitionEvent %s ind=%d", output->name, event->dwellPosition->eventIndex); +#endif /* FUEL_MATH_EXTREME_LOGGING */ +} + void turnSparkPinLow(IgnitionEvent *event) { for (int i = 0; i< MAX_OUTPUTS_FOR_IGNITION;i++) { IgnitionOutputPin *output = event->outputs[i]; @@ -213,51 +256,6 @@ static ALWAYS_INLINE void handleSparkEvent(bool limitedSpark, uint32_t trgEventI } } -// todo: make this a class method? -#define assertPinAssigned(output) { \ - if (!output->isInitialized()) { \ - warning(CUSTOM_OBD_COIL_PIN_NOT_ASSIGNED, "no_pin_cl #%s", (output)->name); \ - } \ -} - -void prepareCylinderIgnitionSchedule(IgnitionEvent *event DECLARE_ENGINE_PARAMETER_SUFFIX) { - // todo: clean up this implementation? does not look too nice as is. - - // change of sign here from 'before TDC' to 'after TDC' - const angle_t localAdvance = -ENGINE(engineState.timingAdvance) + ENGINE(ignitionPositionWithEngineCycle[event->cylinderIndex]) + CONFIG(timing_offset_cylinder[event->cylinderIndex]); - efiAssertVoid(!cisnan(localAdvance), "localAdvance#1"); - const int index = ENGINE(ignitionPin[event->cylinderIndex]); - const int coilIndex = ID2INDEX(getCylinderId(index PASS_ENGINE_PARAMETER_SUFFIX)); - IgnitionOutputPin *output = &enginePins.coils[coilIndex]; - - IgnitionOutputPin *secondOutput; - if (getIgnitionMode(PASS_ENGINE_PARAMETER_SIGNATURE) == IM_WASTED_SPARK && CONFIG(twoWireBatchIgnition)) { - int secondIndex = index + CONFIG(specs.cylindersCount) / 2; - int secondCoilIndex = ID2INDEX(getCylinderId(secondIndex PASS_ENGINE_PARAMETER_SUFFIX)); - secondOutput = &enginePins.coils[secondCoilIndex]; - assertPinAssigned(secondOutput); - } else { - secondOutput = NULL; - } - angle_t dwellAngle = ENGINE(engineState.dwellAngle); - - assertPinAssigned(output); - - event->outputs[0] = output; - event->outputs[1] = secondOutput; - event->advance = localAdvance; - - angle_t a = localAdvance - dwellAngle; - efiAssertVoid(!cisnan(a), "findAngle#5"); - assertAngleRange(a, "findAngle#a6", CUSTOM_ERR_6550); - TRIGGER_SHAPE(findTriggerPosition(&event->dwellPosition, a PASS_ENGINE_PARAMETER_SUFFIX)); - -#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__) - printf("addIgnitionEvent %s ind=%d\n", output->name, event->dwellPosition.eventIndex); - // scheduleMsg(logger, "addIgnitionEvent %s ind=%d", output->name, event->dwellPosition->eventIndex); -#endif /* FUEL_MATH_EXTREME_LOGGING */ -} - static void initializeIgnitionActions(IgnitionEventList *list DECLARE_ENGINE_PARAMETER_SUFFIX) { if (cisnan(ENGINE(engineState.timingAdvance))) { // error should already be reported