From 28289a2d7784d1683c41a896bdb633868cd0203a Mon Sep 17 00:00:00 2001 From: rusEfi Date: Mon, 28 Nov 2016 12:03:02 -0500 Subject: [PATCH] auto-sync --- firmware/controllers/algo/event_registry.h | 7 +++- firmware/controllers/trigger/spark_logic.cpp | 36 ++++++++++++-------- unit_tests/test_trigger_decoder.cpp | 6 ++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/firmware/controllers/algo/event_registry.h b/firmware/controllers/algo/event_registry.h index 02d57a2b10..2b098f101f 100644 --- a/firmware/controllers/algo/event_registry.h +++ b/firmware/controllers/algo/event_registry.h @@ -61,6 +61,7 @@ public: IgnitionOutputPin *getOutputForLoggins(); }; + /** * two wire batch mode would generate twice the number of cylinders events, that's the max */ @@ -69,6 +70,10 @@ public: typedef ArrayList InjectionEventList; -typedef ArrayList IgnitionEventList; +class IgnitionEventList : public ArrayList { +public: + IgnitionEventList(); + bool isReady; +}; #endif /* EVENT_REGISTRY_H_ */ diff --git a/firmware/controllers/trigger/spark_logic.cpp b/firmware/controllers/trigger/spark_logic.cpp index c8d017b7ff..c950813a20 100644 --- a/firmware/controllers/trigger/spark_logic.cpp +++ b/firmware/controllers/trigger/spark_logic.cpp @@ -17,6 +17,10 @@ static Logging *logger; static const char *prevSparkName = NULL; +IgnitionEventList::IgnitionEventList() { + isReady = false; +} + int isInjectionEnabled(engine_configuration_s *engineConfiguration) { // todo: is this worth a method? should this be inlined? return engineConfiguration->isInjectionEnabled; @@ -198,8 +202,8 @@ static ALWAYS_INLINE void handleSparkEvent(bool limitedSpark, uint32_t trgEventI } } -static void addIgnitionEvent(angle_t localAdvance, angle_t dwellAngle, IgnitionEventList *list, IgnitionOutputPin *output, IgnitionOutputPin *secondOutput DECLARE_ENGINE_PARAMETER_S) { - IgnitionEvent *event = list->add(); +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]; if (!isPinAssigned(output)) { // todo: extract method for this index math @@ -222,28 +226,27 @@ static void initializeIgnitionActions(angle_t advance, angle_t dwellAngle, IgnitionEventList *list DECLARE_ENGINE_PARAMETER_S) { efiAssertVoid(engineConfiguration->specs.cylindersCount > 0, "cylindersCount"); - list->reset(); - for (int i = 0; i < CONFIG(specs.cylindersCount); i++) { // 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 = -advance + ENGINE(angleExtra[i]); const int index = ENGINE(ignitionPin[i]); - const int cylinderIndex = ID2INDEX(getCylinderId(CONFIG(specs.firingOrder), index)); - IgnitionOutputPin *output = &enginePins.coils[cylinderIndex]; + const int coilIndex = ID2INDEX(getCylinderId(CONFIG(specs.firingOrder), index)); + IgnitionOutputPin *output = &enginePins.coils[coilIndex]; IgnitionOutputPin *secondOutput; if (CONFIG(ignitionMode) == IM_WASTED_SPARK && CONFIG(twoWireBatchIgnition)) { int secondIndex = index + CONFIG(specs.cylindersCount) / 2; - int secondCylinderIndex = ID2INDEX(getCylinderId(CONFIG(specs.firingOrder), secondIndex)); - secondOutput = &enginePins.coils[secondCylinderIndex]; + int secondCoilIndex = ID2INDEX(getCylinderId(CONFIG(specs.firingOrder), secondIndex)); + secondOutput = &enginePins.coils[secondCoilIndex]; } else { secondOutput = NULL; } - addIgnitionEvent(localAdvance, dwellAngle, list, output, secondOutput PASS_ENGINE_PARAMETER); + addIgnitionEvent(i, localAdvance, dwellAngle, list, output, secondOutput PASS_ENGINE_PARAMETER); } + list->isReady = true; } static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm, int revolutionIndex DECLARE_ENGINE_PARAMETER_S) { @@ -275,7 +278,8 @@ static ALWAYS_INLINE void prepareIgnitionSchedule(int rpm, int revolutionIndex D if (cisnan(ENGINE(engineState.timingAdvance))) { // error should already be reported - list->reset(); // reset is needed to clear previous ignition schedule + // need to invalidate previous ignition schedule + list->isReady = false; return; } initializeIgnitionActions(ENGINE(engineState.timingAdvance), ENGINE(engineState.dwellAngle), list PASS_ENGINE_PARAMETER); @@ -318,11 +322,13 @@ void handleSpark(int revolutionIndex, bool limitedSpark, uint32_t trgEventIndex, } // scheduleSimpleMsg(&logger, "eventId spark ", eventIndex); - for (int i = 0; i < list->size; i++) { - IgnitionEvent *event = &list->elements[i]; - if (event->dwellPosition.eventIndex != trgEventIndex) - continue; - handleSparkEvent(limitedSpark, trgEventIndex, event, rpm PASS_ENGINE_PARAMETER); + if (list->isReady) { + for (int i = 0; i < CONFIG(specs.cylindersCount); i++) { + IgnitionEvent *event = &list->elements[i]; + if (event->dwellPosition.eventIndex != trgEventIndex) + continue; + handleSparkEvent(limitedSpark, trgEventIndex, event, rpm PASS_ENGINE_PARAMETER); + } } } diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index d316cf7b91..ea481c22a0 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -149,7 +149,7 @@ void test1995FordInline6TriggerDecoder(void) { eth.fireTriggerEvents(48); IgnitionEventList *ecl = ð.ec2.ignitionEvents[0]; - assertEqualsM("ford inline ignition events size", 6, ecl->size); + assertEqualsM("ford inline ignition events size", 1, ecl->isReady); assertEqualsM("event index", 0, ecl->elements[0].dwellPosition.eventIndex); assertEqualsM("angle offset#1", 7, ecl->elements[0].dwellPosition.angleOffset); @@ -301,7 +301,7 @@ void testRpmCalculator(void) { EngineTestHelper eth(FORD_INLINE_6_1995); EXPAND_EngineTestHelper; IgnitionEventList *ilist = ð.engine.engineConfiguration2->ignitionEvents[1]; - assertEqualsM("size #1", 0, ilist->size); + assertEqualsM("size #1", 0, ilist->isReady); assertEqualsM("engineCycle", 720, eth.engine.engineCycle); @@ -354,7 +354,7 @@ void testRpmCalculator(void) { assertEqualsM("dwell", 4.5, eth.engine.engineState.dwellAngle); assertEqualsM("fuel #2", 4.5450, eth.engine.fuelMs); assertEqualsM("one degree", 111.1111, eth.engine.rpmCalculator.oneDegreeUs); - assertEqualsM("size #2", 6, ilist->size); + assertEqualsM("size #2", 1, ilist->isReady); assertEqualsM("dwell angle", 0, ilist->elements[0].dwellPosition.eventAngle); assertEqualsM("dwell offset", 8.5, ilist->elements[0].dwellPosition.angleOffset);