pcnt.c 19 KB

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