rusefi-1/firmware/hw_layer/trigger_input.cpp

191 lines
6.2 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file trigger_input.cpp
* @brief Position sensor hardware layer
*
* @date Dec 30, 2012
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
#include "main.h"
#if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__)
#include "trigger_input.h"
#include "digital_input_hw.h"
#include "pin_repository.h"
#include "trigger_structure.h"
#include "trigger_central.h"
#include "engine_configuration.h"
#define TRIGGER_SUPPORTED_CHANNELS 2
static ICUDriver *primaryCrankDriver;
2016-01-24 15:01:56 -08:00
extern bool hasFirmwareErrorFlag;
2015-07-10 06:01:56 -07:00
EXTERN_ENGINE
;
static Logging *logger;
2016-09-17 08:03:00 -07:00
int vvtEventRiseCounter = 0;
int vvtEventFallCounter = 0;
2016-08-20 06:02:06 -07:00
static void cam_icu_width_callback(ICUDriver *icup) {
2017-03-22 16:46:05 -07:00
(void)icup;
2016-09-17 08:03:00 -07:00
vvtEventRiseCounter++;
2016-08-20 20:02:09 -07:00
hwHandleVvtCamSignal(TV_RISE);
2016-08-20 06:02:06 -07:00
}
static void cam_icu_period_callback(ICUDriver *icup) {
2017-03-22 16:46:05 -07:00
(void)icup;
2016-09-17 08:03:00 -07:00
vvtEventFallCounter++;
2016-08-20 20:02:09 -07:00
hwHandleVvtCamSignal(TV_FALL);
2016-08-20 06:02:06 -07:00
}
2015-07-10 06:01:56 -07:00
/**
* that's hardware timer input capture IRQ entry point
* 'width' events happens before the 'period' event
*/
static void shaft_icu_width_callback(ICUDriver *icup) {
// todo: support for 3rd trigger input channel
// todo: start using real event time from HW event, not just software timer?
2016-01-24 15:01:56 -08:00
if (hasFirmwareErrorFlag)
return;
2015-07-10 06:01:56 -07:00
int isPrimary = icup == primaryCrankDriver;
2017-10-16 11:31:02 -07:00
if (!isPrimary && !TRIGGER_SHAPE(needSecondTriggerInput)) {
2015-07-10 06:01:56 -07:00
return;
}
// icucnt_t last_width = icuGetWidth(icup); so far we are fine with system time
// todo: add support for 3rd channel
2017-10-16 12:07:36 -07:00
trigger_event_e signal = isPrimary ? (engineConfiguration->invertPrimaryTriggerSignal ? SHAFT_PRIMARY_FALLING : SHAFT_PRIMARY_RISING) : (engineConfiguration->invertSecondaryTriggerSignal ? SHAFT_SECONDARY_FALLING : SHAFT_SECONDARY_RISING);
2015-07-10 06:01:56 -07:00
hwHandleShaftSignal(signal);
}
static void shaft_icu_period_callback(ICUDriver *icup) {
2016-01-24 15:01:56 -08:00
if (hasFirmwareErrorFlag)
return;
2015-07-10 06:01:56 -07:00
int isPrimary = icup == primaryCrankDriver;
2017-10-16 11:31:02 -07:00
if (!isPrimary && !TRIGGER_SHAPE(needSecondTriggerInput)) {
2015-07-10 06:01:56 -07:00
return;
}
// todo: add support for 3rd channel
// icucnt_t last_period = icuGetPeriod(icup); so far we are fine with system time
2015-08-22 08:02:10 -07:00
trigger_event_e signal =
2017-10-16 12:07:36 -07:00
isPrimary ? (engineConfiguration->invertPrimaryTriggerSignal ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING) : (engineConfiguration->invertSecondaryTriggerSignal ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING);
2015-07-10 06:01:56 -07:00
hwHandleShaftSignal(signal);
}
/**
* the main purpose of this configuration structure is to specify the input interrupt callbacks
*/
2017-03-22 16:46:05 -07:00
static ICUConfig shaft_icucfg = { ICU_INPUT_ACTIVE_LOW,
100000, /* 100kHz ICU clock frequency. */
shaft_icu_width_callback,
shaft_icu_period_callback,
NULL,
ICU_CHANNEL_1,
0};
2015-07-10 06:01:56 -07:00
2016-08-20 06:02:06 -07:00
/**
* this is about VTTi and stuff kind of cam sensor
*/
2017-03-22 16:46:05 -07:00
static ICUConfig cam_icucfg = { ICU_INPUT_ACTIVE_LOW,
100000, /* 100kHz ICU clock frequency. */
cam_icu_width_callback,
cam_icu_period_callback,
NULL,
ICU_CHANNEL_1,
0};
2016-08-20 06:02:06 -07:00
2016-12-27 09:00:48 -08:00
static ICUDriver *turnOnTriggerInputPin(const char *msg, brain_pin_e hwPin, ICUConfig *icucfg) {
2016-08-20 20:02:09 -07:00
if (hwPin == GPIO_UNASSIGNED)
return NULL;
2015-07-10 06:01:56 -07:00
// configure pin
2016-12-27 09:00:48 -08:00
turnOnCapturePin(msg, hwPin);
2016-08-20 16:02:07 -07:00
icucfg->channel = getInputCaptureChannel(hwPin);
2015-07-10 06:01:56 -07:00
2017-02-06 16:03:19 -08:00
ICUDriver *driver = getInputCaptureDriver(msg, hwPin);
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "turnOnTriggerInputPin %s", hwPortname(hwPin));
// todo: reuse 'setWaveReaderMode' method here?
if (driver != NULL) {
// todo: once http://forum.chibios.org/phpbb/viewtopic.php?f=16&t=1757 is fixed
2016-02-27 20:03:34 -08:00
// bool needWidthCallback = !CONFIG(useOnlyRisingEdgeForTrigger) || TRIGGER_SHAPE(useRiseEdge);
2015-07-10 06:01:56 -07:00
// shaft_icucfg.width_cb = needWidthCallback ? shaft_icu_width_callback : NULL;
2016-02-27 20:03:34 -08:00
// bool needPeriodCallback = !CONFIG(useOnlyRisingEdgeForTrigger) || !TRIGGER_SHAPE(useRiseEdge);
2015-07-10 06:01:56 -07:00
// shaft_icucfg.period_cb = needPeriodCallback ? shaft_icu_period_callback : NULL;
2016-08-20 16:02:07 -07:00
efiIcuStart(driver, icucfg);
2015-07-10 06:01:56 -07:00
if (driver->state == ICU_READY) {
efiAssert(driver != NULL, "ti: driver is NULL", NULL);
efiAssert(driver->state == ICU_READY, "ti: driver not ready", NULL);
icuStartCapture(driver); // this would change state from READY to WAITING
2017-03-22 16:46:05 -07:00
icuEnableNotifications(driver);
2015-07-10 06:01:56 -07:00
} else {
// we would be here for example if same pin is used for multiple input capture purposes
2016-10-10 13:02:39 -07:00
firmwareError(CUSTOM_ERR_ICU_STATE, "ICU unexpected state [%s]", hwPortname(hwPin));
2015-07-10 06:01:56 -07:00
}
}
return driver;
}
static void turnOffTriggerInputPin(brain_pin_e hwPin) {
2017-02-06 16:03:19 -08:00
ICUDriver *driver = getInputCaptureDriver("trigger_off", hwPin);
2015-07-10 06:01:56 -07:00
if (driver != NULL) {
2017-03-22 16:46:05 -07:00
icuDisableNotifications(driver);
2017-03-22 16:16:45 -07:00
icuStopCapture(driver);
2015-07-10 06:01:56 -07:00
icuStop(driver);
scheduleMsg(logger, "turnOffTriggerInputPin %s", hwPortname(hwPin));
unmarkPin(hwPin);
}
}
static void rememberPrimaryChannel(void) {
2017-02-06 16:03:19 -08:00
primaryCrankDriver = getInputCaptureDriver("primary",
2015-08-22 08:02:10 -07:00
boardConfiguration->triggerInputPins[0]);
2015-07-10 06:01:56 -07:00
}
void turnOnTriggerInputPins(Logging *sharedLogger) {
logger = sharedLogger;
2016-08-20 20:02:09 -07:00
applyNewTriggerInputPins();
2015-07-10 06:01:56 -07:00
}
void stopTriggerInputPins(void) {
2015-08-22 08:02:10 -07:00
for (int i = 0; i < TRIGGER_SUPPORTED_CHANNELS; i++) {
if (boardConfiguration->triggerInputPins[i]
!= activeConfiguration.bc.triggerInputPins[i]) {
turnOffTriggerInputPin(activeConfiguration.bc.triggerInputPins[i]);
}
2015-07-10 06:01:56 -07:00
}
2016-09-17 10:02:39 -07:00
if (engineConfiguration->camInput != activeConfiguration.camInput) {
turnOffTriggerInputPin(activeConfiguration.camInput);
}
2015-07-10 06:01:56 -07:00
}
void applyNewTriggerInputPins(void) {
// first we will turn off all the changed pins
2015-08-22 08:02:10 -07:00
stopTriggerInputPins();
2015-07-10 06:01:56 -07:00
// then we will enable all the changed pins
2015-08-22 08:02:10 -07:00
for (int i = 0; i < TRIGGER_SUPPORTED_CHANNELS; i++) {
if (boardConfiguration->triggerInputPins[i]
!= activeConfiguration.bc.triggerInputPins[i]) {
2016-12-27 09:00:48 -08:00
const char * msg = (i == 0 ? "trigger#1" : (i == 1 ? "trigger#2" : "trigger#3"));
turnOnTriggerInputPin(msg, boardConfiguration->triggerInputPins[i], &shaft_icucfg);
2015-08-22 08:02:10 -07:00
}
2015-07-10 06:01:56 -07:00
}
2016-08-20 20:02:09 -07:00
if (engineConfiguration->camInput != activeConfiguration.camInput) {
2016-12-27 09:00:48 -08:00
turnOnTriggerInputPin("cam", engineConfiguration->camInput, &cam_icucfg);
2016-08-20 20:02:09 -07:00
}
2016-08-20 16:02:07 -07:00
2015-08-22 08:02:10 -07:00
rememberPrimaryChannel();
2015-07-10 06:01:56 -07:00
}
#endif /* EFI_SHAFT_POSITION_INPUT */