From 83cca6ec7abb74cbf384ba815d34c12e69d0ed85 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 25 Apr 2020 21:07:59 -0700 Subject: [PATCH] allow overriding configuration (#1341) * config overrides * proteus too * comment * remove wno-error * comment --- .../microrusefi/board_configuration.cpp | 10 ++++--- .../boards/proteus/board_configuration.cpp | 15 ++++++----- .../controllers/algo/engine_configuration.cpp | 5 ++++ .../controllers/algo/engine_configuration.h | 20 ++++++++++++++ firmware/hw_layer/drivers/can/can_hw.cpp | 26 +++++++++---------- 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/firmware/config/boards/microrusefi/board_configuration.cpp b/firmware/config/boards/microrusefi/board_configuration.cpp index 08365409e8..8d4e14f1d8 100644 --- a/firmware/config/boards/microrusefi/board_configuration.cpp +++ b/firmware/config/boards/microrusefi/board_configuration.cpp @@ -25,10 +25,13 @@ EXTERN_ENGINE; +static const ConfigOverrides configOverrides = { + .canTxPin = GPIOB_6, + .canRxPin = GPIOB_12, +}; -static void setupCanPins() { - engineConfiguration->canTxPin = GPIOB_6; - engineConfiguration->canRxPin = GPIOB_12; +const ConfigOverrides& getConfigOverrides() { + return configOverrides; } static void setInjectorPins() { @@ -189,7 +192,6 @@ void setBoardConfigurationOverrides(void) { setupVbatt(); setupTle8888(); setupEtb(); - setupCanPins(); // MRE has a special main relay control low side pin // rusEfi firmware is totally not involved with main relay control on microRusEfi board diff --git a/firmware/config/boards/proteus/board_configuration.cpp b/firmware/config/boards/proteus/board_configuration.cpp index 6851b41977..bdf5618d17 100644 --- a/firmware/config/boards/proteus/board_configuration.cpp +++ b/firmware/config/boards/proteus/board_configuration.cpp @@ -43,6 +43,15 @@ static const brain_pin_e ignPins[] = { GPIOG_2, }; +static const ConfigOverrides configOverrides = { + .canTxPin = GPIOD_1, + .canRxPin = GPIOD_0, +}; + +const ConfigOverrides& getConfigOverrides() { + return configOverrides; +} + static void setInjectorPins() { copyArray(engineConfiguration->injectionPins, injPins); engineConfiguration->injectionPinMode = OM_DEFAULT; @@ -106,11 +115,6 @@ static void setupEtb() { engineConfiguration->etbFreq = 800; } -static void setupCanPins() { - engineConfiguration->canTxPin = GPIOD_1; - engineConfiguration->canRxPin = GPIOD_0; -} - static void setupDefaultSensorInputs() { // trigger inputs // Digital channel 1 as default - others not set @@ -162,7 +166,6 @@ void setBoardConfigurationOverrides(void) { setLedPins(); setupVbatt(); setupEtb(); - setupCanPins(); // "required" hardware is done - set some reasonable defaults setupDefaultSensorInputs(); diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 15b89e535c..041da2186b 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -1429,3 +1429,8 @@ void copyTimingTable(ignition_table_t const source, ignition_table_t destination } } +static const ConfigOverrides defaultConfigOverrides{}; +// This symbol is weak so that a board_configuration.cpp file can override it +__attribute__((weak)) const ConfigOverrides& getConfigOverrides() { + return defaultConfigOverrides; +} diff --git a/firmware/controllers/algo/engine_configuration.h b/firmware/controllers/algo/engine_configuration.h index 5b98393966..7e9ceb194b 100644 --- a/firmware/controllers/algo/engine_configuration.h +++ b/firmware/controllers/algo/engine_configuration.h @@ -66,3 +66,23 @@ typedef void (*configuration_callback_t)(engine_configuration_s*); void resetConfigurationExt(Logging * logger, configuration_callback_t boardCallback, engine_type_e engineType DECLARE_ENGINE_PARAMETER_SUFFIX); void resetConfigurationExt(Logging * logger, engine_type_e engineType DECLARE_ENGINE_PARAMETER_SUFFIX); #endif /* __cplusplus */ + +struct ConfigOverrides { + // CAN pinout + brain_pin_e canTxPin; + brain_pin_e canRxPin; +}; + +const ConfigOverrides& getConfigOverrides(); + +// If the overide value is default initialized +// Use the value from config if not overriden +// Otherwise use the override +// the == decltype(CONFIG(__x__)){} part means +// - take the type of the corresponding config field +// - default construct (ie, 0) an object of the corresponding type +// - check if the override value matches that (GPIO_UNASSIGNED, SPI_NONE, etc) +// If it matches, that field hasn't been overridden, so read from the config. +#define CONFIG_OVERRIDE(__x__) ( \ + (( getConfigOverrides().__x__ ) == decltype(CONFIG(__x__)){}) \ + ? (CONFIG( __x__ )) : ( getConfigOverrides().__x__ )) diff --git a/firmware/hw_layer/drivers/can/can_hw.cpp b/firmware/hw_layer/drivers/can/can_hw.cpp index be36c7f367..6145c15e2f 100644 --- a/firmware/hw_layer/drivers/can/can_hw.cpp +++ b/firmware/hw_layer/drivers/can/can_hw.cpp @@ -77,7 +77,7 @@ public: } void ThreadTask() override { - CANDriver* device = detectCanDevice(CONFIG(canRxPin), CONFIG(canTxPin)); + CANDriver* device = detectCanDevice(CONFIG_OVERRIDE(canRxPin), CONFIG_OVERRIDE(canTxPin)); if (!device) { warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN configuration issue"); @@ -117,8 +117,8 @@ static void canInfo(void) { scheduleMsg(&logger, "CAN SLAVE MODE"); #endif - scheduleMsg(&logger, "CAN TX %s", hwPortname(CONFIG(canTxPin))); - scheduleMsg(&logger, "CAN RX %s", hwPortname(CONFIG(canRxPin))); + scheduleMsg(&logger, "CAN TX %s", hwPortname(CONFIG_OVERRIDE(canTxPin))); + scheduleMsg(&logger, "CAN RX %s", hwPortname(CONFIG_OVERRIDE(canRxPin))); scheduleMsg(&logger, "type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType, boolToString(engineConfiguration->canReadEnabled), boolToString(engineConfiguration->canWriteEnabled), engineConfiguration->canSleepPeriodMs); @@ -151,16 +151,16 @@ void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } void startCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - efiSetPadMode("CAN TX", CONFIG(canTxPin), PAL_MODE_ALTERNATE(EFI_CAN_TX_AF)); - efiSetPadMode("CAN RX", CONFIG(canRxPin), PAL_MODE_ALTERNATE(EFI_CAN_RX_AF)); + efiSetPadMode("CAN TX", CONFIG_OVERRIDE(canTxPin), PAL_MODE_ALTERNATE(EFI_CAN_TX_AF)); + efiSetPadMode("CAN RX", CONFIG_OVERRIDE(canRxPin), PAL_MODE_ALTERNATE(EFI_CAN_RX_AF)); } void initCan(void) { addConsoleAction("caninfo", canInfo); isCanEnabled = - (CONFIG(canTxPin) != GPIO_UNASSIGNED) && // both pins are set... - (CONFIG(canRxPin) != GPIO_UNASSIGNED) && + (CONFIG_OVERRIDE(canTxPin) != GPIO_UNASSIGNED) && // both pins are set... + (CONFIG_OVERRIDE(canRxPin) != GPIO_UNASSIGNED) && (CONFIG(canWriteEnabled) || CONFIG(canReadEnabled)) ; // ...and either read or write is enabled // nothing to do if we aren't enabled... @@ -169,13 +169,13 @@ void initCan(void) { } // Validate pins - if (!isValidCanTxPin(CONFIG(canTxPin))) { - firmwareError(CUSTOM_OBD_70, "invalid CAN TX %s", hwPortname(CONFIG(canTxPin))); + if (!isValidCanTxPin(CONFIG_OVERRIDE(canTxPin))) { + firmwareError(CUSTOM_OBD_70, "invalid CAN TX %s", hwPortname(CONFIG_OVERRIDE(canTxPin))); return; } - if (!isValidCanRxPin(CONFIG(canRxPin))) { - firmwareError(CUSTOM_OBD_70, "invalid CAN RX %s", hwPortname(CONFIG(canRxPin))); + if (!isValidCanRxPin(CONFIG_OVERRIDE(canRxPin))) { + firmwareError(CUSTOM_OBD_70, "invalid CAN RX %s", hwPortname(CONFIG_OVERRIDE(canRxPin))); return; } @@ -190,8 +190,8 @@ void initCan(void) { // Plumb CAN device to tx system CanTxMessage::setDevice(detectCanDevice( - CONFIG(canRxPin), - CONFIG(canTxPin) + CONFIG_OVERRIDE(canRxPin), + CONFIG_OVERRIDE(canTxPin) )); // fire up threads, as necessary