pcnt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "esp_log.h"
  8. #include "esp_check.h"
  9. #include "soc/soc_caps.h"
  10. #include "esp_private/periph_ctrl.h"
  11. #include "driver/pcnt.h"
  12. #include "hal/pcnt_hal.h"
  13. #include "hal/pcnt_ll.h"
  14. #include "hal/gpio_hal.h"
  15. #include "soc/pcnt_periph.h"
  16. #include "esp_rom_gpio.h"
  17. #define PCNT_CHANNEL_ERR_STR "PCNT CHANNEL ERROR"
  18. #define PCNT_UNIT_ERR_STR "PCNT UNIT ERROR"
  19. #define PCNT_GPIO_ERR_STR "PCNT GPIO NUM ERROR"
  20. #define PCNT_ADDRESS_ERR_STR "PCNT ADDRESS ERROR"
  21. #define PCNT_PARAM_ERR_STR "PCNT PARAM ERROR"
  22. #define PCNT_COUNT_MODE_ERR_STR "PCNT COUNTER MODE ERROR"
  23. #define PCNT_CTRL_MODE_ERR_STR "PCNT CTRL MODE ERROR"
  24. #define PCNT_EVT_TYPE_ERR_STR "PCNT value type error"
  25. #define PCNT_LIMT_VAL_ERR_STR "PCNT limit value error"
  26. #define PCNT_NUM_ERR_STR "PCNT num error"
  27. #define PCNT_DRIVER_ERR_STR "PCNT driver error"
  28. #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
  29. #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
  30. static const char *TAG = "pcnt";
  31. #define PCNT_CHECK(a, str, ret_val) ESP_RETURN_ON_FALSE(a, ret_val, TAG, "%s", str)
  32. typedef struct {
  33. pcnt_hal_context_t hal; /*!< PCNT hal context*/
  34. } pcnt_obj_t;
  35. static pcnt_obj_t *p_pcnt_obj[PCNT_PORT_MAX] = {0};
  36. #define PCNT_OBJ_CHECK(pcnt_port) { \
  37. PCNT_CHECK((pcnt_port < PCNT_PORT_MAX), PCNT_NUM_ERR_STR, ESP_ERR_INVALID_ARG); \
  38. PCNT_CHECK((p_pcnt_obj[pcnt_port]), PCNT_DRIVER_ERR_STR, ESP_ERR_INVALID_STATE); \
  39. }
  40. typedef struct {
  41. void(*fn)(void *args); /*!< isr function */
  42. void *args; /*!< isr function args */
  43. } pcnt_isr_func_t;
  44. static pcnt_isr_func_t *pcnt_isr_func = NULL;
  45. static pcnt_isr_handle_t pcnt_isr_service = NULL;
  46. static portMUX_TYPE pcnt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  47. static inline esp_err_t _pcnt_set_mode(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode, pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode)
  48. {
  49. PCNT_OBJ_CHECK(pcnt_port);
  50. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  51. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  52. PCNT_CHECK((pos_mode < PCNT_COUNT_MAX) && (neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  53. PCNT_CHECK((hctrl_mode < PCNT_MODE_MAX) && (lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  54. pcnt_ll_set_edge_action(p_pcnt_obj[pcnt_port]->hal.dev, unit, channel, pos_mode, neg_mode);
  55. pcnt_ll_set_level_action(p_pcnt_obj[pcnt_port]->hal.dev, unit, channel, hctrl_mode, lctrl_mode);
  56. return ESP_OK;
  57. }
  58. static inline esp_err_t _pcnt_set_pin(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
  59. {
  60. PCNT_OBJ_CHECK(pcnt_port);
  61. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  62. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  63. PCNT_CHECK(GPIO_IS_VALID_GPIO(pulse_io) || pulse_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  64. PCNT_CHECK(GPIO_IS_VALID_GPIO(ctrl_io) || ctrl_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  65. if (pulse_io >= 0) {
  66. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[pulse_io], PIN_FUNC_GPIO);
  67. gpio_set_direction(pulse_io, GPIO_MODE_INPUT);
  68. gpio_set_pull_mode(pulse_io, GPIO_PULLUP_ONLY);
  69. esp_rom_gpio_connect_in_signal(pulse_io, pcnt_periph_signals.groups[pcnt_port].units[unit].channels[channel].pulse_sig, 0);
  70. }
  71. if (ctrl_io >= 0) {
  72. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctrl_io], PIN_FUNC_GPIO);
  73. gpio_set_direction(ctrl_io, GPIO_MODE_INPUT);
  74. gpio_set_pull_mode(ctrl_io, GPIO_PULLUP_ONLY);
  75. esp_rom_gpio_connect_in_signal(ctrl_io, pcnt_periph_signals.groups[pcnt_port].units[unit].channels[channel].control_sig, 0);
  76. }
  77. return ESP_OK;
  78. }
  79. static inline esp_err_t _pcnt_get_counter_value(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit, int16_t *count)
  80. {
  81. PCNT_OBJ_CHECK(pcnt_port);
  82. PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  83. PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  84. *count = pcnt_ll_get_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
  85. return ESP_OK;
  86. }
  87. static inline esp_err_t _pcnt_counter_pause(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
  88. {
  89. PCNT_OBJ_CHECK(pcnt_port);
  90. PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  91. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  92. pcnt_ll_stop_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
  93. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  94. return ESP_OK;
  95. }
  96. static inline esp_err_t _pcnt_counter_resume(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
  97. {
  98. PCNT_OBJ_CHECK(pcnt_port);
  99. PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  100. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  101. pcnt_ll_start_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
  102. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  103. return ESP_OK;
  104. }
  105. static inline esp_err_t _pcnt_counter_clear(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
  106. {
  107. PCNT_OBJ_CHECK(pcnt_port);
  108. PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  109. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  110. pcnt_ll_clear_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
  111. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  112. return ESP_OK;
  113. }
  114. static inline esp_err_t _pcnt_intr_enable(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit, bool enable)
  115. {
  116. PCNT_OBJ_CHECK(pcnt_port);
  117. PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  118. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  119. pcnt_ll_enable_intr(p_pcnt_obj[pcnt_port]->hal.dev, 1 << pcnt_unit, enable);
  120. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  121. return ESP_OK;
  122. }
  123. static inline esp_err_t _pcnt_event_enable(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, bool enable)
  124. {
  125. PCNT_OBJ_CHECK(pcnt_port);
  126. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  127. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  128. switch (evt_type) {
  129. case PCNT_EVT_THRES_1:
  130. pcnt_ll_enable_thres_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1, enable);
  131. break;
  132. case PCNT_EVT_THRES_0:
  133. pcnt_ll_enable_thres_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0, enable);
  134. break;
  135. case PCNT_EVT_L_LIM:
  136. pcnt_ll_enable_low_limit_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
  137. break;
  138. case PCNT_EVT_H_LIM:
  139. pcnt_ll_enable_high_limit_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
  140. break;
  141. case PCNT_EVT_ZERO:
  142. pcnt_ll_enable_zero_cross_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
  143. break;
  144. default:
  145. PCNT_CHECK(false, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  146. break;
  147. }
  148. return ESP_OK;
  149. }
  150. static inline esp_err_t _pcnt_set_event_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
  151. {
  152. PCNT_OBJ_CHECK(pcnt_port);
  153. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  154. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  155. PCNT_CHECK(!(evt_type == PCNT_EVT_L_LIM && value > 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  156. PCNT_CHECK(!(evt_type == PCNT_EVT_H_LIM && value < 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  157. switch (evt_type) {
  158. case PCNT_EVT_THRES_1:
  159. pcnt_ll_set_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1, value);
  160. break;
  161. case PCNT_EVT_THRES_0:
  162. pcnt_ll_set_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0, value);
  163. break;
  164. case PCNT_EVT_L_LIM:
  165. pcnt_ll_set_low_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, value);
  166. break;
  167. case PCNT_EVT_H_LIM:
  168. pcnt_ll_set_high_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, value);
  169. break;
  170. default:
  171. break;
  172. }
  173. return ESP_OK;
  174. }
  175. static inline esp_err_t _pcnt_get_event_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
  176. {
  177. PCNT_OBJ_CHECK(pcnt_port);
  178. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  179. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  180. PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  181. switch (evt_type) {
  182. case PCNT_EVT_THRES_1:
  183. *value = pcnt_ll_get_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1);
  184. break;
  185. case PCNT_EVT_THRES_0:
  186. *value = pcnt_ll_get_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0);
  187. break;
  188. case PCNT_EVT_L_LIM:
  189. *value = pcnt_ll_get_low_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit);
  190. break;
  191. case PCNT_EVT_H_LIM:
  192. *value = pcnt_ll_get_high_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit);
  193. break;
  194. default:
  195. break;
  196. }
  197. return ESP_OK;
  198. }
  199. static inline esp_err_t _pcnt_get_event_status(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint32_t *status)
  200. {
  201. PCNT_OBJ_CHECK(pcnt_port);
  202. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  203. PCNT_CHECK(status != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  204. *status = pcnt_ll_get_unit_status(p_pcnt_obj[pcnt_port]->hal.dev, unit);
  205. return ESP_OK;
  206. }
  207. static inline esp_err_t _pcnt_set_filter_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint16_t filter_val)
  208. {
  209. PCNT_OBJ_CHECK(pcnt_port);
  210. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  211. PCNT_CHECK(filter_val < 1024, PCNT_PARAM_ERR_STR, ESP_ERR_INVALID_ARG);
  212. pcnt_ll_set_glitch_filter_thres(p_pcnt_obj[pcnt_port]->hal.dev, unit, filter_val);
  213. return ESP_OK;
  214. }
  215. static inline esp_err_t _pcnt_get_filter_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint16_t *filter_val)
  216. {
  217. PCNT_OBJ_CHECK(pcnt_port);
  218. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  219. PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  220. *filter_val = (uint16_t)pcnt_ll_get_glitch_filter_thres(p_pcnt_obj[pcnt_port]->hal.dev, unit);
  221. return ESP_OK;
  222. }
  223. static inline esp_err_t _pcnt_filter_enable(pcnt_port_t pcnt_port, pcnt_unit_t unit, bool enable)
  224. {
  225. PCNT_OBJ_CHECK(pcnt_port);
  226. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  227. pcnt_ll_enable_glitch_filter(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
  228. return ESP_OK;
  229. }
  230. static inline esp_err_t _pcnt_isr_handler_add(pcnt_port_t pcnt_port, pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
  231. {
  232. PCNT_OBJ_CHECK(pcnt_port);
  233. PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed, call pcnt_install_isr_service() first", ESP_ERR_INVALID_STATE);
  234. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, "PCNT unit error", ESP_ERR_INVALID_ARG);
  235. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  236. _pcnt_intr_enable(PCNT_PORT_0, unit, false);
  237. if (pcnt_isr_func) {
  238. pcnt_isr_func[unit].fn = isr_handler;
  239. pcnt_isr_func[unit].args = args;
  240. }
  241. _pcnt_intr_enable(PCNT_PORT_0, unit, true);
  242. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  243. return ESP_OK;
  244. }
  245. static inline esp_err_t _pcnt_isr_handler_remove(pcnt_port_t pcnt_port, pcnt_unit_t unit)
  246. {
  247. PCNT_OBJ_CHECK(pcnt_port);
  248. PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed", ESP_ERR_INVALID_STATE);
  249. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, "PCNT unit error", ESP_ERR_INVALID_ARG);
  250. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  251. _pcnt_intr_enable(PCNT_PORT_0, unit, false);
  252. if (pcnt_isr_func) {
  253. pcnt_isr_func[unit].fn = NULL;
  254. pcnt_isr_func[unit].args = NULL;
  255. }
  256. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  257. return ESP_OK;
  258. }
  259. // pcnt interrupt service
  260. static void IRAM_ATTR pcnt_intr_service(void *arg)
  261. {
  262. uint32_t status = 0;
  263. pcnt_port_t pcnt_port = (pcnt_port_t)arg;
  264. status = pcnt_ll_get_intr_status(p_pcnt_obj[pcnt_port]->hal.dev);
  265. pcnt_ll_clear_intr_status(p_pcnt_obj[pcnt_port]->hal.dev, status);
  266. while (status) {
  267. int unit = __builtin_ffs(status) - 1;
  268. status &= ~(1 << unit);
  269. if (pcnt_isr_func[unit].fn != NULL) {
  270. (pcnt_isr_func[unit].fn)(pcnt_isr_func[unit].args);
  271. }
  272. }
  273. }
  274. static inline esp_err_t _pcnt_isr_service_install(pcnt_port_t pcnt_port, int intr_alloc_flags)
  275. {
  276. PCNT_OBJ_CHECK(pcnt_port);
  277. PCNT_CHECK(pcnt_isr_func == NULL, "ISR service already installed", ESP_ERR_INVALID_STATE);
  278. esp_err_t ret = ESP_FAIL;
  279. pcnt_isr_func = (pcnt_isr_func_t *) calloc(SOC_PCNT_UNITS_PER_GROUP, sizeof(pcnt_isr_func_t));
  280. if (pcnt_isr_func == NULL) {
  281. ret = ESP_ERR_NO_MEM;
  282. } else {
  283. ret = pcnt_isr_register(pcnt_intr_service, (void *)pcnt_port, intr_alloc_flags, &pcnt_isr_service);
  284. if (ret != ESP_OK) {
  285. ESP_LOGE(TAG, "pcnt isr registration failed, maybe you need `pcnt_isr_unregister` to unregister your isr");
  286. free(pcnt_isr_func);
  287. pcnt_isr_func = NULL;
  288. }
  289. }
  290. return ret;
  291. }
  292. static inline esp_err_t _pcnt_isr_service_uninstall(pcnt_port_t pcnt_port)
  293. {
  294. PCNT_OBJ_CHECK(pcnt_port);
  295. PCNT_CHECK(pcnt_isr_func != NULL, "ISR Service not installed yet.", ESP_ERR_INVALID_STATE);
  296. esp_err_t ret = ESP_FAIL;
  297. ret = pcnt_isr_unregister(pcnt_isr_service);
  298. free(pcnt_isr_func);
  299. pcnt_isr_func = NULL;
  300. pcnt_isr_service = NULL;
  301. return ret;
  302. }
  303. static inline esp_err_t _pcnt_unit_config(pcnt_port_t pcnt_port, const pcnt_config_t *pcnt_config)
  304. {
  305. PCNT_OBJ_CHECK(pcnt_port);
  306. uint8_t unit = pcnt_config->unit;
  307. uint8_t channel = pcnt_config->channel;
  308. int input_io = pcnt_config->pulse_gpio_num;
  309. int ctrl_io = pcnt_config->ctrl_gpio_num;
  310. PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  311. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  312. PCNT_CHECK(input_io < 0 || (GPIO_IS_VALID_GPIO(input_io) && (input_io != ctrl_io)), "PCNT pulse input io error", ESP_ERR_INVALID_ARG);
  313. PCNT_CHECK(ctrl_io < 0 || GPIO_IS_VALID_GPIO(ctrl_io), "PCNT ctrl io error", ESP_ERR_INVALID_ARG);
  314. PCNT_CHECK((pcnt_config->pos_mode < PCNT_COUNT_MAX) && (pcnt_config->neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  315. PCNT_CHECK((pcnt_config->hctrl_mode < PCNT_MODE_MAX) && (pcnt_config->lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  316. /*Enalbe hardware module*/
  317. static bool pcnt_enable = false;
  318. if (pcnt_enable == false) {
  319. periph_module_reset(pcnt_periph_signals.groups[pcnt_port].module);
  320. pcnt_enable = true;
  321. }
  322. periph_module_enable(pcnt_periph_signals.groups[pcnt_port].module);
  323. /*Set counter range*/
  324. _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim);
  325. _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim);
  326. /*Default value after reboot is positive, we disable these events like others*/
  327. _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_H_LIM, false);
  328. _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_L_LIM, false);
  329. _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_ZERO, false);
  330. _pcnt_filter_enable(pcnt_port, unit, false);
  331. /*set pulse input and control mode*/
  332. _pcnt_set_mode(pcnt_port, unit, channel, pcnt_config->pos_mode, pcnt_config->neg_mode, pcnt_config->hctrl_mode, pcnt_config->lctrl_mode);
  333. /*Set pulse input and control pins*/
  334. _pcnt_set_pin(pcnt_port, unit, channel, input_io, ctrl_io);
  335. return ESP_OK;
  336. }
  337. esp_err_t pcnt_deinit(pcnt_port_t pcnt_port)
  338. {
  339. PCNT_OBJ_CHECK(pcnt_port);
  340. heap_caps_free(p_pcnt_obj[pcnt_port]);
  341. p_pcnt_obj[pcnt_port] = NULL;
  342. return ESP_OK;
  343. }
  344. esp_err_t pcnt_init(pcnt_port_t pcnt_port)
  345. {
  346. PCNT_CHECK((pcnt_port < PCNT_PORT_MAX), PCNT_NUM_ERR_STR, ESP_ERR_INVALID_ARG);
  347. PCNT_CHECK((p_pcnt_obj[pcnt_port]) == NULL, "pcnt driver already initted", ESP_ERR_INVALID_STATE);
  348. p_pcnt_obj[pcnt_port] = (pcnt_obj_t *)heap_caps_calloc(1, sizeof(pcnt_obj_t), MALLOC_CAP_DEFAULT);
  349. if (p_pcnt_obj[pcnt_port] == NULL) {
  350. ESP_LOGE(TAG, "PCNT driver malloc error");
  351. return ESP_FAIL;
  352. }
  353. pcnt_hal_init(&(p_pcnt_obj[pcnt_port]->hal), pcnt_port);
  354. return ESP_OK;
  355. }
  356. esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
  357. {
  358. esp_err_t ret;
  359. if ((p_pcnt_obj[PCNT_PORT_0]) == NULL) {
  360. ret = pcnt_init(PCNT_PORT_0);
  361. if (ret != ESP_OK) {
  362. return ret;
  363. }
  364. }
  365. return _pcnt_unit_config(PCNT_PORT_0, pcnt_config);
  366. }
  367. esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode, pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode)
  368. {
  369. return _pcnt_set_mode(PCNT_PORT_0, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode);
  370. }
  371. esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
  372. {
  373. return _pcnt_set_pin(PCNT_PORT_0, unit, channel, pulse_io, ctrl_io);
  374. }
  375. esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t *count)
  376. {
  377. return _pcnt_get_counter_value(PCNT_PORT_0, pcnt_unit, count);
  378. }
  379. esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
  380. {
  381. return _pcnt_counter_pause(PCNT_PORT_0, pcnt_unit);
  382. }
  383. esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
  384. {
  385. return _pcnt_counter_resume(PCNT_PORT_0, pcnt_unit);
  386. }
  387. esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
  388. {
  389. return _pcnt_counter_clear(PCNT_PORT_0, pcnt_unit);
  390. }
  391. esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
  392. {
  393. return _pcnt_intr_enable(PCNT_PORT_0, pcnt_unit, true);
  394. }
  395. esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
  396. {
  397. return _pcnt_intr_enable(PCNT_PORT_0, pcnt_unit, false);
  398. }
  399. esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  400. {
  401. return _pcnt_event_enable(PCNT_PORT_0, unit, evt_type, true);
  402. }
  403. esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  404. {
  405. return _pcnt_event_enable(PCNT_PORT_0, unit, evt_type, false);
  406. }
  407. esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
  408. {
  409. return _pcnt_set_event_value(PCNT_PORT_0, unit, evt_type, value);
  410. }
  411. esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
  412. {
  413. return _pcnt_get_event_value(PCNT_PORT_0, unit, evt_type, value);
  414. }
  415. esp_err_t pcnt_get_event_status(pcnt_unit_t unit, uint32_t *status)
  416. {
  417. return _pcnt_get_event_status(PCNT_PORT_0, unit, status);
  418. }
  419. esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
  420. {
  421. return _pcnt_set_filter_value(PCNT_PORT_0, unit, filter_val);
  422. }
  423. esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
  424. {
  425. return _pcnt_get_filter_value(PCNT_PORT_0, unit, filter_val);
  426. }
  427. esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
  428. {
  429. return _pcnt_filter_enable(PCNT_PORT_0, unit, true);
  430. }
  431. esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
  432. {
  433. return _pcnt_filter_enable(PCNT_PORT_0, unit, false);
  434. }
  435. esp_err_t pcnt_isr_unregister(pcnt_isr_handle_t handle)
  436. {
  437. esp_err_t ret = ESP_FAIL;
  438. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  439. ret = esp_intr_free(handle);
  440. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  441. return ret;
  442. }
  443. esp_err_t pcnt_isr_register(void (*fun)(void *), void *arg, int intr_alloc_flags, pcnt_isr_handle_t *handle)
  444. {
  445. esp_err_t ret = ESP_FAIL;
  446. PCNT_CHECK(fun != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  447. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  448. ret = esp_intr_alloc(pcnt_periph_signals.groups[0].irq, intr_alloc_flags, fun, arg, handle);
  449. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  450. return ret;
  451. }
  452. esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
  453. {
  454. return _pcnt_isr_handler_add(PCNT_PORT_0, unit, isr_handler, args);
  455. }
  456. esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit)
  457. {
  458. return _pcnt_isr_handler_remove(PCNT_PORT_0, unit);
  459. }
  460. esp_err_t pcnt_isr_service_install(int intr_alloc_flags)
  461. {
  462. return _pcnt_isr_service_install(PCNT_PORT_0, intr_alloc_flags);
  463. }
  464. void pcnt_isr_service_uninstall()
  465. {
  466. _pcnt_isr_service_uninstall(PCNT_PORT_0);
  467. }