timer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright 2010-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _DRIVER_TIMER_H_
  14. #define _DRIVER_TIMER_H_
  15. #include "esp_err.h"
  16. #include "esp_attr.h"
  17. #include "soc/soc.h"
  18. #include "soc/timer_group_reg.h"
  19. #include "soc/timer_group_struct.h"
  20. #include "esp_intr_alloc.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define TIMER_BASE_CLK (APB_CLK_FREQ) /*!< Frequency of the clock on the input of the timer groups */
  25. /**
  26. * @brief Selects a Timer-Group out of 2 available groups
  27. */
  28. typedef enum {
  29. TIMER_GROUP_0 = 0, /*!<Hw timer group 0*/
  30. TIMER_GROUP_1 = 1, /*!<Hw timer group 1*/
  31. TIMER_GROUP_MAX,
  32. } timer_group_t;
  33. /**
  34. * @brief Select a hardware timer from timer groups
  35. */
  36. typedef enum {
  37. TIMER_0 = 0, /*!<Select timer0 of GROUPx*/
  38. TIMER_1 = 1, /*!<Select timer1 of GROUPx*/
  39. TIMER_MAX,
  40. } timer_idx_t;
  41. /**
  42. * @brief Decides the direction of counter
  43. */
  44. typedef enum {
  45. TIMER_COUNT_DOWN = 0, /*!< Descending Count from cnt.high|cnt.low*/
  46. TIMER_COUNT_UP = 1, /*!< Ascending Count from Zero*/
  47. TIMER_COUNT_MAX
  48. } timer_count_dir_t;
  49. /**
  50. * @brief Decides whether timer is on or paused
  51. */
  52. typedef enum {
  53. TIMER_PAUSE = 0, /*!<Pause timer counter*/
  54. TIMER_START = 1, /*!<Start timer counter*/
  55. } timer_start_t;
  56. /**
  57. * @brief Decides whether to enable alarm mode
  58. */
  59. typedef enum {
  60. TIMER_ALARM_DIS = 0, /*!< Disable timer alarm*/
  61. TIMER_ALARM_EN = 1, /*!< Enable timer alarm*/
  62. TIMER_ALARM_MAX
  63. } timer_alarm_t;
  64. /**
  65. * @brief Select interrupt type if running in alarm mode.
  66. */
  67. typedef enum {
  68. TIMER_INTR_LEVEL = 0, /*!< Interrupt mode: level mode*/
  69. //TIMER_INTR_EDGE = 1, /*!< Interrupt mode: edge mode, Not supported Now*/
  70. TIMER_INTR_MAX
  71. } timer_intr_mode_t;
  72. /**
  73. * @brief Select if Alarm needs to be loaded by software or automatically reload by hardware.
  74. */
  75. typedef enum {
  76. TIMER_AUTORELOAD_DIS = 0, /*!< Disable auto-reload: hardware will not load counter value after an alarm event*/
  77. TIMER_AUTORELOAD_EN = 1, /*!< Enable auto-reload: hardware will load counter value after an alarm event*/
  78. TIMER_AUTORELOAD_MAX,
  79. } timer_autoreload_t;
  80. /**
  81. * @brief Data structure with timer's configuration settings
  82. */
  83. typedef struct {
  84. bool alarm_en; /*!< Timer alarm enable */
  85. bool counter_en; /*!< Counter enable */
  86. timer_intr_mode_t intr_type; /*!< Interrupt mode */
  87. timer_count_dir_t counter_dir; /*!< Counter direction */
  88. bool auto_reload; /*!< Timer auto-reload */
  89. uint32_t divider; /*!< Counter clock divider. The divider's range is from from 2 to 65536. */
  90. } timer_config_t;
  91. /**
  92. * @brief Interrupt handle, used in order to free the isr after use.
  93. * Aliases to an int handle for now.
  94. */
  95. typedef intr_handle_t timer_isr_handle_t;
  96. /**
  97. * @brief Read the counter value of hardware timer.
  98. *
  99. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  100. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  101. * @param timer_val Pointer to accept timer counter value.
  102. *
  103. * @return
  104. * - ESP_OK Success
  105. * - ESP_ERR_INVALID_ARG Parameter error
  106. */
  107. esp_err_t timer_get_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t* timer_val);
  108. /**
  109. * @brief Read the counter value of hardware timer, in unit of a given scale.
  110. *
  111. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  112. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  113. * @param time Pointer, type of double*, to accept timer counter value, in seconds.
  114. *
  115. * @return
  116. * - ESP_OK Success
  117. * - ESP_ERR_INVALID_ARG Parameter error
  118. */
  119. esp_err_t timer_get_counter_time_sec(timer_group_t group_num, timer_idx_t timer_num, double* time);
  120. /**
  121. * @brief Set counter value to hardware timer.
  122. *
  123. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  124. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  125. * @param load_val Counter value to write to the hardware timer.
  126. *
  127. * @return
  128. * - ESP_OK Success
  129. * - ESP_ERR_INVALID_ARG Parameter error
  130. */
  131. esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val);
  132. /**
  133. * @brief Start the counter of hardware timer.
  134. *
  135. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  136. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  137. *
  138. * @return
  139. * - ESP_OK Success
  140. * - ESP_ERR_INVALID_ARG Parameter error
  141. */
  142. esp_err_t timer_start(timer_group_t group_num, timer_idx_t timer_num);
  143. /**
  144. * @brief Pause the counter of hardware timer.
  145. *
  146. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  147. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  148. *
  149. * @return
  150. * - ESP_OK Success
  151. * - ESP_ERR_INVALID_ARG Parameter error
  152. */
  153. esp_err_t timer_pause(timer_group_t group_num, timer_idx_t timer_num);
  154. /**
  155. * @brief Set counting mode for hardware timer.
  156. *
  157. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  158. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  159. * @param counter_dir Counting direction of timer, count-up or count-down
  160. *
  161. * @return
  162. * - ESP_OK Success
  163. * - ESP_ERR_INVALID_ARG Parameter error
  164. */
  165. esp_err_t timer_set_counter_mode(timer_group_t group_num, timer_idx_t timer_num, timer_count_dir_t counter_dir);
  166. /**
  167. * @brief Enable or disable counter reload function when alarm event occurs.
  168. *
  169. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  170. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  171. * @param reload Counter reload mode.
  172. *
  173. * @return
  174. * - ESP_OK Success
  175. * - ESP_ERR_INVALID_ARG Parameter error
  176. */
  177. esp_err_t timer_set_auto_reload(timer_group_t group_num, timer_idx_t timer_num, timer_autoreload_t reload);
  178. /**
  179. * @brief Set hardware timer source clock divider. Timer groups clock are divider from APB clock.
  180. *
  181. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  182. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  183. * @param divider Timer clock divider value. The divider's range is from from 2 to 65536.
  184. *
  185. * @return
  186. * - ESP_OK Success
  187. * - ESP_ERR_INVALID_ARG Parameter error
  188. */
  189. esp_err_t timer_set_divider(timer_group_t group_num, timer_idx_t timer_num, uint32_t divider);
  190. /**
  191. * @brief Set timer alarm value.
  192. *
  193. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  194. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  195. * @param alarm_value A 64-bit value to set the alarm value.
  196. *
  197. * @return
  198. * - ESP_OK Success
  199. * - ESP_ERR_INVALID_ARG Parameter error
  200. */
  201. esp_err_t timer_set_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_value);
  202. /**
  203. * @brief Get timer alarm value.
  204. *
  205. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  206. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  207. * @param alarm_value Pointer of A 64-bit value to accept the alarm value.
  208. *
  209. * @return
  210. * - ESP_OK Success
  211. * - ESP_ERR_INVALID_ARG Parameter error
  212. */
  213. esp_err_t timer_get_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t* alarm_value);
  214. /**
  215. * @brief Enable or disable generation of timer alarm events.
  216. *
  217. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  218. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  219. * @param alarm_en To enable or disable timer alarm function.
  220. *
  221. * @return
  222. * - ESP_OK Success
  223. * - ESP_ERR_INVALID_ARG Parameter error
  224. */
  225. esp_err_t timer_set_alarm(timer_group_t group_num, timer_idx_t timer_num, timer_alarm_t alarm_en);
  226. /**
  227. * @brief Register Timer interrupt handler, the handler is an ISR.
  228. * The handler will be attached to the same CPU core that this function is running on.
  229. *
  230. * @param group_num Timer group number
  231. * @param timer_num Timer index of timer group
  232. * @param fn Interrupt handler function.
  233. * @param arg Parameter for handler function
  234. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  235. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  236. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  237. * be returned here.
  238. *
  239. * @note If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set,
  240. * the handler function must be declared with IRAM_ATTR attribute
  241. * and can only call functions in IRAM or ROM. It cannot call other timer APIs.
  242. * Use direct register access to configure timers from inside the ISR in this case.
  243. *
  244. * @return
  245. * - ESP_OK Success
  246. * - ESP_ERR_INVALID_ARG Parameter error
  247. */
  248. esp_err_t timer_isr_register(timer_group_t group_num, timer_idx_t timer_num, void (*fn)(void*), void * arg, int intr_alloc_flags, timer_isr_handle_t *handle);
  249. /** @brief Initializes and configure the timer.
  250. *
  251. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  252. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  253. * @param config Pointer to timer initialization parameters.
  254. *
  255. * @return
  256. * - ESP_OK Success
  257. * - ESP_ERR_INVALID_ARG Parameter error
  258. */
  259. esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer_config_t* config);
  260. /** @brief Get timer configure value.
  261. *
  262. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  263. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  264. * @param config Pointer of struct to accept timer parameters.
  265. *
  266. * @return
  267. * - ESP_OK Success
  268. * - ESP_ERR_INVALID_ARG Parameter error
  269. */
  270. esp_err_t timer_get_config(timer_group_t group_num, timer_idx_t timer_num, timer_config_t *config);
  271. /** @brief Enable timer group interrupt, by enable mask
  272. *
  273. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  274. * @param en_mask Timer interrupt enable mask.
  275. * Use TIMG_T0_INT_ENA_M to enable t0 interrupt
  276. * Use TIMG_T1_INT_ENA_M to enable t1 interrupt
  277. *
  278. * @return
  279. * - ESP_OK Success
  280. * - ESP_ERR_INVALID_ARG Parameter error
  281. */
  282. esp_err_t timer_group_intr_enable(timer_group_t group_num, uint32_t en_mask);
  283. /** @brief Disable timer group interrupt, by disable mask
  284. *
  285. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  286. * @param disable_mask Timer interrupt disable mask.
  287. * Use TIMG_T0_INT_ENA_M to disable t0 interrupt
  288. * Use TIMG_T1_INT_ENA_M to disable t1 interrupt
  289. *
  290. * @return
  291. * - ESP_OK Success
  292. * - ESP_ERR_INVALID_ARG Parameter error
  293. */
  294. esp_err_t timer_group_intr_disable(timer_group_t group_num, uint32_t disable_mask);
  295. /** @brief Enable timer interrupt
  296. *
  297. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  298. * @param timer_num Timer index.
  299. *
  300. * @return
  301. * - ESP_OK Success
  302. * - ESP_ERR_INVALID_ARG Parameter error
  303. */
  304. esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num);
  305. /** @brief Disable timer interrupt
  306. *
  307. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  308. * @param timer_num Timer index.
  309. *
  310. * @return
  311. * - ESP_OK Success
  312. * - ESP_ERR_INVALID_ARG Parameter error
  313. */
  314. esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num);
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. #endif /* _TIMER_H_ */