libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy::TimerHandle Class Reference

Hardare timer peripheral support. More...

Detailed Description

Hardare timer peripheral support.

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)

#include <tim.h>

Classes

struct  Config
 Configuration struct for the Peripheral. More...
 

Public Types

enum class  Result { OK , ERR }
 Return values for TIM funcitons. More...
 
typedef void(* 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.
 

Public Member Functions

 TimerHandle ()
 
 TimerHandle (const TimerHandle &other)=default
 
TimerHandleoperator= (const TimerHandle &other)=default
 
 ~TimerHandle ()
 
Result Init (const Config &config)
 Initializes the timer according to the configuration.
 
Result DeInit ()
 Deinitializes the timer.
 
const ConfigGetConfig () 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.
 

Member Typedef Documentation

◆ 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
datapointer to arbitrary user-provided data

Member Enumeration Documentation

◆ Result

Return values for TIM funcitons.

Enumerator
OK 
ERR 

Constructor & Destructor Documentation

◆ TimerHandle() [1/2]

daisy::TimerHandle::TimerHandle ( )
inline

◆ TimerHandle() [2/2]

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

◆ ~TimerHandle()

daisy::TimerHandle::~TimerHandle ( )
inline

Member Function Documentation

◆ DeInit()

Result daisy::TimerHandle::DeInit ( )

Deinitializes the timer.

◆ DelayMs()

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

Stay within this function for del milliseconds.

◆ DelayTick()

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

Stay within this function for del ticks.

◆ DelayUs()

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

Stay within this function for del microseconds.

◆ GetConfig()

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

Returns a const reference to the Config struct.

◆ GetFreq()

uint32_t daisy::TimerHandle::GetFreq ( )

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

◆ GetMs()

uint32_t daisy::TimerHandle::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.

◆ GetTick()

uint32_t daisy::TimerHandle::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.

◆ GetUs()

uint32_t daisy::TimerHandle::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.

◆ Init()

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

Initializes the timer according to the configuration.

◆ operator=()

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

◆ SetCallback()

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

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

Parameters
cbuser callback
dataoptional pointer to arbitrary data (defaults to nullptr)

◆ SetPeriod()

Result daisy::TimerHandle::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"

◆ SetPrescaler()

Result daisy::TimerHandle::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"

◆ Start()

Result daisy::TimerHandle::Start ( )

Starts the TIM peripheral specified by Config.

◆ Stop()

Result daisy::TimerHandle::Stop ( )

Stops the TIM peripheral specified by Config.


The documentation for this class was generated from the following file: