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
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
|
2022-09-07 12:56:45 -07:00
|
|
|
|
2019-07-04 00:25:35 -07:00
|
|
|
#include "trigger_input.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "can_hw.h"
|
|
|
|
#include "hardware.h"
|
|
|
|
#include "rtc_helper.h"
|
2020-03-26 05:03:55 -07:00
|
|
|
#include "bench_test.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"
|
2020-12-07 17:01:05 -08:00
|
|
|
#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"
|
2019-12-02 17:31:49 -08:00
|
|
|
#include "sensor_chart.h"
|
2020-05-11 09:17:42 -07:00
|
|
|
#include "serial_hw.h"
|
2021-07-24 15:44:13 -07:00
|
|
|
#include "idle_thread.h"
|
2022-02-24 21:36:16 -08:00
|
|
|
#include "odometer.h"
|
2023-02-17 05:18:36 -08:00
|
|
|
#include "kline.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 */
|
|
|
|
|
2021-03-25 13:22:03 -07:00
|
|
|
#include "mmc_card.h"
|
2017-05-12 18:19:14 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "AdcConfiguration.h"
|
2020-11-03 15:06:32 -08:00
|
|
|
#include "idle_hardware.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "mcp3208.h"
|
2019-04-01 14:04:49 -07:00
|
|
|
#include "hip9011.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "histogram.h"
|
2021-05-16 03:01:00 -07:00
|
|
|
#include "gps_uart.h"
|
2022-10-09 03:38:28 -07:00
|
|
|
#include "sent.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"
|
2021-03-14 16:31:46 -07:00
|
|
|
#include "vvt.h"
|
2020-12-08 01:52:49 -08:00
|
|
|
#include "trigger_emulator_algo.h"
|
2020-02-02 00:56:21 -08:00
|
|
|
#include "boost_control.h"
|
2020-08-28 18:13:50 -07:00
|
|
|
#include "software_knock.h"
|
2022-12-10 20:00:12 -08:00
|
|
|
#include "trigger_scope.h"
|
2021-08-24 13:41:16 -07:00
|
|
|
#include "init.h"
|
2019-05-03 18:13:25 -07:00
|
|
|
#if EFI_MC33816
|
|
|
|
#include "mc33816.h"
|
|
|
|
#endif /* EFI_MC33816 */
|
2023-04-03 08:31:07 -07:00
|
|
|
#if EFI_WS2812
|
|
|
|
#include "WS2812.h"
|
|
|
|
#endif /* EFI_WS2812 */
|
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
|
|
|
|
2021-08-12 14:46:10 -07:00
|
|
|
#if HAL_USE_PAL && EFI_PROD_CODE
|
|
|
|
#include "digital_input_exti.h"
|
|
|
|
#endif // HAL_USE_PAL
|
|
|
|
|
2020-04-21 17:48:37 -07:00
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
#include "can_vss.h"
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2023-04-11 17:01:34 -07:00
|
|
|
efiAssertVoid(ObdCode::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
|
|
|
}
|
|
|
|
|
2019-12-11 14:48:55 -08:00
|
|
|
static void initSpiModules(engine_configuration_s *engineConfiguration) {
|
|
|
|
UNUSED(engineConfiguration);
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->is_enabled_spi_1) {
|
2017-05-09 15:55:38 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_1);
|
|
|
|
}
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->is_enabled_spi_2) {
|
2015-07-10 06:01:56 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_2);
|
|
|
|
}
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->is_enabled_spi_3) {
|
2015-07-10 06:01:56 -07:00
|
|
|
turnOnSpi(SPI_DEVICE_3);
|
|
|
|
}
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->is_enabled_spi_4) {
|
2020-01-06 05:44:23 -08:00
|
|
|
turnOnSpi(SPI_DEVICE_4);
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2019-05-09 15:09:24 -07:00
|
|
|
#endif
|
|
|
|
#if STM32_SPI_USE_SPI4
|
|
|
|
if (spiDevice == SPI_DEVICE_4) {
|
|
|
|
return &SPID4;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
2023-04-11 17:01:34 -07:00
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_UNEXPECTED_SPI, "Unexpected SPI device: %d", spiDevice);
|
2015-07-10 06:01:56 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-07 05:29:01 -07:00
|
|
|
#if HAL_USE_ADC
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-10-07 05:29:01 -07:00
|
|
|
static FastAdcToken fastMapSampleIndex;
|
|
|
|
static FastAdcToken hipSampleIndex;
|
2020-11-23 06:43:16 -08:00
|
|
|
|
|
|
|
#if HAL_TRIGGER_USE_ADC
|
2021-10-07 05:29:01 -07:00
|
|
|
static FastAdcToken triggerSampleIndex;
|
2020-11-23 06:43:16 -08:00
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-01-31 08:57:15 -08:00
|
|
|
extern AdcDevice fastAdc;
|
|
|
|
|
2021-10-10 19:59:25 -07:00
|
|
|
#ifdef FAST_ADC_SKIP
|
|
|
|
// No reason to enable if N = 1
|
|
|
|
static_assert(FAST_ADC_SKIP > 1);
|
|
|
|
static size_t fastAdcSkipCount = 0;
|
|
|
|
#endif // FAST_ADC_SKIP
|
2020-09-12 00:59:53 -07:00
|
|
|
|
2021-10-10 19:59:25 -07:00
|
|
|
/**
|
|
|
|
* This method is not in the adc* lower-level file because it is more business logic then hardware.
|
|
|
|
*/
|
|
|
|
void onFastAdcComplete(adcsample_t*) {
|
|
|
|
ScopePerf perf(PE::AdcCallbackFast);
|
2021-01-19 12:20:35 -08:00
|
|
|
|
2020-09-12 00:59:53 -07:00
|
|
|
#if HAL_TRIGGER_USE_ADC
|
2021-07-23 11:19:59 -07:00
|
|
|
// we need to call this ASAP, because trigger processing is time-critical
|
2021-10-07 05:29:01 -07:00
|
|
|
triggerAdcCallback(getFastAdc(triggerSampleIndex));
|
2020-09-12 00:59:53 -07:00
|
|
|
#endif /* HAL_TRIGGER_USE_ADC */
|
|
|
|
|
2021-10-10 19:59:25 -07:00
|
|
|
#ifdef FAST_ADC_SKIP
|
|
|
|
// If we run the fast ADC _very_ fast for triggerAdcCallback's benefit, we may want to
|
|
|
|
// skip most of the samples for the rest of the callback.
|
|
|
|
if (fastAdcSkipCount++ == FAST_ADC_SKIP) {
|
|
|
|
fastAdcSkipCount = 0;
|
|
|
|
} else {
|
|
|
|
return;
|
2021-07-23 11:19:59 -07:00
|
|
|
}
|
2021-01-19 12:20:35 -08:00
|
|
|
#endif
|
2019-10-11 17:43:21 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2021-07-23 11:19:59 -07:00
|
|
|
* this callback is executed 10 000 times a second, it needs to be as fast as possible
|
|
|
|
*/
|
2023-04-11 17:01:34 -07:00
|
|
|
efiAssertVoid(ObdCode::CUSTOM_STACK_ADC, 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
|
2022-09-15 18:45:48 -07:00
|
|
|
if (getEngineState()->sensorChartMode == SC_AUX_FAST1) {
|
2021-07-23 11:19:59 -07:00
|
|
|
float voltage = getAdcValue("fAux1", engineConfiguration->auxFastSensor1_adcChannel);
|
2021-11-20 03:59:02 -08:00
|
|
|
scAddData(engine->triggerCentral.getCurrentEnginePhase(getTimeNowNt()).value_or(0), voltage);
|
2021-07-23 11:19:59 -07:00
|
|
|
}
|
2019-12-04 04:33:56 -08:00
|
|
|
#endif /* EFI_SENSOR_CHART */
|
2019-12-02 17:31:49 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_MAP_AVERAGING
|
2023-01-08 14:29:29 -08:00
|
|
|
mapAveragingAdcCallback(adcToVoltsDivided(getFastAdc(fastMapSampleIndex), engineConfiguration->map.sensor.hwChannel));
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_MAP_AVERAGING */
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_HIP_9011
|
2021-11-17 00:54:21 -08:00
|
|
|
if (engineConfiguration->isHip9011Enabled) {
|
2023-01-08 14:29:29 -08:00
|
|
|
hipAdcCallback(adcToVoltsDivided(getFastAdc(hipSampleIndex), engineConfiguration->hipOutputChannel));
|
2021-07-23 11:19:59 -07:00
|
|
|
}
|
2019-12-04 04:33:56 -08:00
|
|
|
#endif /* EFI_HIP_9011 */
|
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
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
static void calcFastAdcIndexes() {
|
2021-10-07 05:29:01 -07:00
|
|
|
#if HAL_USE_ADC
|
|
|
|
fastMapSampleIndex = enableFastAdcChannel("Fast MAP", engineConfiguration->map.sensor.hwChannel);
|
|
|
|
hipSampleIndex = enableFastAdcChannel("HIP9011", engineConfiguration->hipOutputChannel);
|
2020-09-12 00:59:53 -07:00
|
|
|
#if HAL_TRIGGER_USE_ADC
|
2021-10-07 05:29:01 -07:00
|
|
|
triggerSampleIndex = enableFastAdcChannel("Trigger ADC", getAdcChannelForTrigger());
|
2020-09-12 00:59:53 -07:00
|
|
|
#endif /* HAL_TRIGGER_USE_ADC */
|
2019-12-02 17:31:49 -08:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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;
|
2020-11-26 07:41:22 -08:00
|
|
|
efiSetPadUnused(getSckPin(device));
|
|
|
|
efiSetPadUnused(getMisoPin(device));
|
|
|
|
efiSetPadUnused(getMosiPin(device));
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* HAL_USE_SPI */
|
2016-09-13 21:03:14 -07:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:51:20 -07:00
|
|
|
/**
|
|
|
|
* this method is NOT currently invoked on ECU start
|
2023-04-22 08:28:50 -07:00
|
|
|
* todo: reduce code duplication by moving more logic into startHardware method
|
2019-10-30 17:51:20 -07:00
|
|
|
*/
|
2020-09-08 08:29:38 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void applyNewHardwareSettings() {
|
2020-12-07 17:42:47 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2020-09-20 11:01:00 -07:00
|
|
|
ButtonDebounce::stopConfigurationList();
|
2020-09-08 08:29:38 -07:00
|
|
|
|
2021-08-24 13:41:16 -07:00
|
|
|
#if EFI_PROD_CODE
|
|
|
|
stopSensors();
|
|
|
|
#endif // EFI_PROD_CODE
|
|
|
|
|
2021-07-14 20:20:18 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
|
2021-11-16 01:15:29 -08:00
|
|
|
stopTriggerInputPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
2019-04-19 11:22:49 -07:00
|
|
|
|
2022-10-09 03:38:28 -07:00
|
|
|
#if EFI_SENT_SUPPORT
|
|
|
|
stopSent();
|
2022-10-22 19:46:37 -07:00
|
|
|
#endif // EFI_SENT_SUPPORT
|
2019-10-18 16:45:32 -07:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_CAN_SUPPORT
|
2016-12-19 17:01:37 -08:00
|
|
|
stopCanPins();
|
2018-11-16 04:40:06 -08:00
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
2019-04-19 12:11:00 -07:00
|
|
|
|
2023-02-23 13:05:59 -08:00
|
|
|
stopKLine();
|
|
|
|
|
2020-05-11 09:17:42 -07:00
|
|
|
#if EFI_AUX_SERIAL
|
|
|
|
stopAuxSerialPins();
|
|
|
|
#endif /* EFI_AUX_SERIAL */
|
|
|
|
|
2019-04-19 12:11:00 -07:00
|
|
|
#if EFI_HIP_9011
|
|
|
|
stopHip9001_pins();
|
|
|
|
#endif /* EFI_HIP_9011 */
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
stopHardware();
|
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)) {
|
2020-01-06 05:44:23 -08:00
|
|
|
stopSpi(SPI_DEVICE_4);
|
2020-03-23 07:32:41 -07:00
|
|
|
}
|
2020-01-06 05:44:23 -08:00
|
|
|
|
2020-03-23 07:32:41 -07:00
|
|
|
if (isPinOrModeChanged(clutchUpPin, clutchUpPinMode)) {
|
2021-07-25 09:36:53 -07:00
|
|
|
// bug? duplication with stopPedalPins?
|
2021-11-16 01:15:29 -08:00
|
|
|
efiSetPadUnused(activeConfiguration.clutchUpPin);
|
2020-03-23 07:32:41 -07:00
|
|
|
}
|
|
|
|
|
2022-04-16 14:04:35 -07:00
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2021-11-16 01:15:29 -08:00
|
|
|
stopTriggerDebugPins();
|
2022-04-16 14:04:35 -07:00
|
|
|
#endif // EFI_SHAFT_POSITION_INPUT
|
2021-07-14 22:28:44 -07:00
|
|
|
|
2017-06-04 13:35:13 -07:00
|
|
|
enginePins.unregisterPins();
|
2016-09-13 21:03:14 -07:00
|
|
|
|
2021-08-24 13:41:16 -07:00
|
|
|
#if EFI_PROD_CODE
|
2021-11-16 01:15:29 -08:00
|
|
|
reconfigureSensors();
|
2021-08-24 13:41:16 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
|
2020-09-20 11:01:00 -07:00
|
|
|
ButtonDebounce::startConfigurationList();
|
|
|
|
|
2021-01-06 05:59:13 -08:00
|
|
|
/*******************************************
|
|
|
|
* Start everything back with new settings *
|
|
|
|
******************************************/
|
2021-11-16 01:15:29 -08:00
|
|
|
startHardware();
|
2019-04-19 11:22:49 -07:00
|
|
|
|
2021-07-14 20:05:51 -07:00
|
|
|
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
|
2021-01-06 05:59:13 -08:00
|
|
|
/* 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) */
|
|
|
|
|
2020-05-11 09:17:42 -07:00
|
|
|
#if EFI_AUX_SERIAL
|
|
|
|
startAuxSerialPins();
|
|
|
|
#endif /* EFI_AUX_SERIAL */
|
|
|
|
|
2023-02-23 13:05:59 -08:00
|
|
|
startKLine();
|
2023-02-17 05:18:36 -08:00
|
|
|
|
|
|
|
|
2019-04-19 12:11:00 -07:00
|
|
|
#if EFI_HIP_9011
|
|
|
|
startHip9001_pins();
|
|
|
|
#endif /* EFI_HIP_9011 */
|
|
|
|
|
|
|
|
|
2021-07-14 20:05:51 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_IDLE_CONTROL
|
2021-02-03 06:04:22 -08:00
|
|
|
if (isIdleHardwareRestartNeeded()) {
|
2021-11-16 01:15:29 -08:00
|
|
|
initIdleHardware();
|
2019-10-19 04:35:21 -07:00
|
|
|
}
|
2019-10-18 16:45:32 -07:00
|
|
|
#endif
|
|
|
|
|
2020-02-02 00:56:21 -08:00
|
|
|
#if EFI_BOOST_CONTROL
|
|
|
|
startBoostPin();
|
|
|
|
#endif
|
2020-12-08 01:52:49 -08:00
|
|
|
#if EFI_EMULATE_POSITION_SENSORS
|
2021-11-16 01:15:29 -08:00
|
|
|
startTriggerEmulatorPins();
|
2020-12-08 01:52:49 -08:00
|
|
|
#endif /* EFI_EMULATE_POSITION_SENSORS */
|
2020-12-07 17:01:05 -08:00
|
|
|
#if EFI_LOGIC_ANALYZER
|
|
|
|
startLogicAnalyzerPins();
|
|
|
|
#endif /* EFI_LOGIC_ANALYZER */
|
2023-03-04 23:07:34 -08:00
|
|
|
#if EFI_VVT_PID
|
2021-02-17 05:53:39 -08:00
|
|
|
startVvtControlPins();
|
2023-03-04 23:07:34 -08:00
|
|
|
#endif /* EFI_VVT_PID */
|
2016-12-19 17:01:37 -08:00
|
|
|
|
2022-10-09 03:38:28 -07:00
|
|
|
#if EFI_SENT_SUPPORT
|
|
|
|
startSent();
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("setting BOR to %d", borValue);
|
2017-02-14 18:01:56 -08:00
|
|
|
BOR_Set((BOR_Level_t)borValue);
|
|
|
|
}
|
2021-07-14 19:37:05 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2017-02-14 18:01:56 -08:00
|
|
|
|
2021-03-25 13:22:03 -07:00
|
|
|
// This function initializes hardware that can do so before configuration is loaded
|
2021-11-16 01:15:29 -08:00
|
|
|
void initHardwareNoConfig() {
|
2023-04-11 17:01:34 -07:00
|
|
|
efiAssertVoid(ObdCode::CUSTOM_IH_STACK, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "init h");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
efiPrintf("initHardware()");
|
2021-03-25 13:22:03 -07:00
|
|
|
|
2021-07-14 20:05:51 -07:00
|
|
|
#if EFI_PROD_CODE
|
2021-03-25 13:22:03 -07:00
|
|
|
initPinRepository();
|
2021-07-14 20:05:51 -07:00
|
|
|
#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 */
|
|
|
|
|
2022-11-18 20:43:43 -08:00
|
|
|
#if EFI_GPIO_HARDWARE
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* We need the LED_ERROR pin even before we read configuration
|
|
|
|
*/
|
2021-04-21 09:53:13 -07:00
|
|
|
initPrimaryPins();
|
2022-11-18 20:43:43 -08:00
|
|
|
#endif // EFI_GPIO_HARDWARE
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-07-14 19:37:05 -07:00
|
|
|
#if EFI_PROD_CODE
|
2021-03-25 13:22:03 -07:00
|
|
|
// it's important to initialize this pretty early in the game before any scheduling usages
|
2021-11-16 01:15:29 -08:00
|
|
|
initSingleTimerExecutorHardware();
|
2022-11-21 00:48:55 -08:00
|
|
|
#if EFI_RTC
|
2021-03-25 13:22:03 -07:00
|
|
|
initRtc();
|
2022-11-21 00:48:55 -08:00
|
|
|
#endif // EFI_RTC
|
|
|
|
#endif // EFI_PROD_CODE
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#if EFI_INTERNAL_FLASH
|
2021-04-21 11:28:48 -07:00
|
|
|
initFlash();
|
2021-03-25 13:22:03 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if EFI_FILE_LOGGING
|
|
|
|
initEarlyMmcCard();
|
|
|
|
#endif // EFI_FILE_LOGGING
|
2022-02-03 08:24:31 -08:00
|
|
|
|
|
|
|
#if HAL_USE_PAL && EFI_PROD_CODE
|
|
|
|
// this should be initialized before detectBoardType()
|
|
|
|
efiExtiInit();
|
|
|
|
#endif // HAL_USE_PAL
|
2021-03-25 13:22:03 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void stopHardware() {
|
|
|
|
stopPedalPins();
|
2021-07-24 15:44:13 -07:00
|
|
|
|
2021-07-24 12:54:51 -07:00
|
|
|
#if EFI_PROD_CODE && (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
stopSmartCsPins();
|
|
|
|
#endif /* (BOARD_EXT_GPIOCHIPS > 0) */
|
|
|
|
|
|
|
|
#if EFI_LOGIC_ANALYZER
|
|
|
|
stopLogicAnalyzerPins();
|
|
|
|
#endif /* EFI_LOGIC_ANALYZER */
|
|
|
|
|
|
|
|
#if EFI_EMULATE_POSITION_SENSORS
|
|
|
|
stopTriggerEmulatorPins();
|
|
|
|
#endif /* EFI_EMULATE_POSITION_SENSORS */
|
|
|
|
|
2023-03-04 23:07:34 -08:00
|
|
|
#if EFI_VVT_PID
|
2021-07-24 12:54:51 -07:00
|
|
|
stopVvtControlPins();
|
2023-03-04 23:07:34 -08:00
|
|
|
#endif /* EFI_VVT_PID */
|
2021-07-24 12:54:51 -07:00
|
|
|
}
|
|
|
|
|
2021-07-14 22:28:44 -07:00
|
|
|
/**
|
|
|
|
* This method is invoked both on ECU start and configuration change
|
2023-04-19 13:51:16 -07:00
|
|
|
* At the moment we have too many system which handle ECU start and configuration change separately
|
|
|
|
* TODO: move move hardware code here
|
2021-07-14 22:28:44 -07:00
|
|
|
*/
|
2021-11-16 01:15:29 -08:00
|
|
|
void startHardware() {
|
2023-04-19 14:27:47 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
|
|
|
|
startTriggerInputPins();
|
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
|
|
|
|
2023-04-19 13:51:16 -07:00
|
|
|
#if EFI_ENGINE_CONTROL
|
|
|
|
enginePins.startPins();
|
|
|
|
#endif /* EFI_ENGINE_CONTROL */
|
|
|
|
|
2022-04-16 14:04:35 -07:00
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2021-11-16 01:15:29 -08:00
|
|
|
validateTriggerInputs();
|
2021-08-03 19:14:22 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
startTriggerDebugPins();
|
2021-07-24 15:44:13 -07:00
|
|
|
|
2022-04-16 14:04:35 -07:00
|
|
|
#endif // EFI_SHAFT_POSITION_INPUT
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
startPedalPins();
|
2021-08-14 01:35:43 -07:00
|
|
|
|
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
startCanPins();
|
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
2021-07-14 22:28:44 -07:00
|
|
|
}
|
|
|
|
|
2022-01-09 22:47:06 -08:00
|
|
|
// Weak link a stub so that every board doesn't have to implement this function
|
|
|
|
__attribute__((weak)) void boardInitHardware() { }
|
|
|
|
|
2022-01-14 19:45:49 -08:00
|
|
|
__attribute__((weak)) void setPinConfigurationOverrides() { }
|
|
|
|
|
2022-02-06 16:48:20 -08:00
|
|
|
#if HAL_USE_I2C
|
2022-02-06 16:39:21 -08:00
|
|
|
const I2CConfig i2cfg = {
|
|
|
|
OPMODE_I2C,
|
|
|
|
400000,
|
|
|
|
FAST_DUTY_CYCLE_2,
|
|
|
|
};
|
2022-02-06 16:48:20 -08:00
|
|
|
#endif
|
2022-02-06 16:39:21 -08:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initHardware() {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (hasFirmwareError()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-06 18:26:22 -08:00
|
|
|
#if STM32_I2C_USE_I2C3
|
2022-02-06 16:39:21 -08:00
|
|
|
if (engineConfiguration->useEeprom) {
|
2022-02-24 21:36:16 -08:00
|
|
|
i2cStart(&EE_U2CD, &i2cfg);
|
2022-02-06 16:39:21 -08:00
|
|
|
}
|
2022-02-06 18:26:22 -08:00
|
|
|
#endif // STM32_I2C_USE_I2C3
|
2022-02-06 16:39:21 -08:00
|
|
|
|
2022-01-16 07:48:31 -08:00
|
|
|
boardInitHardware();
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if HAL_USE_ADC
|
2019-08-01 22:36:02 -07:00
|
|
|
initAdcInputs();
|
2020-09-12 00:59:53 -07:00
|
|
|
|
2019-05-11 07:46:17 -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
|
|
|
|
2020-08-28 18:13:50 -07:00
|
|
|
#if EFI_SOFTWARE_KNOCK
|
|
|
|
initSoftwareKnock();
|
|
|
|
#endif /* EFI_SOFTWARE_KNOCK */
|
|
|
|
|
2022-12-10 20:00:12 -08:00
|
|
|
#ifdef TRIGGER_SCOPE
|
|
|
|
initTriggerScope();
|
|
|
|
#endif // TRIGGER_SCOPE
|
|
|
|
|
2019-04-13 09:02:34 -07:00
|
|
|
#if HAL_USE_SPI
|
2019-12-11 14:48:55 -08:00
|
|
|
initSpiModules(engineConfiguration);
|
2019-06-05 18:39:12 -07:00
|
|
|
#endif /* HAL_USE_SPI */
|
2020-02-26 14:30:02 -08:00
|
|
|
|
2023-03-04 23:42:54 -08:00
|
|
|
#if (EFI_PROD_CODE && BOARD_EXT_GPIOCHIPS > 0) || EFI_SIMULATOR
|
2019-04-13 09:02:34 -07:00
|
|
|
// initSmartGpio depends on 'initSpiModules'
|
2021-11-16 01:15:29 -08:00
|
|
|
initSmartGpio();
|
2020-02-26 14:30:02 -08:00
|
|
|
#endif
|
2019-04-13 09:02:34 -07:00
|
|
|
|
|
|
|
// output pins potentially depend on 'initSmartGpio'
|
2021-11-16 01:15:29 -08:00
|
|
|
initOutputPins();
|
2020-11-02 21:46:03 -08:00
|
|
|
|
2019-05-03 18:13:25 -07:00
|
|
|
#if EFI_MC33816
|
2021-04-21 11:28:48 -07:00
|
|
|
initMc33816();
|
2019-05-03 18:13:25 -07:00
|
|
|
#endif /* EFI_MC33816 */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_MAX_31855
|
2021-11-17 00:54:21 -08:00
|
|
|
initMax31855(engineConfiguration->max31855spiDevice, engineConfiguration->max31855_cs);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_MAX_31855 */
|
|
|
|
|
2023-03-04 22:28:55 -08:00
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
#if EFI_SIMULATOR
|
|
|
|
// Set CAN device name
|
|
|
|
CAND1.deviceName = "can0";
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
initCan();
|
|
|
|
#endif /* EFI_CAN_SUPPORT */
|
|
|
|
|
|
|
|
|
2022-10-21 18:16:37 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
|
2023-04-19 14:19:20 -07:00
|
|
|
onEcuStartTriggerImplementation();
|
2022-10-21 18:16:37 -07:00
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
2023-04-19 14:30:52 -07:00
|
|
|
onEcuStartDoSomethingTriggerInputPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_HIP_9011
|
2021-04-21 11:28:48 -07:00
|
|
|
initHip9011();
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif /* EFI_HIP_9011 */
|
|
|
|
|
2023-04-03 08:31:07 -07:00
|
|
|
#if EFI_WS2812
|
|
|
|
initWS2812();
|
|
|
|
#endif /* EFI_LED_WS2812 */
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_MEMS
|
2021-11-16 01:15:29 -08:00
|
|
|
initAccelerometer();
|
2017-11-02 15:09:13 -07:00
|
|
|
#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
|
|
|
|
|
2020-05-11 09:17:42 -07:00
|
|
|
#if EFI_AUX_SERIAL
|
|
|
|
initAuxSerial();
|
|
|
|
#endif /* EFI_AUX_SERIAL */
|
2017-05-12 18:19:14 -07:00
|
|
|
|
2020-04-21 17:48:37 -07:00
|
|
|
#if EFI_CAN_SUPPORT
|
2021-04-21 11:28:48 -07:00
|
|
|
initCanVssSupport();
|
2020-12-22 17:50:01 -08:00
|
|
|
#endif // EFI_CAN_SUPPORT
|
2020-04-21 17:48:37 -07:00
|
|
|
|
2019-01-04 20:47:39 -08:00
|
|
|
#if EFI_CDM_INTEGRATION
|
|
|
|
cdmIonInit();
|
2020-12-22 17:50:01 -08:00
|
|
|
#endif // EFI_CDM_INTEGRATION
|
2019-01-04 20:47:39 -08:00
|
|
|
|
2022-10-09 03:38:28 -07:00
|
|
|
#if EFI_SENT_SUPPORT
|
|
|
|
initSent();
|
|
|
|
#endif
|
|
|
|
|
2023-02-23 13:05:59 -08:00
|
|
|
initKLine();
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
calcFastAdcIndexes();
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
startHardware();
|
2021-07-14 22:28:44 -07:00
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
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;
|
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 */
|