Clocks

Timers and their capabilities can vary considerably. For example, Timer1 can be configured with a prescaler of 1, 16 or 256 which affects both the resolution and range of the timer. One might also consider the CPU cycle counter to have a selectable prescaler of 1 or 2, depending on whether it’s running at 80MHz or 160MHz.

A Clock definition is a class template which allows us to query timer properties and perform time conversions for a specific timer configuration. These definitions can be found in Sming/Platform/Clocks.h.

Note

A Clock is a purely virtual construct and does not provide any means to configure the hardware, although it does provide the ticks() method to obtain the current timer value.

Clocks are made more useful by TimeSource, a generic class template defined in Sming/Core/NanoTime.h. This provides methods to convert between time values and tick values for a specific time unit.

Let’s say we want a microsecond source using Timer2:

TimeSource<Timer2Clock, NanoTime::Microseconds, uint32_t> t2source;

We can now call methods of t2source like this:

// What's the maximum Timer2 value in microseconds?
Serial.println(t2source.maxClockTime());

// How many clock ticks per microsecond ?
Serial.println(t2source.ticksPerUnit()); // 5/1

// How many clock ticks for 100us ?
Serial.println(t2source.template timeConst<100>().ticks());

Note that all of these values are computed at compile time. Some runtime conversions:

Serial.println(t2source.timeToTicks(100));
Serial.println(t2source.ticksToTime(10000));

The results of conversions are rounded rather than truncated, which provides more accurate results and reduces timing jitter.

For debugging purposes you can print a description:

Serial.println(t2source.toString()); // "Timer2Clock/5MHz/32-bit/microseconds"

See Sming/Core/NanoTime.h for further details.

System Clock API

template<hw_timer_clkdiv_t clkdiv>
struct Timer1Clock : public NanoTime::Clock<Timer1Clock<clkdiv>, HW_TIMER_BASE_CLK / (1 << clkdiv), uint32_t, MAX_HW_TIMER1_INTERVAL>
#include <Clocks.h>

Clock implementation for Hardware Timer 1.

Template Parameters:

clkdiv – Prescaler in use