From bd410d0c1be46834fab576f05d3f1953b1dc4929 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sat, 14 Aug 2021 04:35:43 -0400 Subject: [PATCH] No pins should be restarted for no reason during config change #3151 --- firmware/hw_layer/drivers/can/can_hw.cpp | 54 +++++++++++++----------- firmware/hw_layer/hardware.cpp | 8 ++-- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/firmware/hw_layer/drivers/can/can_hw.cpp b/firmware/hw_layer/drivers/can/can_hw.cpp index 684aafdf8f..e1cc09a0e9 100644 --- a/firmware/hw_layer/drivers/can/can_hw.cpp +++ b/firmware/hw_layer/drivers/can/can_hw.cpp @@ -196,33 +196,18 @@ void enableFrankensoCan(DECLARE_ENGINE_PARAMETER_SIGNATURE) { engineConfiguration->canReadEnabled = false; } -// todo: we usually use 'activeConfiguration' for 'stopPin' why this unusual code here? -// this is related to #1375 -static brain_pin_e currentTxPin = GPIO_UNASSIGNED; -static brain_pin_e currentRxPin = GPIO_UNASSIGNED; - void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - efiSetPadUnused(currentTxPin); - efiSetPadUnused(currentRxPin); + if (isConfigurationChanged(canTxPin)) { + efiSetPadUnused(activeConfiguration.canTxPin); + } + + if (isConfigurationChanged(canRxPin)) { + efiSetPadUnused(activeConfiguration.canRxPin); + } } +// at the moment we support only very limited runtime configuration change, still not supporting online CAN toggle void startCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - // Store pins so we can disable later - currentTxPin = CONFIG(canTxPin); - currentRxPin = CONFIG(canRxPin); - - efiSetPadMode("CAN TX", currentTxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF)); - efiSetPadMode("CAN RX", currentRxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF)); -} - -void initCan(void) { - addConsoleAction("caninfo", canInfo); - - isCanEnabled = - (isBrainPinValid(CONFIG(canTxPin))) && // both pins are set... - (isBrainPinValid(CONFIG(canRxPin))) && - (CONFIG(canWriteEnabled) || CONFIG(canReadEnabled)) ; // ...and either read or write is enabled - // nothing to do if we aren't enabled... if (!isCanEnabled) { return; @@ -239,6 +224,27 @@ void initCan(void) { return; } + if (isConfigurationChanged(canTxPin)) { + efiSetPadMode("CAN TX", CONFIG(canTxPin), PAL_MODE_ALTERNATE(EFI_CAN_TX_AF)); + } + if (isConfigurationChanged(canRxPin)) { + efiSetPadMode("CAN RX", CONFIG(canRxPin), PAL_MODE_ALTERNATE(EFI_CAN_RX_AF)); + } +} + +void initCan(void) { + addConsoleAction("caninfo", canInfo); + + isCanEnabled = + (isBrainPinValid(CONFIG(canTxPin))) && // both pins are set... + (isBrainPinValid(CONFIG(canRxPin))) && + (CONFIG(canWriteEnabled) || CONFIG(canReadEnabled)) ; // ...and either read or write is enabled + + // nothing to do if we aren't enabled... + if (!isCanEnabled) { + return; + } + switch (CONFIG(canBaudRate)) { case B100KBPS: canConfig = &canConfig100; @@ -256,8 +262,6 @@ void initCan(void) { break; } - startCanPins(); - // Initialize hardware #if STM32_CAN_USE_CAN2 // CAN1 is required for CAN2 diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 3ba7891370..73fbc79565 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -375,10 +375,6 @@ void applyNewHardwareSettings(DECLARE_ENGINE_PARAMETER_SIGNATURE) { enginePins.startPins(); -#if EFI_CAN_SUPPORT - startCanPins(); -#endif /* EFI_CAN_SUPPORT */ - #if EFI_AUX_SERIAL startAuxSerialPins(); #endif /* EFI_AUX_SERIAL */ @@ -508,6 +504,10 @@ void startHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) { startTriggerDebugPins(PASS_ENGINE_PARAMETER_SIGNATURE); startPedalPins(PASS_ENGINE_PARAMETER_SIGNATURE); + +#if EFI_CAN_SUPPORT + startCanPins(); +#endif /* EFI_CAN_SUPPORT */ } void initHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) {