rusEFI
The most advanced open source ECU
Functions | Variables
microsecond_timer.cpp File Reference

Detailed Description

Here we have a 1MHz timer dedicated to event scheduling. We are using one of the 32-bit timers here, so this timer can schedule events up to 4B/100M ~ 4000 seconds ~ 1 hour from current time.

GPT5 timer clock: 84000000Hz If only it was a better multiplier of 2 (84000000 = 328125 * 256)

Date
Apr 14, 2014
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file microsecond_timer.cpp.

Functions

void setHardwareSchedulerTimer (efitick_t nowNt, efitick_t setTimeNt)
 
void globalTimerCallback ()
 
void portMicrosecondTimerCallback ()
 
static void watchDogBuddyCallback (void *)
 
static void timerValidationCallback (void *)
 
static void validateHardwareTimer ()
 
void initMicrosecondTimer ()
 

Variables

uint32_t maxPrecisionCallbackDuration = 0
 
static efitick_t lastSetTimerTimeNt
 
static bool isTimerPending = false
 
static int timerCallbackCounter = 0
 
static int timerRestartCounter = 0
 
static const char * msg
 
static int timerFreezeCounter = 0
 
static int setHwTimerCounter = 0
 
static bool hwStarted = false
 
static MicrosecondTimerWatchdogController watchdogControllerInstance
 
static scheduling_s watchDogBuddy
 
static volatile bool testSchedulingHappened = false
 
static efitimems_t testSchedulingStart
 

Function Documentation

◆ globalTimerCallback()

void globalTimerCallback ( )

Definition at line 36 of file single_timer_executor.cpp.

36  {
37  efiAssertVoid(ObdCode::CUSTOM_ERR_6624, hasLotsOfRemainingStack(), "lowstck#2y");
38 
40 }
SingleTimerExecutor executor
Definition: engine.h:237
Engine ___engine
@ CUSTOM_ERR_6624

Referenced by portMicrosecondTimerCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initMicrosecondTimer()

void initMicrosecondTimer ( )

Definition at line 169 of file microsecond_timer.cpp.

169  {
171 
172  hwStarted = true;
173 
175 
177 
178  watchDogBuddyCallback(NULL);
179 #if EFI_EMULATE_POSITION_SENSORS
181 #endif /* EFI_EMULATE_POSITION_SENSORS */
182 }
efitick_t getTimeNowNt()
Definition: efitime.cpp:19
static bool hwStarted
static void watchDogBuddyCallback(void *)
static MicrosecondTimerWatchdogController watchdogControllerInstance
static void validateHardwareTimer()
static efitick_t lastSetTimerTimeNt
void portInitMicrosecondTimer()

Referenced by initSingleTimerExecutorHardware().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ portMicrosecondTimerCallback()

void portMicrosecondTimerCallback ( )

Definition at line 95 of file microsecond_timer.cpp.

95  {
97  isTimerPending = false;
98 
99  uint32_t before = getTimeNowLowerNt();
101  uint32_t precisionCallbackDuration = getTimeNowLowerNt() - before;
102  if (precisionCallbackDuration > maxPrecisionCallbackDuration) {
103  maxPrecisionCallbackDuration = precisionCallbackDuration;
104  }
105 }
void globalTimerCallback()
static bool isTimerPending
uint32_t maxPrecisionCallbackDuration
static int timerCallbackCounter
uint32_t getTimeNowLowerNt()

Referenced by hwTimerCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setHardwareSchedulerTimer()

void setHardwareSchedulerTimer ( efitick_t  nowNt,
efitick_t  setTimeNt 
)

sets the alarm to the specified number of microseconds from now. This function should be invoked under kernel lock which would disable interrupts.

#259 BUG error: not positive deltaTimeNt Once in a while we night get an interrupt where we do not expect it

Definition at line 52 of file microsecond_timer.cpp.

52  {
53  criticalAssertVoid(hwStarted, "HW.started");
54 
55  // How many ticks in the future is this event?
56  auto timeDeltaNt = setTimeNt - nowNt;
57 
59 
60  /**
61  * #259 BUG error: not positive deltaTimeNt
62  * Once in a while we night get an interrupt where we do not expect it
63  */
64  if (timeDeltaNt <= 0) {
67  }
68 
69  // We need the timer to fire after we return - 1 doesn't work as it may actually schedule in the past
70  if (timeDeltaNt < US2NT(2)) {
71  timeDeltaNt = US2NT(2);
72  }
73 
74  if (timeDeltaNt >= TOO_FAR_INTO_FUTURE_NT) {
75  // we are trying to set callback for too far into the future. This does not look right at all
76  firmwareError(ObdCode::CUSTOM_ERR_TIMER_OVERFLOW, "setHardwareSchedulerTimer() too far: %d", timeDeltaNt);
77  return;
78  }
79 
80  // Skip scheduling if there's a firmware error active
81  if (hasFirmwareError()) {
82  return;
83  }
84 
85  // Do the actual hardware-specific timer set operation
86  portSetHardwareSchedulerTimer(nowNt, setTimeNt);
87 
89  isTimerPending = true;
91 }
bool warning(ObdCode code, const char *fmt,...)
void firmwareError(ObdCode code, const char *fmt,...)
static int timerFreezeCounter
static int timerRestartCounter
static int setHwTimerCounter
void portSetHardwareSchedulerTimer(efitick_t nowNt, efitick_t setTimeNt)
@ CUSTOM_ERR_TIMER_OVERFLOW
@ CUSTOM_OBD_LOCAL_FREEZE

Referenced by SingleTimerExecutor::scheduleTimerCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timerValidationCallback()

static void timerValidationCallback ( void *  )
static

Definition at line 141 of file microsecond_timer.cpp.

141  {
142  testSchedulingHappened = true;
143  efitimems_t actualTimeSinceScheduling = (getTimeNowMs() - testSchedulingStart);
144 
145  if (absI(actualTimeSinceScheduling - TEST_CALLBACK_DELAY) > TEST_CALLBACK_DELAY * TIMER_PRECISION_THRESHOLD) {
146  firmwareError(ObdCode::CUSTOM_ERR_TIMER_TEST_CALLBACK_WRONG_TIME, "hwTimer broken precision: %ld ms", actualTimeSinceScheduling);
147  }
148 }
efitimems_t getTimeNowMs()
Returns the 32 bit number of milliseconds since the board initialization.
Definition: efitime.cpp:34
static efitimems_t testSchedulingStart
static volatile bool testSchedulingHappened
@ CUSTOM_ERR_TIMER_TEST_CALLBACK_WRONG_TIME
uint32_t efitimems_t
Definition: rusefi_types.h:48

Referenced by validateHardwareTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validateHardwareTimer()

static void validateHardwareTimer ( )
static

This method would validate that hardware timer callbacks happen with some reasonable precision helps to make sure our GPT hardware settings are somewhat right

Definition at line 154 of file microsecond_timer.cpp.

154  {
155  if (hasFirmwareError()) {
156  return;
157  }
159 
160  // to save RAM let's use 'watchDogBuddy' here once before we enable watchdog
161  engine->executor.scheduleForLater("hw-validate", &watchDogBuddy, MS2US(TEST_CALLBACK_DELAY), timerValidationCallback);
162 
163  chThdSleepMilliseconds(TEST_CALLBACK_DELAY + 2);
164  if (!testSchedulingHappened) {
166  }
167 }
void scheduleForLater(const char *msg, scheduling_s *scheduling, int delayUs, action_s action) override
Engine * engine
static scheduling_s watchDogBuddy
static void timerValidationCallback(void *)
@ CUSTOM_ERR_TIMER_TEST_CALLBACK_NOT_HAPPENED

Referenced by initMicrosecondTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ watchDogBuddyCallback()

static void watchDogBuddyCallback ( void *  )
static

the purpose of this periodic activity is to make watchdogControllerInstance watchdog happy by ensuring that we have scheduler activity even in case of very broken configuration without any PWM or input pins

Definition at line 129 of file microsecond_timer.cpp.

129  {
130  /**
131  * the purpose of this periodic activity is to make watchdogControllerInstance
132  * watchdog happy by ensuring that we have scheduler activity even in case of very broken configuration
133  * without any PWM or input pins
134  */
136 }

Referenced by initMicrosecondTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ hwStarted

bool hwStarted = false
static

Definition at line 46 of file microsecond_timer.cpp.

Referenced by initMicrosecondTimer(), and setHardwareSchedulerTimer().

◆ isTimerPending

bool isTimerPending = false
static

◆ lastSetTimerTimeNt

efitick_t lastSetTimerTimeNt
static

Definition at line 36 of file microsecond_timer.cpp.

Referenced by initMicrosecondTimer(), and setHardwareSchedulerTimer().

◆ maxPrecisionCallbackDuration

uint32_t maxPrecisionCallbackDuration = 0

Maximum duration of complete timer callback, all pending events together See also 'maxEventCallbackDuration' for maximum duration of one event

Definition at line 34 of file microsecond_timer.cpp.

Referenced by portMicrosecondTimerCallback(), and resetMaxValues().

◆ msg

const char* msg
static

Definition at line 42 of file microsecond_timer.cpp.

Referenced by adcTriggerTurnOnInputPin(), WaveChart::addEvent3(), assertCloseTo(), brain_pin_markUsed(), canDashboardBmwE46(), canDashboardBmwE90(), canDashboardFiat(), canDashboardGenesisCoupe(), canDashboardHaltech(), canDashboardNissanVQ(), canDashboardVAG(), canDashboardVagMqb(), canDashboardW202(), canMazdaRX8(), chDbgPanic3(), configTherm(), configureTempSensor(), configureTempSensorFunction(), CanTsListener::decodeFrame(), TriggerDecoderBase::decodeTriggerEvent(), deInitIfValid(), doOneLuaCanRx(), efiExtiEnablePin(), priv::efiPrintfInternal(), efiSetPadMode(), efiSetPadModeWithoutOwnershipAcquisition(), ensureArrayIsAscending(), ensureArrayIsAscendingOrDefault(), extiTriggerTurnOnInputPin(), findIndexMsg(), getAdcChannelBrainPin(), getAdcChannelPort(), getHwPin(), getHwPort(), getInternalAdcValue(), getPinNameByAdcChannel(), GetToothLoggerBufferImpl(), getVoltage(), getVoltageDivided(), gpio_pin_markUsed(), hellenDisableEn(), hellenEnableEn(), hipThread(), incrementGlobalConfigurationVersion(), initIfValid(), OutputPin::initPin(), interpolateMsg(), lua_efi_print(), lua_readpin(), lua_txCan(), CanRxMessage::operator=(), populateFrame(), printSpiConfig(), printTpsSenser(), processLuaCan(), CanStreamer::receive(), ObdCanSensor< Size, Offset >::request(), ReturnToothLoggerBuffer(), runScript(), TriggerScheduler::schedule(), SingleTimerExecutor::scheduleByTimestamp(), SingleTimerExecutor::scheduleByTimestampNt(), SingleTimerExecutor::scheduleForLater(), TriggerScheduler::scheduleOrQueue(), sendOutBoardMeta(), sendPinStatePackets(), sendQcBenchAuxDigitalCounters(), sendQcBenchBoardStatus(), sendQcBenchButtonCounters(), sendQcBenchEventCounters(), sendQcBenchRawAnalogValues(), SENT_ISR_Handler(), SentDecoderThread(), NamedOutputPin::setHigh(), NamedOutputPin::setLow(), OutputPin::setValue(), Pid::showPidStatus(), startInputPinIfValid(), startSimplePwm(), startSimplePwmExt(), startSimplePwmHard(), startTriggerInputPins(), THD_FUNCTION(), thread1(), thread4(), tunerStudioDebug(), tunerStudioError(), turnOnTriggerInputPin(), validateThermistorConfig(), wrapAngle(), wrapAngleMethod(), writePad(), and CanTxMessage::~CanTxMessage().

◆ setHwTimerCounter

int setHwTimerCounter = 0
static

Definition at line 45 of file microsecond_timer.cpp.

Referenced by setHardwareSchedulerTimer().

◆ testSchedulingHappened

volatile bool testSchedulingHappened = false
static

Definition at line 138 of file microsecond_timer.cpp.

Referenced by timerValidationCallback(), and validateHardwareTimer().

◆ testSchedulingStart

efitimems_t testSchedulingStart
static

Definition at line 139 of file microsecond_timer.cpp.

Referenced by timerValidationCallback(), and validateHardwareTimer().

◆ timerCallbackCounter

int timerCallbackCounter = 0
static

Definition at line 39 of file microsecond_timer.cpp.

Referenced by portMicrosecondTimerCallback().

◆ timerFreezeCounter

int timerFreezeCounter = 0
static

Definition at line 44 of file microsecond_timer.cpp.

Referenced by setHardwareSchedulerTimer().

◆ timerRestartCounter

int timerRestartCounter = 0
static

Definition at line 40 of file microsecond_timer.cpp.

Referenced by setHardwareSchedulerTimer().

◆ watchDogBuddy

scheduling_s watchDogBuddy
static

Definition at line 127 of file microsecond_timer.cpp.

Referenced by validateHardwareTimer(), and watchDogBuddyCallback().

◆ watchdogControllerInstance

MicrosecondTimerWatchdogController watchdogControllerInstance
static

Definition at line 125 of file microsecond_timer.cpp.

Referenced by initMicrosecondTimer().

Go to the source code of this file.