@@ -102,6 +102,50 @@ Here's the output from the :source:`BenchmarkPolledTimer <tests/HostTests/app/te
102
102
Using micros(), managed 145441 iterations, average loop time = 688ns (55 CPU cycles)
103
103
Using PolledTimer, managed 266653 iterations, average loop time = 375ns (30 CPU cycles)
104
104
105
+ Timer range
106
+ -----------
107
+
108
+ The maximum interval for a timer varies depending on the clock source and the selected prescaler.
109
+ The count may be further restricted by hardware. For example, Timer1 only provides a 23-bit count
110
+ which means with a /16 prescaler (the default for HardwareTimer) it overflows after only 1.67 seconds.
111
+
112
+ It's therefore important to check that timers are being used within their valid range. There are generally
113
+ two ways to do this:
114
+
115
+ Runtime checks
116
+ This means checking function/method return values and acting accordingly.
117
+ This often gets omitted because it can lead to cluttered code, which then leads to undiagnosed
118
+ bugs creeping in which can be very difficult to track down later on.
119
+
120
+ With a polled timer, you'd use one of these methods to set the time interval::
121
+
122
+ bool reset(const TimeType& timeInterval);
123
+ bool resetTicks(const TimeType& interval);
124
+
125
+ They both return true on success.
126
+
127
+ Static checks
128
+ These checks are performed during code compilation, so if a check fails the code won't compile.
129
+ In regular 'C' code you'd do this using #if statements, but C++ offers a much better way using
130
+ `static_assert <https://en.cppreference.com/w/cpp/language/static_assert >`__.
131
+
132
+ To reset a polled timer and incorporate a static check, use this method::
133
+
134
+ template <uint64_t timeInterval> void reset();
135
+
136
+ Note that timeInterval cannot be a variable (even if it's *const *) as the compiler must be
137
+ able to determine its value. It must therefore be *constexpr compatible *.
138
+
139
+ You can use static checking to pre-validate the range for a timer before using it::
140
+
141
+ timer.checkTime<10>();
142
+ timer.checkTime<10000>();
143
+
144
+ This will throw a compile-time error if the timer is not capable of using intervals in the
145
+ range 10 - 10000 microseconds (or whichever time unit you've selected). It doesn't add any
146
+ code to the application. If the code compiles, then you can be confident that the timer
147
+ will function as expected and you don't need to check return values.
148
+
105
149
Clocks
106
150
------
107
151
@@ -111,12 +155,12 @@ also consider the CPU cycle counter to have a selectable prescaler of 1 or 2, de
111
155
whether it's running at 80MHz or 160MHz.
112
156
113
157
A *Clock * definition is a class template which allows us to query timer properties and perform time
114
- conversions for a specific timer configuration. These definitions can be found in ` Platform/Clocks.h `.
158
+ conversions for a specific timer configuration. These definitions can be found in :source: ` Sming/ Platform/Clocks.h `.
115
159
116
160
.. note :: A Clock is a purely virtual construct and does not provide any means to configure the hardware,
117
161
although it does provide the *ticks() * method to obtain the current timer value.
118
162
119
- Clocks are made more useful by *TimeSource *, a generic class template defined in * NanoTime.h * .
163
+ Clocks are made more useful by *TimeSource *, a generic class template defined in :source: ` Sming/Core/ NanoTime.h` .
120
164
This provides methods to convert between time values and tick values for a specific time unit.
121
165
122
166
Let's say we want a microsecond source using Timer2::
@@ -146,4 +190,4 @@ For debugging purposes you can print a description::
146
190
147
191
Serial.println(t2source.toString()); // "Timer2Clock/5MHz/32-bit/microseconds"
148
192
149
- See * NanoTime.h * for further details.
193
+ See :source: ` Sming/Core/ NanoTime.h` for further details.
0 commit comments