From d6f8aad361a1db8f5588fc1d6d590c7d54db4bd2 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Fri, 26 Dec 2014 14:04:16 -0600 Subject: [PATCH] auto-sync --- firmware/controllers/algo/rusefi_enums.h | 2 + firmware/controllers/flash_main.cpp | 1 - firmware/hw_layer/HIP9011.cpp | 129 ++++++++++++++++++----- firmware/hw_layer/hardware.cpp | 2 +- firmware/rusefi.cpp | 2 +- 5 files changed, 109 insertions(+), 27 deletions(-) diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index c3a92473b5..a76f147c58 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -102,6 +102,8 @@ typedef enum { Force_4b_engine_type = ENUM_SIZE_HACK, } engine_type_e; +#define DEFAULT_ENGINE_TYPE CUSTOM_ENGINE + typedef enum { TT_TOOTHED_WHEEL = 0, TT_FORD_ASPIRE = 1, diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index 1eec913e6b..d782f5ba15 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -22,7 +22,6 @@ #include "engine.h" -#define DEFAULT_ENGINE_TYPE CUSTOM_ENGINE static bool needToWriteConfiguration = false; diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index 00caa0207f..9ba94e9002 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -15,7 +15,7 @@ * 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 * - * SPI frequency: 5MHz + * SPI frequency: 5MHz max * * @date Nov 27, 2013 * @author Andrey Belomutskiy, (c) 2012-2014 @@ -31,9 +31,13 @@ #if EFI_HIP_9011 || defined(__DOXYGEN__) +#define HIP_DEBUG FALSE + extern pin_output_mode_e DEFAULT_OUTPUT; static int bandIndex; +static int intergratorIndex = -1; +static bool_t isHip9011Busy = false; static scheduling_s startTimer[2]; static scheduling_s endTimer[2]; @@ -53,7 +57,9 @@ static scheduling_s endTimer[2]; static Logging logger; +#if HIP_DEBUG static THD_WORKING_AREA(htThreadStack, UTILITY_THREAD_STACK_SIZE); +#endif // SPI_CR1_BR_1 // 5MHz // SPI_CR1_CPHA Clock Phase @@ -69,6 +75,13 @@ SPI_CR1_MSTR | static unsigned char tx_buff[1]; static unsigned char rx_buff[1]; +#define SPI_SYNCHRONOUS(value) \ + spiSelect(driver); \ + tx_buff[0] = value; \ + spiExchange(driver, 1, tx_buff, rx_buff); \ + spiUnselect(driver); + +// todo: make this configurable static SPIDriver *driver = &SPID2; static msg_t ivThread(int param) { @@ -100,19 +113,19 @@ static msg_t ivThread(int param) { // tx_buff[0] = SET_ADVANCED_MODE; // spiExchange(driver, 1, tx_buff, rx_buff); - // BAND_PASS_CMD +// BAND_PASS_CMD tx_buff[0] = 0b00000000 | (40 & 0x3F); spiExchange(driver, 1, tx_buff, rx_buff); - // Set the gain + // Set the gain tx_buff[0] = 0b10000000 | (49 & 0x3F); spiExchange(driver, 1, tx_buff, rx_buff); - // Set the integration time constant + // Set the integration time constant tx_buff[0] = 0b11000000 | (31 & 0x1F); spiExchange(driver, 1, tx_buff, rx_buff); - // SET_ADVANCED_MODE + // SET_ADVANCED_MODE tx_buff[0] = 0b01110001; spiExchange(driver, 1, tx_buff, rx_buff); @@ -132,15 +145,19 @@ EXTERN_ENGINE /** * These are HIP9011 magic values - integrator time constants in uS */ -static const int integratorValues[INT_TIME_COUNT] = { 40, 45, 50, 55, 60, 65, 70, 75, 80, 90, 100, 110, 120, 130, 140, - 150, 160, 180, 200, 220, 240, 260, 280, 300, 320, 360, 400, 440, 480, 520, 560, 600 }; +static const int integratorValues[INT_TIME_COUNT] = { 40, 45, 50, 55, 60, 65, + 70, 75, 80, 90, 100, 110, 120, 130, 140, 150, 160, 180, 200, 220, 240, + 260, 280, 300, 320, 360, 400, 440, 480, 520, 560, 600 }; #define BAND_LOOKUP_SIZE 64 -static const float bandFreqLookup[BAND_LOOKUP_SIZE] = { 1.22, 1.26, 1.31, 1.35, 1.4, 1.45, 1.51, 1.57, 1.63, 1.71, 1.78, - 1.87, 1.96, 2.07, 2.18, 2.31, 2.46, 2.54, 2.62, 2.71, 2.81, 2.92, 3.03, 3.15, 3.28, 3.43, 3.59, 3.76, 3.95, - 4.16, 4.39, 4.66, 4.95, 5.12, 5.29, 5.48, 5.68, 5.9, 6.12, 6.37, 6.64, 6.94, 7.27, 7.63, 8.02, 8.46, 8.95, 9.5, - 10.12, 10.46, 10.83, 11.22, 11.65, 12.1, 12.6, 13.14, 13.72, 14.36, 15.07, 15.84, 16.71, 17.67, 18.76, 19.98 }; +static const float bandFreqLookup[BAND_LOOKUP_SIZE] = { 1.22, 1.26, 1.31, 1.35, + 1.4, 1.45, 1.51, 1.57, 1.63, 1.71, 1.78, 1.87, 1.96, 2.07, 2.18, 2.31, + 2.46, 2.54, 2.62, 2.71, 2.81, 2.92, 3.03, 3.15, 3.28, 3.43, 3.59, 3.76, + 3.95, 4.16, 4.39, 4.66, 4.95, 5.12, 5.29, 5.48, 5.68, 5.9, 6.12, 6.37, + 6.64, 6.94, 7.27, 7.63, 8.02, 8.46, 8.95, 9.5, 10.12, 10.46, 10.83, + 11.22, 11.65, 12.1, 12.6, 13.14, 13.72, 14.36, 15.07, 15.84, 16.71, + 17.67, 18.76, 19.98 }; #define PIF 3.14159f @@ -155,26 +172,34 @@ static float rpmLookup[INT_TIME_COUNT]; * 2.2 volts should * */ -#define DESIRED_OUTPUT_VALUE 2.2f +#define DESIRED_OUTPUT_VALUE 5.0f static void prepareRpmLookup(engine_configuration_s *engineConfiguration) { for (int i = 0; i < INT_TIME_COUNT; i++) { float windowWidthMult = (engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart) / 360.0f; // '60000000' because revolutions per MINUTE in uS conversion - rpmLookup[i] = 60000000.0f / (integratorValues[i] * 2 * PIF * DESIRED_OUTPUT_VALUE * windowWidthMult); + rpmLookup[i] = 60000000.0f + / (integratorValues[i] * 2 * PIF * DESIRED_OUTPUT_VALUE + * windowWidthMult); } } #define BAND(bore) (900 / (PIF * (bore) / 2)) +#define INTEGRATOR_INDEX findIndex(rpmLookup, INT_TIME_COUNT, engine->rpmCalculator.rpmValue) + static void showHipInfo(void) { printSpiState(&logger, boardConfiguration); - scheduleMsg(&logger, "bore=%f freq=%f", engineConfiguration->cylinderBore, BAND(engineConfiguration->cylinderBore)); + scheduleMsg(&logger, "bore=%f freq=%f", engineConfiguration->cylinderBore, + BAND(engineConfiguration->cylinderBore)); scheduleMsg(&logger, "band index=%d", bandIndex); - scheduleMsg(&logger, "spi= int=%s CS=%s", hwPortname(boardConfiguration->hip9011IntHoldPin), + scheduleMsg(&logger, "integrator index=%d", INTEGRATOR_INDEX); + + scheduleMsg(&logger, "spi= int=%s CS=%s", + hwPortname(boardConfiguration->hip9011IntHoldPin), hwPortname(boardConfiguration->hip9011CsPin)); } @@ -188,10 +213,41 @@ void setHip9011FrankensoPinout(void) { boardConfiguration->is_enabled_spi_2 = true; } +static void startIntegration(void) { + if(isHip9011Busy) + return; + + + turnPinHigh(HIP9011_INT_HOLD); + +} + +static void endIntegration(void) { + + turnPinLow(HIP9011_INT_HOLD); + /** + * SPI communication is only allowed while not integrading, so we initiate the exchange + * once we are done inregratng + */ + + isHip9011Busy = true; + + int newValue = INTEGRATOR_INDEX; + if (newValue != intergratorIndex) { + intergratorIndex = newValue; + // todo: send new value, be sure to use non-synchnonious approach! + + } + // todo: move this into the end callback + isHip9011Busy = false; + +} + /** * Shaft Position callback used to start or finish HIP integration */ -static void intHoldCallback(trigger_event_e ckpEventType, uint32_t index DECLARE_ENGINE_PARAMETER_S) { +static void intHoldCallback(trigger_event_e ckpEventType, + uint32_t index DECLARE_ENGINE_PARAMETER_S) { // this callback is invoked on interrupt thread if (index != 0) @@ -203,10 +259,13 @@ static void intHoldCallback(trigger_event_e ckpEventType, uint32_t index DECLARE int structIndex = getRevolutionCounter() % 2; // todo: schedule this based on closest trigger event, same as ignition works - scheduleByAngle(rpm, &startTimer[structIndex], engineConfiguration->knockDetectionWindowStart, - (schfunc_t) &turnPinHigh, (void*) HIP9011_INT_HOLD); - scheduleByAngle(rpm, &endTimer[structIndex], engineConfiguration->knockDetectionWindowEnd, (schfunc_t) &turnPinLow, - (void*) HIP9011_INT_HOLD); + scheduleByAngle(rpm, &startTimer[structIndex], + engineConfiguration->knockDetectionWindowStart, + (schfunc_t) &startIntegration, NULL); + scheduleByAngle(rpm, &endTimer[structIndex], + engineConfiguration->knockDetectionWindowEnd, + (schfunc_t) &endIntegration, + NULL); } void initHip9011(void) { @@ -219,15 +278,37 @@ void initHip9011(void) { spicfg.ssport = getHwPort(boardConfiguration->hip9011CsPin); spicfg.sspad = getHwPin(boardConfiguration->hip9011CsPin); + + outputPinRegisterExt2("hip int/hold", HIP9011_INT_HOLD, + boardConfiguration->hip9011IntHoldPin, &DEFAULT_OUTPUT); + outputPinRegisterExt2("hip CS", SPI_CS_HIP9011, + boardConfiguration->hip9011CsPin, &DEFAULT_OUTPUT); + scheduleMsg(&logger, "Starting HIP9011/TPIC8101 driver"); spiStart(driver, &spicfg); + /** + * Here we initialize the chip in synchronous mode + */ - outputPinRegisterExt2("hip int/hold", HIP9011_INT_HOLD, boardConfiguration->hip9011IntHoldPin, &DEFAULT_OUTPUT); - outputPinRegisterExt2("hip CS", SPI_CS_HIP9011, boardConfiguration->hip9011CsPin, &DEFAULT_OUTPUT); - chThdCreateStatic(htThreadStack, sizeof(htThreadStack), NORMALPRIO, (tfunc_t) ivThread, NULL); - bandIndex = findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, BAND(engineConfiguration->cylinderBore)); +#if HIP_DEBUG + chThdCreateStatic(htThreadStack, sizeof(htThreadStack), NORMALPRIO, + (tfunc_t) ivThread, NULL); +#else + /** + * for runtime we are re-starting SPI in non-synchronous mode + */ + spiStop(driver); + // todo spicfg.end_cb = spiEndCallback; + spiStart(driver, &spicfg); + + + +#endif + + bandIndex = findIndex(bandFreqLookup, BAND_LOOKUP_SIZE, + BAND(engineConfiguration->cylinderBore)); addTriggerEventListener(&intHoldCallback, "DD int/hold", engine); diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index b3b430a727..8cee70ff27 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -236,7 +236,7 @@ void initHardware(Logging *logger, Engine *engine) { readFromFlash(); } #else - engineConfiguration->engineType = DEFAULT_ENGINE; + engineConfiguration->engineType = DEFAULT_ENGINE_TYPE; resetConfigurationExt(logger, engineConfiguration->engineType, engine); #endif /* EFI_INTERNAL_FLASH */ diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index 837efb5c48..8414715366 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -265,5 +265,5 @@ int getRusEfiVersion(void) { return 1; // this is here to make the compiler happy about the unused array if (UNUSED_CCM_SIZE == 0) return 1; // this is here to make the compiler happy about the unused array - return 20141225; + return 20141226; }