gptimer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "driver/gptimer_types.h"
  11. #include "driver/gptimer_etm.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @brief General Purpose Timer configuration
  17. */
  18. typedef struct {
  19. gptimer_clock_source_t clk_src; /*!< GPTimer clock source */
  20. gptimer_count_direction_t direction; /*!< Count direction */
  21. uint32_t resolution_hz; /*!< Counter resolution (working frequency) in Hz,
  22. hence, the step size of each count tick equals to (1 / resolution_hz) seconds */
  23. int intr_priority; /*!< GPTimer interrupt priority,
  24. if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
  25. struct {
  26. uint32_t intr_shared: 1; /*!< Set true, the timer interrupt number can be shared with other peripherals */
  27. } flags; /*!< GPTimer config flags*/
  28. } gptimer_config_t;
  29. /**
  30. * @brief Create a new General Purpose Timer, and return the handle
  31. *
  32. * @note The newly created timer is put in the "init" state.
  33. *
  34. * @param[in] config GPTimer configuration
  35. * @param[out] ret_timer Returned timer handle
  36. * @return
  37. * - ESP_OK: Create GPTimer successfully
  38. * - ESP_ERR_INVALID_ARG: Create GPTimer failed because of invalid argument
  39. * - ESP_ERR_NO_MEM: Create GPTimer failed because out of memory
  40. * - ESP_ERR_NOT_FOUND: Create GPTimer failed because all hardware timers are used up and no more free one
  41. * - ESP_FAIL: Create GPTimer failed because of other error
  42. */
  43. esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *ret_timer);
  44. /**
  45. * @brief Delete the GPTimer handle
  46. *
  47. * @note A timer must be in the "init" state before it can be deleted.
  48. *
  49. * @param[in] timer Timer handle created by `gptimer_new_timer`
  50. * @return
  51. * - ESP_OK: Delete GPTimer successfully
  52. * - ESP_ERR_INVALID_ARG: Delete GPTimer failed because of invalid argument
  53. * - ESP_ERR_INVALID_STATE: Delete GPTimer failed because the timer is not in init state
  54. * - ESP_FAIL: Delete GPTimer failed because of other error
  55. */
  56. esp_err_t gptimer_del_timer(gptimer_handle_t timer);
  57. /**
  58. * @brief Set GPTimer raw count value
  59. *
  60. * @note When updating the raw count of an active timer, the timer will immediately start counting from the new value.
  61. * @note This function is allowed to run within ISR context
  62. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  63. * makes it possible to execute even when the Flash Cache is disabled.
  64. *
  65. * @param[in] timer Timer handle created by `gptimer_new_timer`
  66. * @param[in] value Count value to be set
  67. * @return
  68. * - ESP_OK: Set GPTimer raw count value successfully
  69. * - ESP_ERR_INVALID_ARG: Set GPTimer raw count value failed because of invalid argument
  70. * - ESP_FAIL: Set GPTimer raw count value failed because of other error
  71. */
  72. esp_err_t gptimer_set_raw_count(gptimer_handle_t timer, uint64_t value);
  73. /**
  74. * @brief Get GPTimer raw count value
  75. *
  76. * @note This function will trigger a software capture event and then return the captured count value.
  77. * @note With the raw count value and the resolution returned from `gptimer_get_resolution`, you can convert the count value into seconds.
  78. * @note This function is allowed to run within ISR context
  79. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  80. * makes it possible to execute even when the Flash Cache is disabled.
  81. *
  82. * @param[in] timer Timer handle created by `gptimer_new_timer`
  83. * @param[out] value Returned GPTimer count value
  84. * @return
  85. * - ESP_OK: Get GPTimer raw count value successfully
  86. * - ESP_ERR_INVALID_ARG: Get GPTimer raw count value failed because of invalid argument
  87. * - ESP_FAIL: Get GPTimer raw count value failed because of other error
  88. */
  89. esp_err_t gptimer_get_raw_count(gptimer_handle_t timer, uint64_t *value);
  90. /**
  91. * @brief Return the real resolution of the timer
  92. *
  93. * @note usually the timer resolution is same as what you configured in the `gptimer_config_t::resolution_hz`,
  94. * but some unstable clock source (e.g. RC_FAST) will do a calibration, the real resolution can be different from the configured one.
  95. *
  96. * @param[in] timer Timer handle created by `gptimer_new_timer`
  97. * @param[out] out_resolution Returned timer resolution, in Hz
  98. * @return
  99. * - ESP_OK: Get GPTimer resolution successfully
  100. * - ESP_ERR_INVALID_ARG: Get GPTimer resolution failed because of invalid argument
  101. * - ESP_FAIL: Get GPTimer resolution failed because of other error
  102. */
  103. esp_err_t gptimer_get_resolution(gptimer_handle_t timer, uint32_t *out_resolution);
  104. /**
  105. * @brief Get GPTimer captured count value
  106. *
  107. * @note The capture action can be issued either by ETM event or by software (see also `gptimer_get_raw_count`).
  108. * @note This function is allowed to run within ISR context
  109. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  110. * makes it possible to execute even when the Flash Cache is disabled.
  111. *
  112. * @param[in] timer Timer handle created by `gptimer_new_timer`
  113. * @param[out] value Returned captured count value
  114. * @return
  115. * - ESP_OK: Get GPTimer captured count value successfully
  116. * - ESP_ERR_INVALID_ARG: Get GPTimer captured count value failed because of invalid argument
  117. * - ESP_FAIL: Get GPTimer captured count value failed because of other error
  118. */
  119. esp_err_t gptimer_get_captured_count(gptimer_handle_t timer, uint64_t *value);
  120. /**
  121. * @brief Group of supported GPTimer callbacks
  122. * @note The callbacks are all running under ISR environment
  123. * @note When CONFIG_GPTIMER_ISR_IRAM_SAFE is enabled, the callback itself and functions called by it should be placed in IRAM.
  124. */
  125. typedef struct {
  126. gptimer_alarm_cb_t on_alarm; /*!< Timer alarm callback */
  127. } gptimer_event_callbacks_t;
  128. /**
  129. * @brief Set callbacks for GPTimer
  130. *
  131. * @note User registered callbacks are expected to be runnable within ISR context
  132. * @note The first call to this function needs to be before the call to `gptimer_enable`
  133. * @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
  134. *
  135. * @param[in] timer Timer handle created by `gptimer_new_timer`
  136. * @param[in] cbs Group of callback functions
  137. * @param[in] user_data User data, which will be passed to callback functions directly
  138. * @return
  139. * - ESP_OK: Set event callbacks successfully
  140. * - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
  141. * - ESP_ERR_INVALID_STATE: Set event callbacks failed because the timer is not in init state
  142. * - ESP_FAIL: Set event callbacks failed because of other error
  143. */
  144. esp_err_t gptimer_register_event_callbacks(gptimer_handle_t timer, const gptimer_event_callbacks_t *cbs, void *user_data);
  145. /**
  146. * @brief General Purpose Timer alarm configuration
  147. */
  148. typedef struct {
  149. uint64_t alarm_count; /*!< Alarm target count value */
  150. uint64_t reload_count; /*!< Alarm reload count value, effect only when `auto_reload_on_alarm` is set to true */
  151. struct {
  152. uint32_t auto_reload_on_alarm: 1; /*!< Reload the count value by hardware, immediately at the alarm event */
  153. } flags; /*!< Alarm config flags*/
  154. } gptimer_alarm_config_t;
  155. /**
  156. * @brief Set alarm event actions for GPTimer.
  157. *
  158. * @note This function is allowed to run within ISR context, so that user can set new alarm action immediately in the ISR callback.
  159. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  160. * makes it possible to execute even when the Flash Cache is disabled.
  161. *
  162. * @param[in] timer Timer handle created by `gptimer_new_timer`
  163. * @param[in] config Alarm configuration, especially, set config to NULL means disabling the alarm function
  164. * @return
  165. * - ESP_OK: Set alarm action for GPTimer successfully
  166. * - ESP_ERR_INVALID_ARG: Set alarm action for GPTimer failed because of invalid argument
  167. * - ESP_FAIL: Set alarm action for GPTimer failed because of other error
  168. */
  169. esp_err_t gptimer_set_alarm_action(gptimer_handle_t timer, const gptimer_alarm_config_t *config);
  170. /**
  171. * @brief Enable GPTimer
  172. *
  173. * @note This function will transit the timer state from "init" to "enable".
  174. * @note This function will enable the interrupt service, if it's lazy installed in `gptimer_register_event_callbacks`.
  175. * @note This function will acquire a PM lock, if a specific source clock (e.g. APB) is selected in the `gptimer_config_t`, while `CONFIG_PM_ENABLE` is enabled.
  176. * @note Enable a timer doesn't mean to start it. See also `gptimer_start` for how to make the timer start counting.
  177. *
  178. * @param[in] timer Timer handle created by `gptimer_new_timer`
  179. * @return
  180. * - ESP_OK: Enable GPTimer successfully
  181. * - ESP_ERR_INVALID_ARG: Enable GPTimer failed because of invalid argument
  182. * - ESP_ERR_INVALID_STATE: Enable GPTimer failed because the timer is already enabled
  183. * - ESP_FAIL: Enable GPTimer failed because of other error
  184. */
  185. esp_err_t gptimer_enable(gptimer_handle_t timer);
  186. /**
  187. * @brief Disable GPTimer
  188. *
  189. * @note This function will transit the timer state from "enable" to "init".
  190. * @note This function will disable the interrupt service if it's installed.
  191. * @note This function will release the PM lock if it's acquired in the `gptimer_enable`.
  192. * @note Disable a timer doesn't mean to stop it. See also `gptimer_stop` for how to make the timer stop counting.
  193. *
  194. * @param[in] timer Timer handle created by `gptimer_new_timer`
  195. * @return
  196. * - ESP_OK: Disable GPTimer successfully
  197. * - ESP_ERR_INVALID_ARG: Disable GPTimer failed because of invalid argument
  198. * - ESP_ERR_INVALID_STATE: Disable GPTimer failed because the timer is not enabled yet
  199. * - ESP_FAIL: Disable GPTimer failed because of other error
  200. */
  201. esp_err_t gptimer_disable(gptimer_handle_t timer);
  202. /**
  203. * @brief Start GPTimer (internal counter starts counting)
  204. *
  205. * @note This function will transit the timer state from "enable" to "run".
  206. * @note This function is allowed to run within ISR context
  207. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  208. * makes it possible to execute even when the Flash Cache is disabled.
  209. *
  210. * @param[in] timer Timer handle created by `gptimer_new_timer`
  211. * @return
  212. * - ESP_OK: Start GPTimer successfully
  213. * - ESP_ERR_INVALID_ARG: Start GPTimer failed because of invalid argument
  214. * - ESP_ERR_INVALID_STATE: Start GPTimer failed because the timer is not enabled or is already in running
  215. * - ESP_FAIL: Start GPTimer failed because of other error
  216. */
  217. esp_err_t gptimer_start(gptimer_handle_t timer);
  218. /**
  219. * @brief Stop GPTimer (internal counter stops counting)
  220. *
  221. * @note This function will transit the timer state from "run" to "enable".
  222. * @note This function is allowed to run within ISR context
  223. * @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
  224. * makes it possible to execute even when the Flash Cache is disabled.
  225. *
  226. * @param[in] timer Timer handle created by `gptimer_new_timer`
  227. * @return
  228. * - ESP_OK: Stop GPTimer successfully
  229. * - ESP_ERR_INVALID_ARG: Stop GPTimer failed because of invalid argument
  230. * - ESP_ERR_INVALID_STATE: Stop GPTimer failed because the timer is not in running.
  231. * - ESP_FAIL: Stop GPTimer failed because of other error
  232. */
  233. esp_err_t gptimer_stop(gptimer_handle_t timer);
  234. #ifdef __cplusplus
  235. }
  236. #endif