From 6f9ff112132b774eda92ee5f109cc279683a042a Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 21 Apr 2017 18:11:36 -0400 Subject: [PATCH] refactoring - reducing GPIO complexity --- firmware/console/status_loop.cpp | 10 ++--- firmware/controllers/alternatorController.cpp | 4 +- firmware/controllers/core/fsio_impl.cpp | 2 +- firmware/controllers/electronic_throttle.cpp | 5 +-- firmware/controllers/injector_central.cpp | 6 +-- firmware/controllers/system/efiGpio.cpp | 41 +++++++++---------- firmware/controllers/system/efiGpio.h | 3 +- firmware/controllers/tachometer.cpp | 2 +- .../controllers/trigger/trigger_decoder.cpp | 2 +- firmware/development/trigger_emulator.cpp | 6 +-- firmware/hw_layer/HIP9011.cpp | 4 +- firmware/hw_layer/pwm_generator.cpp | 4 +- firmware/hw_layer/sensors/CJ125.cpp | 2 +- 13 files changed, 40 insertions(+), 51 deletions(-) diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 37f32bb264..8d144015c0 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -546,17 +546,15 @@ static THD_WORKING_AREA(blinkingStack, 128); static OutputPin *leds[] = { &enginePins.warningPin, &enginePins.runningPin, &enginePins.checkEnginePin, &enginePins.errorLedPin, &enginePins.communicationPin, &enginePins.checkEnginePin }; -extern pin_output_mode_e DEFAULT_OUTPUT; - static void initStatusLeds(void) { - enginePins.communicationPin.initPin("led: comm status", engineConfiguration->communicationPin, &DEFAULT_OUTPUT); + enginePins.communicationPin.initPin("led: comm status", engineConfiguration->communicationPin); // we initialize this here so that we can blink it on start-up - enginePins.checkEnginePin.initPin("MalfunctionIndicator", boardConfiguration->malfunctionIndicatorPin, &DEFAULT_OUTPUT); + enginePins.checkEnginePin.initPin("MalfunctionIndicator", boardConfiguration->malfunctionIndicatorPin); #if EFI_WARNING_LED || defined(__DOXYGEN__) - enginePins.warningPin.initPin("led: warning status", LED_WARNING_BRAIN_PIN, &DEFAULT_OUTPUT); - enginePins.runningPin.initPin("led: running status", engineConfiguration->runningPin, &DEFAULT_OUTPUT); + enginePins.warningPin.initPin("led: warning status", LED_WARNING_BRAIN_PIN); + enginePins.runningPin.initPin("led: running status", engineConfiguration->runningPin); #endif /* EFI_WARNING_LED */ } diff --git a/firmware/controllers/alternatorController.cpp b/firmware/controllers/alternatorController.cpp index 75cfcda125..787bd1fc78 100644 --- a/firmware/controllers/alternatorController.cpp +++ b/firmware/controllers/alternatorController.cpp @@ -25,7 +25,6 @@ EXTERN_ENGINE static Logging *logger; -extern pin_output_mode_e DEFAULT_OUTPUT; int alternatorPidResetCounter = 0; static SimplePwm alternatorControl; @@ -172,8 +171,7 @@ void initAlternatorCtrl(Logging *sharedLogger) { return; if (boardConfiguration->onOffAlternatorLogic) { - outputPinRegisterExt2("on/off alternator", &enginePins.alternatorPin, boardConfiguration->alternatorControlPin, - &DEFAULT_OUTPUT); + enginePins.alternatorPin.initPin("on/off alternator", boardConfiguration->alternatorControlPin); } else { startSimplePwmExt(&alternatorControl, "Alternator control", boardConfiguration->alternatorControlPin, diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index 5bca3b8ffb..f561525b62 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -504,7 +504,7 @@ void initFsioImpl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_S) { if (brainPin != GPIO_UNASSIGNED) { int frequency = boardConfiguration->fsioFrequency[i]; if (frequency == 0) { - outputPinRegisterExt2(getGpioPinName(i), &enginePins.fsioOutputs[i], boardConfiguration->fsioPins[i], &defa); + enginePins.fsioOutputs[i].initPin(getGpioPinName(i), boardConfiguration->fsioPins[i], &defa); } else { startSimplePwmExt(&fsioPwm[i], "FSIOpwm", brainPin, &enginePins.fsioOutputs[i], frequency, 0.5f, applyPinState); } diff --git a/firmware/controllers/electronic_throttle.cpp b/firmware/controllers/electronic_throttle.cpp index 3d5d07abfd..d08bc6a223 100644 --- a/firmware/controllers/electronic_throttle.cpp +++ b/firmware/controllers/electronic_throttle.cpp @@ -43,7 +43,6 @@ #include "pwm_generator.h" #define ETB_FREQ 400 -extern pin_output_mode_e DEFAULT_OUTPUT; static LoggingWithStorage logger("ETB"); /** @@ -171,8 +170,8 @@ void startETBPins(void) { 0.80, applyPinState); - outputPinRegisterExt2("etb dir open", &outputDirectionOpen, boardConfiguration->etbDirectionPin1, &DEFAULT_OUTPUT); - outputPinRegisterExt2("etb dir close", &outputDirectionClose, boardConfiguration->etbDirectionPin2, &DEFAULT_OUTPUT); + outputDirectionOpen.initPin("etb dir open", boardConfiguration->etbDirectionPin1); + outputDirectionClose.initPin("etb dir close", boardConfiguration->etbDirectionPin2); } void initElectronicThrottle(void) { diff --git a/firmware/controllers/injector_central.cpp b/firmware/controllers/injector_central.cpp index a168060755..d4794c6db6 100644 --- a/firmware/controllers/injector_central.cpp +++ b/firmware/controllers/injector_central.cpp @@ -251,13 +251,13 @@ void startIgnitionPins(void) { NamedOutputPin *output = &enginePins.coils[i]; // todo: we need to check if mode has changed if (boardConfiguration->ignitionPins[i] != activeConfiguration.bc.ignitionPins[i]) { - outputPinRegisterExt2(output->name, output, boardConfiguration->ignitionPins[i], + output->initPin(output->name, boardConfiguration->ignitionPins[i], &boardConfiguration->ignitionPinMode); } } // todo: we need to check if mode has changed if (engineConfiguration->dizzySparkOutputPin != activeConfiguration.dizzySparkOutputPin) { - outputPinRegisterExt2("dizzy tach", &enginePins.dizzyOutput, engineConfiguration->dizzySparkOutputPin, + enginePins.dizzyOutput.initPin("dizzy tach", engineConfiguration->dizzySparkOutputPin, &engineConfiguration->dizzySparkOutputPinMode); } @@ -270,7 +270,7 @@ void startInjectionPins(void) { // todo: we need to check if mode has changed if (engineConfiguration->bc.injectionPins[i] != activeConfiguration.bc.injectionPins[i]) { - outputPinRegisterExt2(output->name, output, boardConfiguration->injectionPins[i], + output->initPin(output->name, boardConfiguration->injectionPins[i], &boardConfiguration->injectionPinMode); } } diff --git a/firmware/controllers/system/efiGpio.cpp b/firmware/controllers/system/efiGpio.cpp index f449fa9315..6ea54bb331 100644 --- a/firmware/controllers/system/efiGpio.cpp +++ b/firmware/controllers/system/efiGpio.cpp @@ -20,7 +20,7 @@ EXTERN_ENGINE; EnginePins enginePins; extern LoggingWithStorage sharedLogger; -static pin_output_mode_e OUTPUT_MODE_DEFAULT = OM_DEFAULT; +static pin_output_mode_e DEFAULT_OUTPUT = OM_DEFAULT; static const char *sparkNames[IGNITION_PIN_COUNT] = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "cA", "cB", "cD"}; @@ -125,7 +125,7 @@ void IgnitionOutputPin::reset() { } OutputPin::OutputPin() { - modePtr = &OUTPUT_MODE_DEFAULT; + modePtr = &DEFAULT_OUTPUT; #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) port = NULL; pin = 0; @@ -173,9 +173,6 @@ void OutputPin::setDefaultPinState(pin_output_mode_e *outputMode) { setValue(false); // initial state } -pin_output_mode_e DEFAULT_OUTPUT = OM_DEFAULT; -pin_output_mode_e OPENDRAIN_OUTPUT = OM_OPENDRAIN; - void initOutputPins(void) { #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) /** @@ -186,17 +183,17 @@ void initOutputPins(void) { // memset(&outputs, 0, sizeof(outputs)); #if HAL_USE_SPI || defined(__DOXYGEN__) - outputPinRegisterExt2("spi CS5", &enginePins.sdCsPin, boardConfiguration->sdCardCsPin, &DEFAULT_OUTPUT); + enginePins.sdCsPin.initPin("spi CS5", boardConfiguration->sdCardCsPin); #endif /* HAL_USE_SPI */ // todo: should we move this code closer to the fuel pump logic? - outputPinRegisterExt2("fuel pump relay", &enginePins.fuelPumpRelay, boardConfiguration->fuelPumpPin, &DEFAULT_OUTPUT); + enginePins.fuelPumpRelay.initPin("fuel pump relay", boardConfiguration->fuelPumpPin); - outputPinRegisterExt2("main relay", &enginePins.mainRelay, boardConfiguration->mainRelayPin, &boardConfiguration->mainRelayPinMode); + enginePins.mainRelay.initPin("main relay", boardConfiguration->mainRelayPin, &boardConfiguration->mainRelayPinMode); - outputPinRegisterExt2("fan relay", &enginePins.fanRelay, boardConfiguration->fanPin, &DEFAULT_OUTPUT); - outputPinRegisterExt2("o2 heater", &enginePins.o2heater, boardConfiguration->o2heaterPin, &DEFAULT_OUTPUT); - outputPinRegisterExt2("A/C relay", &enginePins.acRelay, boardConfiguration->acRelayPin, &boardConfiguration->acRelayPinMode); + enginePins.fanRelay.initPin("fan relay", boardConfiguration->fanPin); + enginePins.o2heater.initPin("o2 heater", boardConfiguration->o2heaterPin); + enginePins.acRelay.initPin("A/C relay", boardConfiguration->acRelayPin, &boardConfiguration->acRelayPinMode); // digit 1 /* @@ -229,11 +226,11 @@ void initOutputPins(void) { #endif /* EFI_GPIO_HARDWARE */ } -void OutputPin::initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e *outputMode) { - outputPinRegisterExt2(msg, this, brainPin, outputMode); +void OutputPin::initPin(const char *msg, brain_pin_e brainPin) { + initPin(msg, brainPin, &DEFAULT_OUTPUT); } -void outputPinRegisterExt2(const char *msg, OutputPin *outputPin, brain_pin_e brainPin, pin_output_mode_e *outputMode) { +void OutputPin::initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e *outputMode) { #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) if (brainPin == GPIO_UNASSIGNED) return; @@ -245,7 +242,7 @@ void outputPinRegisterExt2(const char *msg, OutputPin *outputPin, brain_pin_e br */ if (port == GPIO_NULL) { // that's for GRIO_NONE - outputPin->port = port; + this->port = port; return; } @@ -256,29 +253,29 @@ void outputPinRegisterExt2(const char *msg, OutputPin *outputPin, brain_pin_e br /** * @brief Initialize the hardware output pin while also assigning it a logical name */ - if (outputPin->port != NULL && (outputPin->port != port || outputPin->pin != pin)) { + if (this->port != NULL && (this->port != port || this->pin != pin)) { /** * here we check if another physical pin is already assigned to this logical output */ // todo: need to clear '&outputs' in io_pins.c - warning(CUSTOM_OBD_PIN_CONFLICT, "outputPin [%s] already assigned to %x%d", msg, outputPin->port, outputPin->pin); + warning(CUSTOM_OBD_PIN_CONFLICT, "outputPin [%s] already assigned to %x%d", msg, this->port, this->pin); engine->withError = true; return; } - outputPin->currentLogicValue = INITIAL_PIN_STATE; - outputPin->port = port; - outputPin->pin = pin; + this->currentLogicValue = INITIAL_PIN_STATE; + this->port = port; + this->pin = pin; mySetPadMode(msg, port, pin, mode); - outputPin->setDefaultPinState(outputMode); + setDefaultPinState(outputMode); #endif /* EFI_GPIO_HARDWARE */ } #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) void initPrimaryPins(void) { - outputPinRegisterExt2("led: ERROR status", &enginePins.errorLedPin, LED_ERROR_BRAIN_PIN, &DEFAULT_OUTPUT); + enginePins.errorLedPin.initPin("led: ERROR status", LED_ERROR_BRAIN_PIN); } /** diff --git a/firmware/controllers/system/efiGpio.h b/firmware/controllers/system/efiGpio.h index 1170272aec..17640a8a48 100644 --- a/firmware/controllers/system/efiGpio.h +++ b/firmware/controllers/system/efiGpio.h @@ -33,6 +33,7 @@ public: bool getLogicValue(); void unregister(); bool isPinAssigned(); + void initPin(const char *msg, brain_pin_e brainPin); void initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e *outputMode); #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) @@ -143,8 +144,6 @@ public: void turnPinHigh(NamedOutputPin *output); void turnPinLow(NamedOutputPin *output); -void outputPinRegisterExt2(const char *msg, OutputPin *output, brain_pin_e brainPin, pin_output_mode_e *outputMode); - #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) ioportmask_t getHwPin(brain_pin_e brainPin); diff --git a/firmware/controllers/tachometer.cpp b/firmware/controllers/tachometer.cpp index 601e2c0ea5..948963f792 100644 --- a/firmware/controllers/tachometer.cpp +++ b/firmware/controllers/tachometer.cpp @@ -35,7 +35,7 @@ void initTachometer(void) { return; } - outputPinRegisterExt2("analog tach output", &enginePins.tachOut, boardConfiguration->tachOutputPin, &boardConfiguration->tachOutputPinMode); + enginePins.tachOut.initPin("analog tach output", boardConfiguration->tachOutputPin, &boardConfiguration->tachOutputPinMode); addTriggerEventListener(tachSignalCallback, "tach", engine); } diff --git a/firmware/controllers/trigger/trigger_decoder.cpp b/firmware/controllers/trigger/trigger_decoder.cpp index 3dac42197e..761f359954 100644 --- a/firmware/controllers/trigger/trigger_decoder.cpp +++ b/firmware/controllers/trigger/trigger_decoder.cpp @@ -727,7 +727,7 @@ void initTriggerDecoderLogger(Logging *sharedLogger) { void initTriggerDecoder(void) { #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) - outputPinRegisterExt2("trg_err", &enginePins.triggerDecoderErrorPin, boardConfiguration->triggerErrorPin, + enginePins.triggerDecoderErrorPin.initPin("trg_err", boardConfiguration->triggerErrorPin, &boardConfiguration->triggerErrorPinMode); #endif /* EFI_GPIO_HARDWARE */ } diff --git a/firmware/development/trigger_emulator.cpp b/firmware/development/trigger_emulator.cpp index 905448499e..947f3c8862 100644 --- a/firmware/development/trigger_emulator.cpp +++ b/firmware/development/trigger_emulator.cpp @@ -41,13 +41,13 @@ void initTriggerEmulator(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_S) { #if EFI_PROD_CODE // todo: refactor, make this a loop - outputPinRegisterExt2("trg emulator ch1", triggerSignal.outputPins[0], boardConfiguration->triggerSimulatorPins[0], + triggerSignal.outputPins[0]->initPin("trg emulator ch1", boardConfiguration->triggerSimulatorPins[0], &boardConfiguration->triggerSimulatorPinModes[0]); - outputPinRegisterExt2("trg emulator ch2", triggerSignal.outputPins[1], boardConfiguration->triggerSimulatorPins[1], + triggerSignal.outputPins[1]->initPin("trg emulator ch2", boardConfiguration->triggerSimulatorPins[1], &boardConfiguration->triggerSimulatorPinModes[1]); - outputPinRegisterExt2("trg emulator ch3", triggerSignal.outputPins[2], boardConfiguration->triggerSimulatorPins[2], + triggerSignal.outputPins[2]->initPin("trg emulator ch3", boardConfiguration->triggerSimulatorPins[2], &boardConfiguration->triggerSimulatorPinModes[2]); #endif /* EFI_PROD_CODE */ diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index 57231dccc2..c322e922ca 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -437,9 +437,9 @@ void initHip9011(Logging *sharedLogger) { hipSpiCfg.ssport = getHwPort(boardConfiguration->hip9011CsPin); hipSpiCfg.sspad = getHwPin(boardConfiguration->hip9011CsPin); - outputPinRegisterExt2("hip int/hold", &intHold, boardConfiguration->hip9011IntHoldPin, + intHold.initPin("hip int/hold", boardConfiguration->hip9011IntHoldPin, &boardConfiguration->hip9011IntHoldPinMode); - outputPinRegisterExt2("hip CS", &enginePins.hipCs, boardConfiguration->hip9011CsPin, + enginePins.hipCs.initPin("hip CS", boardConfiguration->hip9011CsPin, &boardConfiguration->hip9011CsPinMode); scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver"); diff --git a/firmware/hw_layer/pwm_generator.cpp b/firmware/hw_layer/pwm_generator.cpp index 5d2f976efc..3df2997b15 100644 --- a/firmware/hw_layer/pwm_generator.cpp +++ b/firmware/hw_layer/pwm_generator.cpp @@ -50,12 +50,10 @@ void startSimplePwm(PwmConfig *state, const char *msg, OutputPin *output, float state->weComplexInit(msg, 2, switchTimes, 1, pinStates, NULL, stateChangeCallback); } -extern pin_output_mode_e DEFAULT_OUTPUT; - void startSimplePwmExt(PwmConfig *state, const char *msg, brain_pin_e brainPin, OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback) { - outputPinRegisterExt2(msg, output, brainPin, &DEFAULT_OUTPUT); + output->initPin(msg, brainPin); startSimplePwm(state, msg, output, frequency, dutyCycle, stateChangeCallback); } diff --git a/firmware/hw_layer/sensors/CJ125.cpp b/firmware/hw_layer/sensors/CJ125.cpp index a334344dd7..bcf1f95d6f 100644 --- a/firmware/hw_layer/sensors/CJ125.cpp +++ b/firmware/hw_layer/sensors/CJ125.cpp @@ -110,7 +110,7 @@ void initCJ125(Logging *sharedLogger) { driver = getSpiDevice(engineConfiguration->cj125SpiDevice); - outputPinRegisterExt2("cj125 CS", &cj125Cs, boardConfiguration->cj125CsPin, + cj125Cs.initPin("cj125 CS", boardConfiguration->cj125CsPin, &engineConfiguration->cj125CsPinMode); if (boardConfiguration->wboHeaterPin != GPIO_UNASSIGNED) {