From 1b9037314a4fdfa3022faa4a2b705b9abe9d9bc1 Mon Sep 17 00:00:00 2001 From: Andreika Date: Mon, 10 Jan 2022 00:40:08 +0200 Subject: [PATCH] Hellen mcu module detector (experimental) (#3756) * Hellen mcu module detector (experimental) * make unit-tests happy * make unit-tests even more happy * move Hellen-specific stuff to hellen_common.cpp * fix other hellen config builds * helping non-Hellen builds too * helping alpha2ch build * helping build * cleanup * help unit-tests Co-authored-by: Andrei --- .../boards/hellen/alphax-2chan/board.mk | 3 +- .../config/boards/hellen/hellen-nb1/board.mk | 3 +- .../boards/hellen/hellen154hyundai/board.mk | 3 +- .../hellen/hellen64_miataNA6_94/board.mk | 3 +- .../config/boards/hellen/hellen81/board.mk | 3 +- .../boards/hellen/hellenNA8_96/board.mk | 3 +- .../config/boards/hellen/hellen_common.cpp | 60 +++++++++++++++++++ firmware/config/boards/hellen_meta.h | 6 ++ firmware/config/engines/custom_engine.cpp | 26 +++----- firmware/config/engines/custom_engine.h | 3 +- .../controllers/algo/engine_configuration.cpp | 2 + 11 files changed, 88 insertions(+), 27 deletions(-) diff --git a/firmware/config/boards/hellen/alphax-2chan/board.mk b/firmware/config/boards/hellen/alphax-2chan/board.mk index 8d999a02cd..d8fa3ed6ad 100644 --- a/firmware/config/boards/hellen/alphax-2chan/board.mk +++ b/firmware/config/boards/hellen/alphax-2chan/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/alphax-2chan/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/alphax-2chan/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/alphax-2chan # Set this if you want a default engine type other than normal alphax-2chan diff --git a/firmware/config/boards/hellen/hellen-nb1/board.mk b/firmware/config/boards/hellen/hellen-nb1/board.mk index dce7a755aa..451d862914 100644 --- a/firmware/config/boards/hellen/hellen-nb1/board.mk +++ b/firmware/config/boards/hellen/hellen-nb1/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen-nb1/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen-nb1/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/hellen-nb1 # Set this if you want a default engine type other than normal hellen-nb1 diff --git a/firmware/config/boards/hellen/hellen154hyundai/board.mk b/firmware/config/boards/hellen/hellen154hyundai/board.mk index fa0f3fef4f..50dce15d89 100644 --- a/firmware/config/boards/hellen/hellen154hyundai/board.mk +++ b/firmware/config/boards/hellen/hellen154hyundai/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen154hyundai/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen154hyundai/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/hellen154hyundai # Set this if you want a default engine type other than normal hellen121nissan diff --git a/firmware/config/boards/hellen/hellen64_miataNA6_94/board.mk b/firmware/config/boards/hellen/hellen64_miataNA6_94/board.mk index df71b6e03c..b21678f38d 100644 --- a/firmware/config/boards/hellen/hellen64_miataNA6_94/board.mk +++ b/firmware/config/boards/hellen/hellen64_miataNA6_94/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen64_miataNA6_94/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen64_miataNA6_94/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/hellen64_miataNA6_94 # Set this if you want a default engine type other than normal hellen64_miataNA6_94 diff --git a/firmware/config/boards/hellen/hellen81/board.mk b/firmware/config/boards/hellen/hellen81/board.mk index 53c01f130e..b996ca26a3 100644 --- a/firmware/config/boards/hellen/hellen81/board.mk +++ b/firmware/config/boards/hellen/hellen81/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen81/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen81/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/hellen81 # Set this if you want a default engine type other than normal Hellen81 diff --git a/firmware/config/boards/hellen/hellenNA8_96/board.mk b/firmware/config/boards/hellen/hellenNA8_96/board.mk index 6cc08ac1c1..e1e9de5650 100644 --- a/firmware/config/boards/hellen/hellenNA8_96/board.mk +++ b/firmware/config/boards/hellen/hellenNA8_96/board.mk @@ -1,7 +1,8 @@ # Combine the related files for a specific platform and MCU. # Target ECU board design -BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen-nb1/board_configuration.cpp +BOARDCPPSRC = $(BOARDS_DIR)/hellen/hellen-nb1/board_configuration.cpp \ + $(BOARDS_DIR)/hellen/hellen_common.cpp BOARDINC = $(BOARDS_DIR)/hellen/hellen-nb1 # Set this if you want a default engine type other than normal hellen-nb1 diff --git a/firmware/config/boards/hellen/hellen_common.cpp b/firmware/config/boards/hellen/hellen_common.cpp index a9d85f6083..268c651eb7 100644 --- a/firmware/config/boards/hellen/hellen_common.cpp +++ b/firmware/config/boards/hellen/hellen_common.cpp @@ -1,4 +1,5 @@ #include "pch.h" +#include "hellen_meta.h" void hellenWbo() { engineConfiguration->enableAemXSeries = true; @@ -10,3 +11,62 @@ void setHellenDefaultVrThresholds() { setLinearCurve(engineConfiguration->vrThreshold[i].values, 0.6, 1.2, 0.1); } } + +void setHellen144LedPins() { +#ifdef EFI_COMMUNICATION_PIN + engineConfiguration->communicationLedPin = EFI_COMMUNICATION_PIN; +#else + engineConfiguration->communicationLedPin = GPIOE_7; +#endif /* EFI_COMMUNICATION_PIN */ + engineConfiguration->runningLedPin = GPIOG_1; + engineConfiguration->warningLedPin = GPIOE_8; +} + +void setHellen176LedPins() { +#ifdef EFI_COMMUNICATION_PIN + engineConfiguration->communicationLedPin = EFI_COMMUNICATION_PIN; +#else + engineConfiguration->communicationLedPin = GPIOH_10; +#endif /* EFI_COMMUNICATION_PIN */ + engineConfiguration->runningLedPin = GPIOH_9; // green + engineConfiguration->warningLedPin = GPIOH_11; // yellow +} + +// this should be called before setHellenXXXLedPins() +void detectHellenBoardType() { + // we test the LED1 pin because the red LED used has the smallest voltage drop, + // and thus can be detected more accurately + static const brain_pin_e led1Pins[2] = { + // LED1 pin of the 176-pin mcu module (we check it first!) + GPIOH_8, + // LED1 pin of the 144-pin mcu module + GPIOG_0, + }; + int padState[2]; + // check each mcu module type sequentially + for (int mcuType = 0; mcuType < 2; mcuType++) { + brain_pin_e ledPin = led1Pins[mcuType]; + // set LED1 pin to output & clear the state (discharge parasitic capacitance) + palSetPadMode(getBrainPinPort(ledPin), getBrainPinIndex(ledPin), PAL_MODE_OUTPUT_PUSHPULL); + palClearPad(getBrainPinPort(ledPin), getBrainPinIndex(ledPin)); + // set LED1 pin to input + palSetPadMode(getBrainPinPort(ledPin), getBrainPinIndex(ledPin), PAL_MODE_INPUT); // todo: currently we don't use PAL_MODE_INPUT_PULLDOWN - needs more testing + // wait for the pin state to settle down + chThdSleepMilliseconds(1); + // get the pin states + padState[mcuType] = 1; + for (int i = 0; i < 4; i++) { + // we get "1" only if all readings are "1" + padState[mcuType] &= palReadPad(getBrainPinPort(ledPin), getBrainPinIndex(ledPin)); + } + } + efiPrintf("Hellen board pin states = %d %d", padState[0], padState[1]); + if (padState[0] && !padState[1]) { + efiPrintf("* Hellen 176-pin mcu detected!"); + } + else if (!padState[0] && padState[1]) { + efiPrintf("* Hellen 144-pin mcu detected!"); + } else { + efiPrintf("* Cannot detect Hellen mcu module!"); + } +} diff --git a/firmware/config/boards/hellen_meta.h b/firmware/config/boards/hellen_meta.h index 56a61f1aaa..fadbcd8882 100644 --- a/firmware/config/boards/hellen_meta.h +++ b/firmware/config/boards/hellen_meta.h @@ -10,6 +10,12 @@ void hellenWbo(); void setHellenDefaultVrThresholds(); +void setHellen144LedPins(); +void setHellen176LedPins(); + +void detectHellenBoardType(); + + #define H144_LS_1 GPIOG_7 #define H144_LS_2 GPIOG_8 #define H144_LS_3 GPIOD_11 diff --git a/firmware/config/engines/custom_engine.cpp b/firmware/config/engines/custom_engine.cpp index bf5a477833..af17406cb4 100644 --- a/firmware/config/engines/custom_engine.cpp +++ b/firmware/config/engines/custom_engine.cpp @@ -17,6 +17,7 @@ #include "fsio_impl.h" #include "mre_meta.h" #include "proteus_meta.h" +#include "hellen_meta.h" #if EFI_ELECTRONIC_THROTTLE_BODY #include "electronic_throttle.h" @@ -1052,24 +1053,11 @@ end #endif } +void detectBoardType() { #if HW_HELLEN -void setHellen144LedPins() { -#ifdef EFI_COMMUNICATION_PIN - engineConfiguration->communicationLedPin = EFI_COMMUNICATION_PIN; -#else - engineConfiguration->communicationLedPin = GPIOE_7; -#endif /* EFI_COMMUNICATION_PIN */ - engineConfiguration->runningLedPin = GPIOG_1; - engineConfiguration->warningLedPin = GPIOE_8; -} - -void setHellen176LedPins() { -#ifdef EFI_COMMUNICATION_PIN - engineConfiguration->communicationLedPin = EFI_COMMUNICATION_PIN; -#else - engineConfiguration->communicationLedPin = GPIOH_10; -#endif /* EFI_COMMUNICATION_PIN */ - engineConfiguration->runningLedPin = GPIOH_9; // green - engineConfiguration->warningLedPin = GPIOH_11; // yellow -} +#if !EFI_UNIT_TEST + detectHellenBoardType(); +#endif /* EFI_UNIT_TEST */ #endif //HW_HELLEN + // todo: add board ID detection? +} diff --git a/firmware/config/engines/custom_engine.h b/firmware/config/engines/custom_engine.h index 98bc3cd33f..074bc5b670 100644 --- a/firmware/config/engines/custom_engine.h +++ b/firmware/config/engines/custom_engine.h @@ -37,5 +37,4 @@ void setHellen72etb(); void setRotary(); void setVrThresholdTest(); -void setHellen144LedPins(); -void setHellen176LedPins(); +void detectBoardType(); diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index b77ac5f07d..ef509c37e9 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -816,6 +816,8 @@ void loadConfiguration() { resetConfigurationExt(engineConfiguration->engineType); #endif /* EFI_INTERNAL_FLASH */ + detectBoardType(); + // Force any board configuration options that humans shouldn't be able to change setBoardConfigOverrides(); }