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