diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index c0fbbb6013..0308c4efdb 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -50,18 +50,19 @@ static int currentIntergratorIndex = -1; static int settingUpdateCount = 0; static int totalKnockEventsCount = 0; -static hip_state_e state = READY_TO_INTEGRATE; static efitimeus_t timeOfLastKnockEvent = 0; /** * Int/Hold pin is controlled from scheduler callbacks which are set according to current RPM * - * The following flags make sure that we only have SPI communication while not integrating + * The following state make sure that we only have SPI communication while not integrating and that we take + * a good ADC reading after integrating. + * + * Once integtation window is over, we wait for the 2nd ADC callback and then initiate SPI communication if needed + * + * hipOutput should be set to used FAST adc device */ -static bool_t isIntegrating = false; -/** - */ -static bool_t isSendingSpiCommand = false; +static hip_state_e state = READY_TO_INTEGRATE; static scheduling_s startTimer[2]; static scheduling_s endTimer[2]; @@ -128,12 +129,11 @@ void setHip9011FrankensoPinout(void) { } static void startIntegration(void) { - if (!isSendingSpiCommand) { + if (state == READY_TO_INTEGRATE) { /** * SPI communication is only allowed while not integrating, so we postpone the exchange * until we are done integrating */ - isIntegrating = true; state = IS_INTEGRATING; intHold.setValue(true); } @@ -144,35 +144,9 @@ static void endIntegration(void) { * isIntegrating could be 'false' if an SPI command was pending thus we did not integrate during this * engine cycle */ - if (isIntegrating) { + if (state == IS_INTEGRATING) { intHold.setValue(false); - isIntegrating = false; - - int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); - int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); - - if (currentGainIndex != gainIndex) { - state = IS_SENDING_SPI_COMMAND; - tx_buff[0] = gainIndex; - currentGainIndex = gainIndex; - - isSendingSpiCommand = true; - spiSelectI(driver); - spiStartExchangeI(driver, 1, tx_buff, rx_buff); - return; - } - - if (currentIntergratorIndex != integratorIndex) { - state = IS_SENDING_SPI_COMMAND; - tx_buff[0] = integratorIndex; - currentIntergratorIndex = integratorIndex; - - isSendingSpiCommand = true; - spiSelectI(driver); - spiStartExchangeI(driver, 1, tx_buff, rx_buff); - return; - } - state = READY_TO_INTEGRATE; + state = WAITING_FOR_ADC_TO_SKIP; } } @@ -205,7 +179,7 @@ static void setGain(float value) { static void endOfSpiCommunication(SPIDriver *spip) { spiUnselectI(driver); - isSendingSpiCommand = false; + state = READY_TO_INTEGRATE; } void hipAdcCallback(adcsample_t value) { @@ -216,6 +190,28 @@ void hipAdcCallback(adcsample_t value) { totalKnockEventsCount++; timeOfLastKnockEvent = getTimeNowUs(); } + + + int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); + int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); + + if (currentGainIndex != gainIndex) { + state = IS_SENDING_SPI_COMMAND; + tx_buff[0] = gainIndex; + currentGainIndex = gainIndex; + + spiSelectI(driver); + spiStartExchangeI(driver, 1, tx_buff, rx_buff); + } else if (currentIntergratorIndex != integratorIndex) { + state = IS_SENDING_SPI_COMMAND; + tx_buff[0] = integratorIndex; + currentIntergratorIndex = integratorIndex; + + spiSelectI(driver); + spiStartExchangeI(driver, 1, tx_buff, rx_buff); + } else { + state = READY_TO_INTEGRATE; + } } } diff --git a/firmware/hw_layer/HIP9011.h b/firmware/hw_layer/HIP9011.h index b3e26cc54b..2962fa79a2 100644 --- a/firmware/hw_layer/HIP9011.h +++ b/firmware/hw_layer/HIP9011.h @@ -10,11 +10,26 @@ #define HIP9011_H_ typedef enum { + /** + * the step after this one is always IS_INTEGRATING + */ READY_TO_INTEGRATE, + /** + * the step after this one is always WAITING_FOR_ADC_TO_SKIP + */ IS_INTEGRATING, - IS_SENDING_SPI_COMMAND, + /** + * the step after this one is always WAITING_FOR_RESULT_ADC + */ WAITING_FOR_ADC_TO_SKIP, + /** + * the step after this one is always IS_SENDING_SPI_COMMAND or READY_TO_INTEGRATE + */ WAITING_FOR_RESULT_ADC, + /** + * the step after this one is always READY_TO_INTEGRATE + */ + IS_SENDING_SPI_COMMAND, } hip_state_e; // 0b01000000