1 #ifndef _BFLB_TIMER_H
2 #define _BFLB_TIMER_H
3 
4 #include "bflb_core.h"
5 
6 /** @addtogroup LHAL
7   * @{
8   */
9 
10 /** @addtogroup TIMER
11   * @{
12   */
13 
14 /** @defgroup TIMER_CLK_SOURCE timer clock source definition
15   * @{
16   */
17 #if !defined(BL702L)
18 #define TIMER_CLKSRC_BCLK 0
19 #endif
20 #define TIMER_CLKSRC_32K  1
21 #define TIMER_CLKSRC_1K   2
22 #define TIMER_CLKSRC_XTAL 3
23 #if !defined(BL702) && !defined(BL602)
24 #define TIMER_CLKSRC_GPIO 4
25 #endif
26 #define TIMER_CLKSRC_NO   5
27 /**
28   * @}
29   */
30 
31 /** @defgroup TIMER_COUNTER_MODE timer counter mode definition
32   * @{
33   */
34 #define TIMER_COUNTER_MODE_PROLOAD 0
35 #define TIMER_COUNTER_MODE_UP      1
36 /**
37   * @}
38   */
39 
40 /** @defgroup TIMER_COMP_ID timer compare id definition
41   * @{
42   */
43 #define TIMER_COMP_ID_0 0
44 #define TIMER_COMP_ID_1 1
45 #define TIMER_COMP_ID_2 2
46 #define TIMER_COMP_NONE 3
47 /**
48   * @}
49   */
50 
51 /** @defgroup TIMER_CAPTURE_POLARITY timer capture polarity definition
52   * @{
53   */
54 #define TIMER_CAPTURE_POLARITY_POSITIVE 0
55 #define TIMER_CAPTURE_POLARITY_NEGATIVE 1
56 /**
57   * @}
58   */
59 
60 /**
61  * @brief TIMER configuration structure
62  *
63  * @param counter_mode      Timer counter mode, use @ref TIMER_COUNTER_MODE
64  * @param clock_source      Timer clock source, use @ref TIMER_CLK_SOURCE
65  * @param clock_div         Timer clock divison value, from 0 to 255
66  * @param trigger_comp_id   Timer count register preload trigger source slelect, use @ref TIMER_COMP_ID
67  * @param comp0_val         Timer compare 0 value
68  * @param comp1_val         Timer compare 1 value
69  * @param comp2_val         Timer compare 2 value
70  * @param preload_val       Timer preload value
71  */
72 struct bflb_timer_config_s {
73     uint8_t counter_mode;
74     uint8_t clock_source;
75     uint8_t clock_div;
76     uint8_t trigger_comp_id;
77     uint32_t comp0_val;
78     uint32_t comp1_val;
79     uint32_t comp2_val;
80     uint32_t preload_val;
81 };
82 
83 /**
84  * @brief TIMER capture configuration structure
85  *
86  * @param pin      Timer capture pin
87  * @param polarity Timer capture polarity, use @ref TIMER_CAPTURE_POLARITY
88  */
89 struct bflb_timer_capture_config_s {
90     uint8_t pin;
91     uint8_t polarity;
92 };
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 /**
99  * @brief Initialize timer.
100  *
101  * @param [in] dev device handle
102  * @param [in] config pointer to save timer config
103  */
104 void bflb_timer_init(struct bflb_device_s *dev, const struct bflb_timer_config_s *config);
105 
106 /**
107  * @brief Deinitialize timer.
108  *
109  * @param [in] dev device handle
110  */
111 void bflb_timer_deinit(struct bflb_device_s *dev);
112 
113 /**
114  * @brief Start timer.
115  *
116  * @param [in] dev device handle
117  */
118 void bflb_timer_start(struct bflb_device_s *dev);
119 
120 /**
121  * @brief Stop timer.
122  *
123  * @param [in] dev device handle
124  */
125 void bflb_timer_stop(struct bflb_device_s *dev);
126 
127 /**
128  * @brief Set timer preload value.
129  *
130  * @param [in] dev device handle
131  * @param [in] val preload value
132  */
133 void bflb_timer_set_preloadvalue(struct bflb_device_s *dev, uint32_t val);
134 
135 /**
136  * @brief Set compare value of corresponding compare id.
137  *
138  * @param [in] dev device handle
139  * @param [in] cmp_no compare id, use @ref TIMER_COMP_ID
140  * @param [in] val compare value
141  */
142 void bflb_timer_set_compvalue(struct bflb_device_s *dev, uint8_t cmp_no, uint32_t val);
143 
144 /**
145  * @brief Get compare value of corresponding compare id.
146  *
147  * @param [in] dev device handle
148  * @param [in] cmp_no compare id, use @ref TIMER_COMP_ID
149  * @return uint32_t
150  */
151 uint32_t bflb_timer_get_compvalue(struct bflb_device_s *dev, uint8_t cmp_no);
152 
153 /**
154  * @brief Get timer counter value.
155  *
156  * @param [in] dev device handle
157  * @return counter value
158  */
159 uint32_t bflb_timer_get_countervalue(struct bflb_device_s *dev);
160 
161 /**
162  * @brief Enable or disable timer interrupt of corresponding compare id.
163  *
164  * @param [in] dev device handle
165  * @param [in] cmp_no compare id, use @ref TIMER_COMP_ID
166  * @param [in] mask true means disable, false means enable
167  */
168 void bflb_timer_compint_mask(struct bflb_device_s *dev, uint8_t cmp_no, bool mask);
169 
170 /**
171  * @brief Get timer interrupt status of corresponding compare id.
172  *
173  * @param [in] dev device handle
174  * @param [in] cmp_no compare id, use @ref TIMER_COMP_ID
175  * @return true mean yes, otherwise no.
176  */
177 bool bflb_timer_get_compint_status(struct bflb_device_s *dev, uint8_t cmp_no);
178 
179 /**
180  * @brief Clear timer interrupt status of corresponding compare id.
181  *
182  * @param [in] dev device handle
183  * @param [in] cmp_no compare id, use @ref TIMER_COMP_ID
184  */
185 void bflb_timer_compint_clear(struct bflb_device_s *dev, uint8_t cmp_no);
186 
187 #if !defined(BL702) || !defined(BL602)
188 void bflb_timer_capture_init(struct bflb_device_s *dev, const struct bflb_timer_capture_config_s *config);
189 uint32_t bflb_timer_capture_get_pulsewidth(struct bflb_device_s *dev);
190 #endif
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 /**
197   * @}
198   */
199 
200 /**
201   * @}
202   */
203 
204 #endif