1 /*
2  * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include "soc/soc_caps.h"
10 #include "soc/clk_tree_defs.h"
11 #include "hal/timer_types.h"
12 #include "esp_attr.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief Timer-Group ID
20  */
21 typedef enum {
22     TIMER_GROUP_0 = 0, /*!< Hw timer group 0 */
23 #if SOC_TIMER_GROUPS > 1
24     TIMER_GROUP_1 = 1, /*!< Hw timer group 1 */
25 #endif
26     TIMER_GROUP_MAX    /*!< Maximum number of Hw timer groups */
27 } timer_group_t;
28 
29 /**
30  * @brief Timer ID
31  */
32 typedef enum {
33     TIMER_0 = 0, /*!< Select timer0 of GROUPx*/
34 #if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1
35     TIMER_1 = 1, /*!< Select timer1 of GROUPx*/
36 #endif
37     TIMER_MAX,
38 } timer_idx_t;
39 
40 /**
41  * @brief Interrupt types of the timer.
42  */
43 typedef enum {
44     TIMER_INTR_T0 = 1 << 0,  /*!< interrupt of timer 0 */
45 #if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1
46     TIMER_INTR_T1 = 1 << 1,  /*!< interrupt of timer 1 */
47     TIMER_INTR_WDT = 1 << 2, /*!< interrupt of watchdog */
48 #else
49     TIMER_INTR_WDT = 1 << 1, /*!< interrupt of watchdog */
50 #endif
51     TIMER_INTR_NONE = 0
52 } timer_intr_t;
53 FLAG_ATTR(timer_intr_t)
54 
55 /**
56  * @brief Timer count direction
57  */
58 typedef enum {
59     TIMER_COUNT_DOWN = GPTIMER_COUNT_DOWN, /*!< Descending Count from cnt.high|cnt.low*/
60     TIMER_COUNT_UP = GPTIMER_COUNT_UP,     /*!< Ascending Count from Zero*/
61     TIMER_COUNT_MAX                        /*!< Maximum number of timer count directions */
62 } timer_count_dir_t;
63 
64 /**
65  * @brief Timer start/stop command
66  */
67 typedef enum {
68     TIMER_PAUSE, /*!< Pause timer counter*/
69     TIMER_START, /*!< Start timer counter*/
70 } timer_start_t;
71 
72 /**
73  * @brief Timer alarm command
74  */
75 typedef enum {
76     TIMER_ALARM_DIS = 0, /*!< Disable timer alarm*/
77     TIMER_ALARM_EN = 1,  /*!< Enable timer alarm*/
78     TIMER_ALARM_MAX
79 } timer_alarm_t;
80 
81 /**
82  * @brief Timer interrupt type
83  */
84 typedef enum {
85     TIMER_INTR_LEVEL = 0, /*!< Interrupt mode: level mode*/
86     TIMER_INTR_MAX
87 } timer_intr_mode_t;
88 
89 /**
90  * @brief Timer autoreload command
91  */
92 typedef enum {
93     TIMER_AUTORELOAD_DIS = 0, /*!< Disable auto-reload: hardware will not load counter value after an alarm event*/
94     TIMER_AUTORELOAD_EN = 1,  /*!< Enable auto-reload: hardware will load counter value after an alarm event*/
95     TIMER_AUTORELOAD_MAX,
96 } timer_autoreload_t;
97 
98 /**
99  * @brief Timer group clock source
100  */
101 typedef soc_periph_tg_clk_src_legacy_t timer_src_clk_t;
102 
103 /**
104  * @brief Timer configurations
105  */
106 typedef struct {
107     timer_alarm_t alarm_en;         /*!< Timer alarm enable */
108     timer_start_t counter_en;       /*!< Counter enable */
109     timer_intr_mode_t intr_type;    /*!< Interrupt mode */
110     timer_count_dir_t counter_dir;  /*!< Counter direction  */
111     timer_autoreload_t auto_reload; /*!< Timer auto-reload */
112     timer_src_clk_t clk_src;        /*!< Selects source clock. */
113     uint32_t divider;               /*!< Counter clock divider */
114 } timer_config_t;
115 
116 #ifdef __cplusplus
117 }
118 #endif
119