pcnt.c 20 KB

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