esp_timer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 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. * Timer callbacks are called from a task running on the PRO CPU.
  29. */
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include <stdbool.h>
  33. #include "esp_err.h"
  34. #include "esp_etm.h"
  35. #include "sdkconfig.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * @brief Opaque type representing a single esp_timer
  41. */
  42. typedef struct esp_timer* esp_timer_handle_t;
  43. /**
  44. * @brief Timer callback function type
  45. * @param arg pointer to opaque user-specific data
  46. */
  47. typedef void (*esp_timer_cb_t)(void* arg);
  48. /**
  49. * @brief Method for dispatching timer callback
  50. */
  51. typedef enum {
  52. ESP_TIMER_TASK, //!< Callback is called from timer task
  53. #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
  54. ESP_TIMER_ISR, //!< Callback is called from timer ISR
  55. #endif
  56. ESP_TIMER_MAX, //!< Count of the methods for dispatching timer callback
  57. } esp_timer_dispatch_t;
  58. /**
  59. * @brief Timer configuration passed to esp_timer_create
  60. */
  61. typedef struct {
  62. esp_timer_cb_t callback; //!< Function to call when timer expires
  63. void* arg; //!< Argument to pass to the callback
  64. esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
  65. const char* name; //!< Timer name, used in esp_timer_dump function
  66. bool skip_unhandled_events; //!< Skip unhandled events for periodic timers
  67. } esp_timer_create_args_t;
  68. /**
  69. * @brief Minimal initialization of esp_timer
  70. *
  71. * @note This function is called from startup code. Applications do not need
  72. * to call this function before using other esp_timer APIs.
  73. *
  74. * This function can be called very early in startup process, after this call
  75. * only esp_timer_get_time function can be used.
  76. *
  77. * @return
  78. * - ESP_OK on success
  79. */
  80. esp_err_t esp_timer_early_init(void);
  81. /**
  82. * @brief Initialize esp_timer library
  83. *
  84. * @note This function is called from startup code. Applications do not need
  85. * to call this function before using other esp_timer APIs.
  86. * Before calling this function, esp_timer_early_init must be called by the
  87. * startup code.
  88. *
  89. * This function will be called from startup code on every core
  90. * if CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY is enabled,
  91. * It allocates the timer ISR on MULTIPLE cores and
  92. * creates the timer task which can be run on any core.
  93. *
  94. * @return
  95. * - ESP_OK on success
  96. * - ESP_ERR_NO_MEM if allocation has failed
  97. * - ESP_ERR_INVALID_STATE if already initialized
  98. * - other errors from interrupt allocator
  99. */
  100. esp_err_t esp_timer_init(void);
  101. /**
  102. * @brief De-initialize esp_timer library
  103. *
  104. * @note Normally this function should not be called from applications
  105. *
  106. * @return
  107. * - ESP_OK on success
  108. * - ESP_ERR_INVALID_STATE if not yet initialized
  109. */
  110. esp_err_t esp_timer_deinit(void);
  111. /**
  112. * @brief Create an esp_timer instance
  113. *
  114. * @note When done using the timer, delete it with esp_timer_delete function.
  115. *
  116. * @param create_args Pointer to a structure with timer creation arguments.
  117. * Not saved by the library, can be allocated on the stack.
  118. * @param[out] out_handle Output, pointer to esp_timer_handle_t variable which
  119. * will hold the created timer handle.
  120. *
  121. * @return
  122. * - ESP_OK on success
  123. * - ESP_ERR_INVALID_ARG if some of the create_args are not valid
  124. * - ESP_ERR_INVALID_STATE if esp_timer library is not initialized yet
  125. * - ESP_ERR_NO_MEM if memory allocation fails
  126. */
  127. esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
  128. esp_timer_handle_t* out_handle);
  129. /**
  130. * @brief Start one-shot timer
  131. *
  132. * Timer should not be running when this function is called.
  133. *
  134. * @param timer timer handle created using esp_timer_create
  135. * @param timeout_us timer timeout, in microseconds relative to the current moment
  136. * @return
  137. * - ESP_OK on success
  138. * - ESP_ERR_INVALID_ARG if the handle is invalid
  139. * - ESP_ERR_INVALID_STATE if the timer is already running
  140. */
  141. esp_err_t esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us);
  142. /**
  143. * @brief Start a periodic timer
  144. *
  145. * Timer should not be running when this function is called. This function will
  146. * start the timer which will trigger every 'period' microseconds.
  147. *
  148. * @param timer timer handle created using esp_timer_create
  149. * @param period timer period, in microseconds
  150. * @return
  151. * - ESP_OK on success
  152. * - ESP_ERR_INVALID_ARG if the handle is invalid
  153. * - ESP_ERR_INVALID_STATE if the timer is already running
  154. */
  155. esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);
  156. /**
  157. * @brief Restart a currently running timer
  158. *
  159. * If the given timer is a one-shot timer, the timer is restarted immediately and will timeout once in `timeout_us` microseconds.
  160. * If the given timer is a periodic timer, the timer is restarted immediately with a new period of `timeout_us` microseconds.
  161. *
  162. * @param timer timer Handle created using esp_timer_create
  163. * @param timeout_us Timeout, in microseconds relative to the current time.
  164. * In case of a periodic timer, also represents the new period.
  165. * @return
  166. * - ESP_OK on success
  167. * - ESP_ERR_INVALID_ARG if the handle is invalid
  168. * - ESP_ERR_INVALID_STATE if the timer is not running
  169. */
  170. esp_err_t esp_timer_restart(esp_timer_handle_t timer, uint64_t timeout_us);
  171. /**
  172. * @brief Stop the timer
  173. *
  174. * This function stops the timer previously started using esp_timer_start_once
  175. * or esp_timer_start_periodic.
  176. *
  177. * @param timer timer handle created using esp_timer_create
  178. * @return
  179. * - ESP_OK on success
  180. * - ESP_ERR_INVALID_STATE if the timer is not running
  181. */
  182. esp_err_t esp_timer_stop(esp_timer_handle_t timer);
  183. /**
  184. * @brief Delete an esp_timer instance
  185. *
  186. * The timer must be stopped before deleting. A one-shot timer which has expired
  187. * does not need to be stopped.
  188. *
  189. * @param timer timer handle allocated using esp_timer_create
  190. * @return
  191. * - ESP_OK on success
  192. * - ESP_ERR_INVALID_STATE if the timer is running
  193. */
  194. esp_err_t esp_timer_delete(esp_timer_handle_t timer);
  195. /**
  196. * @brief Get time in microseconds since boot
  197. * @return number of microseconds since underlying timer has been started
  198. */
  199. int64_t esp_timer_get_time(void);
  200. /**
  201. * @brief Get the timestamp when the next timeout is expected to occur
  202. * @return Timestamp of the nearest timer event, in microseconds.
  203. * The timebase is the same as for the values returned by esp_timer_get_time.
  204. */
  205. int64_t esp_timer_get_next_alarm(void);
  206. /**
  207. * @brief Get the timestamp when the next timeout is expected to occur skipping those which have skip_unhandled_events flag
  208. * @return Timestamp of the nearest timer event, in microseconds.
  209. * The timebase is the same as for the values returned by esp_timer_get_time.
  210. */
  211. int64_t esp_timer_get_next_alarm_for_wake_up(void);
  212. /**
  213. * @brief Get the period of a timer
  214. *
  215. * This function fetches the timeout period of a timer.
  216. *
  217. * @note The timeout period is the time interval with which a timer restarts after expiry. For one-shot timers, the
  218. * period is 0 as there is no periodicity associated with such timers.
  219. *
  220. * @param timer timer handle allocated using esp_timer_create
  221. * @param period memory to store the timer period value in microseconds
  222. * @return
  223. * - ESP_OK on success
  224. * - ESP_ERR_INVALID_ARG if the arguments are invalid
  225. */
  226. esp_err_t esp_timer_get_period(esp_timer_handle_t timer, uint64_t *period);
  227. /**
  228. * @brief Get the expiry time of a one-shot timer
  229. *
  230. * This function fetches the expiry time of a one-shot timer.
  231. *
  232. * @note This API returns a valid expiry time only for a one-shot timer. It returns an error if the timer handle passed
  233. * to the function is for a periodic timer.
  234. *
  235. * @param timer timer handle allocated using esp_timer_create
  236. * @param expiry memory to store the timeout value in microseconds
  237. * @return
  238. * - ESP_OK on success
  239. * - ESP_ERR_INVALID_ARG if the arguments are invalid
  240. * - ESP_ERR_NOT_SUPPORTED if the timer type is periodic
  241. */
  242. esp_err_t esp_timer_get_expiry_time(esp_timer_handle_t timer, uint64_t *expiry);
  243. /**
  244. * @brief Dump the list of timers to a stream
  245. *
  246. * If CONFIG_ESP_TIMER_PROFILING option is enabled, this prints the list of all
  247. * the existing timers. Otherwise, only the list active timers is printed.
  248. *
  249. * The format is:
  250. *
  251. * name period alarm times_armed times_triggered total_callback_run_time
  252. *
  253. * where:
  254. *
  255. * name — timer name (if CONFIG_ESP_TIMER_PROFILING is defined), or timer pointer
  256. * period — period of timer, in microseconds, or 0 for one-shot timer
  257. * alarm - time of the next alarm, in microseconds since boot, or 0 if the timer
  258. * is not started
  259. *
  260. * The following fields are printed if CONFIG_ESP_TIMER_PROFILING is defined:
  261. *
  262. * times_armed — number of times the timer was armed via esp_timer_start_X
  263. * times_triggered - number of times the callback was called
  264. * total_callback_run_time - total time taken by callback to execute, across all calls
  265. *
  266. * @param stream stream (such as stdout) to dump the information to
  267. * @return
  268. * - ESP_OK on success
  269. * - ESP_ERR_NO_MEM if can not allocate temporary buffer for the output
  270. */
  271. esp_err_t esp_timer_dump(FILE* stream);
  272. #if CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD || defined __DOXYGEN__
  273. /**
  274. * @brief Requests a context switch from a timer callback function.
  275. *
  276. * This only works for a timer that has an ISR dispatch method.
  277. * The context switch will be called after all ISR dispatch timers have been processed.
  278. */
  279. void esp_timer_isr_dispatch_need_yield(void);
  280. #endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD || defined __DOXYGEN__
  281. /**
  282. * @brief Returns status of a timer, active or not
  283. *
  284. * This function is used to identify if the timer is still active or not.
  285. *
  286. * @param timer timer handle created using esp_timer_create
  287. * @return
  288. * - 1 if timer is still active
  289. * - 0 if timer is not active.
  290. */
  291. bool esp_timer_is_active(esp_timer_handle_t timer);
  292. /**
  293. * @brief Get the ETM event handle of esp_timer underlying alarm event
  294. *
  295. * @note The created ETM event object can be deleted later by calling `esp_etm_del_event`
  296. *
  297. * @note The ETM event is generated by the underlying hardware -- systimer,
  298. * therefore, if the esp_timer is not clocked by systimer, then no ETM event will be generated.
  299. *
  300. * @param[out] out_event Returned ETM event handle
  301. * @return
  302. * - ESP_OK Success
  303. * - ESP_ERR_INVALID_ARG Parameter error
  304. */
  305. esp_err_t esp_timer_new_etm_alarm_event(esp_etm_event_handle_t *out_event);
  306. #ifdef __cplusplus
  307. }
  308. #endif