pulse_cnt.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "hal/pcnt_types.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Type of PCNT unit handle
  16. */
  17. typedef struct pcnt_unit_t *pcnt_unit_handle_t;
  18. /**
  19. * @brief Type of PCNT channel handle
  20. */
  21. typedef struct pcnt_chan_t *pcnt_channel_handle_t;
  22. /**
  23. * @brief PCNT watch event data
  24. */
  25. typedef struct {
  26. int watch_point_value; /*!< Watch point value that triggered the event */
  27. pcnt_unit_zero_cross_mode_t zero_cross_mode; /*!< Zero cross mode */
  28. } pcnt_watch_event_data_t;
  29. /**
  30. * @brief PCNT watch event callback prototype
  31. *
  32. * @note The callback function is invoked from an ISR context, so it should meet the restrictions of not calling any blocking APIs when implementing the callback.
  33. * e.g. must use ISR version of FreeRTOS APIs.
  34. *
  35. * @param[in] unit PCNT unit handle
  36. * @param[in] edata PCNT event data, fed by the driver
  37. * @param[in] user_ctx User data, passed from `pcnt_unit_register_event_callbacks()`
  38. * @return Whether a high priority task has been woken up by this function
  39. */
  40. typedef bool (*pcnt_watch_cb_t)(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx);
  41. /**
  42. * @brief Group of supported PCNT callbacks
  43. * @note The callbacks are all running under ISR environment
  44. * @note When CONFIG_PCNT_ISR_IRAM_SAFE is enabled, the callback itself and functions callbed by it should be placed in IRAM.
  45. */
  46. typedef struct {
  47. pcnt_watch_cb_t on_reach; /*!< Called when PCNT unit counter reaches any watch point */
  48. } pcnt_event_callbacks_t;
  49. /**
  50. * @brief PCNT unit configuration
  51. */
  52. typedef struct {
  53. int low_limit; /*!< Low limitation of the count unit, should be lower than 0 */
  54. int high_limit; /*!< High limitation of the count unit, should be higher than 0 */
  55. int intr_priority; /*!< PCNT interrupt priority,
  56. if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
  57. struct {
  58. uint32_t accum_count: 1; /*!< Whether to accumulate the count value when overflows at the high/low limit */
  59. } flags; /*!< Extra flags */
  60. } pcnt_unit_config_t;
  61. /**
  62. * @brief PCNT channel configuration
  63. */
  64. typedef struct {
  65. int edge_gpio_num; /*!< GPIO number used by the edge signal, input mode with pull up enabled. Set to -1 if unused */
  66. int level_gpio_num; /*!< GPIO number used by the level signal, input mode with pull up enabled. Set to -1 if unused */
  67. struct {
  68. uint32_t invert_edge_input: 1; /*!< Invert the input edge signal */
  69. uint32_t invert_level_input: 1; /*!< Invert the input level signal */
  70. uint32_t virt_edge_io_level: 1; /*!< Virtual edge IO level, 0: low, 1: high. Only valid when edge_gpio_num is set to -1 */
  71. uint32_t virt_level_io_level: 1; /*!< Virtual level IO level, 0: low, 1: high. Only valid when level_gpio_num is set to -1 */
  72. uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */
  73. } flags; /*!< Channel config flags */
  74. } pcnt_chan_config_t;
  75. /**
  76. * @brief PCNT glitch filter configuration
  77. */
  78. typedef struct {
  79. uint32_t max_glitch_ns; /*!< Pulse width smaller than this threshold will be treated as glitch and ignored, in the unit of ns */
  80. } pcnt_glitch_filter_config_t;
  81. /**
  82. * @brief Create a new PCNT unit, and return the handle
  83. *
  84. * @note The newly created PCNT unit is put in the init state.
  85. *
  86. * @param[in] config PCNT unit configuration
  87. * @param[out] ret_unit Returned PCNT unit handle
  88. * @return
  89. * - ESP_OK: Create PCNT unit successfully
  90. * - ESP_ERR_INVALID_ARG: Create PCNT unit failed because of invalid argument (e.g. high/low limit value out of the range)
  91. * - ESP_ERR_NO_MEM: Create PCNT unit failed because out of memory
  92. * - ESP_ERR_NOT_FOUND: Create PCNT unit failed because all PCNT units are used up and no more free one
  93. * - ESP_FAIL: Create PCNT unit failed because of other error
  94. */
  95. esp_err_t pcnt_new_unit(const pcnt_unit_config_t *config, pcnt_unit_handle_t *ret_unit);
  96. /**
  97. * @brief Delete the PCNT unit handle
  98. *
  99. * @note A PCNT unit can't be in the enable state when this function is invoked.
  100. * See also `pcnt_unit_disable()` for how to disable a unit.
  101. *
  102. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  103. * @return
  104. * - ESP_OK: Delete the PCNT unit successfully
  105. * - ESP_ERR_INVALID_ARG: Delete the PCNT unit failed because of invalid argument
  106. * - ESP_ERR_INVALID_STATE: Delete the PCNT unit failed because the unit is not in init state or some PCNT channel is still in working
  107. * - ESP_FAIL: Delete the PCNT unit failed because of other error
  108. */
  109. esp_err_t pcnt_del_unit(pcnt_unit_handle_t unit);
  110. /**
  111. * @brief Set glitch filter for PCNT unit
  112. *
  113. * @note The glitch filter module is clocked from APB, and APB frequency can be changed during DFS, which in return make the filter out of action.
  114. * So this function will lazy-install a PM lock internally when the power management is enabled. With this lock, the APB frequency won't be changed.
  115. * The PM lock can be uninstalled in `pcnt_del_unit()`.
  116. * @note This function should be called when the PCNT unit is in the init state (i.e. before calling `pcnt_unit_enable()`)
  117. *
  118. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  119. * @param[in] config PCNT filter configuration, set config to NULL means disabling the filter function
  120. * @return
  121. * - ESP_OK: Set glitch filter successfully
  122. * - ESP_ERR_INVALID_ARG: Set glitch filter failed because of invalid argument (e.g. glitch width is too big)
  123. * - ESP_ERR_INVALID_STATE: Set glitch filter failed because the unit is not in the init state
  124. * - ESP_FAIL: Set glitch filter failed because of other error
  125. */
  126. esp_err_t pcnt_unit_set_glitch_filter(pcnt_unit_handle_t unit, const pcnt_glitch_filter_config_t *config);
  127. #if SOC_PCNT_SUPPORT_CLEAR_SIGNAL
  128. /**
  129. * @brief PCNT clear signal configuration
  130. */
  131. typedef struct {
  132. int clear_signal_gpio_num; /*!< GPIO number used by the clear signal, the default active level is high, input mode with pull down enabled */
  133. struct {
  134. uint32_t invert_clear_signal: 1; /*!< Invert the clear input signal and set input mode with pull up */
  135. uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */
  136. } flags; /*!< clear signal config flags */
  137. } pcnt_clear_signal_config_t;
  138. /**
  139. * @brief Set clear signal for PCNT unit
  140. *
  141. * @note The function of clear signal is the same as `pcnt_unit_clear_count()`. High-level Active
  142. *
  143. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  144. * @param[in] config PCNT clear signal configuration, set config to NULL means disabling the clear signal
  145. * @return
  146. * - ESP_OK: Set clear signal successfully
  147. * - ESP_ERR_INVALID_ARG: Set clear signal failed because of invalid argument
  148. * - ESP_ERR_INVALID_STATE: Set clear signal failed because set clear signal repeatly or disable clear signal before set it
  149. * - ESP_FAIL: Set clear signal failed because of other error
  150. */
  151. esp_err_t pcnt_unit_set_clear_signal(pcnt_unit_handle_t unit, const pcnt_clear_signal_config_t *config);
  152. #endif
  153. /**
  154. * @brief Enable the PCNT unit
  155. *
  156. * @note This function will transit the unit state from init to enable.
  157. * @note This function will enable the interrupt service, if it's lazy installed in `pcnt_unit_register_event_callbacks()`.
  158. * @note This function will acquire the PM lock if it's lazy installed in `pcnt_unit_set_glitch_filter()`.
  159. * @note Enable a PCNT unit doesn't mean to start it. See also `pcnt_unit_start()` for how to start the PCNT counter.
  160. *
  161. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  162. * @return
  163. * - ESP_OK: Enable PCNT unit successfully
  164. * - ESP_ERR_INVALID_ARG: Enable PCNT unit failed because of invalid argument
  165. * - ESP_ERR_INVALID_STATE: Enable PCNT unit failed because the unit is already enabled
  166. * - ESP_FAIL: Enable PCNT unit failed because of other error
  167. */
  168. esp_err_t pcnt_unit_enable(pcnt_unit_handle_t unit);
  169. /**
  170. * @brief Disable the PCNT unit
  171. *
  172. * @note This function will do the opposite work to the `pcnt_unit_enable()`
  173. * @note Disable a PCNT unit doesn't mean to stop it. See also `pcnt_unit_stop()` for how to stop the PCNT counter.
  174. *
  175. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  176. * @return
  177. * - ESP_OK: Disable PCNT unit successfully
  178. * - ESP_ERR_INVALID_ARG: Disable PCNT unit failed because of invalid argument
  179. * - ESP_ERR_INVALID_STATE: Disable PCNT unit failed because the unit is not enabled yet
  180. * - ESP_FAIL: Disable PCNT unit failed because of other error
  181. */
  182. esp_err_t pcnt_unit_disable(pcnt_unit_handle_t unit);
  183. /**
  184. * @brief Start the PCNT unit, the counter will start to count according to the edge and/or level input signals
  185. *
  186. * @note This function should be called when the unit is in the enable state (i.e. after calling `pcnt_unit_enable()`)
  187. * @note This function is allowed to run within ISR context
  188. * @note This function will be placed into IRAM if `CONFIG_PCNT_CTRL_FUNC_IN_IRAM` is on, so that it's allowed to be executed when Cache is disabled
  189. *
  190. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  191. * @return
  192. * - ESP_OK: Start PCNT unit successfully
  193. * - ESP_ERR_INVALID_ARG: Start PCNT unit failed because of invalid argument
  194. * - ESP_ERR_INVALID_STATE: Start PCNT unit failed because the unit is not enabled yet
  195. * - ESP_FAIL: Start PCNT unit failed because of other error
  196. */
  197. esp_err_t pcnt_unit_start(pcnt_unit_handle_t unit);
  198. /**
  199. * @brief Stop PCNT from counting
  200. *
  201. * @note This function should be called when the unit is in the enable state (i.e. after calling `pcnt_unit_enable()`)
  202. * @note The stop operation won't clear the counter. Also see `pcnt_unit_clear_count()` for how to clear pulse count value.
  203. * @note This function is allowed to run within ISR context
  204. * @note This function will be placed into IRAM if `CONFIG_PCNT_CTRL_FUNC_IN_IRAM`, so that it is allowed to be executed when Cache is disabled
  205. *
  206. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  207. * @return
  208. * - ESP_OK: Stop PCNT unit successfully
  209. * - ESP_ERR_INVALID_ARG: Stop PCNT unit failed because of invalid argument
  210. * - ESP_ERR_INVALID_STATE: Stop PCNT unit failed because the unit is not enabled yet
  211. * - ESP_FAIL: Stop PCNT unit failed because of other error
  212. */
  213. esp_err_t pcnt_unit_stop(pcnt_unit_handle_t unit);
  214. /**
  215. * @brief Clear PCNT pulse count value to zero
  216. *
  217. * @note It's recommended to call this function after adding a watch point by `pcnt_unit_add_watch_point()`, so that the newly added watch point is effective immediately.
  218. * @note This function is allowed to run within ISR context
  219. * @note This function will be placed into IRAM if `CONFIG_PCNT_CTRL_FUNC_IN_IRAM`, so that it's allowed to be executed when Cache is disabled
  220. *
  221. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  222. * @return
  223. * - ESP_OK: Clear PCNT pulse count successfully
  224. * - ESP_ERR_INVALID_ARG: Clear PCNT pulse count failed because of invalid argument
  225. * - ESP_FAIL: Clear PCNT pulse count failed because of other error
  226. */
  227. esp_err_t pcnt_unit_clear_count(pcnt_unit_handle_t unit);
  228. /**
  229. * @brief Get PCNT count value
  230. *
  231. * @note This function is allowed to run within ISR context
  232. * @note This function will be placed into IRAM if `CONFIG_PCNT_CTRL_FUNC_IN_IRAM`, so that it's allowed to be executed when Cache is disabled
  233. *
  234. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  235. * @param[out] value Returned count value
  236. * @return
  237. * - ESP_OK: Get PCNT pulse count successfully
  238. * - ESP_ERR_INVALID_ARG: Get PCNT pulse count failed because of invalid argument
  239. * - ESP_FAIL: Get PCNT pulse count failed because of other error
  240. */
  241. esp_err_t pcnt_unit_get_count(pcnt_unit_handle_t unit, int *value);
  242. /**
  243. * @brief Set event callbacks for PCNT unit
  244. *
  245. * @note User registered callbacks are expected to be runnable within ISR context
  246. * @note The first call to this function needs to be before the call to `pcnt_unit_enable`
  247. * @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
  248. *
  249. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  250. * @param[in] cbs Group of callback functions
  251. * @param[in] user_data User data, which will be passed to callback functions directly
  252. * @return
  253. * - ESP_OK: Set event callbacks successfully
  254. * - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
  255. * - ESP_ERR_INVALID_STATE: Set event callbacks failed because the unit is not in init state
  256. * - ESP_FAIL: Set event callbacks failed because of other error
  257. */
  258. esp_err_t pcnt_unit_register_event_callbacks(pcnt_unit_handle_t unit, const pcnt_event_callbacks_t *cbs, void *user_data);
  259. /**
  260. * @brief Add a watch point for PCNT unit, PCNT will generate an event when the counter value reaches the watch point value
  261. *
  262. *
  263. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  264. * @param[in] watch_point Value to be watched
  265. * @return
  266. * - ESP_OK: Add watch point successfully
  267. * - ESP_ERR_INVALID_ARG: Add watch point failed because of invalid argument (e.g. the value to be watched is out of the limitation set in `pcnt_unit_config_t`)
  268. * - ESP_ERR_INVALID_STATE: Add watch point failed because the same watch point has already been added
  269. * - ESP_ERR_NOT_FOUND: Add watch point failed because no more hardware watch point can be configured
  270. * - ESP_FAIL: Add watch point failed because of other error
  271. */
  272. esp_err_t pcnt_unit_add_watch_point(pcnt_unit_handle_t unit, int watch_point);
  273. /**
  274. * @brief Remove a watch point for PCNT unit
  275. *
  276. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  277. * @param[in] watch_point Watch point value
  278. * @return
  279. * - ESP_OK: Remove watch point successfully
  280. * - ESP_ERR_INVALID_ARG: Remove watch point failed because of invalid argument
  281. * - ESP_ERR_INVALID_STATE: Remove watch point failed because the watch point was not added by `pcnt_unit_add_watch_point()` yet
  282. * - ESP_FAIL: Remove watch point failed because of other error
  283. */
  284. esp_err_t pcnt_unit_remove_watch_point(pcnt_unit_handle_t unit, int watch_point);
  285. /**
  286. * @brief Create PCNT channel for specific unit, each PCNT has several channels associated with it
  287. *
  288. * @note This function should be called when the unit is in init state (i.e. before calling `pcnt_unit_enable()`)
  289. *
  290. * @param[in] unit PCNT unit handle created by `pcnt_new_unit()`
  291. * @param[in] config PCNT channel configuration
  292. * @param[out] ret_chan Returned channel handle
  293. * @return
  294. * - ESP_OK: Create PCNT channel successfully
  295. * - ESP_ERR_INVALID_ARG: Create PCNT channel failed because of invalid argument
  296. * - ESP_ERR_NO_MEM: Create PCNT channel failed because of insufficient memory
  297. * - ESP_ERR_NOT_FOUND: Create PCNT channel failed because all PCNT channels are used up and no more free one
  298. * - ESP_ERR_INVALID_STATE: Create PCNT channel failed because the unit is not in the init state
  299. * - ESP_FAIL: Create PCNT channel failed because of other error
  300. */
  301. esp_err_t pcnt_new_channel(pcnt_unit_handle_t unit, const pcnt_chan_config_t *config, pcnt_channel_handle_t *ret_chan);
  302. /**
  303. * @brief Delete the PCNT channel
  304. *
  305. * @param[in] chan PCNT channel handle created by `pcnt_new_channel()`
  306. * @return
  307. * - ESP_OK: Delete the PCNT channel successfully
  308. * - ESP_ERR_INVALID_ARG: Delete the PCNT channel failed because of invalid argument
  309. * - ESP_FAIL: Delete the PCNT channel failed because of other error
  310. */
  311. esp_err_t pcnt_del_channel(pcnt_channel_handle_t chan);
  312. /**
  313. * @brief Set channel actions when edge signal changes (e.g. falling or rising edge occurred).
  314. * The edge signal is input from the `edge_gpio_num` configured in `pcnt_chan_config_t`.
  315. * We use these actions to control when and how to change the counter value.
  316. *
  317. * @param[in] chan PCNT channel handle created by `pcnt_new_channel()`
  318. * @param[in] pos_act Action on posedge signal
  319. * @param[in] neg_act Action on negedge signal
  320. * @return
  321. * - ESP_OK: Set edge action for PCNT channel successfully
  322. * - ESP_ERR_INVALID_ARG: Set edge action for PCNT channel failed because of invalid argument
  323. * - ESP_FAIL: Set edge action for PCNT channel failed because of other error
  324. */
  325. esp_err_t pcnt_channel_set_edge_action(pcnt_channel_handle_t chan, pcnt_channel_edge_action_t pos_act, pcnt_channel_edge_action_t neg_act);
  326. /**
  327. * @brief Set channel actions when level signal changes (e.g. signal level goes from high to low).
  328. * The level signal is input from the `level_gpio_num` configured in `pcnt_chan_config_t`.
  329. * We use these actions to control when and how to change the counting mode.
  330. *
  331. * @param[in] chan PCNT channel handle created by `pcnt_new_channel()`
  332. * @param[in] high_act Action on high level signal
  333. * @param[in] low_act Action on low level signal
  334. * @return
  335. * - ESP_OK: Set level action for PCNT channel successfully
  336. * - ESP_ERR_INVALID_ARG: Set level action for PCNT channel failed because of invalid argument
  337. * - ESP_FAIL: Set level action for PCNT channel failed because of other error
  338. */
  339. esp_err_t pcnt_channel_set_level_action(pcnt_channel_handle_t chan, pcnt_channel_level_action_t high_act, pcnt_channel_level_action_t low_act);
  340. #ifdef __cplusplus
  341. }
  342. #endif