timer.h 15 KB

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