7 #define TimerIntervalUSDefault 4000
9 #define TimerIntervalUSDefault 20000
13 * @brief Hardware abstraction for the hardware timer to provide precise timing
15 * The timer provides callback for "tick" and "tock" alternating at half the update
18 * The timer includes a built-in frequency offset to adjust for differences in actual
19 * timing rates between 2 timers. The offset can be incremented, decremented or reset
22 * The `phaseShift` function can be used to provide a one-time adjustment to shift the
23 * phase of the timer so two timers can be aligned on their tock phase.
29 * @brief Initialise the timer with the specified tick/tock callback functions.
31 * Only the tock callback is used for transmitter targets.
33 * The timer initialised in the stopped state and must be started with resume().
35 static void init(void (*callbackTick
)(), void (*callbackTock
)());
38 * @brief Stop the timer.
40 * The timer is stopped and no more callbacks are performed.
45 * @brief Resume the timer.
47 * The timer is started from this instant, callbacks will be called.
48 * The timer fires a tock callback immediately after it is resumed.
53 * @brief Change the interval between callbacks.
54 * The time between tick and tock is half the provided interval.
55 * The timer should not be running when updateInterval() is called.
57 * @param time in microseconds for a full tick/tock.
59 static void updateInterval(uint32_t time
= TimerIntervalUSDefault
);
61 * @brief Reset the frequency offset to zero microseconds
63 static ICACHE_RAM_ATTR
void inline resetFreqOffset() { FreqOffset
= 0; }
66 * @brief Increment the frequency offset by one microsecond
68 static ICACHE_RAM_ATTR
void inline incFreqOffset() { FreqOffset
++; }
72 * @brief Decrement the frequency offset by one microsecond
74 static ICACHE_RAM_ATTR
void inline decFreqOffset() { FreqOffset
--; }
77 * @brief Get the frequency offset
79 static ICACHE_RAM_ATTR
int32_t inline getFreqOffset() { return FreqOffset
; }
82 * @brief Provides a coarse one time adjustment to the frequency to
83 * align the phase of two timers.
85 * The phase shift is a delay that is added to the timer after the
86 * next tock callback, delaying the tick after by phaseShift microseonds.
88 * The maximum phase shift is 1/4 of the update interval.
90 * @param newPhaseShift time in microseconds to delay the timer.
92 static void phaseShift(int32_t newPhaseShift
);
94 static volatile bool running
;
95 static volatile bool isTick
;
98 static void callback();
100 static void (*callbackTick
)();
101 static void (*callbackTock
)();
103 static volatile uint32_t HWtimerInterval
;
104 static volatile int32_t PhaseShift
;
105 static volatile int32_t FreqOffset
;