1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <esp_bit_defs.h>
24 #include "esp_attr.h"
25 #include "soc/soc_caps.h"
26 
27 /**
28  * @brief Selects a Timer-Group out of 2 available groups
29  */
30 typedef enum {
31     TIMER_GROUP_0 = 0, /*!<Hw timer group 0*/
32 #if SOC_TIMER_GROUPS > 1
33     TIMER_GROUP_1 = 1, /*!<Hw timer group 1*/
34 #endif
35     TIMER_GROUP_MAX,
36 } timer_group_t;
37 
38 /**
39  * @brief Select a hardware timer from timer groups
40  */
41 typedef enum {
42     TIMER_0 = 0, /*!<Select timer0 of GROUPx*/
43 #if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1
44     TIMER_1 = 1, /*!<Select timer1 of GROUPx*/
45 #endif
46     TIMER_MAX,
47 } timer_idx_t;
48 
49 /**
50  * @brief Decides the direction of counter
51  */
52 typedef enum {
53     TIMER_COUNT_DOWN = 0, /*!< Descending Count from cnt.high|cnt.low*/
54     TIMER_COUNT_UP = 1,   /*!< Ascending Count from Zero*/
55     TIMER_COUNT_MAX
56 } timer_count_dir_t;
57 
58 /**
59  * @brief Decides whether timer is on or paused
60  */
61 typedef enum {
62     TIMER_PAUSE = 0, /*!<Pause timer counter*/
63     TIMER_START = 1, /*!<Start timer counter*/
64 } timer_start_t;
65 
66 /**
67  * @brief Interrupt types of the timer.
68  */
69 //this is compatible with the value of esp32.
70 typedef enum {
71     TIMER_INTR_T0 = BIT(0),  /*!< interrupt of timer 0 */
72 #if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1
73     TIMER_INTR_T1 = BIT(1),  /*!< interrupt of timer 1 */
74     TIMER_INTR_WDT = BIT(2), /*!< interrupt of watchdog */
75 #else
76     TIMER_INTR_WDT = BIT(1), /*!< interrupt of watchdog */
77 #endif
78     TIMER_INTR_NONE = 0
79 } timer_intr_t;
80 FLAG_ATTR(timer_intr_t)
81 
82 /**
83  * @brief Decides whether to enable alarm mode
84  */
85 typedef enum {
86     TIMER_ALARM_DIS = 0,  /*!< Disable timer alarm*/
87     TIMER_ALARM_EN = 1,   /*!< Enable timer alarm*/
88     TIMER_ALARM_MAX
89 } timer_alarm_t;
90 
91 /**
92  * @brief Select interrupt type if running in alarm mode.
93  */
94 typedef enum {
95     TIMER_INTR_LEVEL = 0,  /*!< Interrupt mode: level mode*/
96     TIMER_INTR_MAX
97 } timer_intr_mode_t;
98 
99 /**
100  * @brief Select if Alarm needs to be loaded by software or automatically reload by hardware.
101  */
102 typedef enum {
103     TIMER_AUTORELOAD_DIS = 0,  /*!< Disable auto-reload: hardware will not load counter value after an alarm event*/
104     TIMER_AUTORELOAD_EN = 1,   /*!< Enable auto-reload: hardware will load counter value after an alarm event*/
105     TIMER_AUTORELOAD_MAX,
106 } timer_autoreload_t;
107 
108 #if SOC_TIMER_GROUP_SUPPORT_XTAL
109 /**
110  * @brief Select timer source clock.
111  */
112 typedef enum {
113     TIMER_SRC_CLK_APB = 0,  /*!< Select APB as the source clock*/
114     TIMER_SRC_CLK_XTAL = 1, /*!< Select XTAL as the source clock*/
115 } timer_src_clk_t;
116 #endif
117 
118 /**
119  * @brief Data structure with timer's configuration settings
120  */
121 typedef struct {
122     timer_alarm_t alarm_en;      /*!< Timer alarm enable */
123     timer_start_t counter_en;    /*!< Counter enable */
124     timer_intr_mode_t intr_type; /*!< Interrupt mode */
125     timer_count_dir_t counter_dir; /*!< Counter direction  */
126     timer_autoreload_t auto_reload;   /*!< Timer auto-reload */
127     uint32_t divider;   /*!< Counter clock divider. The divider's range is from from 2 to 65536. */
128 #if SOC_TIMER_GROUP_SUPPORT_XTAL
129     timer_src_clk_t clk_src;  /*!< Use XTAL as source clock. */
130 #endif
131 } timer_config_t;
132 
133 #ifdef __cplusplus
134 }
135 #endif
136