2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file hardware.cpp
|
|
|
|
* @brief Hardware package entry point
|
|
|
|
*
|
|
|
|
* @date May 27, 2013
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:25:17 -07:00
|
|
|
#include "global.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
|
|
|
|
#include "trigger_input.h"
|
|
|
|
#include "servo.h"
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "adc_inputs.h"
|
|
|
|
#include "can_hw.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
#include "io_pins.h"
|
|
|
|
#include "rtc_helper.h"
|
|
|
|
#include "rfiutil.h"
|
|
|
|
#include "injector_central.h"
|
|
|
|
#include "vehicle_speed.h"
|
2018-10-16 18:27:05 -07:00
|
|
|
#include "yaw_rate_sensor.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
#include "pin_repository.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "max31855.h"
|
2019-03-25 12:08:32 -07:00
|
|
|
#include "tle8888.h"
|
2017-08-28 17:42:27 -07:00
|
|
|
#include "accelerometer.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "console_io.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-03 21:16:08 -08:00
|
|
|
#include "mpu_util.h"
|
2017-05-12 18:19:14 -07:00
|
|
|
//#include "usb_msd.h"
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "AdcConfiguration.h"
|
2017-03-19 18:44:52 -07:00
|
|
|
#include "electronic_throttle.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "board_test.h"
|
|
|
|
#include "mcp3208.h"
|
|
|
|
#include "HIP9011.h"
|
|
|
|
#include "histogram.h"
|
|
|
|
#include "mmc_card.h"
|
|
|
|
#include "neo6m.h"
|
|
|
|
#include "lcd_HD44780.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "algo.h"
|
|
|
|
#include "joystick.h"
|
2019-01-04 20:47:39 -08:00
|
|
|
#include "cdm_ion_sense.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "trigger_central.h"
|
|
|
|
#include "svnversion.h"
|
|
|
|
#include "engine_configuration.h"
|
2017-05-02 15:40:33 -07:00
|
|
|
#include "aux_pid.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_SPEED_DENSITY
|
|
|
|
#include "map_averaging.h"
|
|
|
|
#endif /* EFI_SPEED_DENSITY */
|
|
|
|
|
|
|
|
#if EFI_INTERNAL_FLASH
|
|
|
|
#include "flash_main.h"
|
|
|
|
#endif /* EFI_INTERNAL_FLASH */
|
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
extern bool hasFirmwareErrorFlag;
|
|
|
|
|
2017-03-21 11:58:14 -07:00
|
|
|
static mutex_t spiMtx;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-01-23 10:04:30 -08:00
|
|
|
/**
|
|
|
|
* this depends on patch to chdebug.c
|
|
|
|
+extern int maxNesting;
|
|
|
|
ch.dbg.isr_cnt++;
|
|
|
|
+ if (ch.dbg.isr_cnt > maxNesting)
|
|
|
|
+ maxNesting = ch.dbg.isr_cnt;
|
|
|
|
port_unlock_from_isr();
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// todo: rename this to 'rusefiMaxISRNesting' one day
|
2015-07-10 06:01:56 -07:00
|
|
|
int maxNesting = 0;
|
|
|
|
|
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
2016-09-14 16:03:00 -07:00
|
|
|
extern bool isSpiInitialized[5];
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-15 19:02:00 -07:00
|
|
|
/**
|
|
|
|
* #311 we want to test RTC before engine start so that we do not test it while engine is running
|
|
|
|
*/
|
|
|
|
bool rtcWorks = true;
|
2016-05-03 20:01:36 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Only one consumer can use SPI bus at a given time
|
|
|
|
*/
|
|
|
|
void lockSpi(spi_device_e device) {
|
2019-03-08 04:01:15 -08:00
|
|
|
UNUSED(device);
|
2019-02-23 09:33:49 -08:00
|
|
|
efiAssertVoid(CUSTOM_ERR_6674, getCurrentRemainingStack() > 128, "lockSpi");
|
2015-07-10 06:01:56 -07:00
|
|
|
// todo: different locks for different SPI devices!
|
|
|
|
chMtxLock(&spiMtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlockSpi(void) {
|
2017-03-21 11:58:14 -07:00
|
|
|
chMtxUnlock(&spiMtx);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initSpiModules(board_configuration_s *boardConfiguration) {
|
2019-03-08 04:01:15 -08:00
|
|
|
UNUSED(boardConfiguration);
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(is_enabled_spi_1)) {
|
2017-05-09 15:55:38 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_1);
|
|
|
|
}
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(is_enabled_spi_2)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_2);
|
|
|
|
}
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(is_enabled_spi_3)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:38:23 -07:00
|
|
|
/**
|
|
|
|
* @return NULL if SPI device not specified
|
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
SPIDriver * getSpiDevice(spi_device_e spiDevice) {
|
|
|
|
if (spiDevice == SPI_NONE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#if STM32_SPI_USE_SPI1 || defined(__DOXYGEN__)
|
|
|
|
if (spiDevice == SPI_DEVICE_1) {
|
|
|
|
return &SPID1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if STM32_SPI_USE_SPI2 || defined(__DOXYGEN__)
|
|
|
|
if (spiDevice == SPI_DEVICE_2) {
|
|
|
|
return &SPID2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if STM32_SPI_USE_SPI3 || defined(__DOXYGEN__)
|
|
|
|
if (spiDevice == SPI_DEVICE_3) {
|
|
|
|
return &SPID3;
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-08 22:10:33 -08:00
|
|
|
firmwareError(CUSTOM_ERR_UNEXPECTED_SPI, "Unexpected SPI device: %d", spiDevice);
|
2015-07-10 06:01:56 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_USE_I2C || defined(__DOXYGEN__)
|
2018-11-16 04:40:06 -08:00
|
|
|
#if defined(STM32F7XX)
|
|
|
|
// values calculated with STM32CubeMX tool, 100kHz I2C clock for Nucleo-767 @168 MHz, PCK1=42MHz
|
|
|
|
#define HAL_I2C_F7_100_TIMINGR 0x00A0A3F7
|
|
|
|
static I2CConfig i2cfg = { HAL_I2C_F7_100_TIMINGR, 0, 0 }; // todo: does it work?
|
|
|
|
#else /* defined(STM32F4XX) */
|
2015-07-10 06:01:56 -07:00
|
|
|
static I2CConfig i2cfg = { OPMODE_I2C, 100000, STD_DUTY_CYCLE, };
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* defined(STM32F4XX) */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
void initI2Cmodule(void) {
|
|
|
|
print("Starting I2C module\r\n");
|
|
|
|
i2cInit();
|
|
|
|
i2cStart(&I2CD1, &i2cfg);
|
|
|
|
|
2017-05-15 05:40:54 -07:00
|
|
|
efiSetPadMode("I2C clock", EFI_I2C_SCL_BRAIN_PIN, PAL_MODE_ALTERNATE(EFI_I2C_AF) | PAL_STM32_OTYPE_OPENDRAIN);
|
|
|
|
efiSetPadMode("I2C data", EFI_I2C_SDA_BRAIN_PIN, PAL_MODE_ALTERNATE(EFI_I2C_AF) | PAL_STM32_OTYPE_OPENDRAIN);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static char txbuf[1];
|
|
|
|
|
|
|
|
static void sendI2Cbyte(int addr, int data) {
|
|
|
|
// i2cAcquireBus(&I2CD1);
|
|
|
|
// txbuf[0] = data;
|
|
|
|
// i2cMasterTransmit(&I2CD1, addr, txbuf, 1, NULL, 0);
|
|
|
|
// i2cReleaseBus(&I2CD1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Logging *sharedLogger;
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
|
|
|
|
#define TPS_IS_SLOW -1
|
|
|
|
|
|
|
|
static int fastMapSampleIndex;
|
|
|
|
static int hipSampleIndex;
|
|
|
|
static int tpsSampleIndex;
|
|
|
|
|
|
|
|
extern int tpsFastAdc;
|
|
|
|
|
2019-01-31 08:57:15 -08:00
|
|
|
#if HAL_USE_ADC
|
|
|
|
extern AdcDevice fastAdc;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This method is not in the adc* lower-level file because it is more business logic then hardware.
|
|
|
|
*/
|
|
|
|
void adc_callback_fast(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
|
|
|
|
|
|
|
|
(void) buffer;
|
|
|
|
(void) n;
|
|
|
|
/**
|
|
|
|
* Note, only in the ADC_COMPLETE state because the ADC driver fires an
|
|
|
|
* intermediate callback when the buffer is half full.
|
|
|
|
* */
|
|
|
|
if (adcp->state == ADC_COMPLETE) {
|
|
|
|
/**
|
|
|
|
* this callback is executed 10 000 times a second, it needs to be as fast as possible
|
|
|
|
*/
|
2019-02-23 09:33:49 -08:00
|
|
|
efiAssertVoid(CUSTOM_ERR_6676, getCurrentRemainingStack() > 128, "lowstck#9b");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_MAP_AVERAGING
|
2018-02-06 13:13:09 -08:00
|
|
|
mapAveragingAdcCallback(fastAdc.samples[fastMapSampleIndex]);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_MAP_AVERAGING */
|
|
|
|
#if EFI_HIP_9011 || defined(__DOXYGEN__)
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(isHip9011Enabled)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
hipAdcCallback(fastAdc.samples[hipSampleIndex]);
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-26 17:27:24 -08:00
|
|
|
// if (tpsSampleIndex != TPS_IS_SLOW) {
|
2016-01-31 18:02:03 -08:00
|
|
|
// tpsFastAdc = fastAdc.samples[tpsSampleIndex];
|
2018-12-26 17:27:24 -08:00
|
|
|
// }
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 08:57:15 -08:00
|
|
|
#endif /* HAL_USE_ADC */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static void calcFastAdcIndexes(void) {
|
2019-01-31 08:57:15 -08:00
|
|
|
#if HAL_USE_ADC
|
2015-07-10 06:01:56 -07:00
|
|
|
fastMapSampleIndex = fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->map.sensor.hwChannel];
|
|
|
|
hipSampleIndex =
|
|
|
|
engineConfiguration->hipOutputChannel == EFI_ADC_NONE ?
|
|
|
|
-1 : fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->hipOutputChannel];
|
|
|
|
if (engineConfiguration->tpsAdcChannel != EFI_ADC_NONE) {
|
|
|
|
tpsSampleIndex = fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->tpsAdcChannel];
|
|
|
|
} else {
|
|
|
|
tpsSampleIndex = TPS_IS_SLOW;
|
|
|
|
}
|
2019-01-31 08:57:15 -08:00
|
|
|
#endif/* HAL_USE_ADC */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void adcConfigListener(Engine *engine) {
|
2019-03-08 04:01:15 -08:00
|
|
|
UNUSED(engine);
|
2017-06-04 12:57:57 -07:00
|
|
|
// todo: something is not right here - looks like should be a callback for each configuration change?
|
2015-07-10 06:01:56 -07:00
|
|
|
calcFastAdcIndexes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void turnOnHardware(Logging *sharedLogger) {
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
|
|
|
|
turnOnTriggerInputPins(sharedLogger);
|
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
|
|
|
}
|
|
|
|
|
2016-09-13 21:03:14 -07:00
|
|
|
static void unregisterPin(brain_pin_e currentPin, brain_pin_e prevPin) {
|
|
|
|
if (currentPin != prevPin) {
|
|
|
|
unmarkPin(prevPin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopSpi(spi_device_e device) {
|
2018-11-16 04:40:06 -08:00
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
2016-09-13 21:03:14 -07:00
|
|
|
if (!isSpiInitialized[device])
|
|
|
|
return; // not turned on
|
|
|
|
isSpiInitialized[device] = false;
|
|
|
|
unmarkPin(getSckPin(device));
|
|
|
|
unmarkPin(getMisoPin(device));
|
|
|
|
unmarkPin(getMosiPin(device));
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* HAL_USE_SPI */
|
2016-09-13 21:03:14 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void applyNewHardwareSettings(void) {
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
|
|
|
|
applyNewTriggerInputPins();
|
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
2016-09-13 21:03:14 -07:00
|
|
|
|
|
|
|
// all 'stop' methods need to go before we begin starting pins
|
|
|
|
|
2017-06-04 15:43:08 -07:00
|
|
|
enginePins.stopInjectionPins();
|
|
|
|
enginePins.stopIgnitionPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_CAN_SUPPORT || defined(__DOXYGEN__)
|
2016-12-19 17:01:37 -08:00
|
|
|
stopCanPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
|
|
|
#if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__)
|
2017-05-30 18:56:56 -07:00
|
|
|
bool etbRestartNeeded = isETBRestartNeeded();
|
|
|
|
if (etbRestartNeeded) {
|
|
|
|
stopETBPins();
|
|
|
|
}
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
|
|
|
|
#if EFI_VEHICLE_SPEED || defined(__DOXYGEN__)
|
2017-04-05 15:08:36 -07:00
|
|
|
stopVSSPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_VEHICLE_SPEED */
|
2017-05-02 15:40:33 -07:00
|
|
|
stopAuxPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-09-14 07:02:54 -07:00
|
|
|
if (engineConfiguration->bc.is_enabled_spi_1 != activeConfiguration.bc.is_enabled_spi_1)
|
|
|
|
stopSpi(SPI_DEVICE_1);
|
|
|
|
|
2016-09-13 21:03:14 -07:00
|
|
|
if (engineConfiguration->bc.is_enabled_spi_2 != activeConfiguration.bc.is_enabled_spi_2)
|
|
|
|
stopSpi(SPI_DEVICE_2);
|
|
|
|
|
2016-09-14 07:02:54 -07:00
|
|
|
if (engineConfiguration->bc.is_enabled_spi_3 != activeConfiguration.bc.is_enabled_spi_3)
|
2016-09-13 21:03:14 -07:00
|
|
|
stopSpi(SPI_DEVICE_3);
|
|
|
|
|
|
|
|
|
2016-09-14 16:03:00 -07:00
|
|
|
|
2016-09-13 21:03:14 -07:00
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_rs, activeConfiguration.bc.HD44780_rs);
|
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_e, activeConfiguration.bc.HD44780_e);
|
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_db4, activeConfiguration.bc.HD44780_db4);
|
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_db5, activeConfiguration.bc.HD44780_db5);
|
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_db6, activeConfiguration.bc.HD44780_db6);
|
|
|
|
unregisterPin(engineConfiguration->bc.HD44780_db7, activeConfiguration.bc.HD44780_db7);
|
|
|
|
|
|
|
|
unregisterPin(engineConfiguration->bc.clutchUpPin, activeConfiguration.bc.clutchUpPin);
|
|
|
|
|
2017-06-04 13:35:13 -07:00
|
|
|
enginePins.unregisterPins();
|
2016-09-13 21:03:14 -07:00
|
|
|
|
|
|
|
|
2017-06-04 15:43:08 -07:00
|
|
|
enginePins.startInjectionPins();
|
|
|
|
enginePins.startIgnitionPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#if EFI_CAN_SUPPORT || defined(__DOXYGEN__)
|
2016-12-19 17:01:37 -08:00
|
|
|
startCanPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
|
|
|
#if EFI_ELECTRONIC_THROTTLE_BODY || defined(__DOXYGEN__)
|
2017-05-30 18:56:56 -07:00
|
|
|
if (etbRestartNeeded) {
|
|
|
|
startETBPins();
|
|
|
|
}
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
|
|
|
|
#if EFI_VEHICLE_SPEED || defined(__DOXYGEN__)
|
2017-04-05 15:08:36 -07:00
|
|
|
startVSSPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_VEHICLE_SPEED */
|
2017-05-02 15:40:33 -07:00
|
|
|
startAuxPins();
|
2016-12-19 17:01:37 -08:00
|
|
|
|
2016-09-14 21:01:50 -07:00
|
|
|
adcConfigListener(engine);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-02-14 18:01:56 -08:00
|
|
|
void setBor(int borValue) {
|
|
|
|
scheduleMsg(sharedLogger, "setting BOR to %d", borValue);
|
|
|
|
BOR_Set((BOR_Level_t)borValue);
|
|
|
|
showBor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void showBor(void) {
|
|
|
|
scheduleMsg(sharedLogger, "BOR=%d", (int)BOR_Get());
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initHardware(Logging *l) {
|
2019-02-23 09:33:49 -08:00
|
|
|
efiAssertVoid(CUSTOM_IH_STACK, getCurrentRemainingStack() > 256, "init h");
|
2015-07-10 06:01:56 -07:00
|
|
|
sharedLogger = l;
|
2019-01-04 21:11:17 -08:00
|
|
|
engine_configuration_s *engineConfiguration = engine->engineConfigurationPtr;
|
2018-09-10 19:00:13 -07:00
|
|
|
efiAssertVoid(CUSTOM_EC_NULL, engineConfiguration!=NULL, "engineConfiguration");
|
2015-07-10 06:01:56 -07:00
|
|
|
board_configuration_s *boardConfiguration = &engineConfiguration->bc;
|
|
|
|
|
|
|
|
printMsg(sharedLogger, "initHardware()");
|
|
|
|
// todo: enable protection. it's disabled because it takes
|
|
|
|
// 10 extra seconds to re-flash the chip
|
|
|
|
//flashProtect();
|
|
|
|
|
2017-03-21 11:58:14 -07:00
|
|
|
chMtxObjectInit(&spiMtx);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_HISTOGRAMS
|
|
|
|
/**
|
|
|
|
* histograms is a data structure for CPU monitor, it does not depend on configuration
|
|
|
|
*/
|
|
|
|
initHistogramsModule();
|
|
|
|
#endif /* EFI_HISTOGRAMS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need the LED_ERROR pin even before we read configuration
|
|
|
|
*/
|
|
|
|
initPrimaryPins();
|
|
|
|
|
|
|
|
if (hasFirmwareError()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_INTERNAL_FLASH
|
|
|
|
|
|
|
|
palSetPadMode(CONFIG_RESET_SWITCH_PORT, CONFIG_RESET_SWITCH_PIN, PAL_MODE_INPUT_PULLUP);
|
|
|
|
|
|
|
|
initFlash(sharedLogger);
|
|
|
|
/**
|
|
|
|
* this call reads configuration from flash memory or sets default configuration
|
|
|
|
* if flash state does not look right.
|
|
|
|
*/
|
|
|
|
if (SHOULD_INGORE_FLASH()) {
|
|
|
|
engineConfiguration->engineType = DEFAULT_ENGINE_TYPE;
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(sharedLogger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
writeToFlashNow();
|
|
|
|
} else {
|
|
|
|
readFromFlash();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
engineConfiguration->engineType = DEFAULT_ENGINE_TYPE;
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(sharedLogger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_INTERNAL_FLASH */
|
|
|
|
|
2019-02-27 14:12:52 -08:00
|
|
|
// it's important to initialize this pretty early in the game before any scheduling usages
|
|
|
|
initSingleTimerExecutorHardware();
|
|
|
|
|
2017-02-24 16:33:06 -08:00
|
|
|
#if EFI_HD44780_LCD
|
|
|
|
// initI2Cmodule();
|
|
|
|
lcd_HD44780_init(sharedLogger);
|
|
|
|
if (hasFirmwareError())
|
|
|
|
return;
|
|
|
|
|
|
|
|
lcd_HD44780_print_string(VCS_VERSION);
|
|
|
|
|
|
|
|
#endif /* EFI_HD44780_LCD */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
if (hasFirmwareError()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
|
|
|
|
initTriggerDecoder();
|
|
|
|
#endif
|
|
|
|
|
2017-05-06 11:54:47 -07:00
|
|
|
bool isBoardTestMode_b;
|
2019-01-09 19:57:33 -08:00
|
|
|
if (CONFIGB(boardTestModeJumperPin) != GPIO_UNASSIGNED) {
|
|
|
|
efiSetPadMode("board test", CONFIGB(boardTestModeJumperPin),
|
2017-05-06 11:54:47 -07:00
|
|
|
PAL_MODE_INPUT_PULLUP);
|
2019-01-09 19:57:33 -08:00
|
|
|
isBoardTestMode_b = (!efiReadPin(CONFIGB(boardTestModeJumperPin)));
|
2017-05-06 11:54:47 -07:00
|
|
|
|
|
|
|
// we can now relese this pin, it is actually used as output sometimes
|
2019-01-09 19:57:33 -08:00
|
|
|
unmarkPin(CONFIGB(boardTestModeJumperPin));
|
2017-05-06 11:54:47 -07:00
|
|
|
} else {
|
|
|
|
isBoardTestMode_b = false;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if HAL_USE_ADC || defined(__DOXYGEN__)
|
|
|
|
initAdcInputs(isBoardTestMode_b);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (isBoardTestMode_b) {
|
|
|
|
// this method never returns
|
|
|
|
initBoardTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
initRtc();
|
|
|
|
|
|
|
|
initOutputPins();
|
|
|
|
|
|
|
|
#if EFI_MAX_31855
|
2019-03-25 19:41:31 -07:00
|
|
|
initMax31855(sharedLogger, CONFIGB(max31855spiDevice), CONFIGB(max31855_cs));
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_MAX_31855 */
|
|
|
|
|
2019-03-25 12:14:22 -07:00
|
|
|
#if EFI_TLE8888
|
2019-03-25 12:08:32 -07:00
|
|
|
initTle8888(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2019-03-25 12:14:22 -07:00
|
|
|
#endif
|
2019-03-25 12:08:32 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
initCan();
|
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
|
|
|
|
|
|
|
// init_adc_mcp3208(&adcState, &SPID2);
|
|
|
|
// requestAdcValue(&adcState, 0);
|
|
|
|
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
|
|
|
|
// todo: figure out better startup logic
|
2017-05-15 17:31:16 -07:00
|
|
|
initTriggerCentral(sharedLogger);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
|
|
|
|
|
|
|
turnOnHardware(sharedLogger);
|
|
|
|
|
2017-02-14 15:02:36 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
|
|
|
initSpiModules(boardConfiguration);
|
|
|
|
#endif
|
|
|
|
|
2017-08-28 19:32:29 -07:00
|
|
|
#if EFI_HIP_9011 || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initHip9011(sharedLogger);
|
|
|
|
#endif /* EFI_HIP_9011 */
|
|
|
|
|
2017-08-28 19:32:29 -07:00
|
|
|
#if EFI_FILE_LOGGING || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initMmcCard();
|
|
|
|
#endif /* EFI_FILE_LOGGING */
|
|
|
|
|
2017-11-02 15:09:13 -07:00
|
|
|
#if EFI_MEMS || defined(__DOXYGEN__)
|
2017-08-28 17:42:27 -07:00
|
|
|
initAccelerometer(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2017-11-02 15:09:13 -07:00
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
// initFixedLeds();
|
|
|
|
|
2018-10-16 18:27:05 -07:00
|
|
|
|
|
|
|
#if EFI_BOSCH_YAW || defined(__DOXYGEN__)
|
|
|
|
initBoschYawRateSensor();
|
|
|
|
#endif /* EFI_BOSCH_YAW */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
// initBooleanInputs();
|
|
|
|
|
2017-08-28 19:32:29 -07:00
|
|
|
#if EFI_UART_GPS || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initGps();
|
|
|
|
#endif
|
|
|
|
|
2018-03-17 18:24:04 -07:00
|
|
|
#if EFI_SERVO
|
|
|
|
initServo();
|
|
|
|
#endif
|
|
|
|
|
2017-08-28 19:32:29 -07:00
|
|
|
#if ADC_SNIFFER || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initAdcDriver();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_USE_I2C || defined(__DOXYGEN__)
|
|
|
|
addConsoleActionII("i2c", sendI2Cbyte);
|
|
|
|
#endif
|
|
|
|
|
2017-05-12 18:19:14 -07:00
|
|
|
|
|
|
|
// USBMassStorageDriver UMSD1;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
// while (true) {
|
|
|
|
// for (int addr = 0x20; addr < 0x28; addr++) {
|
|
|
|
// sendI2Cbyte(addr, 0);
|
|
|
|
// int err = i2cGetErrors(&I2CD1);
|
|
|
|
// print("I2C: err=%x from %d\r\n", err, addr);
|
|
|
|
// chThdSleepMilliseconds(5);
|
|
|
|
// sendI2Cbyte(addr, 255);
|
|
|
|
// chThdSleepMilliseconds(5);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
#if EFI_VEHICLE_SPEED || defined(__DOXYGEN__)
|
|
|
|
initVehicleSpeed(sharedLogger);
|
|
|
|
#endif
|
|
|
|
|
2019-01-04 20:47:39 -08:00
|
|
|
#if EFI_CDM_INTEGRATION
|
|
|
|
cdmIonInit();
|
|
|
|
#endif
|
|
|
|
|
2019-02-05 15:36:25 -08:00
|
|
|
#if (HAL_USE_PAL && EFI_PROD_CODE) || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
initJoystick(sharedLogger);
|
2019-02-05 15:36:25 -08:00
|
|
|
#endif /* HAL_USE_PAL && EFI_PROD_CODE */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
calcFastAdcIndexes();
|
|
|
|
|
|
|
|
printMsg(sharedLogger, "initHardware() OK!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE */
|
2019-01-03 21:16:08 -08:00
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE || EFI_SIMULATOR */
|
2019-03-25 19:41:31 -07:00
|
|
|
|
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
|
|
|
// this is F4 implementation but we will keep it here for now for simplicity
|
|
|
|
int getSpiPrescaler(spi_speed_e speed, spi_device_e device) {
|
|
|
|
switch (speed) {
|
|
|
|
case _5MHz:
|
|
|
|
return device == SPI_DEVICE_1 ? SPI_BaudRatePrescaler_16 : SPI_BaudRatePrescaler_8;
|
2019-03-26 07:35:33 -07:00
|
|
|
case _2_5MHz:
|
|
|
|
return device == SPI_DEVICE_1 ? SPI_BaudRatePrescaler_32 : SPI_BaudRatePrescaler_16;
|
|
|
|
case _1_25MHz:
|
|
|
|
return device == SPI_DEVICE_1 ? SPI_BaudRatePrescaler_64 : SPI_BaudRatePrescaler_32;
|
|
|
|
|
2019-03-25 19:41:31 -07:00
|
|
|
case _150KHz:
|
|
|
|
// SPI1 does not support 150KHz, it would be 300KHz for SPI1
|
|
|
|
return SPI_BaudRatePrescaler_256;
|
|
|
|
default:
|
|
|
|
// unexpected
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_SPI */
|