esp_timer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /**
  8. * @file esp_timer.h
  9. * @brief microsecond-precision 64-bit timer API, replacement for ets_timer
  10. *
  11. * esp_timer APIs allow components to receive callbacks when a hardware timer
  12. * reaches certain value. The timer provides microsecond accuracy and
  13. * up to 64 bit range. Note that while the timer itself provides microsecond
  14. * accuracy, callbacks are dispatched from an auxiliary task. Some time is
  15. * needed to notify this task from timer ISR, and then to invoke the callback.
  16. * If more than one callback needs to be dispatched at any particular time,
  17. * each subsequent callback will be dispatched only when the previous callback
  18. * returns. Therefore, callbacks should not do much work; instead, they should
  19. * use RTOS notification mechanisms (queues, semaphores, event groups, etc.) to
  20. * pass information to other tasks.
  21. *
  22. * To be implemented: it should be possible to request the callback to be called
  23. * directly from the ISR. This reduces the latency, but has potential impact on
  24. * all other callbacks which need to be dispatched. This option should only be
  25. * used for simple callback functions, which do not take longer than a few
  26. * microseconds to run.
  27. *
  28. * Implementation note: on the ESP32, esp_timer APIs use the "legacy" FRC2
  29. * timer. Timer callbacks are called from a task running on the PRO CPU.
  30. */
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <stdbool.h>
  34. #include "esp_err.h"
  35. #include "sdkconfig.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * @brief Opaque type representing a single esp_timer
  41. */
  42. typedef struct esp_timer* esp_timer_handle_t;
  43. /**
  44. * @brief Timer callback function type
  45. * @param arg pointer to opaque user-specific data
  46. */
  47. typedef void (*esp_timer_cb_t)(void* arg);
  48. /**
  49. * @brief Method for dispatching timer callback
  50. */
  51. typedef enum {
  52. ESP_TIMER_TASK, //!< Callback is called from timer task
  53. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  54. ESP_TIMER_ISR, //!< Callback is called from timer ISR
  55. #endif
  56. ESP_TIMER_MAX, //!< Count of the methods for dispatching timer callback
  57. } esp_timer_dispatch_t;
  58. /**
  59. * @brief Timer configuration passed to esp_timer_create
  60. */
  61. typedef struct {
  62. esp_timer_cb_t callback; //!< Function to call when timer expires
  63. void* arg; //!< Argument to pass to the callback
  64. esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
  65. const char* name; //!< Timer name, used in esp_timer_dump function
  66. bool skip_unhandled_events; //!< Skip unhandled events for periodic timers
  67. } esp_timer_create_args_t;
  68. /**
  69. * @brief Minimal initialization of esp_timer
  70. *
  71. * @note This function is called from startup code. Applications do not need
  72. * to call this function before using other esp_timer APIs.
  73. *
  74. * This function can be called very early in startup process, after this call
  75. * only esp_timer_get_time function can be used.
  76. *
  77. * @return
  78. * - ESP_OK on success
  79. */
  80. esp_err_t esp_timer_early_init(void);
  81. /**
  82. * @brief Initialize esp_timer library
  83. *
  84. * @note This function is called from startup code. Applications do not need
  85. * to call this function before using other esp_timer APIs.
  86. * Before calling this function, esp_timer_early_init must be called by the
  87. * startup code.
  88. *
  89. * @return
  90. * - ESP_OK on success
  91. * - ESP_ERR_NO_MEM if allocation has failed
  92. * - ESP_ERR_INVALID_STATE if already initialized
  93. * - other errors from interrupt allocator
  94. */
  95. esp_err_t esp_timer_init(void);
  96. /**
  97. * @brief De-initialize esp_timer library
  98. *
  99. * @note Normally this function should not be called from applications
  100. *
  101. * @return
  102. * - ESP_OK on success
  103. * - ESP_ERR_INVALID_STATE if not yet initialized
  104. */
  105. esp_err_t esp_timer_deinit(void);
  106. /**
  107. * @brief Create an esp_timer instance
  108. *
  109. * @note When done using the timer, delete it with esp_timer_delete function.
  110. *
  111. * @param create_args Pointer to a structure with timer creation arguments.
  112. * Not saved by the library, can be allocated on the stack.
  113. * @param[out] out_handle Output, pointer to esp_timer_handle_t variable which
  114. * will hold the created timer handle.
  115. *
  116. * @return
  117. * - ESP_OK on success
  118. * - ESP_ERR_INVALID_ARG if some of the create_args are not valid
  119. * - ESP_ERR_INVALID_STATE if esp_timer library is not initialized yet
  120. * - ESP_ERR_NO_MEM if memory allocation fails
  121. */
  122. esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
  123. esp_timer_handle_t* out_handle);
  124. /**
  125. * @brief Start one-shot timer
  126. *
  127. * Timer should not be running when this function is called.
  128. *
  129. * @param timer timer handle created using esp_timer_create
  130. * @param timeout_us timer timeout, in microseconds relative to the current moment
  131. * @return
  132. * - ESP_OK on success
  133. * - ESP_ERR_INVALID_ARG if the handle is invalid
  134. * - ESP_ERR_INVALID_STATE if the timer is already running
  135. */
  136. esp_err_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us);
  137. /**
  138. * @brief Start a periodic timer
  139. *
  140. * Timer should not be running when this function is called. This function will
  141. * start the timer which will trigger every 'period' microseconds.
  142. *
  143. * @param timer timer handle created using esp_timer_create
  144. * @param period timer period, in microseconds
  145. * @return
  146. * - ESP_OK on success
  147. * - ESP_ERR_INVALID_ARG if the handle is invalid
  148. * - ESP_ERR_INVALID_STATE if the timer is already running
  149. */
  150. esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
  151. /**
  152. * @brief Stop the timer
  153. *
  154. * This function stops the timer previously started using esp_timer_start_once
  155. * or esp_timer_start_periodic.
  156. *
  157. * @param timer timer handle created using esp_timer_create
  158. * @return
  159. * - ESP_OK on success
  160. * - ESP_ERR_INVALID_STATE if the timer is not running
  161. */
  162. esp_err_t esp_timer_stop(esp_timer_handle_t timer);
  163. /**
  164. * @brief Delete an esp_timer instance
  165. *
  166. * The timer must be stopped before deleting. A one-shot timer which has expired
  167. * does not need to be stopped.
  168. *
  169. * @param timer timer handle allocated using esp_timer_create
  170. * @return
  171. * - ESP_OK on success
  172. * - ESP_ERR_INVALID_STATE if the timer is running
  173. */
  174. esp_err_t esp_timer_delete(esp_timer_handle_t timer);
  175. /**
  176. * @brief Get time in microseconds since boot
  177. * @return number of microseconds since underlying timer has been started
  178. */
  179. int64_t esp_timer_get_time(void);
  180. /**
  181. * @brief Get the timestamp when the next timeout is expected to occur
  182. * @return Timestamp of the nearest timer event, in microseconds.
  183. * The timebase is the same as for the values returned by esp_timer_get_time.
  184. */
  185. int64_t esp_timer_get_next_alarm(void);
  186. /**
  187. * @brief Get the timestamp when the next timeout is expected to occur skipping those which have skip_unhandled_events flag
  188. * @return Timestamp of the nearest timer event, in microseconds.
  189. * The timebase is the same as for the values returned by esp_timer_get_time.
  190. */
  191. int64_t esp_timer_get_next_alarm_for_wake_up(void);
  192. /**
  193. * @brief Dump the list of timers to a stream
  194. *
  195. * If CONFIG_ESP_TIMER_PROFILING option is enabled, this prints the list of all
  196. * the existing timers. Otherwise, only the list active timers is printed.
  197. *
  198. * The format is:
  199. *
  200. * name period alarm times_armed times_triggered total_callback_run_time
  201. *
  202. * where:
  203. *
  204. * name — timer name (if CONFIG_ESP_TIMER_PROFILING is defined), or timer pointer
  205. * period — period of timer, in microseconds, or 0 for one-shot timer
  206. * alarm - time of the next alarm, in microseconds since boot, or 0 if the timer
  207. * is not started
  208. *
  209. * The following fields are printed if CONFIG_ESP_TIMER_PROFILING is defined:
  210. *
  211. * times_armed — number of times the timer was armed via esp_timer_start_X
  212. * times_triggered - number of times the callback was called
  213. * total_callback_run_time - total time taken by callback to execute, across all calls
  214. *
  215. * @param stream stream (such as stdout) to dump the information to
  216. * @return
  217. * - ESP_OK on success
  218. * - ESP_ERR_NO_MEM if can not allocate temporary buffer for the output
  219. */
  220. esp_err_t esp_timer_dump(FILE* stream);
  221. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  222. /**
  223. * @brief Requests a context switch from a timer callback function.
  224. *
  225. * This only works for a timer that has an ISR dispatch method.
  226. * The context switch will be called after all ISR dispatch timers have been processed.
  227. */
  228. void esp_timer_isr_dispatch_need_yield(void);
  229. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  230. /**
  231. * @brief Returns status of a timer, active or not
  232. *
  233. * This function is used to identify if the timer is still active or not.
  234. *
  235. * @param timer timer handle created using esp_timer_create
  236. * @return
  237. * - 1 if timer is still active
  238. * - 0 if timer is not active.
  239. */
  240. bool esp_timer_is_active(esp_timer_handle_t timer);
  241. #ifdef __cplusplus
  242. }
  243. #endif