2015-07-10 06:01:56 -07:00
|
|
|
|
/**
|
|
|
|
|
* @file HIP9011.cpp
|
|
|
|
|
* @brief HIP9011/TPIC8101 driver
|
|
|
|
|
*
|
2017-01-27 09:03:05 -08:00
|
|
|
|
* Jan 2017 status:
|
|
|
|
|
* 1) seems to be kind of working - reacts to parameter changes and does produce variable output
|
|
|
|
|
* 2) only one (first) channel is currently used
|
|
|
|
|
* 3) engine control does not yet react to knock since very little actual testing - no engine runs with proven knock yet
|
|
|
|
|
*
|
|
|
|
|
*
|
2017-01-27 18:04:23 -08:00
|
|
|
|
* http://rusefi.com/forum/viewtopic.php?f=4&t=400
|
|
|
|
|
* http://rusefi.com/forum/viewtopic.php?f=5&t=778
|
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
|
* pin1 VDD
|
|
|
|
|
* pin2 GND
|
|
|
|
|
*
|
|
|
|
|
* pin8 Chip Select - CS
|
2017-01-27 09:03:05 -08:00
|
|
|
|
* pin11 Slave Data Out - MISO
|
2015-07-10 06:01:56 -07:00
|
|
|
|
* pin12 Slave Data In - MOSI
|
|
|
|
|
* pin13 SPI clock - SCLK
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* http://www.ti.com/lit/ds/symlink/tpic8101.pdf
|
|
|
|
|
* http://www.intersil.com/content/dam/Intersil/documents/hip9/hip9011.pdf
|
|
|
|
|
* http://www.intersil.com/content/dam/Intersil/documents/an97/an9770.pdf
|
|
|
|
|
* http://e2e.ti.com/cfs-file/__key/telligent-evolution-components-attachments/00-26-01-00-00-42-36-40/TPIC8101-Training.pdf
|
|
|
|
|
*
|
|
|
|
|
* max SPI frequency: 5MHz max
|
|
|
|
|
*
|
|
|
|
|
* @date Nov 27, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
|
* @Spilly
|
|
|
|
|
*/
|
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
#include "hardware.h"
|
|
|
|
|
#include "trigger_central.h"
|
2019-04-01 14:04:49 -07:00
|
|
|
|
#include "hip9011_logic.h"
|
|
|
|
|
#include "hip9011.h"
|
2021-09-20 13:10:34 -07:00
|
|
|
|
#include "knock_logic.h"
|
2019-03-31 14:44:34 -07:00
|
|
|
|
|
2018-11-03 10:27:48 -07:00
|
|
|
|
#if EFI_PROD_CODE
|
2020-01-06 05:44:23 -08:00
|
|
|
|
#include "mpu_util.h"
|
2021-03-30 07:56:25 -07:00
|
|
|
|
#include "os_util.h"
|
2018-11-03 10:27:48 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
|
#if EFI_HIP_9011
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Local definitions. */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Local variables and types. */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
2019-08-18 12:04:02 -07:00
|
|
|
|
static NamedOutputPin intHold(PROTOCOL_HIP_NAME);
|
2021-03-20 05:40:36 -07:00
|
|
|
|
static NamedOutputPin Cs(PROTOCOL_HIP_NAME);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2019-06-08 06:51:36 -07:00
|
|
|
|
class Hip9011Hardware : public Hip9011HardwareInterface {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
int sendSyncCommand(uint8_t command, uint8_t *rx_ptr) override;
|
2021-05-08 06:50:28 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
public:
|
|
|
|
|
scheduling_s startTimer;
|
|
|
|
|
scheduling_s endTimer;
|
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
private:
|
|
|
|
|
int checkResponseDefMode(uint8_t tx, uint8_t rx);
|
|
|
|
|
int checkResponseAdvMode(uint8_t tx, uint8_t rx);
|
|
|
|
|
|
|
|
|
|
uint8_t rep_mask;
|
|
|
|
|
uint8_t rep_value;
|
2019-01-07 20:55:56 -08:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* TODO: include following stuff in object */
|
|
|
|
|
/* wake semaphore */
|
|
|
|
|
static semaphore_t wake;
|
|
|
|
|
|
|
|
|
|
static SPIDriver *spi;
|
|
|
|
|
|
2019-01-07 20:55:56 -08:00
|
|
|
|
static Hip9011Hardware hardware;
|
|
|
|
|
|
|
|
|
|
HIP9011 instance(&hardware);
|
2019-01-03 21:22:35 -08:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
static float normalizedValue[HIP_INPUT_CHANNELS];
|
|
|
|
|
static float normalizedValueMax[HIP_INPUT_CHANNELS];
|
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
|
|
// SPI_CR1_BR_1 // 5MHz
|
|
|
|
|
// SPI_CR1_CPHA Clock Phase
|
|
|
|
|
// todo: nicer method which would mention SPI speed explicitly?
|
|
|
|
|
|
2018-11-03 06:44:34 -07:00
|
|
|
|
#if EFI_PROD_CODE
|
2020-01-06 05:44:23 -08:00
|
|
|
|
static SPIConfig hipSpiCfg = {
|
|
|
|
|
.circular = false,
|
|
|
|
|
.end_cb = NULL,
|
|
|
|
|
.ssport = NULL,
|
|
|
|
|
.sspad = 0,
|
|
|
|
|
.cr1 =
|
2021-03-20 05:40:36 -07:00
|
|
|
|
SPI_CR1_8BIT_MODE |
|
2020-01-06 05:44:23 -08:00
|
|
|
|
SPI_CR1_MSTR |
|
|
|
|
|
SPI_CR1_CPHA |
|
|
|
|
|
//SPI_CR1_BR_1 // 5MHz
|
2021-03-20 05:40:36 -07:00
|
|
|
|
SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2,
|
2020-01-06 05:44:23 -08:00
|
|
|
|
.cr2 =
|
|
|
|
|
SPI_CR2_8BIT_MODE
|
|
|
|
|
};
|
2019-01-03 19:53:34 -08:00
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Forward declarations */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static void hip_addconsoleActions();
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#endif
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Local functions. */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
int Hip9011Hardware::checkResponseDefMode(uint8_t tx, uint8_t rx) {
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* in default SPI mode SDO is directly equals the SDI (echo function) */
|
2021-03-30 07:56:25 -07:00
|
|
|
|
if (tx == rx) {
|
|
|
|
|
return 0;
|
2015-07-10 15:02:29 -07:00
|
|
|
|
} else {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
return -1;
|
2015-07-10 15:02:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
int Hip9011Hardware::checkResponseAdvMode(uint8_t tx, uint8_t rx) {
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
/* check reply */
|
|
|
|
|
if ((rx & rep_mask) != rep_value)
|
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
|
|
/* extract mask and value for next reply */
|
|
|
|
|
if ((tx & 0xe0) == SET_PRESCALER_CMD(0)){
|
|
|
|
|
/* D7 to D0 of digital integrator output */
|
|
|
|
|
rep_mask = 0x00;
|
|
|
|
|
rep_value = 0x00;
|
|
|
|
|
} else if ((tx & 0xfe) == SET_CHANNEL_CMD(0)) {
|
|
|
|
|
/* D9 to D8 of digital integrator output and six zeroes */
|
|
|
|
|
rep_mask = 0x3f;
|
|
|
|
|
rep_value = 0x00;
|
|
|
|
|
} else if ((tx & 0xc0) == SET_BAND_PASS_CMD(0)) {
|
|
|
|
|
rep_mask = 0xff;
|
|
|
|
|
rep_value = SET_BAND_PASS_REP;
|
|
|
|
|
} else if ((tx & 0xc0) == SET_GAIN_CMD(0)) {
|
|
|
|
|
rep_mask = 0xff;
|
|
|
|
|
rep_value = SET_GAIN_REP;
|
|
|
|
|
} else if ((tx & 0xe0) == SET_INTEGRATOR_CMD(0)) {
|
|
|
|
|
rep_mask = 0xff;
|
|
|
|
|
rep_value = SET_INTEGRATOR_REP;
|
|
|
|
|
} else if ((tx & 0xff) == SET_ADVANCED_MODE_CMD) {
|
|
|
|
|
rep_mask = 0xff;
|
|
|
|
|
rep_value = SET_ADVANCED_MODE_REP;
|
|
|
|
|
} else {
|
|
|
|
|
/* unknown */
|
|
|
|
|
rep_mask = 0x00;
|
|
|
|
|
rep_value = 0x00;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
2021-04-04 15:13:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
int Hip9011Hardware::sendSyncCommand(uint8_t tx, uint8_t *rx_ptr) {
|
|
|
|
|
int ret;
|
|
|
|
|
uint8_t rx;
|
|
|
|
|
|
|
|
|
|
/* Acquire ownership of the bus. */
|
|
|
|
|
spiAcquireBus(spi);
|
|
|
|
|
/* Setup transfer parameters. */
|
|
|
|
|
spiStart(spi, &hipSpiCfg);
|
|
|
|
|
/* Slave Select assertion. */
|
|
|
|
|
spiSelect(spi);
|
|
|
|
|
/* Transfer */
|
|
|
|
|
rx = spiPolledExchange(spi, tx);
|
|
|
|
|
/* Slave Select de-assertion. */
|
|
|
|
|
spiUnselect(spi);
|
|
|
|
|
/* Ownership release. */
|
|
|
|
|
spiReleaseBus(spi);
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* received data */
|
|
|
|
|
if (rx_ptr)
|
|
|
|
|
*rx_ptr = rx;
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* check response */
|
2021-05-08 06:50:28 -07:00
|
|
|
|
if (instance.adv_mode)
|
2021-04-04 15:13:21 -07:00
|
|
|
|
ret = checkResponseAdvMode(tx, rx);
|
2021-05-08 06:50:28 -07:00
|
|
|
|
else
|
|
|
|
|
ret = checkResponseDefMode(tx, rx);
|
2019-01-07 20:55:56 -08:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
/* statistic counters */
|
|
|
|
|
if (ret)
|
|
|
|
|
instance.invalidResponsesCount++;
|
|
|
|
|
else
|
|
|
|
|
instance.correctResponsesCount++;
|
|
|
|
|
#endif
|
2019-01-07 20:55:56 -08:00
|
|
|
|
|
2021-04-04 15:13:21 -07:00
|
|
|
|
return ret;
|
2019-01-07 20:55:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static int hip_wake_driver()
|
2021-03-30 07:56:25 -07:00
|
|
|
|
{
|
|
|
|
|
/* Entering a reentrant critical zone.*/
|
|
|
|
|
syssts_t sts = chSysGetStatusAndLockX();
|
|
|
|
|
chSemSignalI(&wake);
|
|
|
|
|
if (!port_is_isr_context()) {
|
|
|
|
|
/**
|
|
|
|
|
* chSemSignalI above requires rescheduling
|
|
|
|
|
* interrupt handlers have implicit rescheduling
|
|
|
|
|
*/
|
|
|
|
|
chSchRescheduleS();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* Leaving the critical zone.*/
|
|
|
|
|
chSysRestoreStatusX(sts);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
static void startIntegration(HIP9011 *hip) {
|
|
|
|
|
if (hip->state == READY_TO_INTEGRATE) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
/**
|
|
|
|
|
* SPI communication is only allowed while not integrating, so we postpone the exchange
|
|
|
|
|
* until we are done integrating
|
|
|
|
|
*/
|
2021-05-08 15:43:55 -07:00
|
|
|
|
hip->state = IS_INTEGRATING;
|
2017-04-21 16:23:20 -07:00
|
|
|
|
intHold.setHigh();
|
2021-05-08 15:43:55 -07:00
|
|
|
|
} else {
|
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
hip->overrun++;
|
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
static void endIntegration(HIP9011 *hip) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
/**
|
|
|
|
|
* isIntegrating could be 'false' if an SPI command was pending thus we did not integrate during this
|
|
|
|
|
* engine cycle
|
|
|
|
|
*/
|
2021-05-08 15:43:55 -07:00
|
|
|
|
if (hip->state == IS_INTEGRATING) {
|
2017-04-21 16:23:20 -07:00
|
|
|
|
intHold.setLow();
|
2021-09-20 13:10:34 -07:00
|
|
|
|
|
|
|
|
|
hip->knockSampleTimestamp = getTimeNowNt();
|
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
if (instance.adv_mode) {
|
|
|
|
|
/* read value over SPI in thread mode */
|
2021-05-08 15:43:55 -07:00
|
|
|
|
hip->state = NOT_READY;
|
2021-05-08 06:50:28 -07:00
|
|
|
|
hip_wake_driver();
|
|
|
|
|
} else {
|
|
|
|
|
/* wait for ADC samples */
|
2021-05-08 15:43:55 -07:00
|
|
|
|
hip->state = WAITING_FOR_ADC_TO_SKIP;
|
2021-05-08 06:50:28 -07:00
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 14:39:21 -07:00
|
|
|
|
void onStartKnockSampling(uint8_t cylinderIndex, float samplingTimeSeconds, uint8_t channelIdx) {
|
|
|
|
|
/* TODO: @dron0gus: not sure if we need the expectedCylinderNumber logic at all
|
|
|
|
|
|
|
|
|
|
Something like this might be right:
|
|
|
|
|
|
|
|
|
|
startIntegration(&instance);
|
|
|
|
|
|
|
|
|
|
efitick_t windowLength = USF2NT(1e6 * samplingTimeSeconds);
|
|
|
|
|
|
|
|
|
|
engine->executor.scheduleByTimestampNt("knock", &hardware.endTimer, getTimeNowNt() + windowLength, { endIntegration, &instance });
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
/**
|
2021-03-27 11:12:49 -07:00
|
|
|
|
* Ignition callback used to start HIP integration and schedule finish
|
2015-07-10 06:01:56 -07:00
|
|
|
|
*/
|
2021-05-08 15:43:55 -07:00
|
|
|
|
void hip9011_onFireEvent(uint8_t cylinderNumber, efitick_t nowNt) {
|
2021-03-27 11:12:49 -07:00
|
|
|
|
if (!CONFIG(isHip9011Enabled))
|
2015-07-10 06:01:56 -07:00
|
|
|
|
return;
|
2020-03-24 17:19:19 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
/* 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 () */
|
2021-03-27 11:12:49 -07:00
|
|
|
|
|
2021-04-04 15:13:21 -07:00
|
|
|
|
if (cylinderNumber == instance.expectedCylinderNumber) {
|
|
|
|
|
/* save currect cylinder */
|
|
|
|
|
instance.cylinderNumber = cylinderNumber;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
/* 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 });
|
2021-04-04 15:13:21 -07:00
|
|
|
|
} else {
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
/* out of sync */
|
|
|
|
|
if (instance.expectedCylinderNumber >= 0)
|
|
|
|
|
instance.unsync++;
|
|
|
|
|
#endif
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* save currect cylinder */
|
|
|
|
|
instance.cylinderNumber = cylinderNumber;
|
|
|
|
|
/* Skip integration, call driver task to prepare for next cylinder */
|
|
|
|
|
instance.state = NOT_READY;
|
|
|
|
|
hip_wake_driver();
|
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 06:44:34 -07:00
|
|
|
|
void hipAdcCallback(adcsample_t adcValue) {
|
2021-05-08 06:50:28 -07:00
|
|
|
|
/* we read in digital mode */
|
|
|
|
|
if (instance.adv_mode)
|
|
|
|
|
return;
|
2019-01-03 21:22:35 -08:00
|
|
|
|
if (instance.state == WAITING_FOR_ADC_TO_SKIP) {
|
|
|
|
|
instance.state = WAITING_FOR_RESULT_ADC;
|
|
|
|
|
} else if (instance.state == WAITING_FOR_RESULT_ADC) {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* offload calculations to driver thread */
|
2021-05-08 06:50:28 -07:00
|
|
|
|
if (instance.channelIdx < HIP_INPUT_CHANNELS) {
|
|
|
|
|
instance.rawValue[instance.channelIdx] = adcValue;
|
|
|
|
|
}
|
2021-03-30 07:56:25 -07:00
|
|
|
|
instance.state = NOT_READY;
|
|
|
|
|
hip_wake_driver();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static int hip_testAdvMode() {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
int ret;
|
2021-04-04 15:13:21 -07:00
|
|
|
|
uint8_t ret0, ret1, ret2;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* do not care about configuration values, we meed replyes only.
|
|
|
|
|
* correct values will be uploaded later */
|
|
|
|
|
|
|
|
|
|
/* A control byte is written to the SDI and shifted with the MSB
|
|
|
|
|
* first. The response byte on the SDO is shifted out with the MSB
|
|
|
|
|
* first. The response byte corresponds to the previous command.
|
|
|
|
|
* Therefore, the SDI shifts in a control byte n and shifts out a
|
|
|
|
|
* response command byte n − 1. */
|
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_BAND_PASS_CMD(0), NULL);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
2021-04-04 15:13:21 -07:00
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_GAIN_CMD(0), &ret0);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
2021-04-04 15:13:21 -07:00
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_INTEGRATOR_CMD(0), &ret1);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_INTEGRATOR_CMD(0), &ret2);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
/* magic reply bytes from DS Table 2 */
|
|
|
|
|
if ((ret0 == SET_BAND_PASS_REP) &&
|
|
|
|
|
(ret1 == SET_GAIN_REP) &&
|
|
|
|
|
(ret2 == SET_INTEGRATOR_REP))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static int hip_init() {
|
2021-04-04 15:13:21 -07:00
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_PRESCALER_CMD(instance.prescaler), NULL);
|
|
|
|
|
if (ret) {
|
|
|
|
|
/* NOTE: hip9011/tpic8101 can be in default or advansed mode at this point
|
|
|
|
|
* If we supposed not to support advanced mode this is definitely error */
|
|
|
|
|
if (!CONFIG(useTpicAdvancedMode))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2015-07-10 20:01:24 -07:00
|
|
|
|
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* ...othervice or when no error is reported lets try to switch to advanced mode */
|
2019-12-11 14:48:55 -08:00
|
|
|
|
if (CONFIG(useTpicAdvancedMode)) {
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* enable advanced mode */
|
2021-03-30 07:56:25 -07:00
|
|
|
|
ret = instance.hw->sendSyncCommand(SET_ADVANCED_MODE_CMD, NULL);
|
2021-04-04 15:13:21 -07:00
|
|
|
|
if (ret) {
|
|
|
|
|
uint8_t rx;
|
|
|
|
|
/* communication error is detected for default mode...
|
|
|
|
|
* may be we are in advanced mode already?
|
|
|
|
|
* Now we dont care for return value */
|
|
|
|
|
instance.hw->sendSyncCommand(SET_ADVANCED_MODE_CMD, &rx);
|
|
|
|
|
if (rx != SET_ADVANCED_MODE_REP) {
|
|
|
|
|
/* this is realy a communication problem */
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* now we should be in advanced mode... if chip supports...
|
|
|
|
|
* set advanced mode flag now so checkResponse will switch to
|
|
|
|
|
* advanced mode checkig (not implemented) */
|
2021-03-30 07:56:25 -07:00
|
|
|
|
instance.adv_mode = true;
|
2021-04-04 15:13:21 -07:00
|
|
|
|
|
|
|
|
|
ret = hip_testAdvMode();
|
|
|
|
|
if (ret) {
|
|
|
|
|
warning(CUSTOM_OBD_KNOCK_PROCESSOR, "TPIC/HIP does not support advanced mode");
|
|
|
|
|
instance.adv_mode = false;
|
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
/* reset error counter now */
|
|
|
|
|
instance.invalidResponsesCount = 0;
|
|
|
|
|
#endif
|
2021-04-04 15:13:21 -07:00
|
|
|
|
|
2019-01-03 21:22:35 -08:00
|
|
|
|
instance.state = READY_TO_INTEGRATE;
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
|
|
|
|
return 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 15:59:09 -07:00
|
|
|
|
static THD_WORKING_AREA(hipThreadStack, UTILITY_THREAD_STACK_SIZE);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
|
|
static msg_t hipThread(void *arg) {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
int ret;
|
2021-03-20 05:40:36 -07:00
|
|
|
|
UNUSED(arg);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
chRegSetThreadName("hip9011 worker");
|
2015-07-11 12:01:54 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
/* This strange code was here before me.
|
|
|
|
|
* Not sure why we need it */
|
|
|
|
|
#if 0
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* Acquire ownership of the bus. */
|
|
|
|
|
spiAcquireBus(spi);
|
2015-07-11 12:01:54 -07:00
|
|
|
|
// some time to let the hardware start
|
2021-03-20 05:40:36 -07:00
|
|
|
|
Cs.setValue(true);
|
2015-07-11 12:01:54 -07:00
|
|
|
|
chThdSleepMilliseconds(100);
|
2021-03-20 05:40:36 -07:00
|
|
|
|
Cs.setValue(false);
|
2015-07-11 12:01:54 -07:00
|
|
|
|
chThdSleepMilliseconds(100);
|
2021-03-20 05:40:36 -07:00
|
|
|
|
Cs.setValue(true);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* Ownership release. */
|
|
|
|
|
spiReleaseBus(spi);
|
2015-07-11 12:01:54 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
chThdSleepMilliseconds(100);
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#endif
|
2015-07-11 12:01:54 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
do {
|
|
|
|
|
/* retry until success */
|
|
|
|
|
ret = hip_init();
|
|
|
|
|
if (ret) {
|
|
|
|
|
warning(CUSTOM_OBD_KNOCK_PROCESSOR, "TPIC/HIP does not respond: %d", ret);
|
|
|
|
|
chThdSleepMilliseconds(10 * 1000);
|
|
|
|
|
}
|
|
|
|
|
} while (ret);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
msg_t msg;
|
|
|
|
|
|
|
|
|
|
/* load new/updated settings */
|
|
|
|
|
instance.handleSettings(GET_RPM() DEFINE_PARAM_SUFFIX(PASS_HIP_PARAMS));
|
2021-05-08 06:50:28 -07:00
|
|
|
|
/* in advanced more driver will set channel while reading integrator value */
|
|
|
|
|
if (!instance.adv_mode) {
|
|
|
|
|
/* switch input channel */
|
|
|
|
|
instance.handleChannel(DEFINE_PARAM_SUFFIX(PASS_HIP_PARAMS));
|
|
|
|
|
}
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* State */
|
|
|
|
|
instance.state = READY_TO_INTEGRATE;
|
|
|
|
|
|
|
|
|
|
msg = chSemWaitTimeout(&wake, TIME_INFINITE);
|
|
|
|
|
if (msg == MSG_TIMEOUT) {
|
|
|
|
|
/* ??? */
|
|
|
|
|
} else {
|
2021-05-08 06:50:28 -07:00
|
|
|
|
int rawValue;
|
|
|
|
|
/* check now, before readValueAndHandleChannel did not overwrite expectedCylinderNumber */
|
|
|
|
|
bool correctCylinder = (instance.cylinderNumber == instance.expectedCylinderNumber);
|
|
|
|
|
|
|
|
|
|
/* this needs to be called in any case to set proper channel for next cycle */
|
|
|
|
|
if (instance.adv_mode) {
|
|
|
|
|
rawValue = instance.readValueAndHandleChannel(DEFINE_PARAM_SUFFIX(PASS_HIP_PARAMS));
|
|
|
|
|
|
|
|
|
|
/* spi communication issue? */
|
|
|
|
|
if (rawValue < 0)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check that we know channel for current measurement */
|
|
|
|
|
int idx = instance.channelIdx;
|
|
|
|
|
if (!(idx < HIP_INPUT_CHANNELS))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
float knockNormalized = 0.0f;
|
|
|
|
|
float knockVolts = 0.0f;
|
|
|
|
|
|
|
|
|
|
/* calculations */
|
|
|
|
|
if (instance.adv_mode) {
|
|
|
|
|
/* store for debug */
|
|
|
|
|
instance.rawValue[idx] = rawValue;
|
|
|
|
|
/* convert 10 bit integer value to 0.0 .. 1.0 float */
|
|
|
|
|
knockNormalized = ((float)rawValue) / HIP9011_DIGITAL_OUTPUT_MAX;
|
|
|
|
|
/* convert to magic volts */
|
|
|
|
|
knockVolts = knockNormalized * HIP9011_DESIRED_OUTPUT_VALUE;
|
|
|
|
|
} else {
|
|
|
|
|
rawValue = instance.rawValue[idx];
|
|
|
|
|
/* first calculate ouput volts */
|
|
|
|
|
knockVolts = adcToVolts(rawValue) * CONFIG(analogInputDividerCoefficient);
|
|
|
|
|
/* and then normalize */
|
|
|
|
|
knockNormalized = knockVolts / HIP9011_DESIRED_OUTPUT_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 15:13:21 -07:00
|
|
|
|
/* Check for correct cylinder/input */
|
2021-05-08 06:50:28 -07:00
|
|
|
|
if (correctCylinder) {
|
2021-09-20 13:10:34 -07:00
|
|
|
|
// TODO: convert knock level to dBv
|
2021-11-01 20:33:59 -07:00
|
|
|
|
engine->knockController.onKnockSenseCompleted(instance.cylinderNumber, knockVolts, instance.knockSampleTimestamp);
|
2021-04-04 15:13:21 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
/* debug */
|
|
|
|
|
normalizedValue[idx] = knockNormalized;
|
|
|
|
|
normalizedValueMax[idx] = maxF(knockNormalized, normalizedValueMax[idx]);
|
|
|
|
|
/* counters */
|
|
|
|
|
instance.samples++;
|
|
|
|
|
#endif
|
2021-04-04 15:13:21 -07:00
|
|
|
|
} else {
|
|
|
|
|
/* out of sync event already calculated, nothing to do */
|
2021-03-30 07:56:25 -07:00
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Exported functions. */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
2019-04-19 12:11:00 -07:00
|
|
|
|
void stopHip9001_pins() {
|
2020-12-18 14:18:12 -08:00
|
|
|
|
intHold.deInit();
|
2021-03-20 05:40:36 -07:00
|
|
|
|
Cs.deInit();
|
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
|
hipSpiCfg.ssport = NULL;
|
|
|
|
|
#endif
|
2019-04-19 12:11:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startHip9001_pins() {
|
2019-12-11 14:48:55 -08:00
|
|
|
|
intHold.initPin("hip int/hold", CONFIG(hip9011IntHoldPin), &CONFIG(hip9011IntHoldPinMode));
|
2021-03-20 05:40:36 -07:00
|
|
|
|
Cs.initPin("hip CS", CONFIG(hip9011CsPin), &CONFIG(hip9011CsPinMode));
|
2019-04-19 12:11:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 11:28:48 -07:00
|
|
|
|
void initHip9011() {
|
2019-12-11 14:48:55 -08:00
|
|
|
|
if (!CONFIG(isHip9011Enabled))
|
2015-07-10 06:01:56 -07:00
|
|
|
|
return;
|
|
|
|
|
|
2018-11-03 06:44:34 -07:00
|
|
|
|
#if EFI_PROD_CODE
|
2021-03-30 07:56:25 -07:00
|
|
|
|
spi = getSpiDevice(CONFIG(hip9011SpiDevice));
|
|
|
|
|
if (spi == NULL) {
|
2019-03-26 06:38:23 -07:00
|
|
|
|
// error already reported
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2019-12-11 14:48:55 -08:00
|
|
|
|
hipSpiCfg.ssport = getHwPort("hip", CONFIG(hip9011CsPin));
|
|
|
|
|
hipSpiCfg.sspad = getHwPin("hip", CONFIG(hip9011CsPin));
|
2019-05-27 14:02:46 -07:00
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2019-04-19 12:11:00 -07:00
|
|
|
|
startHip9001_pins();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-03-30 07:56:25 -07:00
|
|
|
|
/* load settings */
|
|
|
|
|
instance.prescaler = CONFIG(hip9011PrescalerAndSDO);
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf("Starting HIP9011/TPIC8101 driver");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
/* init semaphore */
|
|
|
|
|
chSemObjectInit(&wake, 10);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
chThdCreateStatic(hipThreadStack, sizeof(hipThreadStack), PRIO_HIP9011, (tfunc_t)(void*) hipThread, NULL);
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
hip_addconsoleActions();
|
|
|
|
|
#endif
|
2021-03-30 07:56:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
/* Debug functions. */
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#if EFI_HIP_9011_DEBUG
|
|
|
|
|
|
2021-05-08 07:08:18 -07:00
|
|
|
|
static const char *hip_state_names[] =
|
|
|
|
|
{
|
|
|
|
|
"Not ready/calculating",
|
|
|
|
|
"Ready for integration",
|
|
|
|
|
"Integrating",
|
|
|
|
|
"Waiting for first ADC sample",
|
|
|
|
|
"Waiting for second ADC sample"
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static void showHipInfo() {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
if (!CONFIG(isHip9011Enabled)) {
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf("hip9011 driver not active");
|
2021-03-30 07:56:25 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf("HIP9011: enabled %s state %s",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
boolToString(CONFIG(isHip9011Enabled)),
|
2021-05-08 07:08:18 -07:00
|
|
|
|
hip_state_names[instance.state]);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Advanced mode: enabled %d used %d",
|
2021-04-04 15:13:21 -07:00
|
|
|
|
CONFIG(useTpicAdvancedMode),
|
|
|
|
|
instance.adv_mode);
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Input Ch %d (cylinder %d next %d)",
|
2021-04-04 15:13:21 -07:00
|
|
|
|
instance.channelIdx,
|
|
|
|
|
instance.cylinderNumber,
|
|
|
|
|
instance.expectedCylinderNumber);
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Cyl bore %.2fmm freq %.2fkHz band idx 0x%x",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
engineConfiguration->cylinderBore,
|
2021-04-04 15:13:21 -07:00
|
|
|
|
instance.getBand(PASS_HIP_PARAMS),
|
|
|
|
|
instance.bandIdx);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Integrator idx 0x%x",
|
2021-04-04 15:13:21 -07:00
|
|
|
|
instance.intergratorIdx);
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Gain %.2f idx 0x%x",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
engineConfiguration->hip9011Gain,
|
2021-04-04 15:13:21 -07:00
|
|
|
|
instance.gainIdx);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" PaSDO=0x%x",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
instance.prescaler);
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" IntHold %s (mode 0x%x)",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
hwPortname(CONFIG(hip9011IntHoldPin)),
|
2021-04-04 15:13:21 -07:00
|
|
|
|
CONFIG(hip9011IntHoldPinMode));
|
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Spi %s CS %s (mode 0x%x)",
|
2021-04-04 15:13:21 -07:00
|
|
|
|
getSpi_device_e(engineConfiguration->hip9011SpiDevice),
|
|
|
|
|
hwPortname(CONFIG(hip9011CsPin)),
|
|
|
|
|
CONFIG(hip9011CsPinMode));
|
2021-03-30 07:56:25 -07:00
|
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
2021-04-21 11:28:48 -07:00
|
|
|
|
printSpiConfig("hip9011", CONFIG(hip9011SpiDevice));
|
2021-03-30 07:56:25 -07:00
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
efiPrintf(" SPI: good response %d incorrect response %d",
|
2021-04-04 15:13:21 -07:00
|
|
|
|
instance.correctResponsesCount,
|
|
|
|
|
instance.invalidResponsesCount);
|
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
efiPrintf(" Counters: samples %d overruns %d sync miss %d",
|
|
|
|
|
instance.samples, instance.overrun, instance.unsync);
|
2021-04-04 15:13:21 -07:00
|
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
|
efiPrintf(" Window start %.2f end %.2f",
|
2021-03-30 07:56:25 -07:00
|
|
|
|
engineConfiguration->knockDetectionWindowStart,
|
|
|
|
|
engineConfiguration->knockDetectionWindowEnd);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
2021-05-08 06:50:28 -07:00
|
|
|
|
if (!instance.adv_mode) {
|
|
|
|
|
efiPrintf(" Adc input %s (%.2f V)",
|
|
|
|
|
getAdc_channel_e(engineConfiguration->hipOutputChannel),
|
|
|
|
|
getVoltage("hipinfo", engineConfiguration->hipOutputChannel));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < HIP_INPUT_CHANNELS; i++) {
|
|
|
|
|
efiPrintf(" input[%d] %d -> %.3f (max %.3f)",
|
|
|
|
|
i,
|
|
|
|
|
instance.rawValue[i],
|
|
|
|
|
normalizedValue[i],
|
|
|
|
|
normalizedValueMax[i]);
|
|
|
|
|
normalizedValueMax[i] = 0.0;
|
|
|
|
|
}
|
2021-03-30 07:56:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void setPrescalerAndSDO(int value) {
|
|
|
|
|
engineConfiguration->hip9011PrescalerAndSDO = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void setHipBand(float value) {
|
|
|
|
|
engineConfiguration->knockBandCustom = value;
|
|
|
|
|
showHipInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void setHipGain(float value) {
|
|
|
|
|
engineConfiguration->hip9011Gain = value;
|
|
|
|
|
showHipInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
|
static void hip_addconsoleActions() {
|
2021-03-30 07:56:25 -07:00
|
|
|
|
addConsoleAction("hipinfo", showHipInfo);
|
2017-01-06 14:01:28 -08:00
|
|
|
|
addConsoleActionF("set_gain", setHipGain);
|
|
|
|
|
addConsoleActionF("set_band", setHipBand);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
addConsoleActionI("set_hip_prescalerandsdo", setPrescalerAndSDO);
|
2021-03-30 07:56:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:43:55 -07:00
|
|
|
|
#endif /* EFI_HIP_9011_DEBUG */
|
|
|
|
|
|
2017-04-09 19:07:41 -07:00
|
|
|
|
#endif /* EFI_HIP_9011 */
|