timer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Copyright 2010-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. // 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. #pragma once
  14. #include "esp_err.h"
  15. #include "esp_attr.h"
  16. #include "soc/soc.h"
  17. #include "soc/timer_periph.h"
  18. #include "esp_intr_alloc.h"
  19. #include "hal/timer_types.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define TIMER_BASE_CLK (APB_CLK_FREQ) /*!< Frequency of the clock on the input of the timer groups */
  24. /**
  25. * @brief Interrupt handle callback function. User need to retrun a bool value
  26. * in callback.
  27. *
  28. * @return
  29. * - True Do task yield at the end of ISR
  30. * - False Not do task yield at the end of ISR
  31. *
  32. * @note If you called FreeRTOS functions in callback, you need to return true or false based on
  33. * the retrun value of argument `pxHigherPriorityTaskWoken`.
  34. * For example, `xQueueSendFromISR` is called in callback, if the return value `pxHigherPriorityTaskWoken`
  35. * of any FreeRTOS calls is pdTRUE, return true; otherwise return false.
  36. */
  37. typedef bool (*timer_isr_t)(void *);
  38. /**
  39. * @brief Interrupt handle, used in order to free the isr after use.
  40. * Aliases to an int handle for now.
  41. */
  42. typedef intr_handle_t timer_isr_handle_t;
  43. /**
  44. * @brief Read the counter value of hardware timer.
  45. *
  46. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  47. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  48. * @param timer_val Pointer to accept timer counter value.
  49. *
  50. * @return
  51. * - ESP_OK Success
  52. * - ESP_ERR_INVALID_ARG Parameter error
  53. */
  54. esp_err_t timer_get_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *timer_val);
  55. /**
  56. * @brief Read the counter value of hardware timer, in unit of a given scale.
  57. *
  58. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  59. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  60. * @param time Pointer, type of double*, to accept timer counter value, in seconds.
  61. *
  62. * @return
  63. * - ESP_OK Success
  64. * - ESP_ERR_INVALID_ARG Parameter error
  65. */
  66. esp_err_t timer_get_counter_time_sec(timer_group_t group_num, timer_idx_t timer_num, double *time);
  67. /**
  68. * @brief Set counter value to hardware timer.
  69. *
  70. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  71. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  72. * @param load_val Counter value to write to the hardware timer.
  73. *
  74. * @return
  75. * - ESP_OK Success
  76. * - ESP_ERR_INVALID_ARG Parameter error
  77. */
  78. esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val);
  79. /**
  80. * @brief Start the counter of hardware timer.
  81. *
  82. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  83. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  84. *
  85. * @return
  86. * - ESP_OK Success
  87. * - ESP_ERR_INVALID_ARG Parameter error
  88. */
  89. esp_err_t timer_start(timer_group_t group_num, timer_idx_t timer_num);
  90. /**
  91. * @brief Pause the counter of hardware timer.
  92. *
  93. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  94. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  95. *
  96. * @return
  97. * - ESP_OK Success
  98. * - ESP_ERR_INVALID_ARG Parameter error
  99. */
  100. esp_err_t timer_pause(timer_group_t group_num, timer_idx_t timer_num);
  101. /**
  102. * @brief Set counting mode for hardware timer.
  103. *
  104. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  105. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  106. * @param counter_dir Counting direction of timer, count-up or count-down
  107. *
  108. * @return
  109. * - ESP_OK Success
  110. * - ESP_ERR_INVALID_ARG Parameter error
  111. */
  112. esp_err_t timer_set_counter_mode(timer_group_t group_num, timer_idx_t timer_num, timer_count_dir_t counter_dir);
  113. /**
  114. * @brief Enable or disable counter reload function when alarm event occurs.
  115. *
  116. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  117. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  118. * @param reload Counter reload mode.
  119. *
  120. * @return
  121. * - ESP_OK Success
  122. * - ESP_ERR_INVALID_ARG Parameter error
  123. */
  124. esp_err_t timer_set_auto_reload(timer_group_t group_num, timer_idx_t timer_num, timer_autoreload_t reload);
  125. /**
  126. * @brief Set hardware timer source clock divider. Timer groups clock are divider from APB clock.
  127. *
  128. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  129. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  130. * @param divider Timer clock divider value. The divider's range is from from 2 to 65536.
  131. *
  132. * @return
  133. * - ESP_OK Success
  134. * - ESP_ERR_INVALID_ARG Parameter error
  135. */
  136. esp_err_t timer_set_divider(timer_group_t group_num, timer_idx_t timer_num, uint32_t divider);
  137. /**
  138. * @brief Set timer alarm value.
  139. *
  140. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  141. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  142. * @param alarm_value A 64-bit value to set the alarm value.
  143. *
  144. * @return
  145. * - ESP_OK Success
  146. * - ESP_ERR_INVALID_ARG Parameter error
  147. */
  148. esp_err_t timer_set_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_value);
  149. /**
  150. * @brief Get timer alarm value.
  151. *
  152. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  153. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  154. * @param alarm_value Pointer of A 64-bit value to accept the alarm value.
  155. *
  156. * @return
  157. * - ESP_OK Success
  158. * - ESP_ERR_INVALID_ARG Parameter error
  159. */
  160. esp_err_t timer_get_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *alarm_value);
  161. /**
  162. * @brief Enable or disable generation of timer alarm events.
  163. *
  164. * @param group_num Timer group, 0 for TIMERG0 or 1 for TIMERG1
  165. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  166. * @param alarm_en To enable or disable timer alarm function.
  167. *
  168. * @return
  169. * - ESP_OK Success
  170. * - ESP_ERR_INVALID_ARG Parameter error
  171. */
  172. esp_err_t timer_set_alarm(timer_group_t group_num, timer_idx_t timer_num, timer_alarm_t alarm_en);
  173. /**
  174. * @brief Add ISR handle callback for the corresponding timer.
  175. *
  176. * @param group_num Timer group number
  177. * @param timer_num Timer index of timer group
  178. * @param isr_handler Interrupt handler function, it is a callback function.
  179. * @param arg Parameter for handler function
  180. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  181. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  182. *
  183. * @note This ISR handler will be called from an ISR.
  184. * This ISR handler do not need to handle interrupt status, and should be kept short.
  185. * If you want to realize some specific applications or write the whole ISR, you can
  186. * call timer_isr_register(...) to register ISR.
  187. *
  188. * The callback should return a bool value to determine whether need to do YIELD at
  189. * the end of the ISR.
  190. *
  191. * If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set,
  192. * the handler function must be declared with IRAM_ATTR attribute
  193. * and can only call functions in IRAM or ROM. It cannot call other timer APIs.
  194. *
  195. * @return
  196. * - ESP_OK Success
  197. * - ESP_ERR_INVALID_ARG Parameter error
  198. */
  199. 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);
  200. /**
  201. * @brief Remove ISR handle callback for the corresponding timer.
  202. *
  203. * @param group_num Timer group number
  204. * @param timer_num Timer index of timer group
  205. *
  206. * @return
  207. * - ESP_OK Success
  208. * - ESP_ERR_INVALID_ARG Parameter error
  209. */
  210. esp_err_t timer_isr_callback_remove(timer_group_t group_num, timer_idx_t timer_num);
  211. /**
  212. * @brief Register Timer interrupt handler, the handler is an ISR.
  213. * The handler will be attached to the same CPU core that this function is running on.
  214. *
  215. * @param group_num Timer group number
  216. * @param timer_num Timer index of timer group
  217. * @param fn Interrupt handler function.
  218. * @param arg Parameter for handler function
  219. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  220. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  221. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  222. * be returned here.
  223. *
  224. * @note If use this function to reigster ISR, you need to write the whole ISR.
  225. * In the interrupt handler, you need to call timer_spinlock_take(..) before
  226. * your handling, and call timer_spinlock_give(...) after your handling.
  227. *
  228. * If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set,
  229. * the handler function must be declared with IRAM_ATTR attribute
  230. * and can only call functions in IRAM or ROM. It cannot call other timer APIs.
  231. * Use direct register access to configure timers from inside the ISR in this case.
  232. *
  233. * @return
  234. * - ESP_OK Success
  235. * - ESP_ERR_INVALID_ARG Parameter error
  236. */
  237. 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);
  238. /** @brief Initializes and configure the timer.
  239. *
  240. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  241. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  242. * @param config Pointer to timer initialization parameters.
  243. *
  244. * @return
  245. * - ESP_OK Success
  246. * - ESP_ERR_INVALID_ARG Parameter error
  247. */
  248. esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer_config_t *config);
  249. /** @brief Deinitializes 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. *
  254. * @return
  255. * - ESP_OK Success
  256. * - ESP_ERR_INVALID_ARG Parameter error
  257. */
  258. esp_err_t timer_deinit(timer_group_t group_num, timer_idx_t timer_num);
  259. /** @brief Get timer configure value.
  260. *
  261. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  262. * @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
  263. * @param config Pointer of struct to accept timer parameters.
  264. *
  265. * @return
  266. * - ESP_OK Success
  267. * - ESP_ERR_INVALID_ARG Parameter error
  268. */
  269. esp_err_t timer_get_config(timer_group_t group_num, timer_idx_t timer_num, timer_config_t *config);
  270. /** @brief Enable timer group interrupt, by enable mask
  271. *
  272. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  273. * @param intr_mask Timer interrupt enable mask.
  274. * - TIMER_INTR_T0: t0 interrupt
  275. * - TIMER_INTR_T1: t1 interrupt
  276. * - TIMER_INTR_WDT: watchdog 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, timer_intr_t intr_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 intr_mask Timer interrupt disable mask.
  287. * - TIMER_INTR_T0: t0 interrupt
  288. * - TIMER_INTR_T1: t1 interrupt
  289. * - TIMER_INTR_WDT: watchdog interrupt
  290. *
  291. * @return
  292. * - ESP_OK Success
  293. * - ESP_ERR_INVALID_ARG Parameter error
  294. */
  295. esp_err_t timer_group_intr_disable(timer_group_t group_num, timer_intr_t intr_mask);
  296. /** @brief Enable timer interrupt
  297. *
  298. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  299. * @param timer_num Timer index.
  300. *
  301. * @return
  302. * - ESP_OK Success
  303. * - ESP_ERR_INVALID_ARG Parameter error
  304. */
  305. esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num);
  306. /** @brief Disable timer interrupt
  307. *
  308. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  309. * @param timer_num Timer index.
  310. *
  311. * @return
  312. * - ESP_OK Success
  313. * - ESP_ERR_INVALID_ARG Parameter error
  314. */
  315. esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num);
  316. /** @brief Clear timer interrupt status, just used in ISR
  317. *
  318. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  319. * @param timer_num Timer index.
  320. *
  321. */
  322. void timer_group_intr_clr_in_isr(timer_group_t group_num, timer_idx_t timer_num) __attribute__((deprecated));
  323. /** @brief Clear timer interrupt status, just used in ISR
  324. *
  325. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  326. * @param timer_num Timer index.
  327. *
  328. */
  329. void timer_group_clr_intr_status_in_isr(timer_group_t group_num, timer_idx_t timer_num);
  330. /** @brief Enable alarm interrupt, just used in ISR
  331. *
  332. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  333. * @param timer_num Timer index.
  334. *
  335. */
  336. void timer_group_enable_alarm_in_isr(timer_group_t group_num, timer_idx_t timer_num);
  337. /** @brief Get the current counter value, just used in ISR
  338. *
  339. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  340. * @param timer_num Timer index.
  341. *
  342. * @return
  343. * - Counter value
  344. */
  345. uint64_t timer_group_get_counter_value_in_isr(timer_group_t group_num, timer_idx_t timer_num);
  346. /** @brief Set the alarm threshold for the timer, just used in ISR
  347. *
  348. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  349. * @param timer_num Timer index.
  350. * @param alarm_val Alarm threshold.
  351. *
  352. */
  353. void timer_group_set_alarm_value_in_isr(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_val);
  354. /** @brief Enable/disable a counter, just used in ISR
  355. *
  356. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  357. * @param timer_num Timer index.
  358. * @param counter_en Enable/disable.
  359. *
  360. */
  361. void timer_group_set_counter_enable_in_isr(timer_group_t group_num, timer_idx_t timer_num, timer_start_t counter_en);
  362. /** @brief Get the masked interrupt status, just used in ISR
  363. *
  364. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  365. *
  366. * @return
  367. * - Interrupt status
  368. */
  369. timer_intr_t timer_group_intr_get_in_isr(timer_group_t group_num) __attribute__((deprecated));
  370. /** @brief Get interrupt status, just used in ISR
  371. *
  372. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  373. *
  374. * @return
  375. * - Interrupt status
  376. */
  377. uint32_t timer_group_get_intr_status_in_isr(timer_group_t group_num);
  378. /** @brief Clear the masked interrupt status, just used in ISR
  379. *
  380. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  381. * @param intr_mask Masked interrupt.
  382. *
  383. */
  384. void timer_group_clr_intr_sta_in_isr(timer_group_t group_num, timer_intr_t intr_mask) __attribute__((deprecated));
  385. /** @brief Get auto reload enable status, just used in ISR
  386. *
  387. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  388. * @param timer_num Timer index
  389. *
  390. * @return
  391. * - True Auto reload enabled
  392. * - False Auto reload disabled
  393. */
  394. bool timer_group_get_auto_reload_in_isr(timer_group_t group_num, timer_idx_t timer_num);
  395. /** @brief Take timer spinlock to enter critical protect
  396. *
  397. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  398. *
  399. * @return
  400. * - ESP_OK Success
  401. * - ESP_ERR_INVALID_ARG Parameter error
  402. */
  403. esp_err_t timer_spinlock_take(timer_group_t group_num);
  404. /** @brief Give timer spinlock to exit critical protect
  405. *
  406. * @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
  407. *
  408. * @return
  409. * - ESP_OK Success
  410. * - ESP_ERR_INVALID_ARG Parameter error
  411. */
  412. esp_err_t timer_spinlock_give(timer_group_t group_num);
  413. #ifdef __cplusplus
  414. }
  415. #endif