pcnt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #ifndef __PCNT_H__
  2. #define __PCNT_H__
  3. #include <esp_types.h>
  4. #include "esp_intr.h"
  5. #include "esp_err.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/semphr.h"
  8. #include "freertos/xtensa_api.h"
  9. #include "soc/soc.h"
  10. #include "soc/pcnt_reg.h"
  11. #include "soc/pcnt_struct.h"
  12. #include "soc/gpio_sig_map.h"
  13. #include "driver/gpio.h"
  14. #include "esp_intr_alloc.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define PCNT_PIN_NOT_USED (-1) /*!< When selected for a pin, this pin will not be used */
  19. /**
  20. * @brief Selection of available modes that determine the counter's action depending on the state of the control signal's input GPIO
  21. * @note Configuration covers two actions, one for high, and one for low level on the control input
  22. */
  23. typedef enum {
  24. PCNT_MODE_KEEP = 0, /*!< Control mode: won't change counter mode*/
  25. PCNT_MODE_REVERSE = 1, /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase) */
  26. PCNT_MODE_DISABLE = 2, /*!< Control mode: Inhibit counter(counter value will not change in this condition) */
  27. PCNT_MODE_MAX
  28. } pcnt_ctrl_mode_t;
  29. /**
  30. * @brief Selection of available modes that determine the counter's action on the edge of the pulse signal's input GPIO
  31. * @note Configuration covers two actions, one for positive, and one for negative edge on the pulse input
  32. */
  33. typedef enum {
  34. PCNT_COUNT_DIS = 0, /*!< Counter mode: Inhibit counter(counter value will not change in this condition) */
  35. PCNT_COUNT_INC = 1, /*!< Counter mode: Increase counter value */
  36. PCNT_COUNT_DEC = 2, /*!< Counter mode: Decrease counter value */
  37. PCNT_COUNT_MAX
  38. } pcnt_count_mode_t;
  39. /**
  40. * @brief Selection of all available PCNT units
  41. */
  42. typedef enum {
  43. PCNT_UNIT_0 = 0, /*!< PCNT unit 0 */
  44. PCNT_UNIT_1 = 1, /*!< PCNT unit 1 */
  45. PCNT_UNIT_2 = 2, /*!< PCNT unit 2 */
  46. PCNT_UNIT_3 = 3, /*!< PCNT unit 3 */
  47. PCNT_UNIT_4 = 4, /*!< PCNT unit 4 */
  48. PCNT_UNIT_5 = 5, /*!< PCNT unit 5 */
  49. PCNT_UNIT_6 = 6, /*!< PCNT unit 6 */
  50. PCNT_UNIT_7 = 7, /*!< PCNT unit 7 */
  51. PCNT_UNIT_MAX,
  52. } pcnt_unit_t;
  53. /**
  54. * @brief Selection of channels available for a single PCNT unit
  55. */
  56. typedef enum {
  57. PCNT_CHANNEL_0 = 0x00, /*!< PCNT channel 0 */
  58. PCNT_CHANNEL_1 = 0x01, /*!< PCNT channel 1 */
  59. PCNT_CHANNEL_MAX,
  60. } pcnt_channel_t;
  61. /**
  62. * @brief Selection of counter's events the may trigger an interrupt
  63. */
  64. typedef enum {
  65. PCNT_EVT_L_LIM = 0, /*!< PCNT watch point event: Minimum counter value */
  66. PCNT_EVT_H_LIM = 1, /*!< PCNT watch point event: Maximum counter value */
  67. PCNT_EVT_THRES_0 = 2, /*!< PCNT watch point event: threshold0 value event */
  68. PCNT_EVT_THRES_1 = 3, /*!< PCNT watch point event: threshold1 value event */
  69. PCNT_EVT_ZERO = 4, /*!< PCNT watch point event: counter value zero event */
  70. PCNT_EVT_MAX
  71. } pcnt_evt_type_t;
  72. /**
  73. * @brief Pulse Counter configuration for a single channel
  74. */
  75. typedef struct {
  76. int pulse_gpio_num; /*!< Pulse input GPIO number, if you want to use GPIO16, enter pulse_gpio_num = 16, a negative value will be ignored */
  77. int ctrl_gpio_num; /*!< Control signal input GPIO number, a negative value will be ignored */
  78. pcnt_ctrl_mode_t lctrl_mode; /*!< PCNT low control mode */
  79. pcnt_ctrl_mode_t hctrl_mode; /*!< PCNT high control mode */
  80. pcnt_count_mode_t pos_mode; /*!< PCNT positive edge count mode */
  81. pcnt_count_mode_t neg_mode; /*!< PCNT negative edge count mode */
  82. int16_t counter_h_lim; /*!< Maximum counter value */
  83. int16_t counter_l_lim; /*!< Minimum counter value */
  84. pcnt_unit_t unit; /*!< PCNT unit number */
  85. pcnt_channel_t channel; /*!< the PCNT channel */
  86. } pcnt_config_t;
  87. typedef intr_handle_t pcnt_isr_handle_t;
  88. /**
  89. * @brief Configure Pulse Counter unit
  90. *
  91. * @param pcnt_config Pointer of Pulse Counter unit configure parameter
  92. *
  93. * @return
  94. * - ESP_OK Success
  95. * - ESP_ERR_INVALID_ARG Parameter error
  96. */
  97. esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config);
  98. /**
  99. * @brief Get pulse counter value
  100. *
  101. * @param pcnt_unit Pulse Counter unit number
  102. * @param count Pointer to accept counter value
  103. *
  104. * @return
  105. * - ESP_OK Success
  106. * - ESP_ERR_INVALID_ARG Parameter error
  107. */
  108. esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t* count);
  109. /**
  110. * @brief Pause PCNT counter of PCNT unit
  111. *
  112. * @param pcnt_unit PCNT unit number
  113. *
  114. * @return
  115. * - ESP_OK Success
  116. * - ESP_ERR_INVALID_ARG Parameter error
  117. */
  118. esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit);
  119. /**
  120. * @brief Resume counting for PCNT counter
  121. *
  122. * @param pcnt_unit PCNT unit number, select from pcnt_unit_t
  123. *
  124. * @return
  125. * - ESP_OK Success
  126. * - ESP_ERR_INVALID_ARG Parameter error
  127. */
  128. esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit);
  129. /**
  130. * @brief Clear and reset PCNT counter value to zero
  131. *
  132. * @param pcnt_unit PCNT unit number, select from pcnt_unit_t
  133. *
  134. * @return
  135. * - ESP_OK Success
  136. * - ESP_ERR_INVALID_ARG Parameter error
  137. */
  138. esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit);
  139. /**
  140. * @brief Enable PCNT interrupt for PCNT unit
  141. * @note
  142. * Each Pulse counter unit has five watch point events that share the same interrupt.
  143. * Configure events with pcnt_event_enable() and pcnt_event_disable()
  144. *
  145. * @param pcnt_unit PCNT unit number
  146. *
  147. * @return
  148. * - ESP_OK Success
  149. * - ESP_ERR_INVALID_ARG Parameter error
  150. */
  151. esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit);
  152. /**
  153. * @brief Disable PCNT interrupt for PCNT unit
  154. *
  155. * @param pcnt_unit PCNT unit number
  156. *
  157. * @return
  158. * - ESP_OK Success
  159. * - ESP_ERR_INVALID_ARG Parameter error
  160. */
  161. esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit);
  162. /**
  163. * @brief Enable PCNT event of PCNT unit
  164. *
  165. * @param unit PCNT unit number
  166. * @param evt_type Watch point event type.
  167. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  168. * @return
  169. * - ESP_OK Success
  170. * - ESP_ERR_INVALID_ARG Parameter error
  171. */
  172. esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type);
  173. /**
  174. * @brief Disable PCNT event of PCNT unit
  175. *
  176. * @param unit PCNT unit number
  177. * @param evt_type Watch point event type.
  178. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  179. * @return
  180. * - ESP_OK Success
  181. * - ESP_ERR_INVALID_ARG Parameter error
  182. */
  183. esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type);
  184. /**
  185. * @brief Set PCNT event value of PCNT unit
  186. *
  187. * @param unit PCNT unit number
  188. * @param evt_type Watch point event type.
  189. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  190. *
  191. * @param value Counter value for PCNT event
  192. *
  193. * @return
  194. * - ESP_OK Success
  195. * - ESP_ERR_INVALID_ARG Parameter error
  196. */
  197. esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value);
  198. /**
  199. * @brief Get PCNT event value of PCNT unit
  200. *
  201. * @param unit PCNT unit number
  202. * @param evt_type Watch point event type.
  203. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  204. * @param value Pointer to accept counter value for PCNT event
  205. *
  206. * @return
  207. * - ESP_OK Success
  208. * - ESP_ERR_INVALID_ARG Parameter error
  209. */
  210. esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value);
  211. /**
  212. * @brief Register PCNT interrupt handler, the handler is an ISR.
  213. * The handler will be attached to the same CPU core that this function is running on.
  214. *
  215. * @param fn Interrupt handler function.
  216. * @param arg Parameter for handler function
  217. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  218. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  219. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  220. * be returned here.
  221. *
  222. * @return
  223. * - ESP_OK Success
  224. * - ESP_ERR_INVALID_ARG Function pointer error.
  225. */
  226. esp_err_t pcnt_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, pcnt_isr_handle_t *handle);
  227. /**
  228. * @brief Configure PCNT pulse signal input pin and control input pin
  229. *
  230. * @param unit PCNT unit number
  231. * @param channel PCNT channel number
  232. * @param pulse_io Pulse signal input GPIO
  233. * @param ctrl_io Control signal input GPIO
  234. *
  235. * @note Set the signal input to PCNT_PIN_NOT_USED if unused.
  236. *
  237. * @return
  238. * - ESP_OK Success
  239. * - ESP_ERR_INVALID_ARG Parameter error
  240. */
  241. esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io);
  242. /**
  243. * @brief Enable PCNT input filter
  244. *
  245. * @param unit PCNT unit number
  246. *
  247. * @return
  248. * - ESP_OK Success
  249. * - ESP_ERR_INVALID_ARG Parameter error
  250. */
  251. esp_err_t pcnt_filter_enable(pcnt_unit_t unit);
  252. /**
  253. * @brief Disable PCNT input filter
  254. *
  255. * @param unit PCNT unit number
  256. *
  257. * @return
  258. * - ESP_OK Success
  259. * - ESP_ERR_INVALID_ARG Parameter error
  260. */
  261. esp_err_t pcnt_filter_disable(pcnt_unit_t unit);
  262. /**
  263. * @brief Set PCNT filter value
  264. *
  265. * @param unit PCNT unit number
  266. * @param filter_val PCNT signal filter value, counter in APB_CLK cycles.
  267. * Any pulses lasting shorter than this will be ignored when the filter is enabled.
  268. * @note
  269. * filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
  270. *
  271. * @return
  272. * - ESP_OK Success
  273. * - ESP_ERR_INVALID_ARG Parameter error
  274. */
  275. esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val);
  276. /**
  277. * @brief Get PCNT filter value
  278. *
  279. * @param unit PCNT unit number
  280. * @param filter_val Pointer to accept PCNT filter value.
  281. *
  282. * @return
  283. * - ESP_OK Success
  284. * - ESP_ERR_INVALID_ARG Parameter error
  285. */
  286. esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val);
  287. /**
  288. * @brief Set PCNT counter mode
  289. *
  290. * @param unit PCNT unit number
  291. * @param channel PCNT channel number
  292. * @param pos_mode Counter mode when detecting positive edge
  293. * @param neg_mode Counter mode when detecting negative edge
  294. * @param hctrl_mode Counter mode when control signal is high level
  295. * @param lctrl_mode Counter mode when control signal is low level
  296. *
  297. * @return
  298. * - ESP_OK Success
  299. * - ESP_ERR_INVALID_ARG Parameter error
  300. */
  301. esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel,
  302. pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode,
  303. pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode);
  304. /**
  305. * @addtogroup pcnt-examples
  306. *
  307. * @{
  308. *
  309. * EXAMPLE OF PCNT CONFIGURATION
  310. * ==============================
  311. * @code{c}
  312. * //1. Config PCNT unit
  313. * pcnt_config_t pcnt_config = {
  314. * .pulse_gpio_num = 4, //set gpio4 as pulse input gpio
  315. * .ctrl_gpio_num = 5, //set gpio5 as control gpio
  316. * .channel = PCNT_CHANNEL_0, //use unit 0 channel 0
  317. * .lctrl_mode = PCNT_MODE_REVERSE, //when control signal is low, reverse the primary counter mode(inc->dec/dec->inc)
  318. * .hctrl_mode = PCNT_MODE_KEEP, //when control signal is high, keep the primary counter mode
  319. * .pos_mode = PCNT_COUNT_INC, //increment the counter
  320. * .neg_mode = PCNT_COUNT_DIS, //keep the counter value
  321. * .counter_h_lim = 10,
  322. * .counter_l_lim = -10,
  323. * };
  324. * pcnt_unit_config(&pcnt_config); //init unit
  325. * @endcode
  326. *
  327. * EXAMPLE OF PCNT EVENT SETTING
  328. * ==============================
  329. * @code{c}
  330. * //2. Configure PCNT watchpoint event.
  331. * pcnt_set_event_value(PCNT_UNIT_0, PCNT_EVT_THRES_1, 5); //set thres1 value
  332. * pcnt_event_enable(PCNT_UNIT_0, PCNT_EVT_THRES_1); //enable thres1 event
  333. * @endcode
  334. *
  335. * For more examples please refer to PCNT example code in IDF_PATH/examples
  336. *
  337. * @}
  338. */
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif