pcnt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_log.h"
  14. #include "esp_intr_alloc.h"
  15. #include "driver/pcnt.h"
  16. #include "driver/periph_ctrl.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_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
  27. #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
  28. #define PCNT_CHECK(a, str, ret_val) \
  29. if (!(a)) { \
  30. ESP_LOGE(PCNT_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  31. return (ret_val); \
  32. }
  33. typedef struct{
  34. void(*fn)(void *args); /*!< isr function */
  35. void* args; /*!< isr function args */
  36. } pcnt_isr_func_t;
  37. static pcnt_isr_func_t *pcnt_isr_func = NULL;
  38. static pcnt_isr_handle_t pcnt_isr_service = NULL;
  39. static portMUX_TYPE pcnt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  40. static const char* PCNT_TAG = "pcnt";
  41. esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
  42. {
  43. uint8_t unit = pcnt_config->unit;
  44. uint8_t channel = pcnt_config->channel;
  45. int input_io = pcnt_config->pulse_gpio_num;
  46. int ctrl_io = pcnt_config->ctrl_gpio_num;
  47. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  48. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  49. PCNT_CHECK(input_io < 0 || (GPIO_IS_VALID_GPIO(input_io) && (input_io != ctrl_io)), "PCNT pluse input io error", ESP_ERR_INVALID_ARG);
  50. PCNT_CHECK(ctrl_io < 0 || GPIO_IS_VALID_GPIO(ctrl_io), "PCNT ctrl io error", ESP_ERR_INVALID_ARG);
  51. 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);
  52. 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);
  53. /*Enalbe hardware module*/
  54. static bool pcnt_enable = false;
  55. if (pcnt_enable == false) {
  56. periph_module_reset(PERIPH_PCNT_MODULE);
  57. pcnt_enable = true;
  58. }
  59. periph_module_enable(PERIPH_PCNT_MODULE);
  60. /*Set counter range*/
  61. pcnt_set_event_value(unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim);
  62. pcnt_set_event_value(unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim);
  63. /*Default value after reboot is positive, we disable these events like others*/
  64. pcnt_event_disable(unit, PCNT_EVT_H_LIM);
  65. pcnt_event_disable(unit, PCNT_EVT_L_LIM);
  66. pcnt_event_disable(unit, PCNT_EVT_ZERO);
  67. pcnt_filter_disable(unit);
  68. /*set pulse input and control mode*/
  69. pcnt_set_mode(unit, channel, pcnt_config->pos_mode, pcnt_config->neg_mode, pcnt_config->hctrl_mode, pcnt_config->lctrl_mode);
  70. /*Set pulse input and control pins*/
  71. pcnt_set_pin(unit, channel, input_io, ctrl_io);
  72. return ESP_OK;
  73. }
  74. 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)
  75. {
  76. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  77. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  78. PCNT_CHECK((pos_mode < PCNT_COUNT_MAX) && (neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  79. PCNT_CHECK((hctrl_mode < PCNT_MODE_MAX) && (lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  80. if(channel == 0) {
  81. PCNT.conf_unit[unit].conf0.ch0_pos_mode = pos_mode;
  82. PCNT.conf_unit[unit].conf0.ch0_neg_mode = neg_mode;
  83. PCNT.conf_unit[unit].conf0.ch0_hctrl_mode = hctrl_mode;
  84. PCNT.conf_unit[unit].conf0.ch0_lctrl_mode = lctrl_mode;
  85. } else {
  86. PCNT.conf_unit[unit].conf0.ch1_pos_mode = pos_mode;
  87. PCNT.conf_unit[unit].conf0.ch1_neg_mode = neg_mode;
  88. PCNT.conf_unit[unit].conf0.ch1_hctrl_mode = hctrl_mode;
  89. PCNT.conf_unit[unit].conf0.ch1_lctrl_mode = lctrl_mode;
  90. }
  91. return ESP_OK;
  92. }
  93. esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
  94. {
  95. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  96. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  97. PCNT_CHECK(GPIO_IS_VALID_GPIO(pulse_io) || pulse_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  98. PCNT_CHECK(GPIO_IS_VALID_GPIO(ctrl_io) || ctrl_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  99. int sig_base = (channel == 0) ? PCNT_SIG_CH0_IN0_IDX : PCNT_SIG_CH1_IN0_IDX;
  100. int ctrl_base = (channel == 0) ? PCNT_CTRL_CH0_IN0_IDX : PCNT_CTRL_CH1_IN0_IDX;
  101. if (unit > 4) {
  102. sig_base += 12; // GPIO matrix assignments have a gap between units 4 & 5
  103. ctrl_base += 12;
  104. }
  105. int input_sig_index = sig_base + (4 * unit);
  106. int ctrl_sig_index = ctrl_base + (4 * unit);
  107. if(pulse_io >= 0) {
  108. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[pulse_io], PIN_FUNC_GPIO);
  109. gpio_set_direction(pulse_io, GPIO_MODE_INPUT);
  110. gpio_set_pull_mode(pulse_io, GPIO_PULLUP_ONLY);
  111. gpio_matrix_in(pulse_io, input_sig_index, 0);
  112. }
  113. if(ctrl_io >= 0) {
  114. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[ctrl_io], PIN_FUNC_GPIO);
  115. gpio_set_direction(ctrl_io, GPIO_MODE_INPUT);
  116. gpio_set_pull_mode(ctrl_io, GPIO_PULLUP_ONLY);
  117. gpio_matrix_in(ctrl_io, ctrl_sig_index, 0);
  118. }
  119. return ESP_OK;
  120. }
  121. esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t* count)
  122. {
  123. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  124. PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  125. *count = (int16_t) PCNT.cnt_unit[pcnt_unit].cnt_val;
  126. return ESP_OK;
  127. }
  128. esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
  129. {
  130. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  131. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  132. PCNT.ctrl.val |= BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2));
  133. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  134. return ESP_OK;
  135. }
  136. esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
  137. {
  138. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  139. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  140. PCNT.ctrl.val &= (~(BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2))));
  141. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  142. return ESP_OK;
  143. }
  144. esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
  145. {
  146. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  147. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  148. uint32_t reset_bit = BIT(PCNT_PLUS_CNT_RST_U0_S + (pcnt_unit * 2));
  149. PCNT.ctrl.val |= reset_bit;
  150. PCNT.ctrl.val &= ~reset_bit;
  151. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  152. return ESP_OK;
  153. }
  154. esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
  155. {
  156. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  157. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  158. PCNT.int_ena.val |= BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit);
  159. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  160. return ESP_OK;
  161. }
  162. esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
  163. {
  164. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  165. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  166. PCNT.int_ena.val &= (~(BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit)));
  167. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  168. return ESP_OK;
  169. }
  170. esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  171. {
  172. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  173. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  174. if(evt_type == PCNT_EVT_L_LIM) {
  175. PCNT.conf_unit[unit].conf0.thr_l_lim_en = 1;
  176. } else if(evt_type == PCNT_EVT_H_LIM) {
  177. PCNT.conf_unit[unit].conf0.thr_h_lim_en = 1;
  178. } else if(evt_type == PCNT_EVT_THRES_0) {
  179. PCNT.conf_unit[unit].conf0.thr_thres0_en = 1;
  180. } else if(evt_type == PCNT_EVT_THRES_1) {
  181. PCNT.conf_unit[unit].conf0.thr_thres1_en = 1;
  182. } else if(evt_type == PCNT_EVT_ZERO) {
  183. PCNT.conf_unit[unit].conf0.thr_zero_en = 1;
  184. }
  185. return ESP_OK;
  186. }
  187. esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  188. {
  189. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  190. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  191. if(evt_type == PCNT_EVT_L_LIM) {
  192. PCNT.conf_unit[unit].conf0.thr_l_lim_en = 0;
  193. } else if(evt_type == PCNT_EVT_H_LIM) {
  194. PCNT.conf_unit[unit].conf0.thr_h_lim_en = 0;
  195. } else if(evt_type == PCNT_EVT_THRES_0) {
  196. PCNT.conf_unit[unit].conf0.thr_thres0_en = 0;
  197. } else if(evt_type == PCNT_EVT_THRES_1) {
  198. PCNT.conf_unit[unit].conf0.thr_thres1_en = 0;
  199. } else if(evt_type == PCNT_EVT_ZERO) {
  200. PCNT.conf_unit[unit].conf0.thr_zero_en = 0;
  201. }
  202. return ESP_OK;
  203. }
  204. esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
  205. {
  206. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  207. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  208. PCNT_CHECK(!(evt_type == PCNT_EVT_L_LIM && value > 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  209. PCNT_CHECK(!(evt_type == PCNT_EVT_H_LIM && value < 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
  210. if(evt_type == PCNT_EVT_L_LIM) {
  211. PCNT.conf_unit[unit].conf2.cnt_l_lim = value;
  212. } else if(evt_type == PCNT_EVT_H_LIM) {
  213. PCNT.conf_unit[unit].conf2.cnt_h_lim = value;
  214. } else if(evt_type == PCNT_EVT_THRES_0) {
  215. PCNT.conf_unit[unit].conf1.cnt_thres0 = value;
  216. } else if(evt_type == PCNT_EVT_THRES_1) {
  217. PCNT.conf_unit[unit].conf1.cnt_thres1 = value;
  218. }
  219. return ESP_OK;
  220. }
  221. esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
  222. {
  223. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  224. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  225. PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  226. if(evt_type == PCNT_EVT_L_LIM) {
  227. *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_l_lim;
  228. } else if(evt_type == PCNT_EVT_H_LIM) {
  229. *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_h_lim;
  230. } else if(evt_type == PCNT_EVT_THRES_0) {
  231. *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres0;
  232. } else if(evt_type == PCNT_EVT_THRES_1) {
  233. *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres1;
  234. } else {
  235. *value = 0;
  236. }
  237. return ESP_OK;
  238. }
  239. esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
  240. {
  241. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  242. PCNT_CHECK(filter_val < 1024, PCNT_PARAM_ERR_STR, ESP_ERR_INVALID_ARG);
  243. PCNT.conf_unit[unit].conf0.filter_thres = filter_val;
  244. return ESP_OK;
  245. }
  246. esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
  247. {
  248. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  249. PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  250. *filter_val = PCNT.conf_unit[unit].conf0.filter_thres;
  251. return ESP_OK;
  252. }
  253. esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
  254. {
  255. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  256. PCNT.conf_unit[unit].conf0.filter_en = 1;
  257. return ESP_OK;
  258. }
  259. esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
  260. {
  261. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  262. PCNT.conf_unit[unit].conf0.filter_en = 0;
  263. return ESP_OK;
  264. }
  265. esp_err_t pcnt_isr_register(void (*fun)(void*), void * arg, int intr_alloc_flags, pcnt_isr_handle_t *handle)
  266. {
  267. PCNT_CHECK(fun != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  268. return esp_intr_alloc(ETS_PCNT_INTR_SOURCE, intr_alloc_flags, fun, arg, handle);
  269. }
  270. // pcnt interrupt service
  271. static void IRAM_ATTR pcnt_intr_service(void* arg)
  272. {
  273. uint32_t intr_status = PCNT.int_st.val;
  274. for (int unit = 0; unit < PCNT_UNIT_MAX; unit++) {
  275. if (intr_status & (BIT(unit))) {
  276. if (pcnt_isr_func[unit].fn != NULL) {
  277. (pcnt_isr_func[unit].fn)(pcnt_isr_func[unit].args);
  278. }
  279. PCNT.int_clr.val = BIT(unit);
  280. }
  281. }
  282. }
  283. esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
  284. {
  285. PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed, call pcnt_install_isr_service() first", ESP_ERR_INVALID_STATE);
  286. PCNT_CHECK(unit < PCNT_UNIT_MAX, "PCNT unit error", ESP_ERR_INVALID_ARG);
  287. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  288. pcnt_intr_disable(unit);
  289. if (pcnt_isr_func) {
  290. pcnt_isr_func[unit].fn = isr_handler;
  291. pcnt_isr_func[unit].args = args;
  292. }
  293. pcnt_intr_enable(unit);
  294. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  295. return ESP_OK;
  296. }
  297. esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit)
  298. {
  299. PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed", ESP_ERR_INVALID_STATE);
  300. PCNT_CHECK(unit < PCNT_UNIT_MAX, "PCNT unit error", ESP_ERR_INVALID_ARG);
  301. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  302. pcnt_intr_disable(unit);
  303. if (pcnt_isr_func) {
  304. pcnt_isr_func[unit].fn = NULL;
  305. pcnt_isr_func[unit].args = NULL;
  306. }
  307. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  308. return ESP_OK;
  309. }
  310. esp_err_t pcnt_isr_service_install(int intr_alloc_flags)
  311. {
  312. PCNT_CHECK(pcnt_isr_func == NULL, "ISR service already installed", ESP_ERR_INVALID_STATE);
  313. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  314. esp_err_t ret = ESP_FAIL;
  315. pcnt_isr_func = (pcnt_isr_func_t*) calloc(PCNT_UNIT_MAX, sizeof(pcnt_isr_func_t));
  316. if (pcnt_isr_func == NULL) {
  317. ret = ESP_ERR_NO_MEM;
  318. } else {
  319. ret = pcnt_isr_register(pcnt_intr_service, NULL, intr_alloc_flags, &pcnt_isr_service);
  320. }
  321. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  322. return ret;
  323. }
  324. void pcnt_isr_service_uninstall(void)
  325. {
  326. if (pcnt_isr_func == NULL) {
  327. return;
  328. }
  329. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  330. esp_intr_free(pcnt_isr_service);
  331. free(pcnt_isr_func);
  332. pcnt_isr_func = NULL;
  333. pcnt_isr_service = NULL;
  334. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  335. }