2019-11-11 18:26:09 -08:00
|
|
|
/**
|
|
|
|
* @file trigger_input_icu.cpp
|
2019-11-11 19:19:35 -08:00
|
|
|
* @brief Position sensor hardware layer - ICU version
|
2019-11-11 18:26:09 -08:00
|
|
|
*
|
2019-11-11 20:32:09 -08:00
|
|
|
* todo: code reuse with digital_input_icu.cpp was never finished
|
2019-11-11 18:26:09 -08:00
|
|
|
* todo: at the moment due to half-done code reuse we already depend on EFI_ICU_INPUTS but still have custom code
|
|
|
|
* todo: VVT implementation is a nasty copy-paste :(
|
|
|
|
*
|
2019-11-11 20:32:09 -08:00
|
|
|
* see digital_input_icu.cpp
|
2019-11-11 18:26:09 -08:00
|
|
|
*
|
|
|
|
* @date Dec 30, 2012
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2019
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
volatile int icuWidthCallbackCounter = 0;
|
|
|
|
volatile int icuWidthPeriodCounter = 0;
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
#if EFI_SHAFT_POSITION_INPUT && (HAL_USE_ICU == TRUE)
|
2019-11-11 18:26:09 -08:00
|
|
|
|
|
|
|
#include "trigger_input.h"
|
2019-11-11 20:32:09 -08:00
|
|
|
#include "digital_input_icu.h"
|
2019-11-11 19:19:35 -08:00
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
2019-11-11 18:26:09 -08:00
|
|
|
|
|
|
|
extern bool hasFirmwareErrorFlag;
|
|
|
|
|
|
|
|
static Logging *logger;
|
|
|
|
|
2019-11-13 19:56:43 -08:00
|
|
|
static void vvtWidthCallback(void *arg) {
|
|
|
|
(void)arg;
|
2019-11-11 18:26:09 -08:00
|
|
|
hwHandleVvtCamSignal(TV_RISE);
|
|
|
|
}
|
|
|
|
|
2019-11-13 19:56:43 -08:00
|
|
|
static void vvtPeriodCallback(void *arg) {
|
|
|
|
(void)arg;
|
2019-11-11 18:26:09 -08:00
|
|
|
hwHandleVvtCamSignal(TV_FALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* that's hardware timer input capture IRQ entry point
|
|
|
|
* 'width' events happens before the 'period' event
|
|
|
|
*/
|
2019-11-13 19:56:43 -08:00
|
|
|
static void shaftWidthCallback(bool isPrimary) {
|
2019-11-11 19:19:35 -08:00
|
|
|
if (!engine->hwTriggerInputEnabled) {
|
2019-11-11 18:26:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
icuWidthCallbackCounter++;
|
|
|
|
// todo: support for 3rd trigger input channel
|
|
|
|
// todo: start using real event time from HW event, not just software timer?
|
|
|
|
if (hasFirmwareErrorFlag)
|
|
|
|
return;
|
2019-12-07 22:09:39 -08:00
|
|
|
if (!isPrimary && !TRIGGER_WAVEFORM(needSecondTriggerInput)) {
|
2019-11-11 18:26:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// icucnt_t last_width = icuGetWidth(icup); so far we are fine with system time
|
|
|
|
// todo: add support for 3rd channel
|
|
|
|
trigger_event_e signal = isPrimary ? (engineConfiguration->invertPrimaryTriggerSignal ? SHAFT_PRIMARY_FALLING : SHAFT_PRIMARY_RISING) : (engineConfiguration->invertSecondaryTriggerSignal ? SHAFT_SECONDARY_FALLING : SHAFT_SECONDARY_RISING);
|
|
|
|
|
|
|
|
hwHandleShaftSignal(signal);
|
|
|
|
}
|
|
|
|
|
2019-11-13 19:56:43 -08:00
|
|
|
static void shaftPeriodCallback(bool isPrimary) {
|
2019-11-11 19:19:35 -08:00
|
|
|
if (!engine->hwTriggerInputEnabled) {
|
2019-11-11 18:26:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
icuWidthPeriodCounter++;
|
|
|
|
if (hasFirmwareErrorFlag)
|
|
|
|
return;
|
2019-12-07 22:09:39 -08:00
|
|
|
if (!isPrimary && !TRIGGER_WAVEFORM(needSecondTriggerInput)) {
|
2019-11-11 18:26:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: add support for 3rd channel
|
|
|
|
// icucnt_t last_period = icuGetPeriod(icup); so far we are fine with system time
|
|
|
|
trigger_event_e signal =
|
|
|
|
isPrimary ? (engineConfiguration->invertPrimaryTriggerSignal ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING) : (engineConfiguration->invertSecondaryTriggerSignal ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING);
|
|
|
|
hwHandleShaftSignal(signal);
|
|
|
|
}
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
/*==========================================================================*/
|
|
|
|
/* Exported functions. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
|
|
int icuTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft) {
|
2019-11-17 05:44:07 -08:00
|
|
|
(void)msg;
|
2019-12-11 14:48:55 -08:00
|
|
|
brain_pin_e brainPin = isTriggerShaft ? CONFIG(triggerInputPins)[index] : engineConfiguration->camInputs[index];
|
2019-11-11 18:26:09 -08:00
|
|
|
if (brainPin == GPIO_UNASSIGNED) {
|
2020-01-07 00:42:08 -08:00
|
|
|
return -1;
|
2019-11-11 18:26:09 -08:00
|
|
|
}
|
|
|
|
|
2019-11-13 19:56:43 -08:00
|
|
|
digital_input_s* input = startDigitalCapture("trigger", brainPin, true);
|
2020-01-03 06:13:55 -08:00
|
|
|
if (input == NULL) {
|
|
|
|
/* error already reported */
|
2020-01-07 00:42:08 -08:00
|
|
|
return -1;
|
2020-01-03 06:13:55 -08:00
|
|
|
}
|
|
|
|
|
2019-11-13 19:56:43 -08:00
|
|
|
if (isTriggerShaft) {
|
|
|
|
void * arg = (void*) (index == 0);
|
|
|
|
input->setWidthCallback((VoidInt)(void*)shaftWidthCallback, arg);
|
|
|
|
input->setPeriodCallback((VoidInt)(void*)shaftPeriodCallback, arg);
|
2019-11-12 16:04:33 -08:00
|
|
|
} else {
|
2019-11-13 19:56:43 -08:00
|
|
|
input->setWidthCallback((VoidInt)(void*)vvtWidthCallback, NULL);
|
|
|
|
input->setPeriodCallback((VoidInt)(void*)vvtPeriodCallback, NULL);
|
2019-11-11 18:26:09 -08:00
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
|
|
|
|
return 0;
|
2019-11-11 18:26:09 -08:00
|
|
|
}
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
void icuTriggerTurnOffInputPin(brain_pin_e brainPin) {
|
2019-11-13 19:56:43 -08:00
|
|
|
|
|
|
|
stopDigitalCapture("trigger", brainPin);
|
2019-11-11 18:26:09 -08:00
|
|
|
}
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
void icuTriggerSetPrimaryChannel(brain_pin_e brainPin) {
|
2019-11-17 05:44:07 -08:00
|
|
|
(void)brainPin;
|
2019-11-11 18:26:09 -08:00
|
|
|
}
|
|
|
|
|
2020-01-07 00:42:08 -08:00
|
|
|
void icuTriggerTurnOnInputPins(Logging *sharedLogger) {
|
2019-11-11 18:26:09 -08:00
|
|
|
logger = sharedLogger;
|
|
|
|
}
|
2020-01-07 00:42:08 -08:00
|
|
|
#endif /* (EFI_SHAFT_POSITION_INPUT && (HAL_USE_ICU == TRUE)) */
|