void hipAdcCallback(adcsample_t value) { if (state == WAITING_FOR_ADC_TO_SKIP) { state = WAITING_FOR_RESULT_ADC; } else if (state == WAITING_FOR_RESULT_ADC) { engine->knockVolts = adcToVoltsDivided(value); hipValueMax = maxF(engine->knockVolts, hipValueMax); engine->knockLogic(engine->knockVolts); float angleWindowWidth = engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart; if (angleWindowWidth != currentAngleWindowWidth) { currentAngleWindowWidth = angleWindowWidth; prepareHip9011RpmLookup(currentAngleWindowWidth); } int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); int bandIndex = getBandIndex(); int prescalerIndex = engineConfiguration->hip9011PrescalerAndSDO; if (currentGainIndex != gainIndex) { currentGainIndex = gainIndex; tx_buff[0] = SET_GAIN_CMD + gainIndex; state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else if (currentIntergratorIndex != integratorIndex) { currentIntergratorIndex = integratorIndex; tx_buff[0] = SET_INTEGRATOR_CMD + integratorIndex; state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else if (currentBandIndex != bandIndex) { currentBandIndex = bandIndex; tx_buff[0] = SET_BAND_PASS_CMD + bandIndex; state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else if (currentPrescaler != prescalerIndex) { currentPrescaler = prescalerIndex; tx_buff[0] = SET_PRESCALER_CMD + prescalerIndex; state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else { state = READY_TO_INTEGRATE; } } }
void initHip9011(Logging *sharedLogger) { driver = getSpiDevice(spiDevice); logger = sharedLogger; addConsoleAction("hipinfo", showHipInfo); if (!boardConfiguration->isHip9011Enabled) return; currentAngleWindowWidth = engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart; prepareHip9011RpmLookup(currentAngleWindowWidth); // todo: configurable // driver = getSpiDevice(boardConfiguration->hip9011SpiDevice); spicfg.ssport = getHwPort(boardConfiguration->hip9011CsPin); spicfg.sspad = getHwPin(boardConfiguration->hip9011CsPin); outputPinRegisterExt2("hip int/hold", &intHold, boardConfiguration->hip9011IntHoldPin, &boardConfiguration->hip9011IntHoldPinMode); outputPinRegisterExt2("hip CS", &hipCs, boardConfiguration->hip9011CsPin, &boardConfiguration->hip9011CsPinMode); scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver"); spiStart(driver, &spicfg); currentBandIndex = getBandIndex(); /** * this engine cycle callback would be scheduling actual integration start and end callbacks */ addTriggerEventListener(&intHoldCallback, "DD int/hold", engine); // MISO PB14 // palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_PUDR_PULLUP); // MOSI PB15 // palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_OTYPE_OPENDRAIN); addConsoleActionF("set_gain", setGain); addConsoleActionF("set_band", setBand); addConsoleActionI("set_hip_prescalerandsdo", setPrescalerAndSDO); addConsoleActionF("set_knock_threshold", setKnockThresh); addConsoleActionI("set_max_knock_sub_deg", setMaxKnockSubDeg); chThdCreateStatic(hipTreadStack, sizeof(hipTreadStack), NORMALPRIO, (tfunc_t) hipThread, NULL); }
void initHip9011(Logging *sharedLogger) { logger = sharedLogger; addConsoleAction("hipinfo", showHipInfo); if (!CONFIGB(isHip9011Enabled)) return; instance.setAngleWindowWidth(); #if EFI_PROD_CODE driver = getSpiDevice(engineConfiguration->hip9011SpiDevice); if (driver == NULL) { // error already reported return; } hipSpiCfg.ssport = getHwPort("hip", CONFIGB(hip9011CsPin)); hipSpiCfg.sspad = getHwPin("hip", CONFIGB(hip9011CsPin)); #endif /* EFI_PROD_CODE */ startHip9001_pins(); scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver"); spiStart(driver, &hipSpiCfg); instance.currentBandIndex = getBandIndex(); /** * this engine cycle callback would be scheduling actual integration start and end callbacks */ addTriggerEventListener(&intHoldCallback, "DD int/hold", engine); // MISO PB14 // palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_PUDR_PULLUP); // MOSI PB15 // palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(EFI_SPI2_AF) | PAL_STM32_OTYPE_OPENDRAIN); addConsoleActionF("set_gain", setHipGain); addConsoleActionF("set_band", setHipBand); addConsoleActionI("set_hip_prescalerandsdo", setPrescalerAndSDO); addConsoleActionF("set_knock_threshold", setKnockThresh); addConsoleActionI("set_max_knock_sub_deg", setMaxKnockSubDeg); chThdCreateStatic(hipTreadStack, sizeof(hipTreadStack), NORMALPRIO, (tfunc_t)(void*) hipThread, NULL); }