esp_timer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Initialize esp_timer library
  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. * @return
  83. * - ESP_OK on success
  84. * - ESP_ERR_NO_MEM if allocation has failed
  85. * - ESP_ERR_INVALID_STATE if already initialized
  86. * - other errors from interrupt allocator
  87. */
  88. esp_err_t esp_timer_init(void);
  89. /**
  90. * @brief De-initialize esp_timer library
  91. *
  92. * @note Normally this function should not be called from applications
  93. *
  94. * @return
  95. * - ESP_OK on success
  96. * - ESP_ERR_INVALID_STATE if not yet initialized
  97. */
  98. esp_err_t esp_timer_deinit(void);
  99. /**
  100. * @brief Create an esp_timer instance
  101. *
  102. * @note When done using the timer, delete it with esp_timer_delete function.
  103. *
  104. * @param create_args Pointer to a structure with timer creation arguments.
  105. * Not saved by the library, can be allocated on the stack.
  106. * @param[out] out_handle Output, pointer to esp_timer_handle_t variable which
  107. * will hold the created timer handle.
  108. *
  109. * @return
  110. * - ESP_OK on success
  111. * - ESP_ERR_INVALID_ARG if some of the create_args are not valid
  112. * - ESP_ERR_INVALID_STATE if esp_timer library is not initialized yet
  113. * - ESP_ERR_NO_MEM if memory allocation fails
  114. */
  115. esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
  116. esp_timer_handle_t* out_handle);
  117. /**
  118. * @brief Start one-shot timer
  119. *
  120. * Timer should not be running when this function is called.
  121. *
  122. * @param timer timer handle created using esp_timer_create
  123. * @param timeout_us timer timeout, in microseconds relative to the current moment
  124. * @return
  125. * - ESP_OK on success
  126. * - ESP_ERR_INVALID_ARG if the handle is invalid
  127. * - ESP_ERR_INVALID_STATE if the timer is already running
  128. */
  129. esp_err_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us);
  130. /**
  131. * @brief Start a periodic timer
  132. *
  133. * Timer should not be running when this function is called. This function will
  134. * start the timer which will trigger every 'period' microseconds.
  135. *
  136. * @param timer timer handle created using esp_timer_create
  137. * @param period timer period, in microseconds
  138. * @return
  139. * - ESP_OK on success
  140. * - ESP_ERR_INVALID_ARG if the handle is invalid
  141. * - ESP_ERR_INVALID_STATE if the timer is already running
  142. */
  143. esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
  144. /**
  145. * @brief Stop the timer
  146. *
  147. * This function stops the timer previously started using esp_timer_start_once
  148. * or esp_timer_start_periodic.
  149. *
  150. * @param timer timer handle created using esp_timer_create
  151. * @return
  152. * - ESP_OK on success
  153. * - ESP_ERR_INVALID_STATE if the timer is not running
  154. */
  155. esp_err_t esp_timer_stop(esp_timer_handle_t timer);
  156. /**
  157. * @brief Delete an esp_timer instance
  158. *
  159. * The timer must be stopped before deleting. A one-shot timer which has expired
  160. * does not need to be stopped.
  161. *
  162. * @param timer timer handle allocated using esp_timer_create
  163. * @return
  164. * - ESP_OK on success
  165. * - ESP_ERR_INVALID_STATE if the timer is running
  166. */
  167. esp_err_t esp_timer_delete(esp_timer_handle_t timer);
  168. /**
  169. * @brief Get time in microseconds since boot
  170. * @return number of microseconds since underlying timer has been started
  171. */
  172. int64_t esp_timer_get_time(void);
  173. /**
  174. * @brief Get the timestamp when the next timeout is expected to occur
  175. * @return Timestamp of the nearest timer event, in microseconds.
  176. * The timebase is the same as for the values returned by esp_timer_get_time.
  177. */
  178. int64_t esp_timer_get_next_alarm(void);
  179. /**
  180. * @brief Get the timestamp when the next timeout is expected to occur skipping those which have skip_unhandled_events flag
  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_for_wake_up(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. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  215. /**
  216. * @brief Requests a context switch from a timer callback function.
  217. *
  218. * This only works for a timer that has an ISR dispatch method.
  219. * The context switch will be called after all ISR dispatch timers have been processed.
  220. */
  221. void esp_timer_isr_dispatch_need_yield(void);
  222. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  223. /**
  224. * @brief Returns status of a timer, active or not
  225. *
  226. * This function is used to identify if the timer is still active or not.
  227. *
  228. * @param timer timer handle created using esp_timer_create
  229. * @return
  230. * - 1 if timer is still active
  231. * - 0 if timer is not active.
  232. */
  233. bool esp_timer_is_active(esp_timer_handle_t timer);
  234. #ifdef __cplusplus
  235. }
  236. #endif