From c6a1d0cbe0119b13100dfb1698047b02e7d8daf3 Mon Sep 17 00:00:00 2001 From: dron0gus Date: Wed, 10 Apr 2019 02:31:10 +0300 Subject: [PATCH] gpios rework (#739) * Some more include fixes for linux * Pass cross-compile path through CROSS_COMPILE * Lower-upper case conversion fixes for linux * Rename unmarkPin to brain_pin_markUnused This is part of external gpiochip integration * Add gpio_pin prefix to markUsed and markUnused To distinguish from similar functions using brain_pin * pin_repository: add helpers this is part of gpio chips integration * efi_gpio: simplify getHwPin using brain_pin_is_onchip --- firmware/controllers/actuators/aux_pid.cpp | 2 +- .../actuators/electronic_throttle.cpp | 8 +- firmware/controllers/algo/rusefi_enums.h | 3 + firmware/controllers/injector_central.cpp | 2 +- firmware/controllers/system/efi_gpio.cpp | 15 ++-- firmware/hw_layer/can_hw.cpp | 4 +- firmware/hw_layer/digital_input_hw.cpp | 2 +- firmware/hw_layer/drivers/gpio/tle6240.c | 12 +-- firmware/hw_layer/drivers/gpio/tle8888.c | 12 +-- firmware/hw_layer/hardware.cpp | 10 +-- firmware/hw_layer/io_pins.cpp | 2 +- firmware/hw_layer/pin_repository.cpp | 86 ++++++++++--------- firmware/hw_layer/pin_repository.h | 8 +- firmware/hw_layer/trigger_input_icu.cpp | 2 +- 14 files changed, 90 insertions(+), 78 deletions(-) diff --git a/firmware/controllers/actuators/aux_pid.cpp b/firmware/controllers/actuators/aux_pid.cpp index 0f76023a93..6a23da4460 100644 --- a/firmware/controllers/actuators/aux_pid.cpp +++ b/firmware/controllers/actuators/aux_pid.cpp @@ -130,7 +130,7 @@ void startAuxPins(void) { void stopAuxPins(void) { for (int i = 0;i < AUX_PID_COUNT;i++) { - unmarkPin(activeConfiguration.auxPidPins[i]); + brain_pin_markUnused(activeConfiguration.auxPidPins[i]); } } diff --git a/firmware/controllers/actuators/electronic_throttle.cpp b/firmware/controllers/actuators/electronic_throttle.cpp index 0fd22bac60..242b6e1d0e 100644 --- a/firmware/controllers/actuators/electronic_throttle.cpp +++ b/firmware/controllers/actuators/electronic_throttle.cpp @@ -358,10 +358,10 @@ bool isETBRestartNeeded(void) { } void stopETBPins(void) { - unmarkPin(activeConfiguration.bc.etb1.controlPin1); - unmarkPin(activeConfiguration.bc.etb1.controlPin2); - unmarkPin(activeConfiguration.bc.etb1.directionPin1); - unmarkPin(activeConfiguration.bc.etb1.directionPin2); + brain_pin_markUnused(activeConfiguration.bc.etb1.controlPin1); + brain_pin_markUnused(activeConfiguration.bc.etb1.controlPin2); + brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin1); + brain_pin_markUnused(activeConfiguration.bc.etb1.directionPin2); } void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) { diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index cdf454df35..0370fdf1d2 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -790,6 +790,9 @@ typedef enum __attribute__ ((__packed__)) { } brain_pin_e; +/* Plase keep updating this define */ +#define BRAIN_PIN_LAST_ONCHIP GPIOH_15 + /** * https://rusefi.com//wiki/index.php?title=Manual:Debug_fields */ diff --git a/firmware/controllers/injector_central.cpp b/firmware/controllers/injector_central.cpp index 49ba66068c..b2e945bbae 100644 --- a/firmware/controllers/injector_central.cpp +++ b/firmware/controllers/injector_central.cpp @@ -237,7 +237,7 @@ void OutputPin::unregisterOutput(brain_pin_e oldPin, brain_pin_e newPin) { if (oldPin != GPIO_UNASSIGNED && oldPin != newPin) { scheduleMsg(logger, "unregistering %s", hwPortname(oldPin)); #if EFI_GPIO_HARDWARE || defined(__DOXYGEN__) - unmarkPin(oldPin); + brain_pin_markUnused(oldPin); port = NULL; #endif /* EFI_GPIO_HARDWARE */ } diff --git a/firmware/controllers/system/efi_gpio.cpp b/firmware/controllers/system/efi_gpio.cpp index f809bd2ed6..d28cc60568 100644 --- a/firmware/controllers/system/efi_gpio.cpp +++ b/firmware/controllers/system/efi_gpio.cpp @@ -474,14 +474,13 @@ const char *portname(ioportid_t GPIOx) { /** * this method returns the numeric part of pin name. For instance, for PC13 this would return '13' */ -ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin) { - if (brainPin == GPIO_UNASSIGNED || brainPin == GPIO_INVALID) - return EFI_ERROR_CODE; - if (brainPin < GPIOA_0 || brainPin > GPIOH_15) { - firmwareError(CUSTOM_ERR_INVALID_PIN, "%s: Invalid brain_pin_e: %d", msg, brainPin); - return EFI_ERROR_CODE; - } - return (brainPin - GPIOA_0) % PORT_SIZE; +ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin) +{ + if (brain_pin_is_onchip(brainPin)) + return (brainPin - GPIOA_0) % PORT_SIZE; + + firmwareError(CUSTOM_ERR_INVALID_PIN, "%s: Invalid on-chip brain_pin_e: %d", msg, brainPin); + return EFI_ERROR_CODE; } #else /* EFI_GPIO_HARDWARE */ diff --git a/firmware/hw_layer/can_hw.cpp b/firmware/hw_layer/can_hw.cpp index 9c75352fc8..b882cf1519 100644 --- a/firmware/hw_layer/can_hw.cpp +++ b/firmware/hw_layer/can_hw.cpp @@ -303,8 +303,8 @@ void enableFrankensoCan(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - unmarkPin(activeConfiguration.bc.canTxPin); - unmarkPin(activeConfiguration.bc.canRxPin); + brain_pin_markUnused(activeConfiguration.bc.canTxPin); + brain_pin_markUnused(activeConfiguration.bc.canRxPin); } void startCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { diff --git a/firmware/hw_layer/digital_input_hw.cpp b/firmware/hw_layer/digital_input_hw.cpp index dc31b3255b..97659a1b7f 100644 --- a/firmware/hw_layer/digital_input_hw.cpp +++ b/firmware/hw_layer/digital_input_hw.cpp @@ -232,7 +232,7 @@ void removeWaveAnalyzerDriver(const char *msg, brain_pin_e brainPin) { if (brainPin == GPIO_UNASSIGNED) { return; } - unmarkPin(brainPin); + brain_pin_markUnused(brainPin); ICUDriver *driver = getInputCaptureDriver(msg, brainPin); if (driver == NULL) { diff --git a/firmware/hw_layer/drivers/gpio/tle6240.c b/firmware/hw_layer/drivers/gpio/tle6240.c index 02b03a32ba..7c890fba28 100644 --- a/firmware/hw_layer/drivers/gpio/tle6240.c +++ b/firmware/hw_layer/drivers/gpio/tle6240.c @@ -200,12 +200,12 @@ static int tle6240_chip_init(struct tle6240_priv *chip) const struct tle6240_config *cfg = chip->cfg; /* mark pins used */ - ret = markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS"); + ret = gpio_pin_markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS"); if (cfg->reset.port != NULL) - ret |= markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST"); + ret |= gpio_pin_markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST"); for (n = 0; n < TLE6240_DIRECT_OUTPUTS; n++) if (cfg->direct_io[n].port) - ret |= markUsed(cfg->direct_io[n].port, cfg->direct_io[n].pad, DRIVER_NAME " DIRECT IO"); + ret |= gpio_pin_markUsed(cfg->direct_io[n].port, cfg->direct_io[n].pad, DRIVER_NAME " DIRECT IO"); if (ret) { ret = -1; @@ -289,12 +289,12 @@ static int tle6240_chip_init(struct tle6240_priv *chip) err_gpios: /* unmark pins */ - markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad); + gpio_pin_markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad); if (cfg->reset.port != NULL) - markUnused(cfg->reset.port, cfg->reset.pad); + gpio_pin_markUnused(cfg->reset.port, cfg->reset.pad); for (n = 0; n < TLE6240_DIRECT_OUTPUTS; n++) if (cfg->direct_io[n].port) - markUnused(cfg->direct_io[n].port, cfg->direct_io[n].pad); + gpio_pin_markUnused(cfg->direct_io[n].port, cfg->direct_io[n].pad); return ret; } diff --git a/firmware/hw_layer/drivers/gpio/tle8888.c b/firmware/hw_layer/drivers/gpio/tle8888.c index cde5729551..57bdd5607b 100644 --- a/firmware/hw_layer/drivers/gpio/tle8888.c +++ b/firmware/hw_layer/drivers/gpio/tle8888.c @@ -166,12 +166,12 @@ int tle8888_chip_init(void * data) int ret = 0; /* mark pins used */ // we do not initialize CS pin so we should not be marking it used -// ret = markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS"); +// ret = gpio_pin_markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS"); if (cfg->reset.port != NULL) - ret |= markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST"); + ret |= gpio_pin_markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST"); for (i = 0; i < TLE8888_DIRECT_MISC; i++) if (cfg->direct_io[i].port) - ret |= markUsed(cfg->direct_io[i].port, cfg->direct_io[i].pad, DRIVER_NAME " DIRECT IO"); + ret |= gpio_pin_markUsed(cfg->direct_io[i].port, cfg->direct_io[i].pad, DRIVER_NAME " DIRECT IO"); if (ret) { ret = -1; @@ -236,12 +236,12 @@ int tle8888_chip_init(void * data) err_gpios: /* unmark pins */ - //markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad); + //gpio_pin_markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad); if (cfg->reset.port != NULL) - markUnused(cfg->reset.port, cfg->reset.pad); + gpio_pin_markUnused(cfg->reset.port, cfg->reset.pad); for (i = 0; i < TLE8888_DIRECT_MISC; i++) if (cfg->direct_io[i].port) - markUnused(cfg->direct_io[i].port, cfg->direct_io[i].pad); + gpio_pin_markUnused(cfg->direct_io[i].port, cfg->direct_io[i].pad); return ret; } diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 188f6243cf..dcfb753794 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -244,7 +244,7 @@ void turnOnHardware(Logging *sharedLogger) { static void unregisterPin(brain_pin_e currentPin, brain_pin_e prevPin) { if (currentPin != prevPin) { - unmarkPin(prevPin); + brain_pin_markUnused(prevPin); } } @@ -253,9 +253,9 @@ void stopSpi(spi_device_e device) { if (!isSpiInitialized[device]) return; // not turned on isSpiInitialized[device] = false; - unmarkPin(getSckPin(device)); - unmarkPin(getMisoPin(device)); - unmarkPin(getMosiPin(device)); + brain_pin_markUnused(getSckPin(device)); + brain_pin_markUnused(getMisoPin(device)); + brain_pin_markUnused(getMosiPin(device)); #endif /* HAL_USE_SPI */ } @@ -420,7 +420,7 @@ void initHardware(Logging *l) { isBoardTestMode_b = (!efiReadPin(CONFIGB(boardTestModeJumperPin))); // we can now relese this pin, it is actually used as output sometimes - unmarkPin(CONFIGB(boardTestModeJumperPin)); + brain_pin_markUnused(CONFIGB(boardTestModeJumperPin)); } else { isBoardTestMode_b = false; } diff --git a/firmware/hw_layer/io_pins.cpp b/firmware/hw_layer/io_pins.cpp index 015ec3a4ca..54e71cd1ca 100644 --- a/firmware/hw_layer/io_pins.cpp +++ b/firmware/hw_layer/io_pins.cpp @@ -57,7 +57,7 @@ void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode) { scheduleMsg(&logger, "%s on %s%d", msg, portname(port), pin); - bool wasUsed = markUsed(port, pin, msg); + bool wasUsed = gpio_pin_markUsed(port, pin, msg); if (wasUsed) { return; } diff --git a/firmware/hw_layer/pin_repository.cpp b/firmware/hw_layer/pin_repository.cpp index 880a3e93e2..70493f3229 100644 --- a/firmware/hw_layer/pin_repository.cpp +++ b/firmware/hw_layer/pin_repository.cpp @@ -66,6 +66,11 @@ static int brainPin_to_index(brain_pin_e brainPin) return index; } +static brain_pin_e index_to_brainPin(int i) +{ + return (brain_pin_e)((int)GPIOA_0 + i); +} + PinRepository::PinRepository() { } @@ -181,16 +186,25 @@ static int getIndex(ioportid_t port, ioportmask_t pin) { bool brain_pin_is_onchip(brain_pin_e brainPin) { - if ((brainPin < GPIOA_0) || (brainPin > GPIOH_15)) + if ((brainPin < GPIOA_0) || (brainPin > BRAIN_PIN_LAST_ONCHIP)) return false; return true; } +bool brain_pin_is_ext(brain_pin_e brainPin) +{ + if (brainPin > BRAIN_PIN_LAST_ONCHIP) + return true; + + return false; +} + /** - * See also unmarkPin() + * See also brain_pin_markUnused() * @return true if this pin was already used, false otherwise */ + bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg) { int index; @@ -220,7 +234,34 @@ bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg) return false; } -bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg) { +/** + * See also brain_pin_markUsed() + */ + +void brain_pin_markUnused(brain_pin_e brainPin) +{ + int index; + + if (!initialized) { + firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized"); + return; + } + + index = brainPin_to_index(brainPin); + if (index < 0) + return; + + if (PIN_USED[index] != NULL) + totalPinsUsed--; + PIN_USED[index] = NULL; +} + +/** + * Marks on-chip gpio port-pin as used. Works only for on-chip gpios + * To be replaced with brain_pin_markUsed later + */ + +bool gpio_pin_markUsed(ioportid_t port, ioportmask_t pin, const char *msg) { if (!initialized) { firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized"); return false; @@ -242,28 +283,11 @@ bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg) { } /** - * See also markUsed() + * Marks on-chip gpio port-pin as UNused. Works only for on-chip gpios + * To be replaced with brain_pin_markUnused later */ -void brain_pin_markUnused(brain_pin_e brainPin) -{ - int index; - - if (!initialized) { - firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized"); - return; - } - - index = brainPin_to_index(brainPin); - if (index < 0) - return; - - if (PIN_USED[index] != NULL) - totalPinsUsed--; - PIN_USED[index] = NULL; -} - -void markUnused(ioportid_t port, ioportmask_t pin) { +void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin) { if (!initialized) { firmwareError(CUSTOM_ERR_PIN_REPO, "repository not initialized"); return; @@ -285,20 +309,4 @@ const char *getPinFunction(brain_input_pin_e brainPin) { return PIN_USED[index]; } - -void unmarkPin(brain_pin_e brainPin) { - if (brainPin == GPIO_UNASSIGNED) { - return; - } - ioportid_t port = getHwPort("unmark", brainPin); - ioportmask_t pin = getHwPin("unmark", brainPin); - - int index = getIndex(port, pin); - - if (PIN_USED[index] != NULL) { - PIN_USED[index] = NULL; - totalPinsUsed--; - } -} - #endif diff --git a/firmware/hw_layer/pin_repository.h b/firmware/hw_layer/pin_repository.h index 31081d06d0..917b3d2d83 100644 --- a/firmware/hw_layer/pin_repository.h +++ b/firmware/hw_layer/pin_repository.h @@ -29,11 +29,13 @@ class PinRepository { void initPinRepository(void); EXTERNC bool brain_pin_is_onchip(brain_pin_e brainPin); -EXTERNC bool markUsed(ioportid_t port, ioportmask_t pin, const char *msg); +EXTERNC bool brain_pin_is_ext(brain_pin_e brainPin); EXTERNC bool brain_pin_markUsed(brain_pin_e brainPin, const char *msg); -EXTERNC void markUnused(ioportid_t port, ioportmask_t pin); EXTERNC void brain_pin_markUnused(brain_pin_e brainPin); const char * getPinFunction(brain_input_pin_e brainPin); -void unmarkPin(brain_pin_e brainPin); + +/* For on-chip gpios only */ +EXTERNC bool gpio_pin_markUsed(ioportid_t port, ioportmask_t pin, const char *msg); +EXTERNC void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin); #endif /* PIN_REPOSITORY_H_ */ diff --git a/firmware/hw_layer/trigger_input_icu.cpp b/firmware/hw_layer/trigger_input_icu.cpp index e12e89b641..8e2d3a36f5 100644 --- a/firmware/hw_layer/trigger_input_icu.cpp +++ b/firmware/hw_layer/trigger_input_icu.cpp @@ -146,7 +146,7 @@ static void turnOffTriggerInputPin(brain_pin_e hwPin) { icuStopCapture(driver); icuStop(driver); scheduleMsg(logger, "turnOffTriggerInputPin %s", hwPortname(hwPin)); - unmarkPin(hwPin); + brain_pin_markUnused(hwPin); } }