optimise mavlink SS packet size (#3029)
[ExpressLRS.git] / src / lib / HWTIMER / hwTimer.h
blobe13fdc25c47b54aab5d51e5487ed19f1dea8366e
1 #pragma once
3 #include "targets.h"
4 #include <stdio.h>
6 #if defined(TARGET_TX)
7 #define TimerIntervalUSDefault 4000
8 #else
9 #define TimerIntervalUSDefault 20000
10 #endif
12 /**
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
16 * interval each.
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
20 * to 0.
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.
25 class hwTimer
27 public:
28 /**
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)());
37 /**
38 * @brief Stop the timer.
40 * The timer is stopped and no more callbacks are performed.
42 static void stop();
44 /**
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.
50 static void resume();
52 /**
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);
60 /**
61 * @brief Reset the frequency offset to zero microseconds
63 static ICACHE_RAM_ATTR void inline resetFreqOffset() { FreqOffset = 0; }
65 /**
66 * @brief Increment the frequency offset by one microsecond
68 static ICACHE_RAM_ATTR void inline incFreqOffset() { FreqOffset++; }
71 /**
72 * @brief Decrement the frequency offset by one microsecond
74 static ICACHE_RAM_ATTR void inline decFreqOffset() { FreqOffset--; }
76 /**
77 * @brief Get the frequency offset
79 static ICACHE_RAM_ATTR int32_t inline getFreqOffset() { return FreqOffset; }
81 /**
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;
97 private:
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;