|
STM32 core 3.0.1-dev
Arduino core for STM32 based boards: API reference
|
← STM32duino documentation
|
Arduino-style wrapper around an STM32 general purpose/advanced timer: PWM, output compare, input capture and update/compare interrupts. More...
#include <HardwareTimer.h>
Public Member Functions | |
| HardwareTimer () | |
| HardwareTimer constructor: make uninitialized timer Before calling any methods, call setup to select and setup the timer to be used. | |
| HardwareTimer (TIM_TypeDef *instance) | |
| HardwareTimer constructor: set default configuration values The timer will be usable directly, there is no need to call setup(). | |
| ~HardwareTimer () | |
| HardwareTimer destructor. | |
| void | setup (TIM_TypeDef *instance) |
| HardwareTimer setup: configuration values. | |
| void | pause (void) |
| Pause HardwareTimer: stop timer. | |
| void | pauseChannel (uint32_t channel) |
| Pause only one channel. | |
| void | resume (void) |
| Start or resume HardwareTimer: all channels are resumed, interrupts are enabled if necessary. | |
| void | resumeChannel (uint32_t channel) |
| Configure specified channel and resume/start timer. | |
| void | setPrescaleFactor (uint32_t prescaler) |
| Configure hardwareTimer prescaler. | |
| uint32_t | getPrescaleFactor () |
| Retrieve prescaler from hardware register. | |
| void | setOverflow (uint32_t overflow, TimerFormat_t format=TICK_FORMAT) |
| Set overflow (rollover) | |
| uint32_t | getOverflow (TimerFormat_t format=TICK_FORMAT) |
| Retrieve overflow (rollover) value from hardware register. | |
| void | setPWM (uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback=nullptr, callback_function_t CompareCallback=nullptr) |
| All in one function to configure PWM. | |
| void | setPWM (uint32_t channel, pin_size_t pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback=nullptr, callback_function_t CompareCallback=nullptr) |
| All in one function to configure PWM, using an Arduino pin number. | |
| void | setCount (uint32_t counter, TimerFormat_t format=TICK_FORMAT) |
| Set timer counter value. | |
| uint32_t | getCount (TimerFormat_t format=TICK_FORMAT) |
| Retrieve timer counter value. | |
| void | setMode (uint32_t channel, TimerModes_t mode, PinName pin=NC, ChannelInputFilter_t filter=FILTER_NONE) |
| Set channel mode: configure timer channel with specified mode on specified pin if available. | |
| void | setMode (uint32_t channel, TimerModes_t mode, pin_size_t pin, ChannelInputFilter_t filter=FILTER_NONE) |
| Set channel mode: configure timer channel with specified mode on specified pin if available. | |
| TimerModes_t | getMode (uint32_t channel) |
| Retrieves channel mode configured. | |
| void | setPreloadEnable (bool value) |
| Enable or disable preloading for overflow value When disabled, changes to the overflow value take effect immediately. | |
| uint32_t | getCaptureCompare (uint32_t channel, TimerCompareFormat_t format=TICK_COMPARE_FORMAT) |
| Retrieve Capture/Compare value. | |
| void | setCaptureCompare (uint32_t channel, uint32_t compare, TimerCompareFormat_t format=TICK_COMPARE_FORMAT) |
| Set channel Capture/Compare register. | |
| void | setInterruptPriority (uint32_t preemptPriority, uint32_t subPriority) |
| Set the priority of the interrupt. | |
| void | attachInterrupt (callback_function_t callback) |
| Attach interrupt callback on update (rollover) event. | |
| void | detachInterrupt () |
| Detach interrupt callback on update (rollover) event. | |
| bool | hasInterrupt () |
| Checks if there's an interrupt callback attached on Rollover event. | |
| void | attachInterrupt (uint32_t channel, callback_function_t callback) |
| Attach interrupt callback on Capture/Compare event. | |
| void | detachInterrupt (uint32_t channel) |
| Detach interrupt callback on Capture/Compare event. | |
| bool | hasInterrupt (uint32_t channel) |
| Checks if there's an interrupt callback attached on Capture/Compare event. | |
| void | timerHandleDeinit () |
| This function will reset the timer. | |
| void | refresh (void) |
| Generate an update event to force all registers (Autoreload, prescaler, compare) to be taken into account. | |
| uint32_t | getTimerClkFreq () |
| This function return the timer clock frequency. | |
| void | updateRegistersIfNotRunning (TIM_TypeDef *TIMx) |
| Take into account registers update immediately if timer is not running, (independently from Preload setting) | |
| bool | isRunning () |
| Check whether HardwareTimer is running (paused or resumed). | |
| bool | isRunningChannel (uint32_t channel) |
| Check whether channel is running (paused or resumed). | |
| TIM_HandleTypeDef * | getHandle () |
| Return the timer object handle object for more advanced setup. | |
| uint32_t | getChannel (uint32_t channel) |
| Convert arduino channel into HAL channel. | |
| uint32_t | getLLChannel (uint32_t channel) |
| Convert arduino channel into LL channels used (regular and/or complementary) | |
| bool | manageIT (uint32_t channel, ChannelITConfig_t config=DISABLE_IT) |
| Handle itterupt based on arduino channel. | |
| uint32_t | getAssociatedChannel (uint32_t channel) |
| Get input associated channel Channel 1 and 2 are associated; channel 3 and 4 are associated. | |
Static Public Member Functions | |
| static void | captureCompareCallback (TIM_HandleTypeDef *htim) |
| Generic Capture and Compare callback which will call user callback. | |
| static void | updateCallback (TIM_HandleTypeDef *htim) |
| Generic Update (rollover) callback which will call user callback. | |
Arduino-style wrapper around an STM32 general purpose/advanced timer: PWM, output compare, input capture and update/compare interrupts.
| HardwareTimer::HardwareTimer | ( | TIM_TypeDef * | instance | ) |
HardwareTimer constructor: set default configuration values The timer will be usable directly, there is no need to call setup().
Using this constructor is not recommended for global variables that are automatically initialized at startup, since this will happen to early to report any errors. Better use the argumentless constructor and call the setup() method during initialization later.
| instance | Timer instance ex: TIM1, ... |
| void HardwareTimer::attachInterrupt | ( | callback_function_t | callback | ) |
Attach interrupt callback on update (rollover) event.
| callback | Interrupt callback |
| void HardwareTimer::attachInterrupt | ( | uint32_t | channel, |
| callback_function_t | callback | ||
| ) |
Attach interrupt callback on Capture/Compare event.
| channel | Arduino channel [1..4] |
| callback | Interrupt callback |
|
static |
Generic Capture and Compare callback which will call user callback.
| htim | HAL timer handle |
| void HardwareTimer::detachInterrupt | ( | uint32_t | channel | ) |
Detach interrupt callback on Capture/Compare event.
| channel | Arduino channel [1..4] |
| uint32_t HardwareTimer::getAssociatedChannel | ( | uint32_t | channel | ) |
Get input associated channel Channel 1 and 2 are associated; channel 3 and 4 are associated.
| channel | Arduino channel [1..4] |
| uint32_t HardwareTimer::getCaptureCompare | ( | uint32_t | channel, |
| TimerCompareFormat_t | format = TICK_COMPARE_FORMAT |
||
| ) |
Retrieve Capture/Compare value.
| channel | Arduino channel [1..4] |
| format | Format of return value (see TimerCompareFormat_t). If omitted default format is Tick TICK_COMPARE_FORMAT: return value is the number of tick for Capture/Compare value MICROSEC_COMPARE_FORMAT: return value is the number of microsecondes for Capture/Compare value HERTZ_COMPARE_FORMAT: return value is the frequency in hertz for Capture/Compare value PERCENT_COMPARE_FORMAT: return value is the dutycycle in percentage RESOLUTION_nB_COMPARE_FORMAT: return value is the dutycycle scaled on n bits [0 .. 2^n - 1] |
| uint32_t HardwareTimer::getChannel | ( | uint32_t | channel | ) |
Convert arduino channel into HAL channel.
| channel | Arduino channel [1..4] |
| uint32_t HardwareTimer::getCount | ( | TimerFormat_t | format = TICK_FORMAT | ) |
Retrieve timer counter value.
| format | Format of returned value. If omitted default format is Tick |
| TIM_HandleTypeDef * HardwareTimer::getHandle | ( | ) |
Return the timer object handle object for more advanced setup.
| uint32_t HardwareTimer::getLLChannel | ( | uint32_t | channel | ) |
Convert arduino channel into LL channels used (regular and/or complementary)
| channel | Arduino channel [1..4] |
| TimerModes_t HardwareTimer::getMode | ( | uint32_t | channel | ) |
Retrieves channel mode configured.
| channel | Arduino channel [1..4] |
| uint32_t HardwareTimer::getOverflow | ( | TimerFormat_t | format = TICK_FORMAT | ) |
Retrieve overflow (rollover) value from hardware register.
| format | Format of returned value. If omitted default format is Tick |
| uint32_t HardwareTimer::getPrescaleFactor | ( | ) |
Retrieve prescaler from hardware register.
| uint32_t HardwareTimer::getTimerClkFreq | ( | ) |
This function return the timer clock frequency.
| bool HardwareTimer::hasInterrupt | ( | ) |
Checks if there's an interrupt callback attached on Rollover event.
| bool HardwareTimer::hasInterrupt | ( | uint32_t | channel | ) |
Checks if there's an interrupt callback attached on Capture/Compare event.
| channel | Arduino channel [1..4] |
| bool HardwareTimer::isRunning | ( | ) |
Check whether HardwareTimer is running (paused or resumed).
| bool HardwareTimer::isRunningChannel | ( | uint32_t | channel | ) |
Check whether channel is running (paused or resumed).
| channel | Arduino channel [1..4] |
| bool HardwareTimer::manageIT | ( | uint32_t | channel, |
| ChannelITConfig_t | config = DISABLE_IT |
||
| ) |
Handle itterupt based on arduino channel.
| channel | Arduino channel [1..4] |
| config | Configuration for the interrupt (enable, disable, clear, clear and disable) |
| void HardwareTimer::pauseChannel | ( | uint32_t | channel | ) |
Pause only one channel.
Timer is still running but channel is disabled (output and interrupt)
| channel | Arduino channel [1..4] |
| void HardwareTimer::refresh | ( | void | ) |
Generate an update event to force all registers (Autoreload, prescaler, compare) to be taken into account.
| void HardwareTimer::resumeChannel | ( | uint32_t | channel | ) |
Configure specified channel and resume/start timer.
| channel | Arduino channel [1..4] |
| void HardwareTimer::setCaptureCompare | ( | uint32_t | channel, |
| uint32_t | compare, | ||
| TimerCompareFormat_t | format = TICK_COMPARE_FORMAT |
||
| ) |
Set channel Capture/Compare register.
| channel | Arduino channel [1..4] |
| compare | Compare value depending on format |
| format | Format of compare parameter (see TimerCompareFormat_t). If omitted default format is Tick TICK_COMPARE_FORMAT: compare is the number of tick MICROSEC_COMPARE_FORMAT: compare is the number of microsecondes HERTZ_COMPARE_FORMAT: compare is the frequency in hertz PERCENT_COMPARE_FORMAT: compare is the dutycycle in percentage RESOLUTION_nB_COMPARE_FORMAT: compare is the dutycycle scaled on n bits [0 .. 2^n - 1] |
| void HardwareTimer::setCount | ( | uint32_t | counter, |
| TimerFormat_t | format = TICK_FORMAT |
||
| ) |
Set timer counter value.
| counter | Counter value, depends on format parameter |
| format | Format of counter parameter. If omitted default format is Tick TICK_FORMAT: counter is the number of tick MICROSEC_FORMAT: counter is the number of microsecondes HERTZ_FORMAT: counter is the frequency in hertz |
| void HardwareTimer::setInterruptPriority | ( | uint32_t | preemptPriority, |
| uint32_t | subPriority | ||
| ) |
Set the priority of the interrupt.
| preemptPriority | The pre-emption priority for the IRQn channel |
| subPriority | The subpriority level for the IRQ channel. |
| void HardwareTimer::setMode | ( | uint32_t | channel, |
| TimerModes_t | mode, | ||
| pin_size_t | pin, | ||
| ChannelInputFilter_t | filter = FILTER_NONE |
||
| ) |
Set channel mode: configure timer channel with specified mode on specified pin if available.
| channel | Arduino channel [1..4] |
| mode | Mode configuration for the channel (see TimerModes_t) |
| pin | Arduino pin number, ex: D1, 1 or PA1 |
| filter | Input capture filter (see ChannelInputFilter_t) |
| void HardwareTimer::setMode | ( | uint32_t | channel, |
| TimerModes_t | mode, | ||
| PinName | pin = NC, |
||
| ChannelInputFilter_t | filter = FILTER_NONE |
||
| ) |
Set channel mode: configure timer channel with specified mode on specified pin if available.
| channel | Arduino channel [1..4] |
| mode | Mode configuration for the channel (see TimerModes_t) |
| pin | Pin name, ex: PB_0 |
| filter | Input capture filter (see ChannelInputFilter_t) |
| void HardwareTimer::setOverflow | ( | uint32_t | overflow, |
| TimerFormat_t | format = TICK_FORMAT |
||
| ) |
Set overflow (rollover)
Note that by default, the new value will not be applied immediately, but become effective at the next update event (usually the next timer overflow). See setPreloadEnable() for controlling this behaviour.
| overflow | Overflow value, depends on format parameter |
| format | Format of overflow parameter. If omitted default format is Tick TICK_FORMAT: overflow is the number of tick for overflow MICROSEC_FORMAT: overflow is the number of microsecondes for overflow HERTZ_FORMAT: overflow is the frequency in hertz for overflow |
| void HardwareTimer::setPreloadEnable | ( | bool | value | ) |
Enable or disable preloading for overflow value When disabled, changes to the overflow value take effect immediately.
When enabled (the default), the value takes effect only at the next update event (typically the next overflow).
Note that the capture/compare register has its own preload enable bit, which is independent and enabled in PWM modes and disabled otherwise. If you need more control of that bit, you can use the HAL functions directly.
| value | true to enable preloading, false to disable |
| void HardwareTimer::setPrescaleFactor | ( | uint32_t | prescaler | ) |
Configure hardwareTimer prescaler.
| prescaler | Prescaler factor |
| void HardwareTimer::setPWM | ( | uint32_t | channel, |
| pin_size_t | pin, | ||
| uint32_t | frequency, | ||
| uint32_t | dutycycle, | ||
| callback_function_t | PeriodCallback = nullptr, |
||
| callback_function_t | CompareCallback = nullptr |
||
| ) |
All in one function to configure PWM, using an Arduino pin number.
| channel | Arduino channel [1..4] |
| pin | Arduino pin number, ex D1, 1 or PA1 |
| frequency | PWM frequency expressed in hertz |
| dutycycle | PWM dutycycle expressed in percentage |
| PeriodCallback | Timer period callback (timer rollover upon update event) |
| CompareCallback | Timer compare callback |
| void HardwareTimer::setPWM | ( | uint32_t | channel, |
| PinName | pin, | ||
| uint32_t | frequency, | ||
| uint32_t | dutycycle, | ||
| callback_function_t | PeriodCallback = nullptr, |
||
| callback_function_t | CompareCallback = nullptr |
||
| ) |
All in one function to configure PWM.
| channel | Arduino channel [1..4] |
| pin | Pin name, ex PB_0 |
| frequency | PWM frequency expressed in hertz |
| dutycycle | PWM dutycycle expressed in percentage |
| PeriodCallback | Timer period callback (timer rollover upon update event) |
| CompareCallback | Timer compare callback |
| void HardwareTimer::setup | ( | TIM_TypeDef * | instance | ) |
HardwareTimer setup: configuration values.
Must be called exactly once before any other methods, except when an instance is passed to the constructor.
| instance | Timer instance ex: TIM1, ... |
|
static |
Generic Update (rollover) callback which will call user callback.
| htim | HAL timer handle |
| void HardwareTimer::updateRegistersIfNotRunning | ( | TIM_TypeDef * | TIMx | ) |
Take into account registers update immediately if timer is not running, (independently from Preload setting)
| TIMx | Timer instance |