From d57dc356c43072c87cda97e6c9d511f82aead3e0 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 10 Jan 2015 18:03:38 -0600 Subject: [PATCH] auto-sync --- .../controllers/algo/auto_generated_enums.cpp | 6 - firmware/controllers/algo/io_pins.h | 4 - firmware/hw_layer/HIP9011.cpp | 107 ++++++------------ firmware/hw_layer/io_pins.cpp | 4 +- 4 files changed, 36 insertions(+), 85 deletions(-) diff --git a/firmware/controllers/algo/auto_generated_enums.cpp b/firmware/controllers/algo/auto_generated_enums.cpp index 26fefb2c3c..121fd3a73f 100644 --- a/firmware/controllers/algo/auto_generated_enums.cpp +++ b/firmware/controllers/algo/auto_generated_enums.cpp @@ -51,8 +51,6 @@ case GPIO_8: return "GPIO_8"; case GPIO_9: return "GPIO_9"; -case HIP9011_INT_HOLD: - return "HIP9011_INT_HOLD"; case INJECTOR_10_OUTPUT: return "INJECTOR_10_OUTPUT"; case INJECTOR_11_OUTPUT: @@ -157,10 +155,6 @@ case SPI_CS_3: return "SPI_CS_3"; case SPI_CS_4: return "SPI_CS_4"; -case SPI_CS_HIP9011: - return "SPI_CS_HIP9011"; -case SPI_CS_SD_MODULE: - return "SPI_CS_SD_MODULE"; } return NULL; } diff --git a/firmware/controllers/algo/io_pins.h b/firmware/controllers/algo/io_pins.h index 997bcf0441..be45187854 100644 --- a/firmware/controllers/algo/io_pins.h +++ b/firmware/controllers/algo/io_pins.h @@ -107,10 +107,6 @@ typedef enum { SPI_CS_2, SPI_CS_3, SPI_CS_4, - SPI_CS_SD_MODULE, - SPI_CS_HIP9011, - - HIP9011_INT_HOLD, MAIN_RELAY, diff --git a/firmware/hw_layer/HIP9011.cpp b/firmware/hw_layer/HIP9011.cpp index fe514144b9..b5c1e4fab8 100644 --- a/firmware/hw_layer/HIP9011.cpp +++ b/firmware/hw_layer/HIP9011.cpp @@ -35,10 +35,8 @@ #if EFI_HIP_9011 || defined(__DOXYGEN__) -#define NEW_CODE TRUE - -#define HIP_DEBUG FALSE -extern OutputPin outputs[IO_PIN_COUNT]; +static OutputPin intHold; +static OutputPin hipCs; extern pin_output_mode_e DEFAULT_OUTPUT; @@ -57,12 +55,7 @@ static int settingUpdateCount = 0; */ static bool_t isIntegrating = false; /** - * we cannot afford relatively slow synchronous SPI communication from the scheduler callbacks, thus - * SPI is taken care from a dedicated thread where we care less about how long it would take - * true by default so that we can update the settings before starting to integrate */ -static bool_t needToSendSpiCommand = true; - static bool_t isSendingSpiCommand = false; static scheduling_s startTimer[2]; @@ -70,8 +63,6 @@ static scheduling_s endTimer[2]; static Logging logger; -static THD_WORKING_AREA(htThreadStack, UTILITY_THREAD_STACK_SIZE); - // SPI_CR1_BR_1 // 5MHz // SPI_CR1_CPHA Clock Phase // todo: nicer method which would mention SPI speed explicitly? @@ -100,59 +91,6 @@ static SPIDriver *driver = &SPID2; EXTERN_ENGINE ; -static msg_t ivThread(int param) { - chRegSetThreadName("HIP"); - - while (true) { - /** - * do we need this configurable? probably not - */ - chThdSleepMilliseconds(HIP_THREAD_PERIOD); - - int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); - int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); - - if (currentGainIndex != gainIndex || currentIntergratorIndex != integratorIndex) { - needToSendSpiCommand = true; - } - - /** - * Loop if nothing has really changed - */ - if (!needToSendSpiCommand) - continue; - /** - * Loop if the chip is busy. The 'needToSend' flag would prevent next integration, but we - * need to let current integration finish - */ - if (isIntegrating) - continue; - settingUpdateCount++; - - SPI_SYNCHRONOUS(SET_GAIN_CMD + gainIndex); - currentGainIndex = gainIndex; - - SPI_SYNCHRONOUS(SET_INTEGRATOR_CMD + integratorIndex); - currentIntergratorIndex = integratorIndex; - - needToSendSpiCommand = false; - -//// BAND_PASS_CMD -// SPI_SYNCHRONOUS(0x0 | (40 & 0x3F)); -// // Set the gain 0b10000000 -// SPI_SYNCHRONOUS(0x80 | (49 & 0x3F)); -// // Set the integration time constant 0b11000000 -// SPI_SYNCHRONOUS(0xC0 | (31 & 0x1F)); -// // SET_ADVANCED_MODE 0b01110001 -// SPI_SYNCHRONOUS(tx_buff[0] = 0x71;) - - } -#if defined __GNUC__ - return 0; -#endif - -} - static void showHipInfo(void) { printSpiState(&logger, boardConfiguration); scheduleMsg(&logger, "bore=%f freq=%f", engineConfiguration->cylinderBore, BAND(engineConfiguration->cylinderBore)); @@ -175,13 +113,13 @@ void setHip9011FrankensoPinout(void) { } static void startIntegration(void) { - if (!needToSendSpiCommand) { + if (!isSendingSpiCommand) { /** * SPI communication is only allowed while not integrating, so we postpone the exchange * until we are done integrating */ isIntegrating = true; - turnPinHigh(HIP9011_INT_HOLD); + intHold.setValue(true); } } @@ -191,8 +129,31 @@ static void endIntegration(void) { * engine cycle */ if (isIntegrating) { - turnPinLow(HIP9011_INT_HOLD); + intHold.setValue(false); isIntegrating = false; + + int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue); + int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain); + + if (currentGainIndex != gainIndex) { + tx_buff[0] = gainIndex; + currentGainIndex = gainIndex; + + isSendingSpiCommand = true; + spiSelectI(driver); + spiStartExchangeI(driver, 1, tx_buff, rx_buff); + return; + } + + if (currentIntergratorIndex != integratorIndex) { + tx_buff[0] = integratorIndex; + currentIntergratorIndex = integratorIndex; + + isSendingSpiCommand = true; + spiSelectI(driver); + spiStartExchangeI(driver, 1, tx_buff, rx_buff); + return; + } } } @@ -243,8 +204,8 @@ void initHip9011(void) { spicfg.ssport = getHwPort(boardConfiguration->hip9011CsPin); spicfg.sspad = getHwPin(boardConfiguration->hip9011CsPin); - outputPinRegisterExt2("hip int/hold", &outputs[(int)HIP9011_INT_HOLD], boardConfiguration->hip9011IntHoldPin, &DEFAULT_OUTPUT); - outputPinRegisterExt2("hip CS", &outputs[(int)SPI_CS_HIP9011], boardConfiguration->hip9011CsPin, &DEFAULT_OUTPUT); + outputPinRegisterExt2("hip int/hold", &intHold, boardConfiguration->hip9011IntHoldPin, &DEFAULT_OUTPUT); + outputPinRegisterExt2("hip CS", &hipCs, boardConfiguration->hip9011CsPin, &DEFAULT_OUTPUT); scheduleMsg(&logger, "Starting HIP9011/TPIC8101 driver"); spiStart(driver, &spicfg); @@ -277,11 +238,9 @@ void initHip9011(void) { * Let's restart SPI to switch it from synchronous mode into * asynchronous mode */ -// spiStop(driver); -// spicfg.end_cb = endOfSpiCommunication; -// spiStart(driver, &spicfg); - - chThdCreateStatic(htThreadStack, sizeof(htThreadStack), NORMALPRIO, (tfunc_t) ivThread, NULL); + spiStop(driver); + spicfg.end_cb = endOfSpiCommunication; + spiStart(driver, &spicfg); } #endif diff --git a/firmware/hw_layer/io_pins.cpp b/firmware/hw_layer/io_pins.cpp index 73f3439824..ad1c69bc9b 100644 --- a/firmware/hw_layer/io_pins.cpp +++ b/firmware/hw_layer/io_pins.cpp @@ -25,6 +25,8 @@ extern board_configuration_s *boardConfiguration; static Logging logger; +static OutputPin sdCsPin; + extern OutputPin outputs[IO_PIN_COUNT]; #if defined(STM32F4XX) @@ -126,7 +128,7 @@ void initOutputPins(void) { // outputPinRegister("spi CS3", SPI_CS_3, SPI_CS3_PORT, SPI_CS3_PIN); // outputPinRegister("spi CS4", SPI_CS_4, SPI_CS4_PORT, SPI_CS4_PIN); #if HAL_USE_SPI || defined(__DOXYGEN__) - outputPinRegister("spi CS5", &outputs[(int)SPI_CS_SD_MODULE], SPI_SD_MODULE_PORT, SPI_SD_MODULE_PIN); + outputPinRegister("spi CS5", &sdCsPin, SPI_SD_MODULE_PORT, SPI_SD_MODULE_PIN); #endif // todo: should we move this code closer to the fuel pump logic?