Arduino Core for STM32  1.0

Hardware Timer

Class documentation: HardwareTimer

Main features:

  • Easy and fast way how to use timers in interrupt mode.
  • Auto calculation of period and prescaler just from interval, but only for 16-bit timers for now.
Note
For now, you can use it just to handle timer in interrupt mode, which is most common timer mode.
This library overrides weak HAL_TIM_PeriodElapsedCallback. Please do not override it again, it can cause compilation errors.

What not to do:

  • Never create two or more instances of this class for same timer instance, because only last set callback function will be called on timer interrupt and others will be ignored.
  • Do not try to start timer when you have not called init(void) method.
  • Do not try to create this instance for timer, that does not exists. All methods will be disabled.

How to use:

  1. Check least one IS_TIMER_INSTANCE_ON_APB1 or IS_TIMER_INSTANCE_ON_APB2 macro is defined for your MCU.
  2. Set timer clock, of timer instance you want to use, in .ioc file settings to internal clock.
  3. Enable NVIC interrupt of that timer.
  4. Create instance of this class for this timer.
Note
This code is only c++ compatible. If you want to use it, convert your project to c++.

How to add support for your MCU:

  1. Check if your MCU is not supported yet.
  2. Add elif macro to include part (#elif defined(YOUR_MCU) ...). It will mean, that you have revisited this file for your MCU.
  3. Define which timer use what APB clock using MACROS: IS_TIMER_INSTANCE_ON_APB1, IS_TIMER_INSTANCE_ON_APB2, ...

Special hidden functions:

1. Sleep timer

  • You can easily sleep timer by calling method deInit(void), that disables timer and it's interrupt.
  • If you want to wake up timer, just call init(void) method, that enables timer and it's interrupt.
    Note
    When you wake up timer using init(void) method, you don't have to set it again, because old settings are still saved in registers or in timer handle structure.

Troubleshooting:

1. Compiler says, that a lot of methods from HardwareTimer class are not declared.

  • Check if you enabled at least one timer in .ioc file.
  • Check if your MCU is supported by this library, else look at section: "How to add support for your MCU".

2. I have setup timers properly, but callback function is never called.

  • Check if you have enabled NVIC interrupt of that timer in .ioc file.
  • Check if you macro HT_USE_HAL_CALLBACK is set to 1UL.
  • Check if you have called init(void) method.
  • Check if you don't use more instances of this class for same timer instance.

Credits

Author
Matej Fitoš
Date
Nov 20, 2020
See also
HardwareTimer
TimerSettings
HardwareTimer.h

Examples:

HardwreTimer