pcnt_legacy.c 21 KB

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