Hardare timer peripheral support. More...
Hardare timer peripheral support.
Supports general-function TIM peripherals:
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.
Fix issues with realtime getters, and wrapping of the timer(s).
Other General purpose timers
Non-internal clock sources
Use of the four-tim channels per tim
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 | |
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. | |
User Callback type that will fire at the end of each timer period. This requires that Config::enable_irq is true before Init.
data | pointer to arbitrary user-provided data |
|
strong |
|
inline |
|
default |
|
inline |
Result daisy::TimerHandle::DeInit | ( | ) |
Deinitializes the timer.
Returns a const reference to the Config struct.
uint32_t daisy::TimerHandle::GetFreq | ( | ) |
Returns the frequency of each tick of the timer in Hz.
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.
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.
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.
Initializes the timer according to the configuration.
|
default |
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.
cb | user callback |
data | optional pointer to arbitrary data (defaults to nullptr) |
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"
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"