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

Public Types

template<>
using TickType = uint32_t
template<>
using TicksConst = TicksConst<Clock, ticks>
template<>
using TimeConst = TimeConst<Clock, unit, time>
template<>
using TicksPerUnit = std::ratio_divide<std::ratio<frequency_>, UnitTickRatio<unit>>
template<>
using TimeSource = TimeSource<Clock, unit, TimeType>
template<>
using Ticks = Ticks<Clock, T>
template<>
using MaxTicks = TicksConst<maxTicks_>
template<>
using MaxTime = typename MaxTicks::template TimeConst<unit>

Public Static Functions

static constexpr uint32_t prescale()
static constexpr const char *typeName()
static uint32_t ticks()
static constexpr uint32_t frequency()
static constexpr MaxTicks maxTicks()
static constexpr MaxTime<unit> maxTime()
static Ratio32 ticksPerUnit(Unit unit)

Get ticks per unit as a Ratio object.

Return Value
  • BasicRatio32:

static constexpr TimeConst<unit, time> timeConst()

Class template defining a fixed time quantity.

Template Parameters
  • time:
Return Value
  • TimeConst:

static constexpr TicksConst<ticks> ticksConst()

Class template defining a fixed tick quantity.

Template Parameters
  • ticks:
Return Value
  • TicksConst<Clockticks>:

static constexpr TimeSource<unit, TimeType> timeSource()

Create a Time Source for this Clock.

Template Parameters
  • unit:
  • TimeType:

static Ticks<TimeType> timeToTicks(TimeType time)

Get the number of ticks for a given time.

Parameters
  • time:
Return Value
  • TimeType: Tick count, rounded to the nearest tick

static Time<TimeType> ticksToTime(TimeType ticks)

Get the time for a given number of clock ticks.

Parameters
  • ticks:
Return Value
  • TimeType: Time count, rounded to the nearest unit

static String toString()