From 0ae6084301b6f61ab5e9d1e736d98264ba64d09e Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 12 Nov 2019 00:56:51 -0500 Subject: [PATCH] refactoring: time to unify digital pin API --- firmware/development/wave_analyzer.cpp | 2 +- firmware/hw_layer/digital_input.cpp | 2 +- firmware/hw_layer/digital_input.h | 2 +- firmware/hw_layer/trigger_input.h | 2 +- firmware/hw_layer/trigger_input_exti.cpp | 4 +- firmware/hw_layer/trigger_input_icu.cpp | 94 ++++-------------------- 6 files changed, 22 insertions(+), 84 deletions(-) diff --git a/firmware/development/wave_analyzer.cpp b/firmware/development/wave_analyzer.cpp index b088eca86a..bb74ebdccb 100644 --- a/firmware/development/wave_analyzer.cpp +++ b/firmware/development/wave_analyzer.cpp @@ -117,7 +117,7 @@ static void initWave(const char *name, int index) { if (reader->hw != NULL) { reader->hw->setWidthCallback((VoidInt)(void*) waAnaWidthCallback, (void*) reader); - reader->hw->setPeridoCallback((VoidInt)(void*) waIcuPeriodCallback, (void*) reader); + reader->hw->setPeriodCallback((VoidInt)(void*) waIcuPeriodCallback, (void*) reader); } print("wave%d input on %s\r\n", index, hwPortname(brainPin)); diff --git a/firmware/hw_layer/digital_input.cpp b/firmware/hw_layer/digital_input.cpp index bb855b89f0..dc0550b324 100644 --- a/firmware/hw_layer/digital_input.cpp +++ b/firmware/hw_layer/digital_input.cpp @@ -11,6 +11,6 @@ void digital_input_s::setWidthCallback(VoidInt handler, void *arg) { widthListeners.registerCallback(handler, arg); } -void digital_input_s::setPeridoCallback(VoidInt handler, void *arg) { +void digital_input_s::setPeriodCallback(VoidInt handler, void *arg) { periodListeners.registerCallback(handler, arg); } diff --git a/firmware/hw_layer/digital_input.h b/firmware/hw_layer/digital_input.h index 5bada2b1f4..a428ee4312 100644 --- a/firmware/hw_layer/digital_input.h +++ b/firmware/hw_layer/digital_input.h @@ -23,5 +23,5 @@ typedef struct { // Width/Period names are historically inherited from ICU implementation, todo: migrate to better names, high/low? rise/hall? void setWidthCallback(VoidInt handler, void *arg); - void setPeridoCallback(VoidInt handler, void *arg); + void setPeriodCallback(VoidInt handler, void *arg); } digital_input_s; diff --git a/firmware/hw_layer/trigger_input.h b/firmware/hw_layer/trigger_input.h index 2d087d4729..529971ced0 100644 --- a/firmware/hw_layer/trigger_input.h +++ b/firmware/hw_layer/trigger_input.h @@ -22,6 +22,6 @@ void startTriggerInputPins(void); void stopTriggerInputPins(void); void setPrimaryChannel(brain_pin_e brainPin); void turnOffTriggerInputPin(brain_pin_e brainPin); -int turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft); +void turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft); #endif /* CRANK_INPUT_H_ */ diff --git a/firmware/hw_layer/trigger_input_exti.cpp b/firmware/hw_layer/trigger_input_exti.cpp index 5198a79c5e..2d836afbc6 100644 --- a/firmware/hw_layer/trigger_input_exti.cpp +++ b/firmware/hw_layer/trigger_input_exti.cpp @@ -64,7 +64,7 @@ static void cam_callback(void *arg) { } } -int turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft) { +void turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft) { brain_pin_e brainPin = isVvtShaft ? engineConfiguration->camInputs[index] : CONFIGB(triggerInputPins)[index]; scheduleMsg(logger, "turnOnTriggerInputPin(PAL) %s %s", msg, hwPortname(brainPin)); @@ -73,7 +73,7 @@ int turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft) { * * do not set to both edges if we need only one * * simplify callback in case of one edge */ ioline_t pal_line = PAL_LINE(getHwPort("trg", brainPin), getHwPin("trg", brainPin)); - return efiExtiEnablePin(msg, brainPin, PAL_EVENT_MODE_BOTH_EDGES, isVvtShaft ? shaft_callback : cam_callback, (void *)pal_line); + efiExtiEnablePin(msg, brainPin, PAL_EVENT_MODE_BOTH_EDGES, isVvtShaft ? shaft_callback : cam_callback, (void *)pal_line); } void turnOffTriggerInputPin(brain_pin_e brainPin) { diff --git a/firmware/hw_layer/trigger_input_icu.cpp b/firmware/hw_layer/trigger_input_icu.cpp index 4c717803df..7bd3cb3d39 100644 --- a/firmware/hw_layer/trigger_input_icu.cpp +++ b/firmware/hw_layer/trigger_input_icu.cpp @@ -28,15 +28,13 @@ extern bool hasFirmwareErrorFlag; static Logging *logger; -static ICUDriver *primaryCrankDriver; - -static void cam_icu_width_callback(ICUDriver *icup) { - (void)icup; +static void cam_icu_width_callback(void *arg) { + (void)arg; hwHandleVvtCamSignal(TV_RISE); } -static void cam_icu_period_callback(ICUDriver *icup) { - (void)icup; +static void cam_icu_period_callback(void *arg) { + (void)arg; hwHandleVvtCamSignal(TV_FALL); } @@ -44,7 +42,7 @@ static void cam_icu_period_callback(ICUDriver *icup) { * that's hardware timer input capture IRQ entry point * 'width' events happens before the 'period' event */ -static void shaft_icu_width_callback(ICUDriver *icup) { +static void shaftWidthCallback(bool isPrimary) { if (!engine->hwTriggerInputEnabled) { return; } @@ -53,7 +51,6 @@ static void shaft_icu_width_callback(ICUDriver *icup) { // todo: start using real event time from HW event, not just software timer? if (hasFirmwareErrorFlag) return; - int isPrimary = icup == primaryCrankDriver; if (!isPrimary && !TRIGGER_SHAPE(needSecondTriggerInput)) { return; } @@ -64,14 +61,13 @@ static void shaft_icu_width_callback(ICUDriver *icup) { hwHandleShaftSignal(signal); } -static void shaft_icu_period_callback(ICUDriver *icup) { +static void shaftPeriodCallback(bool isPrimary) { if (!engine->hwTriggerInputEnabled) { return; } icuWidthPeriodCounter++; if (hasFirmwareErrorFlag) return; - int isPrimary = icup == primaryCrankDriver; if (!isPrimary && !TRIGGER_SHAPE(needSecondTriggerInput)) { return; } @@ -83,89 +79,31 @@ static void shaft_icu_period_callback(ICUDriver *icup) { hwHandleShaftSignal(signal); } -/** - * the main purpose of this configuration structure is to specify the input interrupt callbacks - */ -static ICUConfig shaft_icucfg = { ICU_INPUT_ACTIVE_LOW, - 100000, /* 100kHz ICU clock frequency. */ - shaft_icu_width_callback, - shaft_icu_period_callback, - NULL, - ICU_CHANNEL_1, - 0}; - -/** - * this is about VTTi and stuff kind of cam sensor - */ -static ICUConfig cam_icucfg = { ICU_INPUT_ACTIVE_LOW, - 100000, /* 100kHz ICU clock frequency. */ - cam_icu_width_callback, - cam_icu_period_callback, - NULL, - ICU_CHANNEL_1, - 0}; - -int turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft) { +void turnOnTriggerInputPin(const char *msg, int index, bool isVvtShaft) { brain_pin_e brainPin = isVvtShaft ? engineConfiguration->camInputs[index] : CONFIGB(triggerInputPins)[index]; - - - ICUConfig *icucfg; - if (brainPin == GPIO_UNASSIGNED) { - return -1; + return; } - //digital_input_s* input = startDigitalCapture("trigger", brainPin, true); + digital_input_s* input = startDigitalCapture("trigger", brainPin, true); if (isVvtShaft) { - icucfg = &shaft_icucfg; + input->setWidthCallback((VoidInt)(void*)shaftWidthCallback, NULL); + input->setPeriodCallback((VoidInt)(void*)shaftPeriodCallback, NULL); } else { - icucfg = &cam_icucfg; + void * arg = (void*) (index == 0); + input->setWidthCallback((VoidInt)(void*)shaftWidthCallback, arg); + input->setPeriodCallback((VoidInt)(void*)shaftPeriodCallback, arg); } - - // configure pin - turnOnCapturePin(msg, brainPin); - icucfg->channel = getInputCaptureChannel(brainPin); - - ICUDriver *driver = getInputCaptureDriver(msg, brainPin); - scheduleMsg(logger, "turnOnTriggerInputPin %s", hwPortname(brainPin)); - // todo: reuse 'setWaveReaderMode' method here? - if (driver != NULL) { - // todo: once http://forum.chibios.org/phpbb/viewtopic.php?f=16&t=1757 is fixed -// bool needWidthCallback = !CONFIG(useOnlyRisingEdgeForTrigger) || TRIGGER_SHAPE(useRiseEdge); -// shaft_icucfg.width_cb = needWidthCallback ? shaft_icu_width_callback : NULL; - -// bool needPeriodCallback = !CONFIG(useOnlyRisingEdgeForTrigger) || !TRIGGER_SHAPE(useRiseEdge); -// shaft_icucfg.period_cb = needPeriodCallback ? shaft_icu_period_callback : NULL; - - efiIcuStart(msg, driver, icucfg); - if (driver->state == ICU_READY) { - efiAssert(CUSTOM_ERR_ASSERT, driver != NULL, "ti: driver is NULL", -1); - efiAssert(CUSTOM_ERR_ASSERT, driver->state == ICU_READY, "ti: driver not ready", -1); - icuStartCapture(driver); // this would change state from READY to WAITING - icuEnableNotifications(driver); - } else { - // we would be here for example if same pin is used for multiple input capture purposes - firmwareError(CUSTOM_ERR_ICU_STATE, "ICU unexpected state [%s]", hwPortname(brainPin)); - } - } - return 0; } void turnOffTriggerInputPin(brain_pin_e brainPin) { - ICUDriver *driver = getInputCaptureDriver("trigger_off", brainPin); - if (driver != NULL) { - icuDisableNotifications(driver); - icuStopCapture(driver); - icuStop(driver); - scheduleMsg(logger, "turnOffTriggerInputPin %s", hwPortname(brainPin)); - turnOffCapturePin(brainPin); - } + removeWaveAnalyzerDriver("trigger", brainPin); } void setPrimaryChannel(brain_pin_e brainPin) { - primaryCrankDriver = getInputCaptureDriver("primary", brainPin); + } /*==========================================================================*/