 |
Arduino Core for STM32
1.0
|
Go to the documentation of this file. 1 #ifndef _WIRISH_HARDWARETIMER_H_
2 #define _WIRISH_HARDWARETIMER_H_
25 #if defined(STM32F103xB)
28 #define CHECKTIM5 (_instance_) || (_instance_) == TIM5
33 #define CHECKTIM6 (_instance_) || (_instance_) == TIM6
38 #define CHECKTIM7 (_instance_) || (_instance_) == TIM7
42 #define IS_TIMER_INSTANCE_ON_APB1(_instance_) ((_instance_) == TIM2 \
43 || (_instance_) == TIM3 \
44 || (_instance_) == TIM4 \
48 #define IS_TIMER_INSTANCE_ON_APB2(_instance_) ((_instance_) == TIM1)
50 #elif defined(STM32F031x6) || defined(STM32F070xB) || defined(STM32F070x6) || defined(STM32F030x6)
53 #ifdef HT_ENABLE_MSP_INIT_CB
54 #define TIM1_IRQn TIM1_CC_IRQn
56 #define IS_TIMER_INSTANCE_ON_APB1 IS_TIM_INSTANCE
58 #elif defined(STM32L031xx)
60 #define IS_TIMER_INSTANCE_ON_APB1(_instance_) ((_instance_) == TIM2)
61 #define IS_TIMER_INSTANCE_ON_APB2(_instance_) ((_instance_) == TIM22 || (_instance_) == TIM21)
63 #elif defined(STM32L432xx)
65 #define IS_TIMER_INSTANCE_ON_APB1(_instance_) ((_instance_) == TIM2 || (_instance_) == TIM6 || (_instance_) == TIM7)
66 #define IS_TIMER_INSTANCE_ON_APB2(_instance_) ((_instance_) == TIM1 || (_instance_) == TIM15 || (_instance_) == TIM16)
69 #error #error "Definitions for your MCU are not defined."
76 #if defined(HAL_TIM_MODULE_ENABLED) || defined(DOXYGEN_FORCED)
79 #define min(a,b) (((a)<b)?(a):(b))
82 #define max(a,b) (((a)>b)?(a):(b))
85 #define abs(a) (((a)>=0)?(a):-(a))
88 #define TIMER_16BIT_MAX_PRESCALER (0xFFFFUL)
89 #define TIMER_32BIT_MAX_PERIOD (0xFFFFFFFFUL)
90 #define TIMER_16BIT_MAX_PERIOD (0xFFFFUL)
122 #define CTS_IS_OK(state) ((state)>CTS_NONE)
518 void setCallback(
void (* PeriodElapsedCallback_)());
526 #ifdef HT_ENABLE_ARR_AVAILABLE_TIMERS
530 const static TIM_TypeDef* availableTimers[];
622 uint64_t
inline CalculateTime_ns(uint32_t prescaler, uint32_t period, uint32_t timerFreq, uint8_t clockDiv) {
623 uint64_t time = ((uint64_t)period + 1) * (((uint64_t)prescaler + 1) * 1000000000ULL) / timerFreq;
626 return time * clockDiv;
638 uint64_t
inline CalculateTime_ms(uint32_t prescaler, uint32_t period, uint32_t timerFreq, uint8_t clockDiv) {
639 uint64_t time = ((uint64_t)period + 1) * (((uint64_t)prescaler + 1) * 1000UL) / timerFreq;
642 return time * clockDiv;
656 TimerSettings CalculateTimer(uint64_t ticks, uint32_t maxPrescaler, uint64_t maxPeriod, uint32_t minPeriod, uint32_t timerFreq,
bool divEnabled, uint32_t maxIterations = UINT32_MAX);
658 #ifdef HT_ENABLE_MSP_INIT_CB
663 static void Base_MspInit_callback(TIM_HandleTypeDef* htim_oc);
669 static void Base_MspDeInit_callback(TIM_HandleTypeDef* htim_oc);
TIM_HandleTypeDef & _tim
Timer structure (which include timer instance) that we will use to handle timer.
Definition: HardwareTimer.h:603
CalculateTimerState
Enums of all possible calculation states of setTimer method.
Definition: HardwareTimer.h:96
bool haveLockedSettings()
Checks if settings registers are locked.
bool isAutoReloadPreloadEnabled(void)
Checks if autoReloadPreload is enabled.
static HardwareTimer ** getTimerInstances()
Gets array of all created timers instances (just from this class, not timer structure).
uint16_t getPrescaler(void)
Basic settings methods applied in real time Those setting methods can be called every time,...
TimerState
This is enum of all possible timer states.
Definition: HardwareTimer.h:150
bool start(void)
Starts timer.
static uint32_t getTimerIndex(TIM_TypeDef *instance)
Gets timer instance index, that is used in methods/attributes: handleCallback(TIM_TypeDef* instance),...
void enableAutoReloadPreload(bool enable)
When autoReloadPreload is enabled, timer is running and period was changed, the new value of period w...
bool initDone
This flag is true when init(void) method was successfully called.
Definition: HardwareTimer.h:611
uint32_t setIntervalItLimit
Maximum iterations limit for interval calculation, that is done in method setInterval().
Definition: HardwareTimer.h:587
bool setClockDivision(uint8_t division)
Sets timer clock division.
TimerSettings CalculateTimer(uint64_t ticks, uint32_t maxPrescaler, uint64_t maxPeriod, uint32_t minPeriod, uint32_t timerFreq, bool divEnabled, uint32_t maxIterations=UINT32_MAX)
Calculates prescaler, period and timer clock division.
bool resume(void)
Resumes timer.
@ TS_RUNNING
Timer is running.
Definition: HardwareTimer.h:152
@ TS_STOPPED
Timer is stopped, counter register was restarted.
Definition: HardwareTimer.h:156
uint32_t getPeriod()
Gets period that was set.
static uint8_t availableTimersCount
Available timer instances count.
Definition: HardwareTimer.h:696
void(* TIMPeriodElapsedCallback)()
Pointer to callback function, that is called on timer interrupt(overflow or underflow event).
Definition: HardwareTimer.h:686
uint8_t availableChannelsCount()
Get count of available channels.
uint8_t getClockDivision(void)
Gets clock division value.
uint32_t period
Calculated period.
Definition: HardwareTimer.h:132
void stop(void)
Stops timer.
void setCallback(void(*PeriodElapsedCallback_)())
Sets callback function, that is called when timer period elapsed (interrupt).
@ CTS_OK_JUST_PERIOD
Value was established just with period (prescaler value is 0)
Definition: HardwareTimer.h:106
bool instanceExists(void)
Checks if timer instance really exists and is based on real timer.
TimerSettings(uint16_t prescaler_, uint32_t period_, uint8_t div, CalculateTimerState state_)
Definition: HardwareTimer.h:138
@ CTS_TOO_HIGH_RESULT
Value (duration_100ns) is higher than maximum available time that can be set using this timer with cu...
Definition: HardwareTimer.h:100
@ CTS_NONE
Function was not called yet.
Definition: HardwareTimer.h:104
@ CTS_OK_ROUNDED_DOWN
Value was established with the best possible accuracy, but is slightly lower than expected value.
Definition: HardwareTimer.h:114
bool setCounterMode(uint32_t mode)
Sets timer Counter Mode.
void lockSettings()
Locks settings registers.
uint64_t getInterval(void)
Calculates interval from period, prescaler and timer clock division.
uint64_t getInterval_ns(void)
Calculates interval from period, prescaler and timer clock division.
uint16_t prescaler
Calculated prescaler.
Definition: HardwareTimer.h:130
void setPeriod(uint32_t val)
Sets period that was set.
bool supportsSelectionCounterMode()
Setup methods Those methods have to be called before init(void) method to apply settings.
uint32_t getCounterMode(void)
Gets timer Counter Mode.
static uint8_t getAvailableTimersCount()
Gets available timer instances count.
bool init(void)
Initialize timer instance.
uint32_t getDivTimerClockFreq(void)
Gets timer clock frequency after clock division.
@ TS_ERROR
There is some error with timer.
Definition: HardwareTimer.h:158
void setPrescaler(uint16_t factor)
Sets prescaler that was set.
void unlockSettings()
Advanced settings methods applied in real time Those setting methods can be called every time,...
uint8_t divider
Timer clock division.
Definition: HardwareTimer.h:134
TimerSettings setInterval_ns(uint64_t interval_ns)
Easiest settings methods applied in real time.
uint64_t CalculateTime_ns(uint32_t prescaler, uint32_t period, uint32_t timerFreq, uint8_t clockDiv)
Calculates interval from period and prescaler and clock division.
Definition: HardwareTimer.h:622
void deInit(void)
Deinitialization of timer.
TIM_HandleTypeDef & getHAL_TIM_Struct()
Returns timer structure, that is used for handling timer.
Definition: HardwareTimer.h:575
@ TS_PAUSED
Timer is paused, counter register is unchanged.
Definition: HardwareTimer.h:154
This file contains basic (Arduino like) GPIO manipulation functions, delay, random,...
bool isInitDone(void)
Checks if timer was initialized.
HardwareTimer(TIM_HandleTypeDef &tim)
Hardware timer constructor.
TIM_HandleTypeDef createdTim
We need this structure when calling HardwareTimer(TIM_TypeDef* instance) constructor,...
Definition: HardwareTimer.h:598
@ CTS_BAD_ARGUMENT
Check if values maxPrescaler, maxCounter, minPeriod are correct.
Definition: HardwareTimer.h:98
@ CTS_OK_JUST_PRESCALER
Value was established just with prescaler (period value is 0) - This should never happens when you ha...
Definition: HardwareTimer.h:108
static void handleCallback(TIM_TypeDef *instance)
Expert methods Those methods you probably don't need to call in basic timer operation.
CalculateTimerState state
Calculation state.
Definition: HardwareTimer.h:136
uint32_t getCount(void)
Gets current value in counter register.
uint32_t getTimerClockFreq(void)
Gets timer clock frequency before clock division.
@ CTS_OK_BEST_ACCURACY
Value was established with the best possible accuracy.
Definition: HardwareTimer.h:110
void setCount(uint32_t val)
Sets new value to counter register.
void removeThisFromInstArr()
Removes this instance from instances array.
Structure, those data was calculated in setInterval method.
Definition: HardwareTimer.h:128
~HardwareTimer()
Class destructor.
TimerSettings setInterval(uint64_t interval_ms)
Calculates period and prescaler and clock division from set interval.
void pause(void)
Timer control functions Those methods can be called just after init(void) method.
void addThisToInstArr()
Writes new class instance to array of all timer instances.
bool is32BitTimer()
Checks if timer is 32bit or 16bit.
@ CTS_OK_ROUNDED_UP
Value was established with the best possible accuracy, but is slightly higher than expected value.
Definition: HardwareTimer.h:112
@ CTS_TOO_LOW_RES
Value (duration_100ns) is lower than minumum available time that can be set using this timer with cur...
Definition: HardwareTimer.h:102
static HardwareTimer * instances[]
Array of all created timers instances (just from this class, not timer structure).
Definition: HardwareTimer.h:691
uint64_t CalculateTime_ms(uint32_t prescaler, uint32_t period, uint32_t timerFreq, uint8_t clockDiv)
Calculates interval from period and prescaler and clock division.
Definition: HardwareTimer.h:638
Handle timers in interrupt mode really easy using this class. In table below, you can see methods se...
Definition: HardwareTimer.h:211
bool supportsClockDivision()
Checks if timer supports clock division.
TimerState state(void)
Checks timer state.