From 8b143432d02f4437ac5c0f6040f7c6d20e1ff7f0 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 23 May 2015 21:12:03 -0400 Subject: [PATCH] auto-sync --- ...ngine_configuration_generated_structures.h | 12 ++- firmware/controllers/algo/rusefi_generated.h | 6 +- .../controllers/sensors/hip9011_lookup.cpp | 7 +- firmware/controllers/sensors/hip9011_lookup.h | 2 +- firmware/hw_layer/HIP9011.cpp | 77 +++++++++++++++---- firmware/integration/rusefi_config.txt | 7 +- 6 files changed, 85 insertions(+), 26 deletions(-) diff --git a/firmware/controllers/algo/engine_configuration_generated_structures.h b/firmware/controllers/algo/engine_configuration_generated_structures.h index 2927f9cf76..d27722b9db 100644 --- a/firmware/controllers/algo/engine_configuration_generated_structures.h +++ b/firmware/controllers/algo/engine_configuration_generated_structures.h @@ -1,4 +1,4 @@ -// this section was generated by config_definition.jar on Sat May 23 19:27:45 EDT 2015 +// this section was generated by config_definition.jar on Sat May 23 20:41:38 EDT 2015 // begin #include "rusefi_types.h" typedef struct { @@ -564,7 +564,7 @@ typedef struct { /** * offset 656 */ - brain_pin_e hip9011OutPin; + int unused1280; /** * offset 660 */ @@ -738,7 +738,11 @@ typedef struct { /** * offset 320 */ - float unused[3]; + float unused[2]; + /** + * offset 328 + */ + int hip9011PrescalerAndSDO; /** * offset 332 */ @@ -1333,4 +1337,4 @@ typedef struct { } persistent_config_s; // end -// this section was generated by config_definition.jar on Sat May 23 19:27:45 EDT 2015 +// this section was generated by config_definition.jar on Sat May 23 20:41:38 EDT 2015 diff --git a/firmware/controllers/algo/rusefi_generated.h b/firmware/controllers/algo/rusefi_generated.h index 200af58a95..82d12a610d 100644 --- a/firmware/controllers/algo/rusefi_generated.h +++ b/firmware/controllers/algo/rusefi_generated.h @@ -108,6 +108,8 @@ #define iat_adcChannel_offset 316 #define unused_offset 320 #define unused_offset_hex 140 +#define hip9011PrescalerAndSDO_offset 328 +#define hip9011PrescalerAndSDO_offset_hex 148 #define knockBandCustom_offset 332 #define sparkDwellBins_offset 336 #define sparkDwellBins_offset_hex 150 @@ -432,8 +434,8 @@ #define fsioFrequency16_offset 1270 #define hip9011CsPin_offset 1272 #define hip9011IntHoldPin_offset 1276 -#define hip9011OutPin_offset 1280 -#define hip9011OutPin_offset_hex 500 +#define unused1280_offset 1280 +#define unused1280_offset_hex 500 #define fsio_setting1_offset 1284 #define fsio_setting1_offset_hex 504 #define fsio_setting2_offset 1288 diff --git a/firmware/controllers/sensors/hip9011_lookup.cpp b/firmware/controllers/sensors/hip9011_lookup.cpp index b4273d8a8b..b2ecc9fb75 100644 --- a/firmware/controllers/sensors/hip9011_lookup.cpp +++ b/firmware/controllers/sensors/hip9011_lookup.cpp @@ -74,6 +74,9 @@ int getHip9011GainIndex(float gain) { return i == GAIN_LOOKUP_SIZE ? GAIN_LOOKUP_SIZE - 1 : i; } -int getHip9011BandIndex(float bore) { - return findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, BAND(bore)); +/** + * @param frequency knock frequencey, in kHz + */ +int getHip9011BandIndex(float frequency) { + return findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, frequency); } diff --git a/firmware/controllers/sensors/hip9011_lookup.h b/firmware/controllers/sensors/hip9011_lookup.h index 0fa492ed50..7635874af4 100644 --- a/firmware/controllers/sensors/hip9011_lookup.h +++ b/firmware/controllers/sensors/hip9011_lookup.h @@ -20,7 +20,7 @@ extern const float bandFreqLookup[BAND_LOOKUP_SIZE]; float getRpmByAngleWindowAndTimeUs(int timeUs, float angleWindowWidth); int getHip9011GainIndex(float gain); -int getHip9011BandIndex(float bore); +int getHip9011BandIndex(float frequency); void prepareHip9011RpmLookup(float angleWindowWidth); #define GAIN_INDEX(gain) (GAIN_LOOKUP_SIZE - 1 - findIndex(gainLookupInReverseOrder, GAIN_LOOKUP_SIZE, (gain))) diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index 584bd6affa..6accb75e05 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -50,7 +50,7 @@ uint32_t hipLastExecutionCount; /** * band index is only send to HIP chip on startup */ -static int bandIndex; +static int currentBandIndex; static int currentGainIndex = -1; static int currentIntergratorIndex = -1; static int settingUpdateCount = 0; @@ -104,6 +104,12 @@ EXTERN_ENGINE ; static char pinNameBuffer[16]; + +static float getBand(void) { + return engineConfiguration->knockBandCustom == 0 ? + BAND(engineConfiguration->cylinderBore) : engineConfiguration->knockBandCustom; +} + static void showHipInfo(void) { if (!boardConfiguration->isHip9011Enabled) { scheduleMsg(logger, "hip9011 driver not active"); @@ -111,9 +117,10 @@ static void showHipInfo(void) { } printSpiState(logger, boardConfiguration); - scheduleMsg(logger, "bore=%fmm freq=%fkHz", engineConfiguration->cylinderBore, BAND(engineConfiguration->cylinderBore)); + scheduleMsg(logger, "bore=%fmm freq=%fkHz PaSDO=%d", engineConfiguration->cylinderBore, getBand(), + engineConfiguration->hip9011PrescalerAndSDO); - scheduleMsg(logger, "band_index=%d gain %f/index=%d", bandIndex, boardConfiguration->hip9011Gain, currentGainIndex); + scheduleMsg(logger, "band_index=%d gain %f/index=%d", currentBandIndex, boardConfiguration->hip9011Gain, currentGainIndex); scheduleMsg(logger, "integrator index=%d hip_threshold=%f totalKnockEventsCount=%d", currentIntergratorIndex, engineConfiguration->hipThreshold, totalKnockEventsCount); @@ -186,16 +193,31 @@ static void intHoldCallback(trigger_event_e ckpEventType, uint32_t index DECLARE engine->m.hipCbTime = GET_TIMESTAMP() - engine->m.beforeHipCb; } +static void setPrescalerAndSDO(int value) { + engineConfiguration->hip9011PrescalerAndSDO = value; + scheduleMsg(logger, "Reboot to apply %d", value); +} + +static void setBand(float value) { + engineConfiguration->knockBandCustom = value; + showHipInfo(); +} + static void setGain(float value) { boardConfiguration->hip9011Gain = value; showHipInfo(); } -static void endOfSpiCommunication(SPIDriver *spip) { +static void endOfSpiExchange(SPIDriver *spip) { spiUnselectI(driver); state = READY_TO_INTEGRATE; } +static int getBandIndex(void) { + float freq = getBand(); + return getHip9011BandIndex(freq); +} + void hipAdcCallback(adcsample_t value) { if (state == WAITING_FOR_ADC_TO_SKIP) { state = WAITING_FOR_RESULT_ADC; @@ -207,19 +229,27 @@ void hipAdcCallback(adcsample_t value) { int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); + int bandIndex = getBandIndex(); if (currentGainIndex != gainIndex) { - state = IS_SENDING_SPI_COMMAND; - tx_buff[0] = gainIndex; currentGainIndex = gainIndex; + tx_buff[0] = gainIndex; + state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else if (currentIntergratorIndex != integratorIndex) { - state = IS_SENDING_SPI_COMMAND; - tx_buff[0] = integratorIndex; currentIntergratorIndex = integratorIndex; + tx_buff[0] = integratorIndex; + state = IS_SENDING_SPI_COMMAND; + spiSelectI(driver); + spiStartExchangeI(driver, 1, tx_buff, rx_buff); + } else if (currentBandIndex != bandIndex) { + currentBandIndex = bandIndex; + tx_buff[0] = bandIndex; + + state = IS_SENDING_SPI_COMMAND; spiSelectI(driver); spiStartExchangeI(driver, 1, tx_buff, rx_buff); } else { @@ -231,21 +261,38 @@ void hipAdcCallback(adcsample_t value) { static bool_t needToInit = true; static void hipStartupCode(void) { +// D[4:1] = 0000 : 4 MHz +// D[4:1] = 0001 : 5 MHz +// D[4:1] = 0010 : 6 MHz +// D[4:1] = 0011 ; 8 MHz +// D[4:1] = 0100 ; 10 MHz +// D[4:1] = 0101 ; 12 MHz +// D[4:1] = 0110 : 16 MHz +// D[4:1] = 0111 : 20 MHz +// D[4:1] = 1000 : 24 MHz + + // '0' for 4MHz - SPI_SYNCHRONOUS(SET_PRESCALER_CMD + 0); + SPI_SYNCHRONOUS(SET_PRESCALER_CMD + engineConfiguration->hip9011PrescalerAndSDO); + + chThdSleepMilliseconds(10); // '0' for channel #1 SPI_SYNCHRONOUS(SET_CHANNEL_CMD + 0); + chThdSleepMilliseconds(10); + // band index depends on cylinder bore - SPI_SYNCHRONOUS(SET_BAND_PASS_CMD + bandIndex); + SPI_SYNCHRONOUS(SET_BAND_PASS_CMD + currentBandIndex); + + chThdSleepMilliseconds(10); /** * Let's restart SPI to switch it from synchronous mode into * asynchronous mode */ spiStop(driver); - spicfg.end_cb = endOfSpiCommunication; + spicfg.end_cb = endOfSpiExchange; spiStart(driver, &spicfg); state = READY_TO_INTEGRATE; } @@ -255,8 +302,8 @@ static THD_WORKING_AREA(hipTreadStack, UTILITY_THREAD_STACK_SIZE); static msg_t hipThread(void *arg) { chRegSetThreadName("hip9011 init"); while (true) { - // 100 ms to let the hardware to start - chThdSleepMilliseconds(100); + // some time to let the hardware start + chThdSleepMilliseconds(500); if (needToInit) { hipStartupCode(); needToInit = false; @@ -287,7 +334,7 @@ void initHip9011(Logging *sharedLogger) { scheduleMsg(logger, "Starting HIP9011/TPIC8101 driver"); spiStart(driver, &spicfg); - bandIndex = getHip9011BandIndex(engineConfiguration->cylinderBore); + currentBandIndex = getBandIndex(); /** * this engine cycle callback would be scheduling actual integration start and end callbacks @@ -300,6 +347,8 @@ void initHip9011(Logging *sharedLogger) { // 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); chThdCreateStatic(hipTreadStack, sizeof(hipTreadStack), NORMALPRIO, (tfunc_t) hipThread, NULL); } diff --git a/firmware/integration/rusefi_config.txt b/firmware/integration/rusefi_config.txt index ff84f18773..84d2718bd0 100644 --- a/firmware/integration/rusefi_config.txt +++ b/firmware/integration/rusefi_config.txt @@ -159,8 +159,9 @@ MAP_sensor_config_s map;@see hasMapSensor\n@see isMapAveragingEnabled ThermistorConf clt;todo: merge with channel settings, use full-scale Thermistor here! ThermistorConf iat; - float[3] unused; - float knockBandCustom; + float[2] unused; + int hip9011PrescalerAndSDO;;"integer", 1, 0.0, 0.0, 32, 0 + float knockBandCustom;;"kHz", 1, 0.0, 0.0, 10.0, 2 float[DWELL_CURVE_SIZE] sparkDwellBins;;"RPM", 1, 0.0, 0.0, 18000, 2 @@ -416,7 +417,7 @@ custom fsio_pwm_freq_t 2 scalar, U16, @OFFSET@, "Hz", 1, 0, brain_pin_e hip9011CsPin; brain_pin_e hip9011IntHoldPin; - brain_pin_e hip9011OutPin; + int unused1280; custom fsio_setting_t 4 scalar, F32, @OFFSET@, "Val", 1, 0, 0, 18000, 0 fsio_setting_t[LE_COMMAND_COUNT iterate] fsio_setting;