rusEFI
The most advanced open source ECU
microsecond_timer_gpt.cpp
Go to the documentation of this file.
1 #include "pch.h"
3 
4 #if EFI_PROD_CODE && HAL_USE_GPT
5 
6 void portSetHardwareSchedulerTimer(efitick_t nowNt, efitick_t setTimeNt) {
7  int32_t deltaTimeUs = NT2US((int32_t)setTimeNt - (int32_t)nowNt);
8 
9  // If already set, reset the timer
10  if (GPTDEVICE.state == GPT_ONESHOT) {
11  gptStopTimerI(&GPTDEVICE);
12  }
13 
14  if (GPTDEVICE.state != GPT_READY) {
15  firmwareError(ObdCode::CUSTOM_HW_TIMER, "HW timer state %d", GPTDEVICE.state);
16  return;
17  }
18 
19  // Start the timer
20  gptStartOneShotI(&GPTDEVICE, deltaTimeUs);
21 }
22 
23 static void hwTimerCallback(GPTDriver*) {
25 }
26 
27 /*
28  * The specific 1MHz frequency is important here since 'setHardwareUsTimer' method takes microsecond parameter
29  * For any arbitrary frequency to work we would need an additional layer of conversion.
30  */
31 static constexpr GPTConfig gpt5cfg = { 1000000, /* 1 MHz timer clock.*/
32  hwTimerCallback, /* Timer callback.*/
33 0, 0 };
34 
36  gptStart(&GPTDEVICE, &gpt5cfg);
37  efiAssertVoid(ObdCode::CUSTOM_ERR_TIMER_STATE, GPTDEVICE.state == GPT_READY, "hw state");
38 }
39 
40 #endif // EFI_PROD_CODE
41 
42 // This implementation just uses the generic port counter - this usually returns a count of CPU cycles since start
43 uint32_t getTimeNowLowerNt() {
44  return port_rt_get_counter_value();
45 }
void firmwareError(ObdCode code, const char *fmt,...)
void portMicrosecondTimerCallback()
uint32_t getTimeNowLowerNt()
void portSetHardwareSchedulerTimer(efitick_t nowNt, efitick_t setTimeNt)
static void hwTimerCallback(GPTDriver *)
void portInitMicrosecondTimer()
static constexpr GPTConfig gpt5cfg
@ CUSTOM_HW_TIMER
@ CUSTOM_ERR_TIMER_STATE
Driver configuration structure.
Definition: hal_gpt_lld.h:207
Structure representing a GPT driver.
Definition: hal_gpt_lld.h:239