HAL TIM Overview

Introducing TIM

group TIM_Introduction

  • The TIM hardware abstraction layer (HAL) provides a set of APIs to interface with STM32 timers.

  • STM32 timers (TIM) are used for precise time base generation, pulse width modulation (PWM), and event measurement, such as input capture and output compare. They enable motor control, signal timing, and encoder decoding in embedded systems.

  • The TIM HAL driver simplifies the configuration, initialization, and management of timer operations by supporting various modes such as polling, interrupt, and DMA, enabling flexible and efficient timer control.

  • Additionally, it supports multiple timer types (advanced-control, general-purpose, lite, and basic timers) depending on the STM32 device, ensuring portability and consistency across different STM32 series.

Module and files

The following diagram illustrates the TIM module and its associated files.

Module and files diagram

Component diagram

The following diagram illustrates the software components involved in the TIM module. It shows the interactions between the user application, HAL drivers, low-level drivers, and the hardware components.


@startuml
<style>
componentDiagram {
   arrow {
      FontSize 8
   }
}
</style>

title TIM Software Component Diagram

package "HAL" {
    [HAL_SERVICE (RCC, CORTEX, ...)]
    [HAL_TIM]
    [HAL_DMA]
}

package "Low Layer" {
    [LL_TIM]
    [CMSIS]
    [TIM Interrupt routine service]
    [DMA Interrupt routine service]
}

package "HW" {
    [STM32_HW]
}

[Appli] --> [HAL_TIM] : HAL TIM API
[Appli] <-- [HAL_TIM] : TIM callback
[HAL_TIM] --> [LL_TIM] : LL TIM API
[HAL_TIM] -l-> [CMSIS] : IS_TIM_<FEATURE>_INSTANCE
[HAL_TIM] -r-> [HAL_DMA] : HAL_DMA_StartDirectXfer_IT\nHAL_DMA_Abort_IT
[HAL_TIM] -l-> [HAL_SERVICE (RCC, CORTEX, ...)] : HAL_GetTick
[HAL_TIM] <-- [TIM Interrupt routine service]
[HAL_TIM] <-r- [HAL_DMA]:Half transfer complete\nTransfer complete\nError
[LL_TIM] --> [CMSIS]:TIM register def
[HAL_DMA] <-d- [DMA Interrupt routine service]
[STM32_HW] -u--> [TIM Interrupt routine service]: TIMx Irq
[STM32_HW] -u--> [DMA Interrupt routine service] : DMA Channel Irq
[STM32_HW] <-- [LL_TIM] : TIM registers R/W
@enduml

Configuration table

The following table lists the configuration defines for the HAL TIM module, specifying their locations, default values, and descriptions:

group TIM_Configuration_Table

Configuration inside the TIM driver

Config defines

Where

Default value

Note

USE_HAL_TIM_MODULE

hal_conf.h

1

HAL TIM module is enabled

USE_HAL_TIM_DMA

hal_conf.h

1

Enable the DMA code inside TIM

USE_HAL_TIM_REGISTER_CALLBACKS

hal_conf.h

0

Enable register callback feature

USE_HAL_TIM_CLK_ENABLE_MODEL

hal_conf.h

HAL_CLK_ENABLE_NO

Enable the gating of the peripheral clock

USE_HAL_CHECK_PARAM

hal_conf.h

0

Enable run-time parameter check

USE_HAL_CHECK_PROCESS_STATE

hal_conf.h

0

Enable atomic access to process state check

USE_ASSERT_DBG_PARAM

PreProcessor env

NA

Enable the parameter assert

USE_ASSERT_DBG_STATE

PreProcessor env

NA

Enable the state assert

USE_HAL_TIM_GET_LAST_ERRORS

hal_conf.h

0

Enable retrieving the error codes

USE_HAL_TIM_USER_DATA

hal_conf.h

0

Add an user data inside HAL TIM handle