2020-03-22 21:01:59 -07:00
|
|
|
/*
|
|
|
|
* @file engine_controller_misc.cpp
|
|
|
|
*
|
|
|
|
* @date Mar 22, 2020
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2020-10-15 05:57:13 -07:00
|
|
|
#include "os_access.h"
|
2020-03-22 21:01:59 -07:00
|
|
|
|
2020-09-09 00:22:15 -07:00
|
|
|
extern ButtonDebounce startStopButtonDebounce;
|
2020-04-01 17:25:44 -07:00
|
|
|
|
2020-04-29 07:53:35 -07:00
|
|
|
static uint8_t nextThreadId = 0;
|
|
|
|
void threadInitHook(void* vtp) {
|
|
|
|
// No lock required, this is already under lock
|
|
|
|
auto tp = reinterpret_cast<thread_t*>(vtp);
|
|
|
|
tp->threadId = ++nextThreadId;
|
|
|
|
}
|
2020-03-22 21:01:59 -07:00
|
|
|
|
2021-07-05 15:18:58 -07:00
|
|
|
#if ENABLE_PERF_TRACE
|
2020-04-27 13:07:05 -07:00
|
|
|
void irqEnterHook() {
|
2020-03-22 21:01:59 -07:00
|
|
|
perfEventBegin(PE::ISR);
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:07:05 -07:00
|
|
|
void irqExitHook() {
|
2020-03-22 21:01:59 -07:00
|
|
|
perfEventEnd(PE::ISR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void contextSwitchHook() {
|
|
|
|
perfEventInstantGlobal(PE::ContextSwitch);
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:07:05 -07:00
|
|
|
#else
|
|
|
|
void irqEnterHook() {}
|
|
|
|
void irqExitHook() {}
|
|
|
|
void contextSwitchHook() {}
|
2020-03-22 21:01:59 -07:00
|
|
|
#endif /* ENABLE_PERF_TRACE */
|
|
|
|
|
|
|
|
#if EFI_ENABLE_MOCK_ADC
|
|
|
|
void setMockVoltage(int hwChannel, float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
engine->engineState.mockAdcState.setMockVoltage(hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMockMafVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
setMockVoltage(engineConfiguration->mafAdcChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMockAfrVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
setMockVoltage(engineConfiguration->afr.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMockMapVoltage(float voltage DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
setMockVoltage(engineConfiguration->map.sensor.hwChannel, voltage PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
}
|
|
|
|
#endif /* EFI_ENABLE_MOCK_ADC */
|
|
|
|
|
2021-11-08 05:24:20 -08:00
|
|
|
#if !EFI_UNIT_TEST
|
2020-03-22 21:01:59 -07:00
|
|
|
/**
|
|
|
|
* 64-bit result would not overflow, but that's complex stuff for our 32-bit MCU
|
|
|
|
*/
|
|
|
|
efitimeus_t getTimeNowUs(void) {
|
|
|
|
ScopePerf perf(PE::GetTimeNowUs);
|
2020-11-15 21:06:11 -08:00
|
|
|
return NT2US(getTimeNowNt());
|
2020-03-22 21:01:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-08 05:24:20 -08:00
|
|
|
// this is bits 30-61, not 32-63. We only support 62-bit time. You can fire me in 36,533 years
|
|
|
|
// (1,461 on the simulator).
|
|
|
|
static volatile uint32_t upperTimeNt = 0;
|
2020-03-22 21:01:59 -07:00
|
|
|
|
2021-11-08 05:24:20 -08:00
|
|
|
efitick_t getTimeNowNt() {
|
|
|
|
// Shift cannot be 31, as we wouldn't be able to tell if time is moving forward or backward
|
|
|
|
// relative to upperTimeNt. We do need to handle both directions as our "thread" can be
|
|
|
|
// racing with other "threads" in sampling stamp and updating upperTimeNt.
|
|
|
|
constexpr unsigned shift = 30;
|
|
|
|
|
2020-10-15 05:57:13 -07:00
|
|
|
uint32_t stamp = getTimeNowLowerNt();
|
2021-11-08 05:24:20 -08:00
|
|
|
uint32_t upper = upperTimeNt;
|
|
|
|
uint32_t relative_unsigned = stamp - (upper << shift);
|
|
|
|
efitick_t time64 = (efitick_t(upper) << shift) + (int32_t)relative_unsigned;
|
|
|
|
upperTimeNt = time64 >> shift;
|
2020-10-15 05:57:13 -07:00
|
|
|
|
2021-11-08 05:24:20 -08:00
|
|
|
return time64;
|
2020-10-15 05:57:13 -07:00
|
|
|
}
|
2021-11-08 05:24:20 -08:00
|
|
|
#endif /* !EFI_UNIT_TEST */
|
2020-11-18 18:08:01 -08:00
|
|
|
|
2020-04-01 17:25:44 -07:00
|
|
|
static void onStartStopButtonToggle(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|
|
|
engine->startStopStateToggleCounter++;
|
|
|
|
|
2020-11-18 18:08:01 -08:00
|
|
|
if (engine->rpmCalculator.isStopped()) {
|
2020-04-01 17:25:44 -07:00
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(1);
|
|
|
|
if (!wasStarterEngaged) {
|
2020-11-18 20:54:30 -08:00
|
|
|
engine->startStopStateLastPushTime = getTimeNowNt();
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("Let's crank this engine for up to %d seconds via %s!",
|
2020-11-19 14:15:28 -08:00
|
|
|
CONFIG(startCrankingDuration),
|
|
|
|
hwPortname(CONFIG(starterControlPin)));
|
2020-04-01 17:25:44 -07:00
|
|
|
}
|
2020-09-05 15:49:42 -07:00
|
|
|
} else if (engine->rpmCalculator.isRunning()) {
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("Let's stop this engine!");
|
2020-11-18 18:08:01 -08:00
|
|
|
doScheduleStopEngine(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2020-04-01 17:25:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:48:52 -08:00
|
|
|
|
2020-04-01 17:25:44 -07:00
|
|
|
void slowStartStopButtonCallback(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2020-09-09 00:22:15 -07:00
|
|
|
bool startStopState = startStopButtonDebounce.readPinEvent();
|
2020-04-01 17:25:44 -07:00
|
|
|
|
2020-09-09 00:22:15 -07:00
|
|
|
if (startStopState && !engine->startStopState) {
|
|
|
|
// we are here on transition from 0 to 1
|
|
|
|
onStartStopButtonToggle(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2020-04-01 17:25:44 -07:00
|
|
|
}
|
2020-09-09 00:22:15 -07:00
|
|
|
engine->startStopState = startStopState;
|
2020-04-01 17:25:44 -07:00
|
|
|
|
2020-11-18 20:54:30 -08:00
|
|
|
if (engine->startStopStateLastPushTime == 0) {
|
|
|
|
// nothing is going on with startStop button
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-01 17:25:44 -07:00
|
|
|
// todo: should this be simply FSIO?
|
2020-09-05 15:49:42 -07:00
|
|
|
if (engine->rpmCalculator.isRunning()) {
|
2020-04-01 17:25:44 -07:00
|
|
|
// turn starter off once engine is running
|
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(0);
|
|
|
|
if (wasStarterEngaged) {
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("Engine runs we can disengage the starter");
|
2020-11-18 20:54:30 -08:00
|
|
|
engine->startStopStateLastPushTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getTimeNowNt() - engine->startStopStateLastPushTime > NT_PER_SECOND * CONFIG(startCrankingDuration)) {
|
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(0);
|
|
|
|
if (wasStarterEngaged) {
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("Cranking timeout %d seconds", CONFIG(startCrankingDuration));
|
2020-11-18 20:54:30 -08:00
|
|
|
engine->startStopStateLastPushTime = 0;
|
2020-04-01 17:25:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|