cj125 refactoring

This commit is contained in:
rusefi 2019-01-03 08:26:42 -05:00
parent 19c3f7370f
commit 943bc8e577
2 changed files with 54 additions and 33 deletions

View File

@ -47,6 +47,10 @@ static pid_s heaterPidConfig;
static Pid heaterPid(&heaterPidConfig); static Pid heaterPid(&heaterPidConfig);
static float heaterDuty = 0.0f; static float heaterDuty = 0.0f;
// todo: only define this variable in EIF_PROD
static CJ125 globalInstance;
#if ! EFI_UNIT_TEST || defined(__DOXYGEN__) #if ! EFI_UNIT_TEST || defined(__DOXYGEN__)
static THD_WORKING_AREA(cjThreadStack, UTILITY_THREAD_STACK_SIZE); static THD_WORKING_AREA(cjThreadStack, UTILITY_THREAD_STACK_SIZE);
@ -57,19 +61,21 @@ static SPIDriver *driver;
static SPIConfig cj125spicfg = { NULL, static SPIConfig cj125spicfg = { NULL,
/* HW dependent part.*/ /* HW dependent part.*/
NULL, 0, SPI_CR1_MSTR | SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 }; NULL, 0, SPI_CR1_MSTR | SPI_CR1_CPHA | SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2 };
#endif #endif /* EFI_PROD_CODE */
#endif #endif /* EFI_UNIT_TEST */
CJ125::CJ125() {
state = CJ125_IDLE;
errorCode = CJ125_ERROR_NONE;
}
// Used by CJ125 driver state machine
static volatile cj125_state_e state = CJ125_IDLE;
// Chip diagnostics register contents // Chip diagnostics register contents
static volatile int diag = 0; static volatile int diag = 0;
// Last Error code
static volatile cj125_error_e errorCode = CJ125_ERROR_NONE;
// Current values // Current values
static volatile float vUa = 0.0f, vUr = 0.0f; static volatile float vUa = 0.0f;
static volatile float vUr = 0.0f;
// Calibration values // Calibration values
static volatile float vUaCal = 0.0f, vUrCal = 0.0f; static volatile float vUaCal = 0.0f, vUrCal = 0.0f;
@ -330,7 +336,7 @@ static void cjCalibrate(void) {
backupRamSave(BACKUP_CJ125_CALIBRATION_HEATER, storedHeater); backupRamSave(BACKUP_CJ125_CALIBRATION_HEATER, storedHeater);
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
state = CJ125_IDLE; globalInstance.state = CJ125_IDLE;
} }
static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) { static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
@ -398,7 +404,7 @@ static void cjStartHeaterControl(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
} }
} }
static void cjSetError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX) { void CJ125::cjSetError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX) {
errorCode = errCode; errorCode = errCode;
state = CJ125_ERROR; state = CJ125_ERROR;
cjPrintErrorCode(errorCode); cjPrintErrorCode(errorCode);
@ -409,7 +415,7 @@ static void cjSetError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX) {
cjWriteRegister(INIT_REG2_WR, CJ125_INIT2_RESET); cjWriteRegister(INIT_REG2_WR, CJ125_INIT2_RESET);
} }
static bool cjIsWorkingState(void) { bool CJ125::cjIsWorkingState(void) {
return state != CJ125_ERROR && state != CJ125_INIT && state != CJ125_IDLE; return state != CJ125_ERROR && state != CJ125_INIT && state != CJ125_IDLE;
} }
@ -468,7 +474,7 @@ static void cjStartSpi(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
/** /**
* @return true if currently in IDLE or ERROR state * @return true if currently in IDLE or ERROR state
*/ */
static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUFFIX) { static bool cj125periodic(CJ125 *instance, CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUFFIX) {
{ {
efitick_t nowNt = getTimeNowNt(); efitick_t nowNt = getTimeNowNt();
bool isStopped = engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE); bool isStopped = engine->rpmCalculator.isStopped(PASS_ENGINE_PARAMETER_SIGNATURE);
@ -476,14 +482,14 @@ static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUF
cjUpdateAnalogValues(); cjUpdateAnalogValues();
// If the controller is disabled // If the controller is disabled
if (state == CJ125_IDLE || state == CJ125_ERROR) { if (instance->state == CJ125_IDLE || instance->state == CJ125_ERROR) {
return true; return true;
} }
if (state == CJ125_CALIBRATION) { if (instance->state == CJ125_CALIBRATION) {
cjCalibrate(); cjCalibrate();
// Start normal operation // Start normal operation
state = CJ125_INIT; instance->state = CJ125_INIT;
cjSetMode(CJ125_MODE_NORMAL_17); cjSetMode(CJ125_MODE_NORMAL_17);
} }
@ -492,23 +498,23 @@ static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUF
// check heater state // check heater state
if (vUr > CJ125_UR_PREHEAT_THR || heaterDuty < CJ125_PREHEAT_MIN_DUTY) { if (vUr > CJ125_UR_PREHEAT_THR || heaterDuty < CJ125_PREHEAT_MIN_DUTY) {
// Check if RPM>0 and it's time to start pre-heating // Check if RPM>0 and it's time to start pre-heating
if (state == CJ125_INIT && !isStopped) { if (instance->state == CJ125_INIT && !isStopped) {
// start preheating // start preheating
state = CJ125_PREHEAT; instance->state = CJ125_PREHEAT;
anotherState->startHeatingNt = anotherState->prevNt = getTimeNowNt(); anotherState->startHeatingNt = anotherState->prevNt = getTimeNowNt();
cjSetMode(CJ125_MODE_NORMAL_17); cjSetMode(CJ125_MODE_NORMAL_17);
} }
} else if (vUr > CJ125_UR_GOOD_THR) { } else if (vUr > CJ125_UR_GOOD_THR) {
state = CJ125_HEAT_UP; instance->state = CJ125_HEAT_UP;
} else if (vUr < CJ125_UR_OVERHEAT_THR) { } else if (vUr < CJ125_UR_OVERHEAT_THR) {
state = CJ125_OVERHEAT; instance->state = CJ125_OVERHEAT;
} else { } else {
// This indicates that the heater temperature is optimal for UA measurement // This indicates that the heater temperature is optimal for UA measurement
state = CJ125_READY; instance->state = CJ125_READY;
} }
if (isStopped && cjIsWorkingState()) { if (isStopped && instance->cjIsWorkingState()) {
state = CJ125_INIT; instance->state = CJ125_INIT;
cjSetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE); cjSetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE);
} }
@ -517,7 +523,7 @@ static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUF
cjSetMode(lambda > 1.0f ? CJ125_MODE_NORMAL_17 : CJ125_MODE_NORMAL_8); cjSetMode(lambda > 1.0f ? CJ125_MODE_NORMAL_17 : CJ125_MODE_NORMAL_8);
#endif #endif
switch (state) { switch (instance->state) {
case CJ125_PREHEAT: case CJ125_PREHEAT:
// use constant-speed startup heat-up // use constant-speed startup heat-up
if (nowNt - anotherState->prevNt >= CJ125_HEATER_PREHEAT_PERIOD) { if (nowNt - anotherState->prevNt >= CJ125_HEATER_PREHEAT_PERIOD) {
@ -527,7 +533,7 @@ static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUF
cjSetHeater(preheatDuty PASS_ENGINE_PARAMETER_SUFFIX); cjSetHeater(preheatDuty PASS_ENGINE_PARAMETER_SUFFIX);
// If we are heating too long, and there's still no result, then something is wrong... // If we are heating too long, and there's still no result, then something is wrong...
if (nowNt - anotherState->startHeatingNt > US2NT(US_PER_SECOND_LL) * CJ125_PREHEAT_TIMEOUT) { if (nowNt - anotherState->startHeatingNt > US2NT(US_PER_SECOND_LL) * CJ125_PREHEAT_TIMEOUT) {
cjSetError(CJ125_ERROR_HEATER_MALFUNCTION PASS_ENGINE_PARAMETER_SUFFIX); instance->cjSetError(CJ125_ERROR_HEATER_MALFUNCTION PASS_ENGINE_PARAMETER_SUFFIX);
} }
cjPrintData(); cjPrintData();
anotherState->prevNt = nowNt; anotherState->prevNt = nowNt;
@ -550,7 +556,7 @@ static bool cj125periodic(CJ125_state *anotherState DECLARE_ENGINE_PARAMETER_SUF
break; break;
case CJ125_OVERHEAT: case CJ125_OVERHEAT:
if (nowNt - anotherState->prevNt >= CJ125_HEATER_OVERHEAT_PERIOD) { if (nowNt - anotherState->prevNt >= CJ125_HEATER_OVERHEAT_PERIOD) {
cjSetError(CJ125_ERROR_OVERHEAT PASS_ENGINE_PARAMETER_SUFFIX); instance->cjSetError(CJ125_ERROR_OVERHEAT PASS_ENGINE_PARAMETER_SUFFIX);
anotherState->prevNt = nowNt; anotherState->prevNt = nowNt;
} }
default: default:
@ -573,7 +579,7 @@ static msg_t cjThread(void)
globalStateInstance.startHeatingNt = 0; globalStateInstance.startHeatingNt = 0;
globalStateInstance.prevNt = getTimeNowNt(); globalStateInstance.prevNt = getTimeNowNt();
while (1) { while (1) {
bool needIdleSleep = cj125periodic(&globalStateInstance PASS_ENGINE_PARAMETER_SUFFIX); bool needIdleSleep = cj125periodic(&globalInstance, &globalStateInstance PASS_ENGINE_PARAMETER_SUFFIX);
chThdSleepMilliseconds(needIdleSleep ? CJ125_IDLE_TICK_DELAY : CJ125_TICK_DELAY); chThdSleepMilliseconds(needIdleSleep ? CJ125_IDLE_TICK_DELAY : CJ125_TICK_DELAY);
} }
return -1; return -1;
@ -592,18 +598,18 @@ static bool cjCheckConfig(void) {
static void cjStartCalibration(void) { static void cjStartCalibration(void) {
if (!cjCheckConfig()) if (!cjCheckConfig())
return; return;
if (cjIsWorkingState()) { if (globalInstance.cjIsWorkingState()) {
// todo: change this later for the normal thread operation (auto pre-heating) // todo: change this later for the normal thread operation (auto pre-heating)
scheduleMsg(logger, "cj125: Cannot start calibration. Please restart the board and make sure that your sensor is not heating"); scheduleMsg(logger, "cj125: Cannot start calibration. Please restart the board and make sure that your sensor is not heating");
return; return;
} }
state = CJ125_CALIBRATION; globalInstance.state = CJ125_CALIBRATION;
} }
static void cjStartTest(void) { static void cjStartTest(void) {
if (!cjCheckConfig()) if (!cjCheckConfig())
return; return;
state = CJ125_INIT; globalInstance.state = CJ125_INIT;
} }
#endif /* EFI_UNIT_TEST */ #endif /* EFI_UNIT_TEST */
@ -640,9 +646,11 @@ float cjGetAfr(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) { bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
if (!boardConfiguration->isCJ125Enabled) if (!boardConfiguration->isCJ125Enabled)
return false; return false;
#if ! EFI_UNIT_TEST
// check if controller is functioning // check if controller is functioning
if (!cjIsWorkingState()) if (!globalInstance.cjIsWorkingState())
return false; return false;
#endif /* EFI_UNIT_TEST */
// check if amplification is turned on // check if amplification is turned on
if (amplCoeff == 0.0f) if (amplCoeff == 0.0f)
return false; return false;
@ -652,7 +660,6 @@ bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
return true; return true;
} }
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__) #if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
// used by DBG_CJ125 // used by DBG_CJ125
void cjPostState(TunerStudioOutputChannels *tsOutputChannels) { void cjPostState(TunerStudioOutputChannels *tsOutputChannels) {
@ -663,7 +670,7 @@ void cjPostState(TunerStudioOutputChannels *tsOutputChannels) {
tsOutputChannels->debugFloatField5 = vUr; tsOutputChannels->debugFloatField5 = vUr;
tsOutputChannels->debugFloatField6 = vUaCal; tsOutputChannels->debugFloatField6 = vUaCal;
tsOutputChannels->debugFloatField7 = vUrCal; tsOutputChannels->debugFloatField7 = vUrCal;
tsOutputChannels->debugIntField1 = state; tsOutputChannels->debugIntField1 = globalInstance.state;
tsOutputChannels->debugIntField2 = diag; tsOutputChannels->debugIntField2 = diag;
} }
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
@ -690,7 +697,7 @@ void initCJ125(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
cjStart(PASS_ENGINE_PARAMETER_SIGNATURE); cjStart(PASS_ENGINE_PARAMETER_SIGNATURE);
#if 1 #if 1
state = CJ125_INIT; globalInstance.state = CJ125_INIT;
#endif #endif
#ifdef CJ125_DEBUG #ifdef CJ125_DEBUG

View File

@ -175,13 +175,27 @@ typedef enum {
} cj125_sensor_type_e; } cj125_sensor_type_e;
class CJ125 {
public:
CJ125();
// Used by CJ125 driver state machine
volatile cj125_state_e state;
// Last Error code
volatile cj125_error_e errorCode;
void cjSetError(cj125_error_e errCode DECLARE_ENGINE_PARAMETER_SUFFIX);
bool cjIsWorkingState(void);
};
#if EFI_TUNER_STUDIO || defined(__DOXYGEN__) #if EFI_TUNER_STUDIO || defined(__DOXYGEN__)
void cjPostState(TunerStudioOutputChannels *tsOutputChannels); void cjPostState(TunerStudioOutputChannels *tsOutputChannels);
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
void initCJ125(Logging *shared DECLARE_ENGINE_PARAMETER_SUFFIX); void initCJ125(Logging *shared DECLARE_ENGINE_PARAMETER_SUFFIX);
float cjGetAfr(DECLARE_ENGINE_PARAMETER_SIGNATURE); float cjGetAfr(DECLARE_ENGINE_PARAMETER_SIGNATURE);
bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE); bool cjHasAfrSensor(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void cj125defaultPinout(); void cj125defaultPinout();