2019-11-11 18:25:40 -08:00
|
|
|
/*
|
2019-04-12 05:05:53 -07:00
|
|
|
* @file trigger_input.cpp
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
2019-11-11 18:25:40 -08:00
|
|
|
* @date Nov 11, 2019
|
2021-01-31 19:10:10 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2021
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "trigger_input.h"
|
2019-04-12 05:05:53 -07:00
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
/* TODO:
|
|
|
|
* - merge comparator trigger
|
|
|
|
*/
|
|
|
|
|
2021-07-14 19:02:28 -07:00
|
|
|
#if (EFI_SHAFT_POSITION_INPUT) || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2022-07-07 12:27:59 -07:00
|
|
|
#if (HAL_TRIGGER_USE_PAL == TRUE) || (HAL_TRIGGER_USE_ADC == TRUE)
|
2020-01-07 00:42:08 -08:00
|
|
|
|
|
|
|
#if (HAL_TRIGGER_USE_PAL == TRUE)
|
|
|
|
int extiTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft);
|
|
|
|
void extiTriggerTurnOffInputPin(brain_pin_e brainPin);
|
|
|
|
#else
|
|
|
|
int extiTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft) {
|
|
|
|
UNUSED(msg);
|
|
|
|
UNUSED(index);
|
|
|
|
UNUSED(isTriggerShaft);
|
|
|
|
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
#define extiTriggerTurnOffInputPin(brainPin) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2021-12-01 14:26:58 -08:00
|
|
|
#if (HAL_TRIGGER_USE_ADC == TRUE)
|
|
|
|
void adcTriggerTurnOnInputPins();
|
|
|
|
int adcTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft);
|
|
|
|
void adcTriggerTurnOffInputPin(brain_pin_e brainPin);
|
|
|
|
#else
|
|
|
|
#define adcTriggerTurnOnInputPins() ((void)0)
|
|
|
|
int adcTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft) {
|
|
|
|
UNUSED(msg);
|
|
|
|
UNUSED(index);
|
|
|
|
UNUSED(isTriggerShaft);
|
|
|
|
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
#define adcTriggerTurnOffInputPin(brainPin) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
enum triggerType {
|
|
|
|
TRIGGER_NONE,
|
2021-12-01 14:26:58 -08:00
|
|
|
TRIGGER_EXTI,
|
|
|
|
TRIGGER_ADC,
|
2020-01-07 00:42:08 -08:00
|
|
|
};
|
|
|
|
|
2022-06-01 18:24:20 -07:00
|
|
|
static triggerType shaftTriggerType[TRIGGER_INPUT_PIN_COUNT];
|
2020-01-07 00:42:08 -08:00
|
|
|
static triggerType camTriggerType[CAM_INPUTS_COUNT];
|
|
|
|
|
|
|
|
static int turnOnTriggerInputPin(const char *msg, int index, bool isTriggerShaft) {
|
|
|
|
brain_pin_e brainPin = isTriggerShaft ?
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->triggerInputPins[index] : engineConfiguration->camInputs[index];
|
2020-01-07 00:42:08 -08:00
|
|
|
|
2021-01-31 18:45:01 -08:00
|
|
|
if (isTriggerShaft) {
|
2020-01-07 00:42:08 -08:00
|
|
|
shaftTriggerType[index] = TRIGGER_NONE;
|
2021-01-31 18:45:01 -08:00
|
|
|
} else {
|
2020-01-07 00:42:08 -08:00
|
|
|
camTriggerType[index] = TRIGGER_NONE;
|
2021-01-31 18:45:01 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
|
2021-01-31 18:45:01 -08:00
|
|
|
if (!isBrainPinValid(brainPin)) {
|
2020-01-07 00:42:08 -08:00
|
|
|
return 0;
|
2021-01-31 18:45:01 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
|
2021-12-01 14:26:58 -08:00
|
|
|
/* ... then ADC */
|
|
|
|
#if HAL_TRIGGER_USE_ADC
|
|
|
|
if (adcTriggerTurnOnInputPin(msg, index, isTriggerShaft) >= 0) {
|
|
|
|
if (isTriggerShaft) {
|
|
|
|
shaftTriggerType[index] = TRIGGER_ADC;
|
|
|
|
} else {
|
|
|
|
camTriggerType[index] = TRIGGER_ADC;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
/* ... then EXTI */
|
|
|
|
if (extiTriggerTurnOnInputPin(msg, index, isTriggerShaft) >= 0) {
|
2021-01-31 18:45:01 -08:00
|
|
|
if (isTriggerShaft) {
|
2020-01-07 00:42:08 -08:00
|
|
|
shaftTriggerType[index] = TRIGGER_EXTI;
|
2021-01-31 18:45:01 -08:00
|
|
|
} else {
|
2020-01-07 00:42:08 -08:00
|
|
|
camTriggerType[index] = TRIGGER_EXTI;
|
2021-01-31 18:45:01 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:01:34 -07:00
|
|
|
firmwareError(ObdCode::CUSTOM_ERR_NOT_INPUT_PIN, "%s: Not input pin %s", msg, hwPortname(brainPin));
|
2020-01-07 00:42:08 -08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void turnOffTriggerInputPin(int index, bool isTriggerShaft) {
|
|
|
|
brain_pin_e brainPin = isTriggerShaft ?
|
|
|
|
activeConfiguration.triggerInputPins[index] : activeConfiguration.camInputs[index];
|
|
|
|
|
|
|
|
if (isTriggerShaft) {
|
2021-12-01 14:26:58 -08:00
|
|
|
if (shaftTriggerType[index] == TRIGGER_ADC) {
|
|
|
|
adcTriggerTurnOffInputPin(brainPin);
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:45:01 -08:00
|
|
|
if (shaftTriggerType[index] == TRIGGER_EXTI) {
|
2020-01-07 00:42:08 -08:00
|
|
|
extiTriggerTurnOffInputPin(brainPin);
|
2021-01-31 18:45:01 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
|
|
|
|
shaftTriggerType[index] = TRIGGER_NONE;
|
|
|
|
} else {
|
2021-12-01 14:26:58 -08:00
|
|
|
if (camTriggerType[index] == TRIGGER_ADC) {
|
|
|
|
adcTriggerTurnOffInputPin(brainPin);
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:45:01 -08:00
|
|
|
if (camTriggerType[index] == TRIGGER_EXTI) {
|
2020-01-07 00:42:08 -08:00
|
|
|
extiTriggerTurnOffInputPin(brainPin);
|
2021-01-31 18:45:01 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
|
|
|
|
camTriggerType[index] = TRIGGER_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Exported functions. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void stopTriggerInputPins() {
|
2022-06-01 18:24:20 -07:00
|
|
|
for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
|
2019-12-11 14:48:55 -08:00
|
|
|
if (isConfigurationChanged(triggerInputPins[i])) {
|
2020-01-07 00:42:08 -08:00
|
|
|
turnOffTriggerInputPin(i, true);
|
2019-11-11 19:19:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
|
|
|
|
if (isConfigurationChanged(camInputs[i])) {
|
2020-01-07 00:42:08 -08:00
|
|
|
turnOffTriggerInputPin(i, false);
|
2019-11-11 19:19:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 15:04:57 -07:00
|
|
|
static const char* const camNames[] = { "cam1", "cam2", "cam3", "cam4"};
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void startTriggerInputPins() {
|
2022-06-01 18:24:20 -07:00
|
|
|
for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
|
2019-12-11 14:48:55 -08:00
|
|
|
if (isConfigurationChanged(triggerInputPins[i])) {
|
2023-04-19 14:36:47 -07:00
|
|
|
const char * msg = (i == 0 ? "Trigger #1" : "Trigger #2");
|
2019-11-13 19:56:43 -08:00
|
|
|
turnOnTriggerInputPin(msg, i, true);
|
2019-11-11 19:19:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
|
|
|
|
if (isConfigurationChanged(camInputs[i])) {
|
2021-08-11 15:04:57 -07:00
|
|
|
turnOnTriggerInputPin(camNames[i], i, false);
|
2019-11-11 19:19:35 -08:00
|
|
|
}
|
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
}
|
|
|
|
|
2023-04-19 14:19:20 -07:00
|
|
|
void onEcuStartTriggerImplementation() {
|
2019-11-11 19:19:35 -08:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:27:59 -07:00
|
|
|
#endif /* (HAL_TRIGGER_USE_PAL == TRUE) || (HAL_TRIGGER_USE_ADC == TRUE) */
|
2019-11-11 19:19:35 -08:00
|
|
|
|
2021-07-14 21:50:29 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void stopTriggerDebugPins() {
|
2021-07-14 21:50:29 -07:00
|
|
|
for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
|
2021-08-14 01:58:10 -07:00
|
|
|
efiSetPadUnusedIfConfigurationChanged(triggerInputDebugPins[i]);
|
2021-07-14 21:50:29 -07:00
|
|
|
}
|
|
|
|
for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
|
2021-08-14 01:58:10 -07:00
|
|
|
efiSetPadUnusedIfConfigurationChanged(camInputsDebug[i]);
|
2021-07-14 21:50:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void startTriggerDebugPins() {
|
2021-07-14 21:50:29 -07:00
|
|
|
for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
|
2021-08-14 03:15:29 -07:00
|
|
|
efiSetPadModeIfConfigurationChanged("trigger debug", triggerInputDebugPins[i], PAL_MODE_OUTPUT_PUSHPULL);
|
2021-07-14 21:50:29 -07:00
|
|
|
}
|
|
|
|
for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
|
2021-08-14 03:15:29 -07:00
|
|
|
efiSetPadModeIfConfigurationChanged("cam debug", camInputsDebug[i], PAL_MODE_OUTPUT_PUSHPULL);
|
2021-07-14 21:50:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 14:30:52 -07:00
|
|
|
#endif /* EFI_SHAFT_POSITION_INPUT */
|
|
|
|
|
2023-04-19 14:16:19 -07:00
|
|
|
void onEcuStartDoSomethingTriggerInputPins() {
|
2022-10-22 06:27:41 -07:00
|
|
|
if (hasFirmwareError()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-19 14:30:52 -07:00
|
|
|
#if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
|
2022-10-27 20:17:02 -07:00
|
|
|
if (isBrainPinValid(engineConfiguration->triggerInputPins[0])) {
|
2023-04-19 14:30:52 -07:00
|
|
|
// todo: we have another 'rpmCalculator.Register' for UNIT_TEST would be great to unify
|
2022-10-27 20:17:02 -07:00
|
|
|
engine->rpmCalculator.Register();
|
|
|
|
} else {
|
|
|
|
// if we do not have primary input channel maybe it's BCM mode and we inject RPM value via Lua?
|
2022-10-22 06:27:41 -07:00
|
|
|
engine->rpmCalculator.unregister();
|
|
|
|
}
|
2023-04-19 14:30:52 -07:00
|
|
|
#endif /* EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT */
|
2019-04-09 19:15:55 -07:00
|
|
|
}
|