Skip to content

Class daisy::TimerHandle

ClassList > daisy > TimerHandle

Hardare timer peripheral support. More...

  • #include <tim.h>

Classes

Type Name
struct Config
Configuration struct for the Peripheral.

Public Types

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

Public Functions

Type Name
Result DeInit ()
Deinitializes the timer.
void DelayMs (uint32_t del)
Stay within this function for del milliseconds.
void DelayTick (uint32_t del)
Stay within this function for del ticks.
void DelayUs (uint32_t del)
Stay within this function for del microseconds.
const Config & GetConfig () const
Returns a const reference to the Config struct.
uint32_t GetFreq ()
Returns the frequency of each tick of the timer in Hz.
uint32_t GetMs ()
Returns the ticks scaled as milliseconds.
uint32_t GetTick ()
Returns the number of counter position.
uint32_t GetUs ()
Returns the ticks scaled as microseconds.
Result Init (const Config & config)
Initializes the timer according to the configuration.
void SetCallback (PeriodElapsedCallback cb, void * data=nullptr)
Sets the PeriodElapsedCallback that will fire whenever the timer reaches the end of it's period.
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 .
TimerHandle ()
TimerHandle (const TimerHandle & other) = default
TimerHandle & operator= (const TimerHandle & other) = default
~TimerHandle ()

Detailed Description

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.

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)

Public Types Documentation

typedef PeriodElapsedCallback

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

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

Parameters:

  • data pointer to arbitrary user-provided data

enum Result

Return values for TIM funcitons.

enum daisy::TimerHandle::Result {
    OK,
    ERR
};


Public Functions Documentation

function DeInit

Deinitializes the timer.

Result daisy::TimerHandle::DeInit () 


function DelayMs

Stay within this function for del milliseconds.

void daisy::TimerHandle::DelayMs (
    uint32_t del
) 


function DelayTick

Stay within this function for del ticks.

void daisy::TimerHandle::DelayTick (
    uint32_t del
) 


function DelayUs

Stay within this function for del microseconds.

void daisy::TimerHandle::DelayUs (
    uint32_t del
) 


function GetConfig

Returns a const reference to the Config struct.

const  Config & daisy::TimerHandle::GetConfig () const


function GetFreq

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

uint32_t daisy::TimerHandle::GetFreq () 


function GetMs

Returns the ticks scaled as milliseconds.

uint32_t daisy::TimerHandle::GetMs () 

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


function GetTick

Returns the number of counter position.

uint32_t daisy::TimerHandle::GetTick () 

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 GetUs

Returns the ticks scaled as microseconds.

uint32_t daisy::TimerHandle::GetUs () 

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


function Init

Initializes the timer according to the configuration.

Result daisy::TimerHandle::Init (
    const  Config & config
) 


function SetCallback

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

void daisy::TimerHandle::SetCallback (
    PeriodElapsedCallback cb,
    void * data=nullptr
) 

Parameters:

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

function SetPeriod

Sets the period of the Timer.

Result daisy::TimerHandle::SetPeriod (
    uint32_t ticks
) 

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

Sets the Prescalar applied to the TIM peripheral.

Result daisy::TimerHandle::SetPrescaler (
    uint32_t val
) 

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

Starts the TIM peripheral specified by Config .

Result daisy::TimerHandle::Start () 


function Stop

Stops the TIM peripheral specified by Config .

Result daisy::TimerHandle::Stop () 


function TimerHandle [½]

inline daisy::TimerHandle::TimerHandle () 

function TimerHandle [2/2]

daisy::TimerHandle::TimerHandle (
    const  TimerHandle & other
) = default

function operator=

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

function ~TimerHandle

inline daisy::TimerHandle::~TimerHandle () 


The documentation for this class was generated from the following file external-docs/libDaisy/src/per/tim.h