Skip to content

daisy::TimerHandle

Hardare timer peripheral support. More...

#include <tim.h>

Public Classes

Name
struct Config
Configuration struct for the Peripheral.

Public Types

Name
enum class Result { OK, ERR}
Return values for TIM funcitons.
typedef void(*)(void *data) PeriodElapsedCallback
User Callback type that will fire at the end of each timer period. This requires that Config::enable_irq is true before Init.

Public Functions

Name
TimerHandle()
TimerHandle(const TimerHandle & other) =default
TimerHandle & operator=(const TimerHandle & other) =default
~TimerHandle()
Result Init(const Config & config)
Initializes the timer according to the configuration.
Result DeInit()
Deinitializes the timer.
const Config & GetConfig() const
Returns a const reference to the Config struct.
Result SetPeriod(uint32_t ticks)
Sets the period of the Timer.
Result SetPrescaler(uint32_t val)
Sets the Prescalar applied to the TIM peripheral.
Result Start()
Starts the TIM peripheral specified by Config.
Result Stop()
Stops the TIM peripheral specified by Config.
uint32_t GetFreq()
Returns the frequency of each tick of the timer in Hz.
uint32_t GetTick()
Returns the number of counter position.
uint32_t GetMs()
Returns the ticks scaled as milliseconds.
uint32_t GetUs()
Returns the ticks scaled as microseconds.
void DelayTick(uint32_t del)
Stay within this function for del ticks.
void DelayMs(uint32_t del)
Stay within this function for del milliseconds.
void DelayUs(uint32_t del)
Stay within this function for del microseconds.
void SetCallback(PeriodElapsedCallback cb, void * data =nullptr)
Sets the PeriodElapsedCallback that will fire whenever the timer reaches the end of it's period.

Detailed Description

class daisy::TimerHandle;

Hardare timer peripheral support.

Todo: Fix issues with realtime getters, and wrapping of the timer(s).

  • This very noticeable with default settings for the 16-bit counters.

Other General purpose timers

Non-internal clock sources

Use of the four-tim channels per tim

  • PWM, etc.
  • InputCapture/OutputCompare, etc.

HRTIM

Advanced timers (TIM1/TIM8)

Supports general-function TIM peripherals:

  • TIM2, TIM3, TIM4, TIM5

DaisySeed, and many internal peripherals utilize TIM2 for timing/delay purposes. It is configured to be at the maximum frequency: typically 200MHz or 240MHz (boost) for measuring/delaying for very short periods.

The GetUs/GetMs functions are available for convenience (and backwards compatibility), but to avoid wrapping errors on math when doing time-delta calculations, using ticks is recommended. The data can be converted to the final time-base after getting the difference in ticks. (Using GetFreq() can be used for these time-base calculations).

User callbacks can be set, and changed at any point during operation. However, the Config::enable_irq must be set to true when initializing for the interrupts to turn on and function.

Public Types Documentation

enum Result

Enumerator Value Description
OK
ERR

Return values for TIM funcitons.

typedef PeriodElapsedCallback

typedef void(* daisy::TimerHandle::PeriodElapsedCallback) (void *data);

User Callback type that will fire at the end of each timer period. This requires that Config::enable_irq is true before Init.

Parameters:

  • data pointer to arbitrary user-provided data

Public Functions Documentation

function TimerHandle

inline TimerHandle()

function TimerHandle

TimerHandle(
    const TimerHandle & other
) =default

function operator=

TimerHandle & operator=(
    const TimerHandle & other
) =default

function ~TimerHandle

inline ~TimerHandle()

function Init

Result Init(
    const Config & config
)

Initializes the timer according to the configuration.


function DeInit

Result DeInit()

Deinitializes the timer.


function GetConfig

const Config & GetConfig() const

Returns a const reference to the Config struct.


function SetPeriod

Result SetPeriod(
    uint32_t ticks
)

Sets the period of the Timer.

This is the number of ticks it takes before it wraps back around. For self-managed timing, this can be left at the default. (0xffff for 16-bit and 0xffffffff for 32-bit timers). This can be changed "on-the-fly"


function SetPrescaler

Result SetPrescaler(
    uint32_t val
)

Sets the Prescalar applied to the TIM peripheral.

This can be any number up to 0xffff This will adjust the rate of ticks: Calculated as APBN_Freq / prescalar per tick where APBN is APB1 for Most general purpose timers, and APB2 for HRTIM,a nd the advanced timers. This can be changed "on-the-fly"


function Start

Result Start()

Starts the TIM peripheral specified by Config.


function Stop

Result Stop()

Stops the TIM peripheral specified by Config.


function GetFreq

uint32_t GetFreq()

Returns the frequency of each tick of the timer in Hz.


function GetTick

uint32_t GetTick()

Returns the number of counter position.

This increments according to Config::CounterDir, and wraps around at the specified period (maxing out at 2^16 or 2^32 depending on the chosen TIM peripheral.


function GetMs

uint32_t GetMs()

Returns the ticks scaled as milliseconds.

Use care when using for measurements and ensure that the TIM period can handle the maximum desired measurement.


function GetUs

uint32_t GetUs()

Returns the ticks scaled as microseconds.

Use care when using for measurements and ensure that the TIM period can handle the maximum desired measurement.


function DelayTick

void DelayTick(
    uint32_t del
)

Stay within this function for del ticks.


function DelayMs

void DelayMs(
    uint32_t del
)

Stay within this function for del milliseconds.


function DelayUs

void DelayUs(
    uint32_t del
)

Stay within this function for del microseconds.


function SetCallback

void SetCallback(
    PeriodElapsedCallback cb,
    void * data =nullptr
)

Sets the PeriodElapsedCallback that will fire whenever the timer reaches the end of it's period.

Parameters:

  • cb user callback
  • data optional pointer to arbitrary data (defaults to nullptr)

---

Updated on 2024-01-03 at 19:41:00 +0000