Running Timers in FreeRTOS

This application note explains how to use the STM32H7 timers in the FreeRTOS demo application.

1. Understanding Timer Interfaces

1.1. FreeRTOS Timers Implementation

The FreeRTOS BSP makes use of the STM32CubeH7 software component to provide a device driver for the STM32H7 general-purpose timers. The driver is configured (enabled / disabled) at the BSP build time, using the HAL_TIM_MODULE_ENABLED configuration option, defined in the stm32h7xx_hal_conf.h file.

1.2. FreeRTOS Timers C-Binding API

The Timers driver implements the following C-binding API:

Function

Description

Comments

HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)

 

Initialise the TIM Time base Unit according to the specified parameters by htim

htim is a pointer to a structure that contains the configuration information for TIM module; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT}

HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)

 

De-initialise the TIM Base peripheral

htim is a pointer to a structure that contains the configuration information for TIM module; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT}

HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)

 

Starts the TIM Time Base generation in interrupt mode

htim is a pointer to a structure that contains the configuration information for TIM module; returns one of {HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT}

The TIM_HandleTypeDef data structure used in this API has the following definition:

typedef struct { TIM_TypeDef *Instance; /* Register base address */ TIM_Base_InitTypeDef Init; /* TIM Time Base required parameters */ HAL_TIM_ActiveChannel Channel; /* Active channel */ DMA_HandleTypeDef *hdma[7]; /* DMA Handlers array */ HAL_LockTypeDef Lock; /* Locking object */ __IO HAL_TIM_StateTypeDef State; /* TIM operation state */ } TIM_HandleTypeDef;

The TIM_Base_InitTypeDef data structure used in TIM_HandleTypeDef has the following definition:

typedef struct { uint32_t Prescaler; /* Specifies the prescaler value used to divide the TIM clock. */ uint32_t CounterMode; /* Specifies the counter mode. */ uint32_t Period; /* Specifies the period value to be loaded into the active Auto-Reload Register at the next update event. */ uint32_t ClockDivision; /* Specifies the clock division. */ uint32_t RepetitionCounter; /* Specifies the repetition counter value. */ uint32_t AutoReloadPreload; /* Specifies the auto-reload preload. */ } TIM_Base_InitTypeDef;

1.3. FreeRTOS Timers Task

The FreeRTOS demo application implements a separate thread (called "Timers Thread") illustrating use of the general-purpose timers API from a FreeRTOS application. Namely, the timers thread implements counting number of interrupts from the TIM7 timer. TIM7 is configured to generate an interrupt 1000 times per second.

2. Timers CLI Command

The FreeRTOS application implements the following timers related CLI command:

Command

Description

Comments

timer_cnt

Command to output the value of the number interrupts counted by the timers thread

 

3. Validating Timers Operation

Use the following step-wise procedure to validate the FreeRTOS timers operation:

  1. Power cycle the target board and let it boot to the FreeRTOS CLI.

  2. From the CLI, run the timer_cnt command:

    CLI> timer_cnt 8496
  3. Wait for 10 seconds and run the timer_cnt command again:

  4. Compare the results of running commands. The difference in two values should be about 10000.