From 2b852ea48e8b3b6802f2d5db5f4ef822b6203166 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 1 Feb 2019 23:48:11 -0500 Subject: [PATCH] CJ125 unit-tests coverage #617 --- .../system/pwm_generator_logic.cpp | 1 + firmware/hw_layer/sensors/CJ125.cpp | 23 ++++--------------- firmware/hw_layer/sensors/CJ125_logic.cpp | 9 ++++++++ firmware/hw_layer/sensors/CJ125_logic.h | 3 +++ unit_tests/main.cpp | 2 +- unit_tests/tests/test_cj125.cpp | 8 +++++++ 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/firmware/controllers/system/pwm_generator_logic.cpp b/firmware/controllers/system/pwm_generator_logic.cpp index 7d8aea18ef..808e868cc9 100644 --- a/firmware/controllers/system/pwm_generator_logic.cpp +++ b/firmware/controllers/system/pwm_generator_logic.cpp @@ -301,6 +301,7 @@ void startSimplePwm(SimplePwm *state, const char *msg, ExecutorInterface *execut OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback) { efiAssertVoid(CUSTOM_ERR_6692, state != NULL, "state"); efiAssertVoid(CUSTOM_ERR_6665, dutyCycle >= 0 && dutyCycle <= 1, "dutyCycle"); + efiAssertVoid(CUSTOM_ERR_6693, stateChangeCallback != NULL, "listener"); if (frequency < 1) { warning(CUSTOM_OBD_LOW_FREQUENCY, "low frequency %.2f", frequency); return; diff --git a/firmware/hw_layer/sensors/CJ125.cpp b/firmware/hw_layer/sensors/CJ125.cpp index fa2ae5aba1..07d97d0b31 100644 --- a/firmware/hw_layer/sensors/CJ125.cpp +++ b/firmware/hw_layer/sensors/CJ125.cpp @@ -29,8 +29,6 @@ EXTERN_ENGINE; #include "pin_repository.h" extern TunerStudioOutputChannels tsOutputChannels; -static OutputPin wboHeaterPin; -static OutputPin cj125Cs; static Logging *logger; static unsigned char tx_buff[2]; static unsigned char rx_buff[1]; @@ -351,15 +349,6 @@ static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) { #endif } -static void cjStartHeaterControl(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - scheduleMsg(logger, "cj125: Starting heater control"); - // todo: use custom pin state method, turn pin off while not running - startSimplePwmExt(&globalInstance.wboHeaterControl, "wboHeaterPin", - &engine->executor, - CONFIGB(wboHeaterPin), - &wboHeaterPin, CJ125_HEATER_PWM_FREQ, 0.0f, applyPinState); -} - void CJ125::setError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX) { errorCode = errCode; state = CJ125_ERROR; @@ -411,18 +400,16 @@ void cj125defaultPinout(DECLARE_ENGINE_PARAMETER_SIGNATURE) { } static void cjStartSpi(DECLARE_ENGINE_PARAMETER_SIGNATURE) { - cj125Cs.initPin("cj125 CS", CONFIGB(cj125CsPin), + globalInstance.cj125Cs.initPin("cj125 CS", CONFIGB(cj125CsPin), &engineConfiguration->cj125CsPinMode); // Idle CS pin - SPI CS is high when idle - cj125Cs.setValue(true); + globalInstance.cj125Cs.setValue(true); -#if EFI_PROD_CODE cj125spicfg.ssport = getHwPort("cj125", CONFIGB(cj125CsPin)); cj125spicfg.sspad = getHwPin("cj125", CONFIGB(cj125CsPin)); driver = getSpiDevice(engineConfiguration->cj125SpiDevice); scheduleMsg(logger, "cj125: Starting SPI driver"); spiStart(driver, &cj125spicfg); -#endif /* EFI_PROD_CODE */ } /** @@ -595,11 +582,9 @@ float cjGetAfr(DECLARE_ENGINE_PARAMETER_SIGNATURE) { bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { if (!CONFIGB(isCJ125Enabled)) return false; -#if ! EFI_UNIT_TEST // check if controller is functioning if (!globalInstance.isWorkingState()) return false; -#endif /* EFI_UNIT_TEST */ // check if amplification is turned on if (amplCoeff == 0.0f) return false; @@ -642,8 +627,8 @@ void initCJ125(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) { cjInitPid(PASS_ENGINE_PARAMETER_SIGNATURE); cjStartSpi(PASS_ENGINE_PARAMETER_SIGNATURE); - cjStartHeaterControl(PASS_ENGINE_PARAMETER_SIGNATURE); - globalInstance.SetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE); + scheduleMsg(logger, "cj125: Starting heater control"); + globalInstance.StartHeaterControl(applyPinState PASS_ENGINE_PARAMETER_SUFFIX); cjStart(PASS_ENGINE_PARAMETER_SIGNATURE); #ifdef CJ125_DEBUG diff --git a/firmware/hw_layer/sensors/CJ125_logic.cpp b/firmware/hw_layer/sensors/CJ125_logic.cpp index 39b85f7415..f4e036f8e5 100644 --- a/firmware/hw_layer/sensors/CJ125_logic.cpp +++ b/firmware/hw_layer/sensors/CJ125_logic.cpp @@ -36,3 +36,12 @@ void CJ125::SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE) { bool CJ125::isWorkingState(void) { return state != CJ125_ERROR && state != CJ125_INIT && state != CJ125_IDLE; } + +void CJ125::StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX) { + // todo: use custom pin state method, turn pin off while not running + startSimplePwmExt(&wboHeaterControl, "wboHeaterPin", + &engine->executor, + CONFIGB(wboHeaterPin), + &wboHeaterPin, CJ125_HEATER_PWM_FREQ, 0.0f, stateChangeCallback); + SetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE); +} diff --git a/firmware/hw_layer/sensors/CJ125_logic.h b/firmware/hw_layer/sensors/CJ125_logic.h index adc79110d1..9c50824ec2 100644 --- a/firmware/hw_layer/sensors/CJ125_logic.h +++ b/firmware/hw_layer/sensors/CJ125_logic.h @@ -54,6 +54,8 @@ public: efitick_t prevNt; float heaterDuty = 0.0f; + OutputPin wboHeaterPin; + OutputPin cj125Cs; // Used by CJ125 driver state machine volatile cj125_state_e state = CJ125_INIT; @@ -64,6 +66,7 @@ public: bool isWorkingState(void); void SetHeater(float value DECLARE_ENGINE_PARAMETER_SUFFIX); void SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE); + void StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX); }; // Heater params for Idle(cold), Preheating and Control stages diff --git a/unit_tests/main.cpp b/unit_tests/main.cpp index 6c9aeba332..517f806179 100644 --- a/unit_tests/main.cpp +++ b/unit_tests/main.cpp @@ -50,7 +50,7 @@ GTEST_API_ int main(int argc, char **argv) { // printTriggerDebug = true; // resizeMap(); - printf("Success 20190117\r\n"); + printf("Success 20190201\r\n"); printAllTriggers(); // printConvertedTable(); testing::InitGoogleTest(&argc, argv); diff --git a/unit_tests/tests/test_cj125.cpp b/unit_tests/tests/test_cj125.cpp index 50501e59a7..9d97cf993d 100644 --- a/unit_tests/tests/test_cj125.cpp +++ b/unit_tests/tests/test_cj125.cpp @@ -9,15 +9,23 @@ #include "CJ125_logic.h" #include "engine_test_helper.h" +static void applyHeaterPinState(PwmConfig *state, int stateIndex) { + +} + TEST(testCJ125, testInitialState) { CJ125 cj; ASSERT_EQ(cj.state, CJ125_INIT); ASSERT_FALSE(cj.isWorkingState()); + ASSERT_EQ(cj.heaterDuty, 0); WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996); ASSERT_EQ(engine->sensors.vBatt, 0); + cj.StartHeaterControl(&applyHeaterPinState PASS_ENGINE_PARAMETER_SUFFIX); + ASSERT_EQ(cj.heaterDuty, CJ125_HEATER_IDLE_RATE); + }