From f96de7eef3f4fb316458decd7491d8717e886b3f Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 21 Apr 2017 16:52:02 -0400 Subject: [PATCH] refactoring - reducing GPIO complexity --- firmware/controllers/system/efiGpio.cpp | 35 +++++++++++++++++- firmware/controllers/system/efiGpio.h | 47 ++----------------------- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/firmware/controllers/system/efiGpio.cpp b/firmware/controllers/system/efiGpio.cpp index 05a70537a2..2a99ecb0eb 100644 --- a/firmware/controllers/system/efiGpio.cpp +++ b/firmware/controllers/system/efiGpio.cpp @@ -41,6 +41,28 @@ EnginePins::EnginePins() { } } +/** + * Sets the value of the pin. On this layer the value is assigned as is, without any conversion. + */ + +#if EFI_PROD_CODE \ + +#define setPinValue(outputPin, electricalValue, logicValue) \ + { \ + if ((outputPin)->currentLogicValue != (logicValue)) { \ + palWritePad((outputPin)->port, (outputPin)->pin, (electricalValue)); \ + (outputPin)->currentLogicValue = (logicValue); \ + } \ + } +#else /* EFI_PROD_CODE */ +#define setPinValue(outputPin, electricalValue, logicValue) \ + { \ + if ((outputPin)->currentLogicValue != (logicValue)) { \ + (outputPin)->currentLogicValue = (logicValue); \ + } \ + } +#endif /* EFI_PROD_CODE */ + bool EnginePins::stopPins() { bool result = false; for (int i = 0; i < IGNITION_PIN_COUNT; i++) { @@ -120,7 +142,18 @@ bool OutputPin::isInitialized() { } void OutputPin::setValue(int logicValue) { - doSetOutputPinValue2(this, logicValue); +#if EFI_PROD_CODE + if (port != GPIO_NULL) { + efiAssertVoid(modePtr!=NULL, "pin mode not initialized"); + pin_output_mode_e mode = *modePtr; + efiAssertVoid(mode <= OM_OPENDRAIN_INVERTED, "invalid pin_output_mode_e"); + int eValue = getElectricalValue(logicValue, mode); + setPinValue(this, eValue, logicValue); + } + +#else /* EFI_PROD_CODE */ + setPinValue(this, eValue, logicValue); +#endif /* EFI_PROD_CODE */ } bool OutputPin::getLogicValue() { diff --git a/firmware/controllers/system/efiGpio.h b/firmware/controllers/system/efiGpio.h index 911913323f..faee054025 100644 --- a/firmware/controllers/system/efiGpio.h +++ b/firmware/controllers/system/efiGpio.h @@ -32,10 +32,10 @@ public: void setDefaultPinState(pin_output_mode_e *defaultState); bool getLogicValue(); void unregister(); -#if EFI_PROD_CODE || defined(__DOXYGEN__) +#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) ioportid_t port; uint8_t pin; -#endif /* EFI_PROD_CODE */ +#endif /* EFI_GPIO_HARDWARE */ int8_t currentLogicValue; // 4 byte pointer is a bit of a memory waste here pin_output_mode_e *modePtr; @@ -129,27 +129,6 @@ public: */ #define getElectricalValue1(mode) ((mode) == OM_DEFAULT || (mode) == OM_OPENDRAIN) -/** - * Sets the value of the pin. On this layer the value is assigned as is, without any conversion. - */ - -#if EFI_PROD_CODE \ - -#define setPinValue(outputPin, electricalValue, logicValue) \ - { \ - if ((outputPin)->currentLogicValue != (logicValue)) { \ - palWritePad((outputPin)->port, (outputPin)->pin, (electricalValue)); \ - (outputPin)->currentLogicValue = (logicValue); \ - } \ - } -#else /* EFI_PROD_CODE */ -#define setPinValue(outputPin, electricalValue, logicValue) \ - { \ - if ((outputPin)->currentLogicValue != (logicValue)) { \ - (outputPin)->currentLogicValue = (logicValue); \ - } \ - } -#endif /* EFI_PROD_CODE */ #define getElectricalValue(logicalValue, mode) \ (logicalValue ? getElectricalValue1(mode) : getElectricalValue0(mode)) @@ -160,28 +139,6 @@ public: #define isPinAssigned(output) (true) #endif /* EFI_PROD_CODE */ -#define doSetOutputPinValue(pin, logicValue) doSetOutputPinValue2((&outputs[pin]), logicValue) - - -#if EFI_PROD_CODE -#define doSetOutputPinValue2(output, logicValue) { \ - if ((output)->port != GPIO_NULL) { \ - efiAssertVoid((output)->modePtr!=NULL, "pin mode not initialized"); \ - pin_output_mode_e mode = *(output)->modePtr; \ - efiAssertVoid(mode <= OM_OPENDRAIN_INVERTED, "invalid pin_output_mode_e"); \ - int eValue = getElectricalValue(logicValue, mode); \ - setPinValue(output, eValue, logicValue); \ - } \ - } -#else /* EFI_PROD_CODE */ - #define doSetOutputPinValue2(output, logicValue) { \ - pin_output_mode_e mode = OM_DEFAULT; \ - /* int eValue = getElectricalValue(logicValue, mode); */ \ - setPinValue(output, eValue, logicValue); \ - } -#endif /* EFI_PROD_CODE */ - - void turnPinHigh(NamedOutputPin *output); void turnPinLow(NamedOutputPin *output);