ESPIDF-定时器

概述

ESP32 内置 4 个 64-bit 通用定时器。每个定时器包含一个 16-bit 预分频器和一个 64-bit 可自动重新加载向上/向下计数器
ESP32 的定时器分为 2 组,每组 2 个。TIMGn_Tx 的 n 代表组别,x 代表定时器编号。

功能描述

16-bit 预分频器

每个定时器都以 APB 时钟(缩写 APB_CLK,频率通常为 80 MHz)作为基础时钟。16-bit 预分频器对 APB 时钟进
行分频,产生时基计数器时钟(TB_clk)。TB_clk 每过一个周期,时基计数器会向上数一或者向下数一。在使用寄存
器 TIMGn_Tx_DIVIDER 配置分频器除数前,必须关闭定时器(清零 TIMGn_Tx_DIVIDER)。定时器使能时配置预分
频器会导致不可预知的结果。预分频器可以对 APB 时钟进行 2 到 65536 的分频。具体来说,TIMGn_Tx_DIVIDER
为 1 或 2 时,时钟分频器是 2;TIMGn_Tx_DIVIDER 为 0 时,时钟分频器是 65536。如 TIMGn_Tx_DIVIDER 为
其他任意值,时钟会被相同数值分频。

64-bit 时基计数器

TIMGn_Tx_INCREASE 置 1 或清零可以将 64-bit 时基计数器分别配置为向上计数或向下计数。同时,64-bit 时基计数器支持自动重新加载和软件即时重新加载,计数器达到软件设定值时会触发报警事件。
TIMGn_Tx_EN 置 1 或清零可以使能或关闭计数。清零后计数器暂停计数,并会在 TIMGn_Tx_ EN 重新置 1 前保持其值不变。清零 TIMGn_Tx _EN 会重新加载计数器并改变计数器的值,但在设置 TIMGn_Tx_EN 前计数不会恢复。

timer_config_t

Data structure with timer’s configuration settings

1
2
3
4
5
6
7
8
9
10
11
typedef struct {
timer_alarm_t alarm_en; /*!< Timer alarm enable */
timer_start_t counter_en; /*!< Counter enable */
timer_intr_mode_t intr_type; /*!< Interrupt mode */
timer_count_dir_t counter_dir; /*!< Counter direction */
timer_autoreload_t auto_reload; /*!< Timer auto-reload */
uint32_t divider; /*!< Counter clock divider. The divider's range is from from 2 to 65536. */
#if SOC_TIMER_GROUP_SUPPORT_XTAL
timer_src_clk_t clk_src; /*!< Use XTAL as source clock. */
#endif
} timer_config_t;

timer_set_counter_value

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @brief Set counter value to hardware timer.
*
* @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
* @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
* @param load_val Counter value to write to the hardware timer.
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val);

timer_set_alarm_value

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @brief Set timer alarm value.
*
* @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
* @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
* @param alarm_value A 64-bit value to set the alarm value.
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t timer_set_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_value);

timer_isr_callback_add

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* @brief Add ISR handle callback for the corresponding timer.
*
* @param group_num Timer group number
* @param timer_num Timer index of timer group
* @param isr_handler Interrupt handler function, it is a callback function.
* @param arg Parameter for handler function
* @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
* ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
*
* @note This ISR handler will be called from an ISR.
* This ISR handler do not need to handle interrupt status, and should be kept short.
* If you want to realize some specific applications or write the whole ISR, you can
* call timer_isr_register(...) to register ISR.
*
* The callback should return a bool value to determine whether need to do YIELD at
* the end of the ISR.
*
* If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set,
* the handler function must be declared with IRAM_ATTR attribute
* and can only call functions in IRAM or ROM. It cannot call other timer APIs.
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t timer_isr_callback_add(timer_group_t group_num, timer_idx_t timer_num, timer_isr_t isr_handler, void *arg, int intr_alloc_flags);