cj125 unit test progress!

This commit is contained in:
rusefi 2019-12-02 01:52:54 -05:00
parent 8760ef2874
commit 0b4f624b2a
5 changed files with 49 additions and 12 deletions

View File

@ -164,7 +164,7 @@ static void cjPrintErrorCode(cj125_error_e errCode) {
case CJ125_ERROR_OVERHEAT:
errString = "Sensor overheating";
break;
case CJ125_ERROR_NONE:
case CJ125_NO_ERROR:
errString = "N/A";
break;
case CJ125_ERROR_WRONG_IDENT:
@ -540,18 +540,22 @@ void initCJ125(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
globalInstance.spi = &spi;
globalInstance.logger = sharedLogger;
if (!CONFIGB(isCJ125Enabled))
if (!CONFIGB(isCJ125Enabled)) {
globalInstance.errorCode = CJ125_ERROR_DISABLED;
return;
}
if (CONFIG(cj125ur) == EFI_ADC_NONE || CONFIG(cj125ua) == EFI_ADC_NONE) {
scheduleMsg(logger, "cj125 init error! cj125ur and cj125ua are required.");
warning(CUSTOM_CJ125_0, "cj ur ua");
globalInstance.errorCode = CJ125_ERROR_DISABLED;
return;
}
if (CONFIGB(wboHeaterPin) == GPIO_UNASSIGNED) {
scheduleMsg(logger, "cj125 init error! wboHeaterPin is required.");
warning(CUSTOM_CJ125_1, "cj heater");
globalInstance.errorCode = CJ125_ERROR_DISABLED;
return;
}

View File

@ -7,6 +7,7 @@
#include "cj125_logic.h"
#include "engine.h"
#include "error_handling.h"
EXTERN_ENGINE;
@ -47,7 +48,12 @@ void CJ125::StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENG
SetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE);
}
void CJ125::cjIdentify(void) {
/**
* @return true in case of positive SPI identification
* false in case of unexpected SPI response
*/
bool CJ125::cjIdentify(void) {
efiAssert(OBD_PCM_Processor_Fault, spi!= NULL, "No SPI pointer", false);
// read Ident register
int ident = spi->ReadRegister(IDENT_REG_RD) & CJ125_IDENT_MASK;
@ -62,15 +68,22 @@ void 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;
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;
return false;
}
#if 0
if (diag != CJ125_DIAG_NORM) {
scheduleMsg(logger, "cj125: Diag error!");
}
#endif
return true;
}
void CJ125::cjSetMode(cj125_mode_e m) {

View File

@ -31,11 +31,12 @@ typedef enum {
} cj125_state_e;
typedef enum {
CJ125_ERROR_NONE = 0,
CJ125_NO_ERROR = 0,
CJ125_ERROR_HEATER_MALFUNCTION = 1,
CJ125_ERROR_OVERHEAT = 2,
CJ125_ERROR_WRONG_IDENT = 3,
CJ125_ERROR_WRONG_INIT = 4,
CJ125_ERROR_DISABLED = 5,
} cj125_error_e;
typedef enum {
@ -90,14 +91,14 @@ public:
// Used by CJ125 driver state machine
volatile cj125_state_e state = CJ125_INIT;
// Last Error code
volatile cj125_error_e errorCode = CJ125_ERROR_NONE;
volatile cj125_error_e errorCode = CJ125_NO_ERROR;
void setError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX);
bool isWorkingState(void) const;
void SetHeater(float value DECLARE_ENGINE_PARAMETER_SUFFIX);
void SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX);
void cjIdentify(void);
bool cjIdentify(void);
void cjSetMode(cj125_mode_e m);
bool isValidState() const;
void cjInitPid(DECLARE_ENGINE_PARAMETER_SIGNATURE);

View File

@ -1,11 +1,10 @@
/*
* test_cj125.cpp
* @file test_cj125.cpp
*
* Created on: Jan 3, 2019
* @author Andrey Belomutskiy, (c) 2012-2019
*/
#include "gtest/gtest.h"
#include "cj125_logic.h"
#include "engine_test_helper.h"
@ -35,13 +34,32 @@ TEST(testCJ125, testInitialState) {
TestSpi mock;
cj.spi = &mock;
EXPECT_CALL(mock, ReadRegister(IDENT_REG_RD)).Times(1);
EXPECT_CALL(mock, ReadRegister(INIT_REG1_RD)).Times(1);
EXPECT_CALL(mock, ReadRegister(INIT_REG2_RD)).Times(1);
EXPECT_CALL(mock, ReadRegister(IDENT_REG_RD)).Times(1).WillOnce(Return(CJ125_IDENT));
EXPECT_CALL(mock, ReadRegister(INIT_REG1_RD)).Times(1).WillOnce(Return(CJ125_INIT1_NORMAL_17));
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();
// validate that error state was not set
ASSERT_EQ(cj.state, CJ125_INIT);
}
TEST(testCJ125, testFailedIdentify) {
CJ125 cj;
ASSERT_EQ(cj.state, CJ125_INIT);
TestSpi mock;
cj.spi = &mock;
cj.cjIdentify();
ASSERT_EQ(cj.errorCode, CJ125_ERROR_WRONG_IDENT);
ASSERT_EQ(cj.state, CJ125_ERROR);
}
TEST(testCJ125, testMode) {
}

View File

@ -11,6 +11,7 @@
#include "engine.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::Return;
// This lets us inspect private state from unit tests
#define private public