pcnt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "freertos/FreeRTOS.h"
  8. #include "esp_types.h"
  9. #include "esp_err.h"
  10. #include "esp_intr_alloc.h"
  11. #include "driver/gpio.h"
  12. #include "soc/soc_caps.h"
  13. #include "hal/pcnt_types.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define PCNT_PIN_NOT_USED (-1) /*!< When selected for a pin, this pin will not be used */
  18. typedef intr_handle_t pcnt_isr_handle_t;
  19. /**
  20. * @brief PCNT port number, the max port number is (PCNT_PORT_MAX - 1).
  21. */
  22. typedef enum {
  23. PCNT_PORT_0, /*!< PCNT port 0 */
  24. PCNT_PORT_MAX, /*!< PCNT port max */
  25. } pcnt_port_t;
  26. /**
  27. * @brief Selection of all available PCNT units
  28. */
  29. typedef enum {
  30. PCNT_UNIT_0, /*!< PCNT unit 0 */
  31. PCNT_UNIT_1, /*!< PCNT unit 1 */
  32. PCNT_UNIT_2, /*!< PCNT unit 2 */
  33. PCNT_UNIT_3, /*!< PCNT unit 3 */
  34. #if SOC_PCNT_UNITS_PER_GROUP > 4
  35. PCNT_UNIT_4, /*!< PCNT unit 4 */
  36. PCNT_UNIT_5, /*!< PCNT unit 5 */
  37. PCNT_UNIT_6, /*!< PCNT unit 6 */
  38. PCNT_UNIT_7, /*!< PCNT unit 7 */
  39. #endif
  40. PCNT_UNIT_MAX,
  41. } pcnt_unit_t;
  42. /**
  43. * @brief Selection of channels available for a single PCNT unit
  44. */
  45. typedef enum {
  46. PCNT_CHANNEL_0, /*!< PCNT channel 0 */
  47. PCNT_CHANNEL_1, /*!< PCNT channel 1 */
  48. PCNT_CHANNEL_MAX,
  49. } pcnt_channel_t;
  50. /**
  51. * @brief Selection of counter's events the may trigger an interrupt
  52. */
  53. typedef enum {
  54. PCNT_EVT_THRES_1 = 1 << 2, /*!< PCNT watch point event: threshold1 value event */
  55. PCNT_EVT_THRES_0 = 1 << 3, /*!< PCNT watch point event: threshold0 value event */
  56. PCNT_EVT_L_LIM = 1 << 4, /*!< PCNT watch point event: Minimum counter value */
  57. PCNT_EVT_H_LIM = 1 << 5, /*!< PCNT watch point event: Maximum counter value */
  58. PCNT_EVT_ZERO = 1 << 6, /*!< PCNT watch point event: counter value zero event */
  59. PCNT_EVT_MAX
  60. } pcnt_evt_type_t;
  61. /**
  62. * @brief Selection of available modes that determine the counter's action depending on the state of the control signal's input GPIO
  63. * @note Configuration covers two actions, one for high, and one for low level on the control input
  64. */
  65. typedef pcnt_channel_level_action_t pcnt_ctrl_mode_t;
  66. #define PCNT_MODE_KEEP PCNT_CHANNEL_LEVEL_ACTION_KEEP /*!< Control mode: won't change counter mode*/
  67. #define PCNT_MODE_REVERSE PCNT_CHANNEL_LEVEL_ACTION_INVERSE /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase) */
  68. #define PCNT_MODE_DISABLE PCNT_CHANNEL_LEVEL_ACTION_HOLD /*!< Control mode: Inhibit counter(counter value will not change in this condition) */
  69. #define PCNT_MODE_MAX 3
  70. /**
  71. * @brief Selection of available modes that determine the counter's action on the edge of the pulse signal's input GPIO
  72. * @note Configuration covers two actions, one for positive, and one for negative edge on the pulse input
  73. */
  74. typedef pcnt_channel_edge_action_t pcnt_count_mode_t;
  75. #define PCNT_COUNT_DIS PCNT_CHANNEL_EDGE_ACTION_HOLD /*!< Counter mode: Inhibit counter(counter value will not change in this condition) */
  76. #define PCNT_COUNT_INC PCNT_CHANNEL_EDGE_ACTION_INCREASE /*!< Counter mode: Increase counter value */
  77. #define PCNT_COUNT_DEC PCNT_CHANNEL_EDGE_ACTION_DECREASE /*!< Counter mode: Decrease counter value */
  78. #define PCNT_COUNT_MAX 3
  79. /**
  80. * @brief Pulse Counter configuration for a single channel
  81. */
  82. typedef struct {
  83. 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 */
  84. int ctrl_gpio_num; /*!< Control signal input GPIO number, a negative value will be ignored */
  85. pcnt_ctrl_mode_t lctrl_mode; /*!< PCNT low control mode */
  86. pcnt_ctrl_mode_t hctrl_mode; /*!< PCNT high control mode */
  87. pcnt_count_mode_t pos_mode; /*!< PCNT positive edge count mode */
  88. pcnt_count_mode_t neg_mode; /*!< PCNT negative edge count mode */
  89. int16_t counter_h_lim; /*!< Maximum counter value */
  90. int16_t counter_l_lim; /*!< Minimum counter value */
  91. pcnt_unit_t unit; /*!< PCNT unit number */
  92. pcnt_channel_t channel; /*!< the PCNT channel */
  93. } pcnt_config_t;
  94. /**
  95. * @brief Configure Pulse Counter unit
  96. * @note
  97. * This function will disable three events: PCNT_EVT_L_LIM, PCNT_EVT_H_LIM, PCNT_EVT_ZERO.
  98. *
  99. * @param pcnt_config Pointer of Pulse Counter unit configure parameter
  100. *
  101. * @return
  102. * - ESP_OK Success
  103. * - ESP_ERR_INVALID_STATE pcnt driver already initialized
  104. * - ESP_ERR_INVALID_ARG Parameter error
  105. */
  106. esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config);
  107. /**
  108. * @brief Get pulse counter value
  109. *
  110. * @param pcnt_unit Pulse Counter unit number
  111. * @param count Pointer to accept counter value
  112. *
  113. * @return
  114. * - ESP_OK Success
  115. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  116. * - ESP_ERR_INVALID_ARG Parameter error
  117. */
  118. esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t *count);
  119. /**
  120. * @brief Pause PCNT counter of PCNT unit
  121. *
  122. * @param pcnt_unit PCNT unit number
  123. *
  124. * @return
  125. * - ESP_OK Success
  126. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  127. * - ESP_ERR_INVALID_ARG Parameter error
  128. */
  129. esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit);
  130. /**
  131. * @brief Resume counting for PCNT counter
  132. *
  133. * @param pcnt_unit PCNT unit number, select from pcnt_unit_t
  134. *
  135. * @return
  136. * - ESP_OK Success
  137. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  138. * - ESP_ERR_INVALID_ARG Parameter error
  139. */
  140. esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit);
  141. /**
  142. * @brief Clear and reset PCNT counter value to zero
  143. *
  144. * @param pcnt_unit PCNT unit number, select from pcnt_unit_t
  145. *
  146. * @return
  147. * - ESP_OK Success
  148. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  149. * - ESP_ERR_INVALID_ARG Parameter error
  150. */
  151. esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit);
  152. /**
  153. * @brief Enable PCNT interrupt for PCNT unit
  154. * @note
  155. * Each Pulse counter unit has five watch point events that share the same interrupt.
  156. * Configure events with pcnt_event_enable() and pcnt_event_disable()
  157. *
  158. * @param pcnt_unit PCNT unit number
  159. *
  160. * @return
  161. * - ESP_OK Success
  162. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  163. * - ESP_ERR_INVALID_ARG Parameter error
  164. */
  165. esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit);
  166. /**
  167. * @brief Disable PCNT interrupt for PCNT unit
  168. *
  169. * @param pcnt_unit PCNT unit number
  170. *
  171. * @return
  172. * - ESP_OK Success
  173. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  174. * - ESP_ERR_INVALID_ARG Parameter error
  175. */
  176. esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit);
  177. /**
  178. * @brief Enable PCNT event of PCNT unit
  179. *
  180. * @param unit PCNT unit number
  181. * @param evt_type Watch point event type.
  182. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  183. * @return
  184. * - ESP_OK Success
  185. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  186. * - ESP_ERR_INVALID_ARG Parameter error
  187. */
  188. esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type);
  189. /**
  190. * @brief Disable PCNT event of PCNT unit
  191. *
  192. * @param unit PCNT unit number
  193. * @param evt_type Watch point event type.
  194. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  195. * @return
  196. * - ESP_OK Success
  197. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  198. * - ESP_ERR_INVALID_ARG Parameter error
  199. */
  200. esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type);
  201. /**
  202. * @brief Set PCNT event value of PCNT unit
  203. *
  204. * @param unit PCNT unit number
  205. * @param evt_type Watch point event type.
  206. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  207. *
  208. * @param value Counter value for PCNT event
  209. *
  210. * @return
  211. * - ESP_OK Success
  212. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  213. * - ESP_ERR_INVALID_ARG Parameter error
  214. */
  215. esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value);
  216. /**
  217. * @brief Get PCNT event value of PCNT unit
  218. *
  219. * @param unit PCNT unit number
  220. * @param evt_type Watch point event type.
  221. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  222. * @param value Pointer to accept counter value for PCNT event
  223. *
  224. * @return
  225. * - ESP_OK Success
  226. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  227. * - ESP_ERR_INVALID_ARG Parameter error
  228. */
  229. esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value);
  230. /**
  231. * @brief Get PCNT event status of PCNT unit
  232. *
  233. * @param unit PCNT unit number
  234. * @param status Pointer to accept event status word
  235. * @return
  236. * - ESP_OK Success
  237. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  238. * - ESP_ERR_INVALID_ARG Parameter error
  239. */
  240. esp_err_t pcnt_get_event_status(pcnt_unit_t unit, uint32_t *status);
  241. /**
  242. * @brief Unregister PCNT interrupt handler (registered by pcnt_isr_register), the handler is an ISR.
  243. * The handler will be attached to the same CPU core that this function is running on.
  244. * If the interrupt service is registered by pcnt_isr_service_install, please call pcnt_isr_service_uninstall instead
  245. *
  246. * @param handle handle to unregister the ISR service.
  247. *
  248. * @return
  249. * - ESP_OK Success
  250. * - ESP_ERR_NOT_FOUND Can not find the interrupt that matches the flags.
  251. * - ESP_ERR_INVALID_ARG Function pointer error.
  252. */
  253. esp_err_t pcnt_isr_unregister(pcnt_isr_handle_t handle);
  254. /**
  255. * @brief Register PCNT interrupt handler, the handler is an ISR.
  256. * The handler will be attached to the same CPU core that this function is running on.
  257. * Please do not use pcnt_isr_service_install if this function was called.
  258. *
  259. * @param fn Interrupt handler function.
  260. * @param arg Parameter for handler function
  261. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  262. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  263. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  264. * be returned here. Calling pcnt_isr_unregister to unregister this ISR service if needed,
  265. * but only if the handle is not NULL.
  266. *
  267. * @return
  268. * - ESP_OK Success
  269. * - ESP_ERR_NOT_FOUND Can not find the interrupt that matches the flags.
  270. * - ESP_ERR_INVALID_ARG Function pointer error.
  271. */
  272. esp_err_t pcnt_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, pcnt_isr_handle_t *handle);
  273. /**
  274. * @brief Configure PCNT pulse signal input pin and control input pin
  275. *
  276. * @param unit PCNT unit number
  277. * @param channel PCNT channel number
  278. * @param pulse_io Pulse signal input GPIO
  279. * @param ctrl_io Control signal input GPIO
  280. *
  281. * @note Set the signal input to PCNT_PIN_NOT_USED if unused.
  282. *
  283. * @return
  284. * - ESP_OK Success
  285. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  286. * - ESP_ERR_INVALID_ARG Parameter error
  287. */
  288. esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io);
  289. /**
  290. * @brief Enable PCNT input filter
  291. *
  292. * @param unit PCNT unit number
  293. *
  294. * @return
  295. * - ESP_OK Success
  296. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  297. * - ESP_ERR_INVALID_ARG Parameter error
  298. */
  299. esp_err_t pcnt_filter_enable(pcnt_unit_t unit);
  300. /**
  301. * @brief Disable PCNT input filter
  302. *
  303. * @param unit PCNT unit number
  304. *
  305. * @return
  306. * - ESP_OK Success
  307. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  308. * - ESP_ERR_INVALID_ARG Parameter error
  309. */
  310. esp_err_t pcnt_filter_disable(pcnt_unit_t unit);
  311. /**
  312. * @brief Set PCNT filter value
  313. *
  314. * @param unit PCNT unit number
  315. * @param filter_val PCNT signal filter value, counter in APB_CLK cycles.
  316. * Any pulses lasting shorter than this will be ignored when the filter is enabled.
  317. * @note
  318. * filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
  319. *
  320. * @return
  321. * - ESP_OK Success
  322. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  323. * - ESP_ERR_INVALID_ARG Parameter error
  324. */
  325. esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val);
  326. /**
  327. * @brief Get PCNT filter value
  328. *
  329. * @param unit PCNT unit number
  330. * @param filter_val Pointer to accept PCNT filter value.
  331. *
  332. * @return
  333. * - ESP_OK Success
  334. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  335. * - ESP_ERR_INVALID_ARG Parameter error
  336. */
  337. esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val);
  338. /**
  339. * @brief Set PCNT counter mode
  340. *
  341. * @param unit PCNT unit number
  342. * @param channel PCNT channel number
  343. * @param pos_mode Counter mode when detecting positive edge
  344. * @param neg_mode Counter mode when detecting negative edge
  345. * @param hctrl_mode Counter mode when control signal is high level
  346. * @param lctrl_mode Counter mode when control signal is low level
  347. *
  348. * @return
  349. * - ESP_OK Success
  350. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  351. * - ESP_ERR_INVALID_ARG Parameter error
  352. */
  353. esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel,
  354. pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode,
  355. pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode);
  356. /**
  357. * @brief Add ISR handler for specified unit.
  358. *
  359. * Call this function after using pcnt_isr_service_install() to
  360. * install the PCNT driver's ISR handler service.
  361. *
  362. * The ISR handlers do not need to be declared with IRAM_ATTR,
  363. * unless you pass the ESP_INTR_FLAG_IRAM flag when allocating the
  364. * ISR in pcnt_isr_service_install().
  365. *
  366. * This ISR handler will be called from an ISR. So there is a stack
  367. * size limit (configurable as "ISR stack size" in menuconfig). This
  368. * limit is smaller compared to a global PCNT interrupt handler due
  369. * to the additional level of indirection.
  370. *
  371. * @param unit PCNT unit number
  372. * @param isr_handler Interrupt handler function.
  373. * @param args Parameter for handler function
  374. *
  375. * @return
  376. * - ESP_OK Success
  377. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  378. * - ESP_ERR_INVALID_ARG Parameter error
  379. */
  380. esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void(*isr_handler)(void *), void *args);
  381. /**
  382. * @brief Install PCNT ISR service.
  383. * @note We can manage different interrupt service for each unit.
  384. * This function will use the default ISR handle service, Calling pcnt_isr_service_uninstall to
  385. * uninstall the default service if needed. Please do not use pcnt_isr_register if this function was called.
  386. *
  387. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  388. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  389. *
  390. * @return
  391. * - ESP_OK Success
  392. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  393. * - ESP_ERR_NO_MEM No memory to install this service
  394. * - ESP_ERR_INVALID_STATE ISR service already installed
  395. */
  396. esp_err_t pcnt_isr_service_install(int intr_alloc_flags);
  397. /**
  398. * @brief Uninstall PCNT ISR service, freeing related resources.
  399. */
  400. void pcnt_isr_service_uninstall(void);
  401. /**
  402. * @brief Delete ISR handler for specified unit.
  403. *
  404. * @param unit PCNT unit number
  405. *
  406. * @return
  407. * - ESP_OK Success
  408. * - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
  409. * - ESP_ERR_INVALID_ARG Parameter error
  410. */
  411. esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit);
  412. #ifdef __cplusplus
  413. }
  414. #endif