esp_timer.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * @brief Opaque type representing a single esp_timer
  40. */
  41. typedef struct esp_timer* esp_timer_handle_t;
  42. /**
  43. * @brief Timer callback function type
  44. * @param arg pointer to opaque user-specific data
  45. */
  46. typedef void (*esp_timer_cb_t)(void* arg);
  47. /**
  48. * @brief Method for dispatching timer callback
  49. */
  50. typedef enum {
  51. ESP_TIMER_TASK, //!< Callback is called from timer task
  52. /* Not supported for now, provision to allow callbacks to run directly
  53. * from an ISR:
  54. ESP_TIMER_ISR, //!< Callback is called from timer ISR
  55. */
  56. } esp_timer_dispatch_t;
  57. /**
  58. * @brief Timer configuration passed to esp_timer_create
  59. */
  60. typedef struct {
  61. esp_timer_cb_t callback; //!< Function to call when timer expires
  62. void* arg; //!< Argument to pass to the callback
  63. esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
  64. const char* name; //!< Timer name, used in esp_timer_dump function
  65. bool skip_unhandled_events; //!< Skip unhandled events for periodic timers
  66. } esp_timer_create_args_t;
  67. /**
  68. * @brief Minimal initialization of esp_timer
  69. *
  70. * @note This function is called from startup code. Applications do not need
  71. * to call this function before using other esp_timer APIs.
  72. *
  73. * This function can be called very early in startup process, after this call
  74. * only esp_timer_get_time function can be used.
  75. *
  76. * @return
  77. * - ESP_OK on success
  78. */
  79. esp_err_t esp_timer_early_init(void);
  80. /**
  81. * @brief Initialize esp_timer library
  82. *
  83. * @note This function is called from startup code. Applications do not need
  84. * to call this function before using other esp_timer APIs.
  85. * Before calling this function, esp_timer_early_init must be called by the
  86. * startup code.
  87. *
  88. * @return
  89. * - ESP_OK on success
  90. * - ESP_ERR_NO_MEM if allocation has failed
  91. * - ESP_ERR_INVALID_STATE if already initialized
  92. * - other errors from interrupt allocator
  93. */
  94. esp_err_t esp_timer_init(void);
  95. /**
  96. * @brief De-initialize esp_timer library
  97. *
  98. * @note Normally this function should not be called from applications
  99. *
  100. * @return
  101. * - ESP_OK on success
  102. * - ESP_ERR_INVALID_STATE if not yet initialized
  103. */
  104. esp_err_t esp_timer_deinit(void);
  105. /**
  106. * @brief Create an esp_timer instance
  107. *
  108. * @note When done using the timer, delete it with esp_timer_delete function.
  109. *
  110. * @param create_args Pointer to a structure with timer creation arguments.
  111. * Not saved by the library, can be allocated on the stack.
  112. * @param[out] out_handle Output, pointer to esp_timer_handle_t variable which
  113. * will hold the created timer handle.
  114. *
  115. * @return
  116. * - ESP_OK on success
  117. * - ESP_ERR_INVALID_ARG if some of the create_args are not valid
  118. * - ESP_ERR_INVALID_STATE if esp_timer library is not initialized yet
  119. * - ESP_ERR_NO_MEM if memory allocation fails
  120. */
  121. esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
  122. esp_timer_handle_t* out_handle);
  123. /**
  124. * @brief Start one-shot timer
  125. *
  126. * Timer should not be running when this function is called.
  127. *
  128. * @param timer timer handle created using esp_timer_create
  129. * @param timeout_us timer timeout, in microseconds relative to the current moment
  130. * @return
  131. * - ESP_OK on success
  132. * - ESP_ERR_INVALID_ARG if the handle is invalid
  133. * - ESP_ERR_INVALID_STATE if the timer is already running
  134. */
  135. esp_err_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us);
  136. /**
  137. * @brief Start a periodic timer
  138. *
  139. * Timer should not be running when this function is called. This function will
  140. * start the timer which will trigger every 'period' microseconds.
  141. *
  142. * @param timer timer handle created using esp_timer_create
  143. * @param period timer period, in microseconds
  144. * @return
  145. * - ESP_OK on success
  146. * - ESP_ERR_INVALID_ARG if the handle is invalid
  147. * - ESP_ERR_INVALID_STATE if the timer is already running
  148. */
  149. esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
  150. /**
  151. * @brief Stop the timer
  152. *
  153. * This function stops the timer previously started using esp_timer_start_once
  154. * or esp_timer_start_periodic.
  155. *
  156. * @param timer timer handle created using esp_timer_create
  157. * @return
  158. * - ESP_OK on success
  159. * - ESP_ERR_INVALID_STATE if the timer is not running
  160. */
  161. esp_err_t esp_timer_stop(esp_timer_handle_t timer);
  162. /**
  163. * @brief Delete an esp_timer instance
  164. *
  165. * The timer must be stopped before deleting. A one-shot timer which has expired
  166. * does not need to be stopped.
  167. *
  168. * @param timer timer handle allocated using esp_timer_create
  169. * @return
  170. * - ESP_OK on success
  171. * - ESP_ERR_INVALID_STATE if the timer is running
  172. */
  173. esp_err_t esp_timer_delete(esp_timer_handle_t timer);
  174. /**
  175. * @brief Get time in microseconds since boot
  176. * @return number of microseconds since underlying timer has been started
  177. */
  178. int64_t esp_timer_get_time(void);
  179. /**
  180. * @brief Get the timestamp when the next timeout is expected to occur
  181. * @return Timestamp of the nearest timer event, in microseconds.
  182. * The timebase is the same as for the values returned by esp_timer_get_time.
  183. */
  184. int64_t esp_timer_get_next_alarm(void);
  185. /**
  186. * @brief Dump the list of timers to a stream
  187. *
  188. * If CONFIG_ESP_TIMER_PROFILING option is enabled, this prints the list of all
  189. * the existing timers. Otherwise, only the list active timers is printed.
  190. *
  191. * The format is:
  192. *
  193. * name period alarm times_armed times_triggered total_callback_run_time
  194. *
  195. * where:
  196. *
  197. * name — timer name (if CONFIG_ESP_TIMER_PROFILING is defined), or timer pointer
  198. * period — period of timer, in microseconds, or 0 for one-shot timer
  199. * alarm - time of the next alarm, in microseconds since boot, or 0 if the timer
  200. * is not started
  201. *
  202. * The following fields are printed if CONFIG_ESP_TIMER_PROFILING is defined:
  203. *
  204. * times_armed — number of times the timer was armed via esp_timer_start_X
  205. * times_triggered - number of times the callback was called
  206. * total_callback_run_time - total time taken by callback to execute, across all calls
  207. *
  208. * @param stream stream (such as stdout) to dump the information to
  209. * @return
  210. * - ESP_OK on success
  211. * - ESP_ERR_NO_MEM if can not allocate temporary buffer for the output
  212. */
  213. esp_err_t esp_timer_dump(FILE* stream);
  214. /**
  215. * @brief Returns status of a timer, active or not
  216. *
  217. * This function is used to identify if the timer is still active or not.
  218. *
  219. * @param timer timer handle created using esp_timer_create
  220. * @return
  221. * - 1 if timer is still active
  222. * - 0 if timer is not active.
  223. */
  224. bool esp_timer_is_active(esp_timer_handle_t timer);
  225. #ifdef __cplusplus
  226. }
  227. #endif