pcnt.c 19 KB

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