diff --git a/firmware/hw_layer/sensors/cj125.cpp b/firmware/hw_layer/sensors/cj125.cpp index 5862c9cbe8..0da5cf6108 100644 --- a/firmware/hw_layer/sensors/cj125.cpp +++ b/firmware/hw_layer/sensors/cj125.cpp @@ -170,31 +170,27 @@ static uint32_t get16bitFromVoltage(float v) { return (uint32_t)(v * CJ125_VOLTAGE_TO_16BIT_FACTOR); } -static void cjPrintState() { - scheduleMsg(logger, "cj125: state=%d diag=0x%x (vUa=%.3f vUr=%.3f) (vUaCal=%.3f vUrCal=%.3f)", - globalInstance.state, globalInstance.diag, - globalInstance.vUa, globalInstance.vUr, - globalInstance.vUaCal, globalInstance.vUrCal); - - scheduleMsg(logger, "cj125 P=%f I=%f D=%f", - globalInstance.heaterPidConfig.pFactor, - globalInstance.heaterPidConfig.iFactor, - globalInstance.heaterPidConfig.dFactor); -} - -static void cjInfo() { - cjPrintState(); -#if HAL_USE_SPI - printSpiConfig(logger, "cj125", CONFIG(cj125SpiDevice)); -#endif /* HAL_USE_SPI */ -} - -static void cjPrintData() { -#if ! EFI_UNIT_TEST - if (engineConfiguration->isCJ125Verbose) { - cjPrintState(); +static const char * getCjState(cj125_state_e stateCode) { + switch (stateCode) { + case CJ125_INIT: + return "INIT"; + case CJ125_IDLE: + return "IDLE"; + case CJ125_CALIBRATION: + return "CALIBRATION"; + case CJ125_PREHEAT: + return "PREHEAT"; + case CJ125_HEAT_UP: + return "HEAT UP"; + case CJ125_READY: + return "READY"; + case CJ125_OVERHEAT: + return "OVERHEAT"; + case CJ125_ERROR: + return "ERROR"; + default: + return "UNKNOWN"; } -#endif /* EFI_UNIT_TEST */ } static void cjPrintErrorCode(cj125_error_e errCode) { @@ -222,6 +218,39 @@ static void cjPrintErrorCode(cj125_error_e errCode) { scheduleMsg(logger, "cj125 ERROR: %s.", errString); } +static void cjPrintState() { + scheduleMsg(logger, "cj125: state=%s %s diag=0x%x (vUa=%.3f vUr=%.3f) (vUaCal=%.3f vUrCal=%.3f)", + getCjState(globalInstance.state), globalInstance.diag, + globalInstance.vUa, globalInstance.vUr, + globalInstance.vUaCal, globalInstance.vUrCal); + + globalInstance.printDiag(); + + if (globalInstance.state == CJ125_ERROR) { + cjPrintErrorCode(globalInstance.errorCode); + } + + scheduleMsg(logger, "cj125 P=%f I=%f D=%f", + globalInstance.heaterPidConfig.pFactor, + globalInstance.heaterPidConfig.iFactor, + globalInstance.heaterPidConfig.dFactor); +} + +static void cjInfo() { + cjPrintState(); +#if HAL_USE_SPI + printSpiConfig(logger, "cj125", CONFIG(cj125SpiDevice)); +#endif /* HAL_USE_SPI */ +} + +static void cjPrintData() { +#if ! EFI_UNIT_TEST + if (engineConfiguration->isCJ125Verbose) { + cjPrintState(); + } +#endif /* EFI_UNIT_TEST */ +} + class RealSpi : public Cj125SpiStream { public: uint8_t ReadRegister(uint8_t reg) override { @@ -249,12 +278,14 @@ static void cjUpdateAnalogValues() { #endif /* EFI_PROD_CODE */ } +#if ! EFI_UNIT_TEST void cjCalibrate(void) { - globalInstance.calibrate(); + globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE); } +#endif /* EFI_UNIT_TEST */ -void CJ125::calibrate(void) { - cjIdentify(); +void CJ125::calibrate(DECLARE_ENGINE_PARAMETER_SIGNATURE) { + cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE); scheduleMsg(logger, "cj125: Starting calibration..."); cjSetMode(CJ125_MODE_CALIBRATION); @@ -318,7 +349,7 @@ static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) { return; } - globalInstance.cjIdentify(); + globalInstance.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE); // Load calibration values #if EFI_PROD_CODE @@ -330,7 +361,7 @@ static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #endif // if no calibration, try to calibrate now and store new values if (storedLambda == 0 || storedHeater == 0) { - globalInstance.calibrate(); + globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE); } else { scheduleMsg(logger, "cj125: Loading stored calibration data (%d %d)", storedLambda, storedHeater); globalInstance.vUaCal = getVoltageFrom16bit(storedLambda); @@ -395,7 +426,7 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) { } if (instance->state == CJ125_CALIBRATION) { - globalInstance.calibrate(); + globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE); // Start normal operation instance->state = CJ125_INIT; globalInstance.cjSetMode(CJ125_MODE_NORMAL_17); diff --git a/firmware/hw_layer/sensors/cj125_logic.cpp b/firmware/hw_layer/sensors/cj125_logic.cpp index 3ce7def93c..85543f59cc 100644 --- a/firmware/hw_layer/sensors/cj125_logic.cpp +++ b/firmware/hw_layer/sensors/cj125_logic.cpp @@ -61,6 +61,21 @@ static void printDiagCode(Logging * logging, const char *msg, int code, const ch case 2: scheduleMsg(logging, "%s Short to Vbatt", msg); return; + case 3: + scheduleMsg(logging, "%s LOOKS GOOD", msg); + return; + } +} + +void CJ125::printDiag() { + if (diag == CJ125_DIAG_NORM) { + scheduleMsg(logger, "cj125: diag Looks great!"); + } else { + scheduleMsg(logger, "cj125: diag NOT GOOD"); + printDiagCode(logger, "VM", diag, LOW_VOLTAGE); + printDiagCode(logger, "UN", diag >> 2, LOW_VOLTAGE); + printDiagCode(logger, "IA", diag >> 4, LOW_VOLTAGE); + printDiagCode(logger, "HR", diag >> 6, "open load"); } } @@ -68,7 +83,7 @@ static void printDiagCode(Logging * logging, const char *msg, int code, const ch * @return true in case of positive SPI identification * false in case of unexpected SPI response */ -bool CJ125::cjIdentify(void) { +bool CJ125::cjIdentify(DECLARE_ENGINE_PARAMETER_SIGNATURE) { efiAssert(OBD_PCM_Processor_Fault, spi!= NULL, "No SPI pointer", false); // read Ident register int ident = spi->ReadRegister(IDENT_REG_RD) & CJ125_IDENT_MASK; @@ -84,24 +99,15 @@ bool CJ125::cjIdentify(void) { scheduleMsg(logger, "cj125: Check ident=0x%x diag=0x%x init1=0x%x init2=0x%x", ident, diag, init1, init2); if (ident != CJ125_IDENT) { scheduleMsg(logger, "cj125: Error! Wrong ident! Cannot communicate with CJ125!"); - errorCode = CJ125_ERROR_WRONG_IDENT; - state = CJ125_ERROR; + setError(CJ125_ERROR_WRONG_IDENT PASS_ENGINE_PARAMETER_SUFFIX); return false; } if (init1 != CJ125_INIT1_NORMAL_17 || init2 != CJ125_INIT2_DIAG) { scheduleMsg(logger, "cj125: Error! Cannot set init registers! Cannot communicate with CJ125!"); - errorCode = CJ125_ERROR_WRONG_INIT; - state = CJ125_ERROR; + setError(CJ125_ERROR_WRONG_IDENT PASS_ENGINE_PARAMETER_SUFFIX); return false; } - if (diag == CJ125_DIAG_NORM) { - scheduleMsg(logger, "cj125: Looks great!"); - } else { - printDiagCode(logger, "VM", diag, LOW_VOLTAGE); - printDiagCode(logger, "UN", diag >> 2, LOW_VOLTAGE); - printDiagCode(logger, "IA", diag >> 4, LOW_VOLTAGE); - printDiagCode(logger, "HR", diag >> 6, "open load"); - } + printDiag(); return true; } diff --git a/firmware/hw_layer/sensors/cj125_logic.h b/firmware/hw_layer/sensors/cj125_logic.h index fb5c3360e6..9949bb8cd6 100644 --- a/firmware/hw_layer/sensors/cj125_logic.h +++ b/firmware/hw_layer/sensors/cj125_logic.h @@ -18,14 +18,14 @@ typedef enum { } cj125_sensor_type_e; typedef enum { - CJ125_INIT, - CJ125_IDLE, - CJ125_CALIBRATION, - CJ125_PREHEAT, - CJ125_HEAT_UP, - CJ125_READY, - CJ125_OVERHEAT, - CJ125_ERROR, + CJ125_INIT = 0, + CJ125_IDLE = 1, + CJ125_CALIBRATION = 2, + CJ125_PREHEAT = 3, + CJ125_HEAT_UP = 4, + CJ125_READY = 5, + CJ125_OVERHEAT = 6, + CJ125_ERROR = 7, } cj125_state_e; @@ -101,8 +101,9 @@ public: void SetHeater(float value DECLARE_ENGINE_PARAMETER_SUFFIX); void SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE); void StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX); - bool cjIdentify(void); - void calibrate(); + bool cjIdentify(DECLARE_ENGINE_PARAMETER_SIGNATURE); + void printDiag(); + void calibrate(DECLARE_ENGINE_PARAMETER_SIGNATURE); void cjSetMode(cj125_mode_e m); bool isValidState() const; void cjInitPid(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/unit_tests/tests/test_cj125.cpp b/unit_tests/tests/test_cj125.cpp index f44baf045f..e1544d0371 100644 --- a/unit_tests/tests/test_cj125.cpp +++ b/unit_tests/tests/test_cj125.cpp @@ -39,7 +39,7 @@ TEST(testCJ125, testInitialState) { EXPECT_CALL(mock, ReadRegister(INIT_REG2_RD)).Times(1).WillOnce(Return(CJ125_INIT2_DIAG)); EXPECT_CALL(mock, ReadRegister(DIAG_REG_RD)).Times(1); - cj.cjIdentify(); + cj.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE); // validate that error state was not set ASSERT_EQ(cj.state, CJ125_INIT); } @@ -53,7 +53,9 @@ TEST(testCJ125, testFailedIdentify) { TestSpi mock; cj.spi = &mock; - cj.cjIdentify(); + WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); + + cj.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE); ASSERT_EQ(cj.errorCode, CJ125_ERROR_WRONG_IDENT); ASSERT_EQ(cj.state, CJ125_ERROR); }