esp_timer.h 9.4 KB

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