rusefi/firmware/hw_layer/hardware.cpp

646 lines
16 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file hardware.cpp
* @brief Hardware package entry point
*
* @date May 27, 2013
2020-01-07 21:02:40 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
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 "os_access.h"
#include "trigger_input.h"
#include "servo.h"
2015-07-10 06:01:56 -07:00
#include "adc_inputs.h"
#include "can_hw.h"
#include "hardware.h"
#include "rtc_helper.h"
2019-07-06 17:15:49 -07:00
#include "os_util.h"
#include "bench_test.h"
2015-07-10 06:01:56 -07:00
#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"
#include "logic_analyzer.h"
2019-04-13 07:58:52 -07:00
#include "smart_gpio.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"
#include "sensor_chart.h"
#include "serial_hw.h"
2015-07-10 06:01:56 -07:00
2021-07-14 19:37:05 -07:00
#if EFI_PROD_CODE
2019-01-03 21:16:08 -08:00
#include "mpu_util.h"
2021-07-14 19:37:05 -07:00
#endif /* EFI_PROD_CODE */
#include "mmc_card.h"
2017-05-12 18:19:14 -07:00
2015-07-10 06:01:56 -07:00
#include "AdcConfiguration.h"
#include "idle_hardware.h"
2015-07-10 06:01:56 -07:00
#include "mcp3208.h"
#include "hip9011.h"
2015-07-10 06:01:56 -07:00
#include "histogram.h"
#include "gps_uart.h"
#include "HD44780.h"
2015-07-10 06:01:56 -07:00
#include "settings.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"
#include "vvt.h"
2019-10-11 17:43:21 -07:00
#include "perf_trace.h"
#include "trigger_emulator_algo.h"
#include "boost_control.h"
#include "software_knock.h"
#if EFI_MC33816
#include "mc33816.h"
#endif /* EFI_MC33816 */
2015-07-10 06:01:56 -07:00
2020-02-26 14:30:02 -08:00
#if EFI_MAP_AVERAGING
2015-07-10 06:01:56 -07:00
#include "map_averaging.h"
2020-02-26 14:30:02 -08:00
#endif
2015-07-10 06:01:56 -07:00
#if EFI_INTERNAL_FLASH
#include "flash_main.h"
2020-02-26 14:30:02 -08:00
#endif
2015-07-10 06:01:56 -07:00
#if EFI_CAN_SUPPORT
#include "can_vss.h"
#endif
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;
2021-02-05 21:37:12 -08:00
#if HAL_USE_SPI
extern bool isSpiInitialized[5];
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) {
efiAssertVoid(CUSTOM_STACK_SPI, getCurrentRemainingStack() > 128, "lockSpi");
2020-10-25 14:26:46 -07:00
spiAcquireBus(getSpiDevice(device));
2015-07-10 06:01:56 -07:00
}
2020-08-02 14:58:57 -07:00
void unlockSpi(spi_device_e device) {
2020-10-25 14:26:46 -07:00
spiReleaseBus(getSpiDevice(device));
2015-07-10 06:01:56 -07:00
}
static void initSpiModules(engine_configuration_s *engineConfiguration) {
UNUSED(engineConfiguration);
if (CONFIG(is_enabled_spi_1)) {
2017-05-09 15:55:38 -07:00
turnOnSpi(SPI_DEVICE_1);
}
if (CONFIG(is_enabled_spi_2)) {
2015-07-10 06:01:56 -07:00
turnOnSpi(SPI_DEVICE_2);
}
if (CONFIG(is_enabled_spi_3)) {
2015-07-10 06:01:56 -07:00
turnOnSpi(SPI_DEVICE_3);
}
if (CONFIG(is_enabled_spi_4)) {
turnOnSpi(SPI_DEVICE_4);
}
2015-07-10 06:01:56 -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;
}
2019-04-12 17:52:51 -07:00
#if STM32_SPI_USE_SPI1
2015-07-10 06:01:56 -07:00
if (spiDevice == SPI_DEVICE_1) {
return &SPID1;
}
#endif
2019-04-12 17:52:51 -07:00
#if STM32_SPI_USE_SPI2
2015-07-10 06:01:56 -07:00
if (spiDevice == SPI_DEVICE_2) {
return &SPID2;
}
#endif
2019-04-12 17:52:51 -07:00
#if STM32_SPI_USE_SPI3
2015-07-10 06:01:56 -07:00
if (spiDevice == SPI_DEVICE_3) {
return &SPID3;
}
#endif
#if STM32_SPI_USE_SPI4
if (spiDevice == SPI_DEVICE_4) {
return &SPID4;
}
2015-07-10 06:01:56 -07:00
#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
#define TPS_IS_SLOW -1
static int fastMapSampleIndex;
static int hipSampleIndex;
static int tpsSampleIndex;
#if HAL_TRIGGER_USE_ADC
2020-09-12 00:59:53 -07:00
static int triggerSampleIndex;
#endif
2015-07-10 06:01:56 -07:00
2019-01-31 08:57:15 -08:00
#if HAL_USE_ADC
extern AdcDevice fastAdc;
2020-09-12 00:59:53 -07:00
#if EFI_FASTER_UNIFORM_ADC
static int adcCallbackCounter = 0;
static volatile int averagedSamples[ADC_MAX_CHANNELS_COUNT];
static adcsample_t avgBuf[ADC_MAX_CHANNELS_COUNT];
void adc_callback_fast_internal(ADCDriver *adcp);
void adc_callback_fast(ADCDriver *adcp) {
adcsample_t *buffer = adcp->samples;
//size_t n = adcp->depth;
2020-09-12 00:59:53 -07:00
if (adcp->state == ADC_COMPLETE) {
#if HAL_TRIGGER_USE_ADC
// we need to call this ASAP, because trigger processing is time-critical
if (triggerSampleIndex >= 0)
triggerAdcCallback(buffer[triggerSampleIndex]);
#endif /* HAL_TRIGGER_USE_ADC */
// store the values for averaging
for (int i = fastAdc.size() - 1; i >= 0; i--) {
averagedSamples[i] += fastAdc.samples[i];
}
// if it's time to process the data
if (++adcCallbackCounter >= ADC_BUF_NUM_AVG) {
// get an average
for (int i = fastAdc.size() - 1; i >= 0; i--) {
avgBuf[i] = (adcsample_t)(averagedSamples[i] / ADC_BUF_NUM_AVG); // todo: rounding?
}
// call the real callback (see below)
adc_callback_fast_internal(adcp);
2020-09-12 00:59:53 -07:00
// reset the avg buffer & counter
for (int i = fastAdc.size() - 1; i >= 0; i--) {
averagedSamples[i] = 0;
}
adcCallbackCounter = 0;
}
}
}
#endif /* EFI_FASTER_UNIFORM_ADC */
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.
*/
#if EFI_FASTER_UNIFORM_ADC
void adc_callback_fast_internal(ADCDriver *adcp) {
#else
void adc_callback_fast(ADCDriver *adcp) {
#endif
adcsample_t *buffer = adcp->samples;
size_t n = adcp->depth;
2015-07-10 06:01:56 -07:00
(void) buffer;
(void) n;
2019-10-11 17:43:21 -07:00
ScopePerf perf(PE::AdcCallbackFast);
2015-07-10 06:01:56 -07:00
/**
* 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) {
2019-10-14 23:34:12 -07:00
ScopePerf perf(PE::AdcCallbackFastComplete);
2019-10-11 17:43:21 -07:00
2015-07-10 06:01:56 -07:00
/**
* 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
2020-02-26 14:30:02 -08:00
#if EFI_SENSOR_CHART && EFI_SHAFT_POSITION_INPUT
if (ENGINE(sensorChartMode) == SC_AUX_FAST1) {
float voltage = getAdcValue("fAux1", engineConfiguration->auxFastSensor1_adcChannel);
scAddData(getCrankshaftAngleNt(getTimeNowNt() PASS_ENGINE_PARAMETER_SUFFIX), voltage);
}
2019-12-04 04:33:56 -08:00
#endif /* EFI_SENSOR_CHART */
2015-07-10 06:01:56 -07:00
#if EFI_MAP_AVERAGING
2020-09-12 00:59:53 -07:00
mapAveragingAdcCallback(buffer[fastMapSampleIndex]);
2015-07-10 06:01:56 -07:00
#endif /* EFI_MAP_AVERAGING */
2019-04-12 17:52:51 -07:00
#if EFI_HIP_9011
if (CONFIG(isHip9011Enabled)) {
2020-09-12 00:59:53 -07:00
hipAdcCallback(buffer[hipSampleIndex]);
2015-07-10 06:01:56 -07:00
}
2019-12-04 04:33:56 -08:00
#endif /* EFI_HIP_9011 */
2018-12-26 17:27:24 -08:00
// if (tpsSampleIndex != TPS_IS_SLOW) {
2020-09-12 00:59:53 -07:00
// tpsFastAdc = buffer[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) {
#if HAL_USE_ADC && EFI_USE_FAST_ADC
2015-07-10 06:01:56 -07:00
fastMapSampleIndex = fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->map.sensor.hwChannel];
hipSampleIndex =
isAdcChannelValid(engineConfiguration->hipOutputChannel) ?
fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->hipOutputChannel] : -1;
tpsSampleIndex =
isAdcChannelValid(engineConfiguration->tps1_1AdcChannel) ?
fastAdc.internalAdcIndexByHardwareIndex[engineConfiguration->tps1_1AdcChannel] : TPS_IS_SLOW;
2020-09-12 00:59:53 -07:00
#if HAL_TRIGGER_USE_ADC
adc_channel_e triggerChannel = getAdcChannelForTrigger();
triggerSampleIndex = isAdcChannelValid(triggerChannel) ?
fastAdc.internalAdcIndexByHardwareIndex[triggerChannel] : -1;
2020-09-12 00:59:53 -07:00
#endif /* HAL_TRIGGER_USE_ADC */
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) {
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();
}
static void turnOnHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
2020-09-12 00:59:53 -07:00
#if EFI_FASTER_UNIFORM_ADC
for (int i = 0; i < ADC_MAX_CHANNELS_COUNT; i++) {
averagedSamples[i] = 0;
}
adcCallbackCounter = 0;
#endif /* EFI_FASTER_UNIFORM_ADC */
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
turnOnTriggerInputPins(PASS_ENGINE_PARAMETER_SIGNATURE);
2015-07-10 06:01:56 -07:00
#endif /* EFI_SHAFT_POSITION_INPUT */
}
2016-09-13 21:03:14 -07:00
void stopSpi(spi_device_e device) {
2019-04-12 17:52:51 -07:00
#if HAL_USE_SPI
2020-03-23 07:32:41 -07:00
if (!isSpiInitialized[device]) {
2016-09-13 21:03:14 -07:00
return; // not turned on
2020-03-23 07:32:41 -07:00
}
2016-09-13 21:03:14 -07:00
isSpiInitialized[device] = false;
efiSetPadUnused(getSckPin(device));
efiSetPadUnused(getMisoPin(device));
efiSetPadUnused(getMosiPin(device));
#endif /* HAL_USE_SPI */
2016-09-13 21:03:14 -07:00
}
/**
* this method is NOT currently invoked on ECU start
* todo: maybe start invoking this method on ECU start so that peripheral start-up initialization and restart are unified?
*/
2021-07-14 19:37:05 -07:00
void applyNewHardwareSettings(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
/**
* All 'stop' methods need to go before we begin starting pins.
*
* We take settings from 'activeConfiguration' not 'engineConfiguration' while stopping hardware.
* Some hardware is restart unconditionally on change of parameters while for some systems we make extra effort and restart only
* relevant settings were changes.
*
*/
ButtonDebounce::stopConfigurationList();
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
stopTriggerInputPins(PASS_ENGINE_PARAMETER_SIGNATURE);
2015-07-10 06:01:56 -07:00
#endif /* EFI_SHAFT_POSITION_INPUT */
2019-10-18 16:45:32 -07:00
#if (HAL_USE_PAL && EFI_JOYSTICK)
stopJoystickPins();
#endif /* HAL_USE_PAL && EFI_JOYSTICK */
2019-04-12 17:52:51 -07:00
#if EFI_CAN_SUPPORT
2016-12-19 17:01:37 -08:00
stopCanPins();
#endif /* EFI_CAN_SUPPORT */
#if EFI_AUX_SERIAL
stopAuxSerialPins();
#endif /* EFI_AUX_SERIAL */
#if EFI_HIP_9011
stopHip9001_pins();
#endif /* EFI_HIP_9011 */
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
stopSmartCsPins();
#endif /* (BOARD_EXT_GPIOCHIPS > 0) */
2019-04-12 17:52:51 -07:00
#if EFI_VEHICLE_SPEED
2017-04-05 15:08:36 -07:00
stopVSSPins();
#endif /* EFI_VEHICLE_SPEED */
#if EFI_LOGIC_ANALYZER
stopLogicAnalyzerPins();
#endif /* EFI_LOGIC_ANALYZER */
#if EFI_EMULATE_POSITION_SENSORS
stopTriggerEmulatorPins();
#endif /* EFI_EMULATE_POSITION_SENSORS */
2019-04-12 17:52:51 -07:00
#if EFI_AUX_PID
2021-02-17 05:57:18 -08:00
stopVvtControlPins();
#endif /* EFI_AUX_PID */
2015-07-10 06:01:56 -07:00
2020-03-23 07:32:41 -07:00
if (isConfigurationChanged(is_enabled_spi_1)) {
2016-09-14 07:02:54 -07:00
stopSpi(SPI_DEVICE_1);
2020-03-23 07:32:41 -07:00
}
2016-09-14 07:02:54 -07:00
2020-03-23 07:32:41 -07:00
if (isConfigurationChanged(is_enabled_spi_2)) {
2016-09-13 21:03:14 -07:00
stopSpi(SPI_DEVICE_2);
2020-03-23 07:32:41 -07:00
}
2016-09-13 21:03:14 -07:00
2020-03-23 07:32:41 -07:00
if (isConfigurationChanged(is_enabled_spi_3)) {
2016-09-13 21:03:14 -07:00
stopSpi(SPI_DEVICE_3);
2020-03-23 07:32:41 -07:00
}
2016-09-13 21:03:14 -07:00
2020-03-23 07:32:41 -07:00
if (isConfigurationChanged(is_enabled_spi_4)) {
stopSpi(SPI_DEVICE_4);
2020-03-23 07:32:41 -07:00
}
#if EFI_HD44780_LCD
stopHD44780_pins();
#endif /* #if EFI_HD44780_LCD */
2016-09-13 21:03:14 -07:00
2020-03-23 07:32:41 -07:00
if (isPinOrModeChanged(clutchUpPin, clutchUpPinMode)) {
efiSetPadUnused(activeConfiguration.clutchUpPin PASS_ENGINE_PARAMETER_SUFFIX);
2020-03-23 07:32:41 -07:00
}
stopTriggerDebugPins(PASS_ENGINE_PARAMETER_SIGNATURE);
2017-06-04 13:35:13 -07:00
enginePins.unregisterPins();
2016-09-13 21:03:14 -07:00
ButtonDebounce::startConfigurationList();
/*******************************************
* Start everything back with new settings *
******************************************/
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
startTriggerInputPins(PASS_ENGINE_PARAMETER_SIGNATURE);
#endif /* EFI_SHAFT_POSITION_INPUT */
2016-09-13 21:03:14 -07:00
startHardware(PASS_ENGINE_PARAMETER_SIGNATURE);
#if EFI_HD44780_LCD
startHD44780_pins();
#endif /* #if EFI_HD44780_LCD */
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
/* TODO: properly restart gpio chips...
* This is only workaround for "CS pin lost" bug
* see: https://github.com/rusefi/rusefi/issues/2107
* We should provide better way to gracefully stop all
* gpio chips: set outputs to safe state, release all
* on-chip resources (gpios, SPIs, etc) and then restart
* with updated settings.
* Following code just re-inits CS pins for all external
* gpio chips, but does not update CS pin definition in
* gpio chips private data/settings. So changing CS pin
* on-fly does not work */
startSmartCsPins();
#endif /* (BOARD_EXT_GPIOCHIPS > 0) */
enginePins.startPins();
2019-04-12 17:52:51 -07:00
#if EFI_CAN_SUPPORT
2016-12-19 17:01:37 -08:00
startCanPins();
#endif /* EFI_CAN_SUPPORT */
#if EFI_AUX_SERIAL
startAuxSerialPins();
#endif /* EFI_AUX_SERIAL */
#if EFI_HIP_9011
startHip9001_pins();
#endif /* EFI_HIP_9011 */
#if EFI_PROD_CODE && EFI_IDLE_CONTROL
if (isIdleHardwareRestartNeeded()) {
2021-07-14 19:37:05 -07:00
initIdleHardware(PASS_ENGINE_PARAMETER_SIGNATURE);
}
2019-10-18 16:45:32 -07:00
#endif
2019-04-12 17:52:51 -07:00
#if EFI_VEHICLE_SPEED
2017-04-05 15:08:36 -07:00
startVSSPins();
#endif /* EFI_VEHICLE_SPEED */
#if EFI_BOOST_CONTROL
startBoostPin();
#endif
#if EFI_EMULATE_POSITION_SENSORS
2021-07-14 19:37:05 -07:00
startTriggerEmulatorPins(PASS_ENGINE_PARAMETER_SIGNATURE);
#endif /* EFI_EMULATE_POSITION_SENSORS */
#if EFI_LOGIC_ANALYZER
startLogicAnalyzerPins();
#endif /* EFI_LOGIC_ANALYZER */
2019-04-12 17:52:51 -07:00
#if EFI_AUX_PID
2021-02-17 05:53:39 -08:00
startVvtControlPins();
#endif /* EFI_AUX_PID */
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
}
2021-07-14 19:37:05 -07:00
#if EFI_PROD_CODE
2017-02-14 18:01:56 -08:00
void setBor(int borValue) {
efiPrintf("setting BOR to %d", borValue);
2017-02-14 18:01:56 -08:00
BOR_Set((BOR_Level_t)borValue);
showBor();
}
void showBor(void) {
efiPrintf("BOR=%d", (int)BOR_Get());
2017-02-14 18:01:56 -08:00
}
2021-07-14 19:37:05 -07:00
#endif /* EFI_PROD_CODE */
2017-02-14 18:01:56 -08:00
// This function initializes hardware that can do so before configuration is loaded
2021-07-14 19:37:05 -07:00
void initHardwareNoConfig(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
efiAssertVoid(CUSTOM_IH_STACK, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "init h");
2018-09-10 19:00:13 -07:00
efiAssertVoid(CUSTOM_EC_NULL, engineConfiguration!=NULL, "engineConfiguration");
2015-07-10 06:01:56 -07:00
efiPrintf("initHardware()");
#if EFI_PROD_CODE
initPinRepository();
#endif
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();
2015-07-10 06:01:56 -07:00
2021-07-14 19:37:05 -07:00
#if EFI_PROD_CODE
// it's important to initialize this pretty early in the game before any scheduling usages
2021-07-14 19:37:05 -07:00
initSingleTimerExecutorHardware(PASS_ENGINE_PARAMETER_SIGNATURE);
initRtc();
2021-07-14 19:37:05 -07:00
#endif /* EFI_PROD_CODE */
2015-07-10 06:01:56 -07:00
#if EFI_INTERNAL_FLASH
initFlash();
#endif
#if EFI_SHAFT_POSITION_INPUT
// todo: figure out better startup logic
initTriggerCentral();
#endif /* EFI_SHAFT_POSITION_INPUT */
2015-07-10 06:01:56 -07:00
#if EFI_FILE_LOGGING
initEarlyMmcCard();
#endif // EFI_FILE_LOGGING
}
/**
* This method is invoked both on ECU start and configuration change
*/
void startHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
#if (HAL_USE_PAL && EFI_JOYSTICK)
startJoystickPins();
#endif /* HAL_USE_PAL && EFI_JOYSTICK */
startTriggerDebugPins(PASS_ENGINE_PARAMETER_SIGNATURE);
}
2021-07-14 19:37:05 -07:00
void initHardware(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
#if EFI_HD44780_LCD
lcd_HD44780_init();
if (hasFirmwareError())
return;
lcd_HD44780_print_string(VCS_VERSION);
#endif /* EFI_HD44780_LCD */
2015-07-10 06:01:56 -07:00
if (hasFirmwareError()) {
return;
}
2019-04-12 17:52:51 -07:00
#if HAL_USE_ADC
initAdcInputs();
2020-09-12 00:59:53 -07:00
// wait for first set of ADC values so that we do not produce invalid sensor data
waitForSlowAdc(1);
#endif /* HAL_USE_ADC */
2015-07-10 06:01:56 -07:00
#if EFI_SOFTWARE_KNOCK
initSoftwareKnock();
#endif /* EFI_SOFTWARE_KNOCK */
2019-04-13 09:02:34 -07:00
#if HAL_USE_SPI
initSpiModules(engineConfiguration);
2019-06-05 18:39:12 -07:00
#endif /* HAL_USE_SPI */
2020-02-26 14:30:02 -08:00
2021-07-14 19:37:05 -07:00
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
2019-04-13 09:02:34 -07:00
// initSmartGpio depends on 'initSpiModules'
initSmartGpio(PASS_ENGINE_PARAMETER_SIGNATURE);
2020-02-26 14:30:02 -08:00
#endif
2019-04-13 09:02:34 -07:00
// output pins potentially depend on 'initSmartGpio'
2019-09-19 18:41:52 -07:00
initOutputPins(PASS_ENGINE_PARAMETER_SIGNATURE);
#if EFI_ENGINE_CONTROL
2021-07-09 14:02:25 -07:00
enginePins.startPins();
#endif /* EFI_ENGINE_CONTROL */
2015-07-10 06:01:56 -07:00
#if EFI_MC33816
initMc33816();
#endif /* EFI_MC33816 */
2015-07-10 06:01:56 -07:00
#if EFI_MAX_31855
initMax31855(CONFIG(max31855spiDevice), CONFIG(max31855_cs));
2015-07-10 06:01:56 -07:00
#endif /* EFI_MAX_31855 */
#if EFI_CAN_SUPPORT
initCan();
#endif /* EFI_CAN_SUPPORT */
// init_adc_mcp3208(&adcState, &SPID2);
// requestAdcValue(&adcState, 0);
turnOnHardware(PASS_ENGINE_PARAMETER_SIGNATURE);
2015-07-10 06:01:56 -07:00
2019-04-12 17:52:51 -07:00
#if EFI_HIP_9011
initHip9011();
2015-07-10 06:01:56 -07:00
#endif /* EFI_HIP_9011 */
2019-04-12 17:52:51 -07:00
#if EFI_MEMS
2017-08-28 17:42:27 -07:00
initAccelerometer(PASS_ENGINE_PARAMETER_SIGNATURE);
#endif
2018-10-16 18:27:05 -07:00
2019-04-12 17:52:51 -07:00
#if EFI_BOSCH_YAW
2018-10-16 18:27:05 -07:00
initBoschYawRateSensor();
#endif /* EFI_BOSCH_YAW */
2019-04-12 17:52:51 -07:00
#if EFI_UART_GPS
2015-07-10 06:01:56 -07:00
initGps();
#endif
2018-03-17 18:24:04 -07:00
#if EFI_SERVO
initServo();
#endif
#if EFI_AUX_SERIAL
initAuxSerial();
#endif /* EFI_AUX_SERIAL */
2017-05-12 18:19:14 -07:00
2019-04-12 17:52:51 -07:00
#if EFI_VEHICLE_SPEED
initVehicleSpeed();
#endif // EFI_VEHICLE_SPEED
2015-07-10 06:01:56 -07:00
#if EFI_CAN_SUPPORT
initCanVssSupport();
#endif // EFI_CAN_SUPPORT
2019-01-04 20:47:39 -08:00
#if EFI_CDM_INTEGRATION
cdmIonInit();
#endif // EFI_CDM_INTEGRATION
2019-01-04 20:47:39 -08:00
2019-04-12 17:52:51 -07:00
#if (HAL_USE_PAL && EFI_JOYSTICK)
initJoystick();
#endif /* HAL_USE_PAL && EFI_JOYSTICK */
2015-07-10 06:01:56 -07:00
calcFastAdcIndexes();
startHardware(PASS_ENGINE_PARAMETER_SIGNATURE);
efiPrintf("initHardware() OK!");
2015-07-10 06:01:56 -07:00
}
2019-04-12 17:52:51 -07:00
#if HAL_USE_SPI
2019-03-25 19:41:31 -07:00
// 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;
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 */