Arduino Core for STM32  1.0
HardwreTimer
  1. Basic timer handling
    #include "HardwareTimer.h"
    //At first, enable timer 22 and its NVIC in .ioc file settings
    extern TIM_HandleTypeDef htim22;
    HardwareTimer tim22(htim22); //creating instance of HardwareTimer for TIM22
    //This is also an alternative, but we recommend to use first one, because it accepts settings, that were done in .ioc file
    //HardwareTimer tim22(TIM22); //creating instance of HardwareTimer for TIM22
    //Creating callback function
    void change_LED_state(){
    //Toggle led pin
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
    }
    //This method have to be called in main function in main.c file
    void app(void)
    {
    //Timer initialization
    tim22.init();
    //Callback setting
    tim22.setCallback(change_LED_state);
    //Setting timer interval to 1s (10000000UL*100ns)
    tim22.setInterval(10000000UL);
    //Starting timer
    tim22.start();
    //From now, led will change state every second
    while (1)
    {
    //Nothing to do here
    }
    }
  2. Advanced timer handling
    #include "HardwareTimer.h"
    //At first, enable timer 22 and its NVIC in .ioc file settings
    extern TIM_HandleTypeDef htim22; //exported from main.h
    HardwareTimer tim22(htim22); //creating instance of HardwareTimer for TIM22
    //This is also an alternative, but we recommend to use first one, because it accepts settings, that were done in .ioc file
    //HardwareTimer tim22(TIM22); //creating instance of HardwareTimer for TIM22
    //Creating callback function
    void change_LED_state(){
    //Toggle led pin
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
    }
    //This method have to be called in main function in main.c file
    void app(void)
    {
    //Check if timer instance really exists
    bool timerExists = tim22.instanceExists();
    //Checks if timer supports selection of counter mode
    bool supportsSelectionCounterMode = tim22.supportsSelectionCounterMode();
    //Get end set counter mode to TIM_COUNTERMODE_UP
    uint32_t counterMode = tim22.getCounterMode();
    tim22.setCounterMode(TIM_COUNTERMODE_UP);
    counterMode = tim22.getCounterMode();
    //Get available channels count
    uint32_t channelsCount = tim22.availableChannelsCount();
    //Get count of all available timers in this MCU
    uint8_t availableTimersCount = getAvailableTimersCount();
    //Enable autoReload preload
    tim22.enableAutoReloadPreload(true);
    bool AutoReloadPreloadEnabled = tim22.isAutoReloadPreloadEnabled();
    //Timer initialization
    tim22.init();
    //Setting timer interval and reading values back
    tim22.setPrescaler(5000);
    tim22.setPeriod(20);
    uint16_t prescaler = tim22.getPrescaler();
    uint16_t period = tim22.getPeriod();
    //Setting counter register and getting value back
    tim22.setCount(0);
    uint16_t count = tim22.getCount();
    //Checks if timer instance supports timer clock division
    bool supportsClockDivision = tim22.supportsClockDivision();
    //Set timer clock division to 1
    tim22.setClockDivision(1);
    uint32_t clockDiv = tim22.getClockDivision();
    //Get frequency of timer clock before division
    uint32_t timerFreq = tim22.getTimerClockFreq();
    //Get frequency of timer clock after division
    uint32_t timerDivFreq = tim22.getDivTimerClockFreq();
    //Callback setting
    tim22.setCallback(change_LED_state);
    //Starting timer
    tim22.start();
    //From now, led will change state every second
    //Infinite loop
    while (1)
    {
    //Nothing to do here
    }
    }
Note
Those examples works only with c++, but STM32CubeIDE does not allow you to change main.c to main.cpp, so you have to create own c++ file, and include it in main.h. In your cpp file create function, that will be called in main() function. In this case, function name is app() and is called in main() function between tags USER CODE BEGIN 2 and USER CODE END 2.
See also
Hardware Timer documentation
HardwareTimer
HardwareTimer.h
HardwareTimer.h
This file contains HardwareTimer class, that you can use to easy timer handling.
HardwareTimer
Handle timers in interrupt mode really easy using this class. In table below, you can see methods se...
Definition: HardwareTimer.h:211