Timer

[中文]

ESP Timer Classification

  • Timer
    • System Timer
      • esp_timer

      • FreeRTOS Tick

    • General Timer
      • Independent Hardware Timer

The following will introduce each type of ESP timer one by one.

esp_timer

Since there already exists an esp_timer document in the ESP-IDF programming guide, only some additional supplements will be made here.

The features of esp_timer are as follows:

  • The clock source is XTAL_CLK

  • Supports time compensation by loading the sleep time recorded by the RTC timer through the system timer after waking up from light-sleep

  • Taking ESP32 as an example, ESP32 often runs at a CPU frequency of 240 MHz and uses task distribution to determine the minimum period of esp_timer. At this time, the timeout value configured by the one-time timer needs to be greater than 20 μs, and the timeout value of the periodic timer needs to be greater than 50 μs

There are two types of esp_timer callbacks:

  • Task distribution method (default): Distribute timer callback functions from a single high-priority esp_timer task. This method is suitable for timer callback functions that do not have high timing requirements

  • Interrupt distribution method: Directly distribute timer callback functions from the interrupt service routine. This method is suitable for simple, low-latency timer callback functions that only run for a few μs. The advantage is that it can ensure that the delay between the event and the execution of the callback is short and is not affected by other tasks.

The way to configure as an interrupt distribution method is: Enable CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD in menuconfig.

GPTimer

Since there already exists a GPTimer document in the ESP-IDF programming guide, only some additional supplements will be made here.

The features of GPTimer are as follows:

  • The clock source is XTAL_CLK && APB_CLK

  • Implemented based on timer group peripherals

  • The application exclusively uses hardware resources and is not easily interfered with by other modules

  • More functions, can be linked with other peripherals through ETM (only new chips such as ESP32-C6 support)

FreeRTOS Timer

Since there already exists a FreeRTOS Timer API description in the ESP-IDF programming guide, only some additional supplements will be made here.

The features of the FreeRTOS Timer are as follows:

  • The clock source is FreeRTOS Tick

  • Pure software timer

  • Uses a low-priority timer task (the priority can be configured through the CONFIG_FREERTOS_TIMER_TASK_PRIORITY option), the maximum resolution is the FreeRTOS Tick cycle

Comparison of Different Clocks

Clock

Advantages

Disadvantages

esp_timer

  1. High precision and accuracy

  2. Easy to use

The system also uses it, and when there is a blockage in the application layer, it will affect the system

GPTimer

  1. Highest precision and accuracy, can be used for waveform generation

  2. Provides additional features such as event capture

Not convenient to use

FreeRTOS Timer

Easy to use, does not affect the system

  1. Maximum resolution equals the RTOS tick cycle

  2. Timer callback functions come from the low-priority timer task. This task may be preempted by other tasks, leading to a decrease in precision and accuracy