esp_timer.h 7.9 KB

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