* hip9011: isolate debug stuff

* hip9011: renames

* hip9011: start sensing knockDetectionWindowStart degres from fire

* engine: more realistic values for knock window

* hip9011: remove magic CS toggling

* hip9011: EFI_HIP_9011_DEBUG should depend on EFI_HIP_9011
This commit is contained in:
Andrey G 2021-05-09 01:43:55 +03:00 committed by GitHub
parent dd6b60de2e
commit e18ba01fe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 101 additions and 57 deletions

View File

@ -652,7 +652,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_
#endif // HW_CHECK_MODE
tsOutputChannels->isWarnNow = engine->engineState.warnings.isWarningNow(timeSeconds, true);
#if EFI_HIP_9011
#if EFI_HIP_9011_DEBUG
tsOutputChannels->isKnockChipOk = (instance.invalidResponsesCount == 0);
#endif /* EFI_HIP_9011 */
@ -830,7 +830,7 @@ void updateTunerStudioState(TunerStudioOutputChannels *tsOutputChannels DECLARE_
case DBG_CRANKING_DETAILS:
tsOutputChannels->debugIntField1 = engine->rpmCalculator.getRevolutionCounterSinceStart();
break;
#if EFI_HIP_9011
#if EFI_HIP_9011_DEBUG
case DBG_KNOCK:
// todo: maybe extract hipPostState(tsOutputChannels);
tsOutputChannels->debugIntField1 = instance.correctResponsesCount;

View File

@ -1041,8 +1041,12 @@ static void setDefaultEngineConfiguration(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
engineConfiguration->debugMode = DBG_ALTERNATOR_PID;
engineConfiguration->acIdleRpmBump = 200;
engineConfiguration->knockDetectionWindowStart = 35;
engineConfiguration->knockDetectionWindowEnd = 135;
/* these two are used for HIP9011 only
* Currently this is offset from fire event, not TDC */
/* TODO: convert to offset from TDC */
engineConfiguration->knockDetectionWindowStart = 15.0 + 5.0;
engineConfiguration->knockDetectionWindowEnd = 15.0 + 45.0;
/**
* this is RPM. 10000 rpm is only 166Hz, 800 rpm is 13Hz

View File

@ -207,7 +207,7 @@ if (engineConfiguration->debugMode == DBG_DWELL_METRIC) {
startKnockSampling(event->cylinderNumber);
#endif
#if EFI_HIP_9011
hip9011_startKnockSampling(event->cylinderNumber, nowNt);
hip9011_onFireEvent(event->cylinderNumber, nowNt);
#endif
}

View File

@ -68,6 +68,10 @@ static NamedOutputPin Cs(PROTOCOL_HIP_NAME);
class Hip9011Hardware : public Hip9011HardwareInterface {
int sendSyncCommand(uint8_t command, uint8_t *rx_ptr) override;
public:
scheduling_s startTimer;
scheduling_s endTimer;
private:
int checkResponseDefMode(uint8_t tx, uint8_t rx);
int checkResponseAdvMode(uint8_t tx, uint8_t rx);
@ -84,12 +88,12 @@ static SPIDriver *spi;
static Hip9011Hardware hardware;
static float normalizedValue[HIP_INPUT_CHANNELS];
static float normalizedValueMax[HIP_INPUT_CHANNELS];
HIP9011 instance(&hardware);
static scheduling_s endTimer;
#if EFI_HIP_9011_DEBUG
static float normalizedValue[HIP_INPUT_CHANNELS];
static float normalizedValueMax[HIP_INPUT_CHANNELS];
#endif
// SPI_CR1_BR_1 // 5MHz
// SPI_CR1_CPHA Clock Phase
@ -116,7 +120,9 @@ static SPIConfig hipSpiCfg = {
/* Forward declarations */
/*==========================================================================*/
static void hip_addconsoleActions(void);
#if EFI_HIP_9011_DEBUG
static void hip_addconsoleActions(void);
#endif
/*==========================================================================*/
/* Local functions. */
@ -193,11 +199,13 @@ int Hip9011Hardware::sendSyncCommand(uint8_t tx, uint8_t *rx_ptr) {
else
ret = checkResponseDefMode(tx, rx);
/* statistic counters */
if (ret)
instance.invalidResponsesCount++;
else
instance.correctResponsesCount++;
#if EFI_HIP_9011_DEBUG
/* statistic counters */
if (ret)
instance.invalidResponsesCount++;
else
instance.correctResponsesCount++;
#endif
return ret;
}
@ -222,31 +230,35 @@ static int hip_wake_driver(void)
return 0;
}
static void startIntegration(void *) {
if (instance.state == READY_TO_INTEGRATE) {
static void startIntegration(HIP9011 *hip) {
if (hip->state == READY_TO_INTEGRATE) {
/**
* SPI communication is only allowed while not integrating, so we postpone the exchange
* until we are done integrating
*/
instance.state = IS_INTEGRATING;
hip->state = IS_INTEGRATING;
intHold.setHigh();
} else {
#if EFI_HIP_9011_DEBUG
hip->overrun++;
#endif
}
}
static void endIntegration(void *) {
static void endIntegration(HIP9011 *hip) {
/**
* isIntegrating could be 'false' if an SPI command was pending thus we did not integrate during this
* engine cycle
*/
if (instance.state == IS_INTEGRATING) {
if (hip->state == IS_INTEGRATING) {
intHold.setLow();
if (instance.adv_mode) {
/* read value over SPI in thread mode */
instance.state = NOT_READY;
hip->state = NOT_READY;
hip_wake_driver();
} else {
/* wait for ADC samples */
instance.state = WAITING_FOR_ADC_TO_SKIP;
hip->state = WAITING_FOR_ADC_TO_SKIP;
}
}
}
@ -254,29 +266,37 @@ static void endIntegration(void *) {
/**
* Ignition callback used to start HIP integration and schedule finish
*/
void hip9011_startKnockSampling(uint8_t cylinderNumber, efitick_t nowNt) {
void hip9011_onFireEvent(uint8_t cylinderNumber, efitick_t nowNt) {
if (!CONFIG(isHip9011Enabled))
return;
/* overrun? */
if (instance.state != READY_TO_INTEGRATE) {
instance.overrun++;
return;
}
/* We are not checking here for READY_TO_INTEGRATE state as
* previous integration may be stil in progress, while
* we are scheduling next integration start only
* knockDetectionWindowStart from now.
* Check for correct state will be done at startIntegration () */
if (cylinderNumber == instance.expectedCylinderNumber) {
/* save currect cylinder */
instance.cylinderNumber = cylinderNumber;
startIntegration(NULL);
/* TODO: reference to knockDetectionWindowStart */
scheduleByAngle(&endTimer, nowNt,
engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart,
&endIntegration);
/* smart books says we need to sence knock few degrees after TDC
* currently I have no idea how to hook to cylinder TDC in correct way.
* So schedule start of integration + knockDetectionWindowStart from fire event
* Keep this is mind when setting knockDetectionWindowStart */
scheduleByAngle(&hardware.startTimer, nowNt,
engineConfiguration->knockDetectionWindowStart,
{ startIntegration, &instance });
scheduleByAngle(&hardware.endTimer, nowNt,
engineConfiguration->knockDetectionWindowEnd,
{ endIntegration, &instance });
} else {
/* out of sync */
if (instance.expectedCylinderNumber >= 0)
instance.unsync++;
#if EFI_HIP_9011_DEBUG
/* out of sync */
if (instance.expectedCylinderNumber >= 0)
instance.unsync++;
#endif
/* save currect cylinder */
instance.cylinderNumber = cylinderNumber;
/* Skip integration, call driver task to prepare for next cylinder */
@ -374,8 +394,10 @@ static int hip_init(void) {
}
}
/* reset error counter now */
instance.invalidResponsesCount = 0;
#if EFI_HIP_9011_DEBUG
/* reset error counter now */
instance.invalidResponsesCount = 0;
#endif
instance.state = READY_TO_INTEGRATE;
@ -389,6 +411,9 @@ static msg_t hipThread(void *arg) {
UNUSED(arg);
chRegSetThreadName("hip9011 worker");
/* This strange code was here before me.
* Not sure why we need it */
#if 0
/* Acquire ownership of the bus. */
spiAcquireBus(spi);
// some time to let the hardware start
@ -400,10 +425,8 @@ static msg_t hipThread(void *arg) {
/* Ownership release. */
spiReleaseBus(spi);
/* init semaphore */
chSemObjectInit(&wake, 10);
chThdSleepMilliseconds(100);
#endif
do {
/* retry until success */
@ -470,10 +493,6 @@ static msg_t hipThread(void *arg) {
/* Check for correct cylinder/input */
if (correctCylinder) {
/* debug */
normalizedValue[idx] = knockNormalized;
normalizedValueMax[idx] = maxF(knockNormalized, normalizedValueMax[idx]);
/* report */
engine->knockLogic(knockVolts);
@ -481,8 +500,13 @@ static msg_t hipThread(void *arg) {
tsOutputChannels.knockLevels[instance.cylinderNumber] = knockVolts;
tsOutputChannels.knockLevel = knockVolts;
/* counters */
instance.samples++;
#if EFI_HIP_9011_DEBUG
/* debug */
normalizedValue[idx] = knockNormalized;
normalizedValueMax[idx] = maxF(knockNormalized, normalizedValueMax[idx]);
/* counters */
instance.samples++;
#endif
} else {
/* out of sync event already calculated, nothing to do */
}
@ -531,15 +555,21 @@ void initHip9011() {
efiPrintf("Starting HIP9011/TPIC8101 driver");
/* init semaphore */
chSemObjectInit(&wake, 10);
chThdCreateStatic(hipThreadStack, sizeof(hipThreadStack), PRIO_HIP9011, (tfunc_t)(void*) hipThread, NULL);
hip_addconsoleActions();
#if EFI_HIP_9011_DEBUG
hip_addconsoleActions();
#endif
}
/*==========================================================================*/
/* Debug functions. */
/*==========================================================================*/
#if EFI_HIP_9011_DEBUG
static const char *hip_state_names[] =
{
"Not ready/calculating",
@ -663,4 +693,6 @@ static void hip_addconsoleActions(void) {
addConsoleActionI("set_max_knock_sub_deg", setMaxKnockSubDeg);
}
#endif /* EFI_HIP_9011_DEBUG */
#endif /* EFI_HIP_9011 */

View File

@ -17,4 +17,4 @@ void stopHip9001_pins();
void hipAdcCallback(adcsample_t value);
#endif /* HAL_USE_ADC */
void hip9011_startKnockSampling(uint8_t cylinderNumber, efitick_t nowNt);
void hip9011_onFireEvent(uint8_t cylinderNumber, efitick_t nowNt);

View File

@ -10,6 +10,11 @@
#include "efifeatures.h"
#include "rusefi_enums.h"
/* enable debug by default */
#ifndef EFI_HIP_9011_DEBUG
#define EFI_HIP_9011_DEBUG EFI_HIP_9011
#endif
#define PIF 3.14159f
#define HIP9011_BAND(bore) (900 / (PIF * (bore) / 2))
#define HIP9011_DESIRED_OUTPUT_VALUE 5.0f
@ -102,11 +107,8 @@ public:
uint8_t gainIdx = 0xff;
uint8_t channelIdx = 0xff;
int correctResponsesCount = 0;
int invalidResponsesCount = 0;
float angleWindowWidth = - 1;
int totalKnockEventsCount = 0;
Hip9011HardwareInterface *hw;
bool adv_mode = false;
/**
@ -124,12 +126,18 @@ public:
int8_t expectedCylinderNumber = -1;
int rawValue[HIP_INPUT_CHANNELS];
/* counters */
int samples = 0;
int overrun = 0;
int unsync = 0;
float rpmLookup[INT_LOOKUP_SIZE];
#if EFI_HIP_9011_DEBUG
/* SPI counters */
int correctResponsesCount = 0;
int invalidResponsesCount = 0;
/* counters */
int samples = 0;
int overrun = 0;
int unsync = 0;
#endif
};
// 0b010x.xxxx