pulse_cnt.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <sys/lock.h>
  8. #include "sdkconfig.h"
  9. #if CONFIG_PCNT_ENABLE_DEBUG_LOG
  10. // The local log level must be defined before including esp_log.h
  11. // Set the maximum log level for this source file
  12. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  13. #endif
  14. #include "freertos/FreeRTOS.h"
  15. #include "esp_heap_caps.h"
  16. #include "esp_intr_alloc.h"
  17. #include "esp_attr.h"
  18. #include "esp_log.h"
  19. #include "esp_check.h"
  20. #include "esp_pm.h"
  21. #include "esp_rom_gpio.h"
  22. #include "soc/soc_caps.h"
  23. #include "soc/pcnt_periph.h"
  24. #include "hal/pcnt_hal.h"
  25. #include "hal/pcnt_ll.h"
  26. #include "hal/gpio_hal.h"
  27. #include "esp_private/esp_clk.h"
  28. #include "esp_private/periph_ctrl.h"
  29. #include "driver/gpio.h"
  30. #include "driver/pulse_cnt.h"
  31. // If ISR handler is allowed to run whilst cache is disabled,
  32. // Make sure all the code and related variables used by the handler are in the SRAM
  33. #if CONFIG_PCNT_ISR_IRAM_SAFE || CONFIG_PCNT_CTRL_FUNC_IN_IRAM
  34. #define PCNT_MEM_ALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
  35. #else
  36. #define PCNT_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
  37. #endif
  38. #if CONFIG_PCNT_ISR_IRAM_SAFE
  39. #define PCNT_INTR_ALLOC_FLAGS (ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_SHARED)
  40. #else
  41. #define PCNT_INTR_ALLOC_FLAGS (ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_SHARED)
  42. #endif
  43. #define PCNT_PM_LOCK_NAME_LEN_MAX 16
  44. static const char *TAG = "pcnt";
  45. typedef struct pcnt_platform_t pcnt_platform_t;
  46. typedef struct pcnt_group_t pcnt_group_t;
  47. typedef struct pcnt_unit_t pcnt_unit_t;
  48. typedef struct pcnt_chan_t pcnt_chan_t;
  49. struct pcnt_platform_t {
  50. _lock_t mutex; // platform level mutex lock
  51. pcnt_group_t *groups[SOC_PCNT_GROUPS]; // pcnt group pool
  52. int group_ref_counts[SOC_PCNT_GROUPS]; // reference count used to protect group install/uninstall
  53. };
  54. struct pcnt_group_t {
  55. int group_id; // Group ID, index from 0
  56. portMUX_TYPE spinlock; // to protect per-group register level concurrent access
  57. pcnt_hal_context_t hal;
  58. pcnt_unit_t *units[SOC_PCNT_UNITS_PER_GROUP]; // array of PCNT units
  59. };
  60. typedef struct {
  61. pcnt_ll_watch_event_id_t event_id; // event type
  62. int watch_point_value; // value to be watched
  63. } pcnt_watch_point_t;
  64. typedef enum {
  65. PCNT_UNIT_FSM_INIT,
  66. PCNT_UNIT_FSM_ENABLE,
  67. } pcnt_unit_fsm_t;
  68. struct pcnt_unit_t {
  69. pcnt_group_t *group; // which group the pcnt unit belongs to
  70. portMUX_TYPE spinlock; // Spinlock, stop one unit from accessing different parts of a same register concurrently
  71. uint32_t unit_id; // allocated unit numerical ID
  72. int low_limit; // low limit value
  73. int high_limit; // high limit value
  74. pcnt_chan_t *channels[SOC_PCNT_CHANNELS_PER_UNIT]; // array of PCNT channels
  75. pcnt_watch_point_t watchers[PCNT_LL_WATCH_EVENT_MAX]; // array of PCNT watchers
  76. intr_handle_t intr; // interrupt handle
  77. esp_pm_lock_handle_t pm_lock; // PM lock, for glitch filter, as that module can only be functional under APB
  78. #if CONFIG_PM_ENABLE
  79. char pm_lock_name[PCNT_PM_LOCK_NAME_LEN_MAX]; // pm lock name
  80. #endif
  81. pcnt_unit_fsm_t fsm; // record PCNT unit's driver state
  82. pcnt_watch_cb_t on_reach; // user registered callback function
  83. void *user_data; // user data registered by user, which would be passed to the right callback function
  84. };
  85. struct pcnt_chan_t {
  86. pcnt_unit_t *unit; // pointer to the PCNT unit where it derives from
  87. uint32_t channel_id; // channel ID, index from 0
  88. int edge_gpio_num;
  89. int level_gpio_num;
  90. };
  91. // pcnt driver platform, it's always a singleton
  92. static pcnt_platform_t s_platform;
  93. static pcnt_group_t *pcnt_acquire_group_handle(int group_id);
  94. static void pcnt_release_group_handle(pcnt_group_t *group);
  95. static void pcnt_default_isr(void *args);
  96. static esp_err_t pcnt_register_to_group(pcnt_unit_t *unit)
  97. {
  98. pcnt_group_t *group = NULL;
  99. int unit_id = -1;
  100. for (int i = 0; i < SOC_PCNT_GROUPS; i++) {
  101. group = pcnt_acquire_group_handle(i);
  102. ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", i);
  103. // loop to search free unit in the group
  104. portENTER_CRITICAL(&group->spinlock);
  105. for (int j = 0; j < SOC_PCNT_UNITS_PER_GROUP; j++) {
  106. if (!group->units[j]) {
  107. unit_id = j;
  108. group->units[j] = unit;
  109. break;
  110. }
  111. }
  112. portEXIT_CRITICAL(&group->spinlock);
  113. if (unit_id < 0) {
  114. pcnt_release_group_handle(group);
  115. group = NULL;
  116. } else {
  117. unit->group = group;
  118. unit->unit_id = unit_id;
  119. break;
  120. }
  121. }
  122. ESP_RETURN_ON_FALSE(unit_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free unit");
  123. return ESP_OK;
  124. }
  125. static void pcnt_unregister_from_group(pcnt_unit_t *unit)
  126. {
  127. pcnt_group_t *group = unit->group;
  128. int unit_id = unit->unit_id;
  129. portENTER_CRITICAL(&group->spinlock);
  130. group->units[unit_id] = NULL;
  131. portEXIT_CRITICAL(&group->spinlock);
  132. // unit has a reference on group, release it now
  133. pcnt_release_group_handle(group);
  134. }
  135. static esp_err_t pcnt_destory(pcnt_unit_t *unit)
  136. {
  137. if (unit->pm_lock) {
  138. ESP_RETURN_ON_ERROR(esp_pm_lock_delete(unit->pm_lock), TAG, "delete pm lock failed");
  139. }
  140. if (unit->intr) {
  141. ESP_RETURN_ON_ERROR(esp_intr_free(unit->intr), TAG, "delete interrupt service failed");
  142. }
  143. if (unit->group) {
  144. pcnt_unregister_from_group(unit);
  145. }
  146. free(unit);
  147. return ESP_OK;
  148. }
  149. esp_err_t pcnt_new_unit(const pcnt_unit_config_t *config, pcnt_unit_handle_t *ret_unit)
  150. {
  151. #if CONFIG_PCNT_ENABLE_DEBUG_LOG
  152. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  153. #endif
  154. esp_err_t ret = ESP_OK;
  155. pcnt_unit_t *unit = NULL;
  156. ESP_GOTO_ON_FALSE(config && ret_unit, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  157. ESP_GOTO_ON_FALSE(config->low_limit < 0 && config->high_limit > 0 && config->low_limit >= PCNT_LL_MIN_LIN &&
  158. config->high_limit <= PCNT_LL_MAX_LIM, ESP_ERR_INVALID_ARG, err, TAG,
  159. "invalid limit range:[%d,%d]", config->low_limit, config->high_limit);
  160. unit = heap_caps_calloc(1, sizeof(pcnt_unit_t), PCNT_MEM_ALLOC_CAPS);
  161. ESP_GOTO_ON_FALSE(unit, ESP_ERR_NO_MEM, err, TAG, "no mem for unit");
  162. // register unit to the group (because one group can have several units)
  163. ESP_GOTO_ON_ERROR(pcnt_register_to_group(unit), err, TAG, "register unit failed");
  164. pcnt_group_t *group = unit->group;
  165. int group_id = group->group_id;
  166. int unit_id = unit->unit_id;
  167. // some events are enabled by default, disable them all
  168. pcnt_ll_disable_all_events(group->hal.dev, unit_id);
  169. // disable filter by default
  170. pcnt_ll_enable_glitch_filter(group->hal.dev, unit_id, false);
  171. // set default high/low limitation value
  172. // note: limit value takes effect only after counter clear
  173. pcnt_ll_set_high_limit_value(group->hal.dev, unit_id, config->high_limit);
  174. pcnt_ll_set_low_limit_value(group->hal.dev, unit_id, config->low_limit);
  175. unit->high_limit = config->high_limit;
  176. unit->low_limit = config->low_limit;
  177. // clear/pause register is shared by all units, so using group's spinlock
  178. portENTER_CRITICAL(&group->spinlock);
  179. pcnt_ll_stop_count(group->hal.dev, unit_id);
  180. pcnt_ll_clear_count(group->hal.dev, unit_id);
  181. pcnt_ll_enable_intr(group->hal.dev, PCNT_LL_UNIT_WATCH_EVENT(unit_id), false);
  182. pcnt_ll_clear_intr_status(group->hal.dev, PCNT_LL_UNIT_WATCH_EVENT(unit_id));
  183. portEXIT_CRITICAL(&group->spinlock);
  184. unit->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  185. unit->fsm = PCNT_UNIT_FSM_INIT;
  186. for (int i = 0; i < PCNT_LL_WATCH_EVENT_MAX; i++) {
  187. unit->watchers[i].event_id = PCNT_LL_WATCH_EVENT_INVALID; // invalid all watch point
  188. }
  189. ESP_LOGD(TAG, "new pcnt unit (%d,%d) at %p, count range:[%d,%d]", group_id, unit_id, unit, unit->low_limit, unit->high_limit);
  190. *ret_unit = unit;
  191. return ESP_OK;
  192. err:
  193. if (unit) {
  194. pcnt_destory(unit);
  195. }
  196. return ret;
  197. }
  198. esp_err_t pcnt_del_unit(pcnt_unit_handle_t unit)
  199. {
  200. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  201. ESP_RETURN_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "unit not in init state");
  202. pcnt_group_t *group = unit->group;
  203. int group_id = group->group_id;
  204. int unit_id = unit->unit_id;
  205. for (int i = 0; i < SOC_PCNT_CHANNELS_PER_UNIT; i++) {
  206. ESP_RETURN_ON_FALSE(!unit->channels[i], ESP_ERR_INVALID_STATE, TAG, "channel %d still in working", i);
  207. }
  208. ESP_LOGD(TAG, "del unit (%d,%d)", group_id, unit_id);
  209. // recycle memory resource
  210. ESP_RETURN_ON_ERROR(pcnt_destory(unit), TAG, "destory pcnt unit failed");
  211. return ESP_OK;
  212. }
  213. esp_err_t pcnt_unit_set_glitch_filter(pcnt_unit_handle_t unit, const pcnt_glitch_filter_config_t *config)
  214. {
  215. pcnt_group_t *group = NULL;
  216. uint32_t glitch_filter_thres = 0;
  217. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  218. // glitch filter should be set only when unit is in init state
  219. ESP_RETURN_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "unit not in init state");
  220. group = unit->group;
  221. if (config) {
  222. glitch_filter_thres = esp_clk_apb_freq() / 1000000 * config->max_glitch_ns / 1000;
  223. ESP_RETURN_ON_FALSE(glitch_filter_thres <= PCNT_LL_MAX_GLITCH_WIDTH, ESP_ERR_INVALID_ARG, TAG, "glitch width out of range");
  224. // The filter module is working against APB clock, so lazy install PM lock
  225. #if CONFIG_PM_ENABLE
  226. if (!unit->pm_lock) {
  227. sprintf(unit->pm_lock_name, "pcnt_%d_%d", group->group_id, unit->unit_id); // e.g. pcnt_0_0
  228. ESP_RETURN_ON_ERROR(esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, unit->pm_lock_name, &unit->pm_lock), TAG, "install pm lock failed");
  229. ESP_LOGD(TAG, "install APB_FREQ_MAX lock for unit (%d,%d)", group->group_id, unit->unit_id);
  230. }
  231. #endif
  232. }
  233. // filter control bit is mixed with other PCNT control bits in the same register
  234. portENTER_CRITICAL(&unit->spinlock);
  235. if (config) {
  236. pcnt_ll_set_glitch_filter_thres(group->hal.dev, unit->unit_id, glitch_filter_thres);
  237. pcnt_ll_enable_glitch_filter(group->hal.dev, unit->unit_id, true);
  238. } else {
  239. pcnt_ll_enable_glitch_filter(group->hal.dev, unit->unit_id, false);
  240. }
  241. portEXIT_CRITICAL(&unit->spinlock);
  242. return ESP_OK;
  243. }
  244. esp_err_t pcnt_unit_enable(pcnt_unit_handle_t unit)
  245. {
  246. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  247. ESP_RETURN_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "unit not in init state");
  248. // acquire power manager lock
  249. if (unit->pm_lock) {
  250. ESP_RETURN_ON_ERROR(esp_pm_lock_acquire(unit->pm_lock), TAG, "acquire pm_lock failed");
  251. }
  252. // enable interupt service
  253. if (unit->intr) {
  254. ESP_RETURN_ON_ERROR(esp_intr_enable(unit->intr), TAG, "enable interrupt service failed");
  255. }
  256. unit->fsm = PCNT_UNIT_FSM_ENABLE;
  257. return ESP_OK;
  258. }
  259. esp_err_t pcnt_unit_disable(pcnt_unit_handle_t unit)
  260. {
  261. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  262. ESP_RETURN_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "unit not in enable state");
  263. // disable interrupt service
  264. if (unit->intr) {
  265. ESP_RETURN_ON_ERROR(esp_intr_disable(unit->intr), TAG, "disable interrupt service failed");
  266. }
  267. // release power manager lock
  268. if (unit->pm_lock) {
  269. ESP_RETURN_ON_ERROR(esp_pm_lock_release(unit->pm_lock), TAG, "release APB_FREQ_MAX lock failed");
  270. }
  271. unit->fsm = PCNT_UNIT_FSM_INIT;
  272. return ESP_OK;
  273. }
  274. esp_err_t pcnt_unit_start(pcnt_unit_handle_t unit)
  275. {
  276. ESP_RETURN_ON_FALSE_ISR(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  277. ESP_RETURN_ON_FALSE_ISR(unit->fsm == PCNT_UNIT_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "unit not enabled yet");
  278. pcnt_group_t *group = unit->group;
  279. // all PCNT units share the same register to control counter
  280. portENTER_CRITICAL_SAFE(&group->spinlock);
  281. pcnt_ll_start_count(group->hal.dev, unit->unit_id);
  282. portEXIT_CRITICAL_SAFE(&group->spinlock);
  283. return ESP_OK;
  284. }
  285. esp_err_t pcnt_unit_stop(pcnt_unit_handle_t unit)
  286. {
  287. ESP_RETURN_ON_FALSE_ISR(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  288. ESP_RETURN_ON_FALSE_ISR(unit->fsm == PCNT_UNIT_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "unit not enabled yet");
  289. pcnt_group_t *group = unit->group;
  290. // all PCNT units share the same register to control counter
  291. portENTER_CRITICAL_SAFE(&group->spinlock);
  292. pcnt_ll_stop_count(group->hal.dev, unit->unit_id);
  293. portEXIT_CRITICAL_SAFE(&group->spinlock);
  294. return ESP_OK;
  295. }
  296. esp_err_t pcnt_unit_clear_count(pcnt_unit_handle_t unit)
  297. {
  298. pcnt_group_t *group = NULL;
  299. ESP_RETURN_ON_FALSE_ISR(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  300. group = unit->group;
  301. // all PCNT units share the same register to control counter
  302. portENTER_CRITICAL_SAFE(&group->spinlock);
  303. pcnt_ll_clear_count(group->hal.dev, unit->unit_id);
  304. portEXIT_CRITICAL_SAFE(&group->spinlock);
  305. return ESP_OK;
  306. }
  307. esp_err_t pcnt_unit_get_count(pcnt_unit_handle_t unit, int *value)
  308. {
  309. pcnt_group_t *group = NULL;
  310. ESP_RETURN_ON_FALSE_ISR(unit && value, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  311. group = unit->group;
  312. *value = pcnt_ll_get_count(group->hal.dev, unit->unit_id);
  313. return ESP_OK;
  314. }
  315. esp_err_t pcnt_unit_register_event_callbacks(pcnt_unit_handle_t unit, const pcnt_event_callbacks_t *cbs, void *user_data)
  316. {
  317. ESP_RETURN_ON_FALSE(unit && cbs, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  318. // unit event callbacks should be registered in init state
  319. ESP_RETURN_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "unit not in init state");
  320. pcnt_group_t *group = unit->group;
  321. int group_id = group->group_id;
  322. int unit_id = unit->unit_id;
  323. #if CONFIG_PCNT_ISR_IRAM_SAFE
  324. if (cbs->on_reach) {
  325. ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_reach), ESP_ERR_INVALID_ARG, TAG, "on_reach callback not in IRAM");
  326. }
  327. if (user_data) {
  328. ESP_RETURN_ON_FALSE(esp_ptr_internal(user_data), ESP_ERR_INVALID_ARG, TAG, "user context not in internal RAM");
  329. }
  330. #endif
  331. // lazy install interrupt service
  332. if (!unit->intr) {
  333. int isr_flags = PCNT_INTR_ALLOC_FLAGS;
  334. ESP_RETURN_ON_ERROR(esp_intr_alloc_intrstatus(pcnt_periph_signals.groups[group_id].irq, isr_flags,
  335. (uint32_t)pcnt_ll_get_intr_status_reg(group->hal.dev), PCNT_LL_UNIT_WATCH_EVENT(unit_id),
  336. pcnt_default_isr, unit, &unit->intr),
  337. TAG, "install interrupt service failed");
  338. }
  339. // enable/disable PCNT interrupt events
  340. portENTER_CRITICAL(&group->spinlock);
  341. pcnt_ll_enable_intr(group->hal.dev, PCNT_LL_UNIT_WATCH_EVENT(unit_id), cbs->on_reach != NULL);
  342. portEXIT_CRITICAL(&group->spinlock);
  343. unit->on_reach = cbs->on_reach;
  344. unit->user_data = user_data;
  345. return ESP_OK;
  346. }
  347. esp_err_t pcnt_unit_add_watch_point(pcnt_unit_handle_t unit, int watch_point)
  348. {
  349. esp_err_t ret = ESP_OK;
  350. pcnt_group_t *group = NULL;
  351. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  352. ESP_RETURN_ON_FALSE(watch_point <= unit->high_limit && watch_point >= unit->low_limit,
  353. ESP_ERR_INVALID_ARG, TAG, "watch_point out of limit");
  354. group = unit->group;
  355. // event enable/disable is mixed with other control function in the same register
  356. portENTER_CRITICAL(&unit->spinlock);
  357. // zero cross watch point
  358. if (watch_point == 0) {
  359. if (unit->watchers[PCNT_LL_WATCH_EVENT_ZERO_CROSS].event_id != PCNT_LL_WATCH_EVENT_INVALID) {
  360. ret = ESP_ERR_INVALID_STATE; // zero cross event watcher has been installed already
  361. } else {
  362. unit->watchers[PCNT_LL_WATCH_EVENT_ZERO_CROSS].event_id = PCNT_LL_WATCH_EVENT_ZERO_CROSS;
  363. unit->watchers[PCNT_LL_WATCH_EVENT_ZERO_CROSS].watch_point_value = 0;
  364. pcnt_ll_enable_zero_cross_event(group->hal.dev, unit->unit_id, true);
  365. }
  366. }
  367. // high limit watch point
  368. else if (watch_point == unit->high_limit) {
  369. if (unit->watchers[PCNT_LL_WATCH_EVENT_HIGH_LIMIT].event_id != PCNT_LL_WATCH_EVENT_INVALID) {
  370. ret = ESP_ERR_INVALID_STATE; // high limit event watcher has been installed already
  371. } else {
  372. unit->watchers[PCNT_LL_WATCH_EVENT_HIGH_LIMIT].event_id = PCNT_LL_WATCH_EVENT_HIGH_LIMIT;
  373. unit->watchers[PCNT_LL_WATCH_EVENT_HIGH_LIMIT].watch_point_value = unit->high_limit;
  374. pcnt_ll_enable_high_limit_event(group->hal.dev, unit->unit_id, true);
  375. }
  376. }
  377. // low limit watch point
  378. else if (watch_point == unit->low_limit) {
  379. if (unit->watchers[PCNT_LL_WATCH_EVENT_LOW_LIMIT].event_id != PCNT_LL_WATCH_EVENT_INVALID) {
  380. ret = ESP_ERR_INVALID_STATE; // low limit event watcher has been installed already
  381. } else {
  382. unit->watchers[PCNT_LL_WATCH_EVENT_LOW_LIMIT].event_id = PCNT_LL_WATCH_EVENT_LOW_LIMIT;
  383. unit->watchers[PCNT_LL_WATCH_EVENT_LOW_LIMIT].watch_point_value = unit->low_limit;
  384. pcnt_ll_enable_low_limit_event(group->hal.dev, unit->unit_id, true);
  385. }
  386. }
  387. // other threshold watch point
  388. else {
  389. int thres_num = SOC_PCNT_THRES_POINT_PER_UNIT - 1;
  390. switch (thres_num) {
  391. case 1:
  392. if (unit->watchers[PCNT_LL_WATCH_EVENT_THRES1].event_id == PCNT_LL_WATCH_EVENT_INVALID) {
  393. unit->watchers[PCNT_LL_WATCH_EVENT_THRES1].event_id = PCNT_LL_WATCH_EVENT_THRES1;
  394. unit->watchers[PCNT_LL_WATCH_EVENT_THRES1].watch_point_value = watch_point;
  395. pcnt_ll_set_thres_value(group->hal.dev, unit->unit_id, 1, watch_point);
  396. pcnt_ll_enable_thres_event(group->hal.dev, unit->unit_id, 1, true);
  397. break;
  398. } else if (unit->watchers[PCNT_LL_WATCH_EVENT_THRES1].watch_point_value == watch_point) {
  399. ret = ESP_ERR_INVALID_STATE;
  400. break;
  401. }
  402. /* fall-through */
  403. case 0:
  404. if (unit->watchers[PCNT_LL_WATCH_EVENT_THRES0].event_id == PCNT_LL_WATCH_EVENT_INVALID) {
  405. unit->watchers[PCNT_LL_WATCH_EVENT_THRES0].event_id = PCNT_LL_WATCH_EVENT_THRES0;
  406. unit->watchers[PCNT_LL_WATCH_EVENT_THRES0].watch_point_value = watch_point;
  407. pcnt_ll_set_thres_value(group->hal.dev, unit->unit_id, 0, watch_point);
  408. pcnt_ll_enable_thres_event(group->hal.dev, unit->unit_id, 0, true);
  409. break;
  410. } else if (unit->watchers[PCNT_LL_WATCH_EVENT_THRES0].watch_point_value == watch_point) {
  411. ret = ESP_ERR_INVALID_STATE;
  412. break;
  413. }
  414. /* fall-through */
  415. default:
  416. ret = ESP_ERR_NOT_FOUND; // no free threshold watch point available
  417. break;
  418. }
  419. }
  420. portEXIT_CRITICAL(&unit->spinlock);
  421. ESP_RETURN_ON_ERROR(ret, TAG, "add watchpoint %d failed", watch_point);
  422. return ESP_OK;
  423. }
  424. esp_err_t pcnt_unit_remove_watch_point(pcnt_unit_handle_t unit, int watch_point)
  425. {
  426. pcnt_group_t *group = NULL;
  427. ESP_RETURN_ON_FALSE(unit, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  428. group = unit->group;
  429. pcnt_ll_watch_event_id_t event_id = PCNT_LL_WATCH_EVENT_INVALID;
  430. // event enable/disable is mixed with other control function in the same register
  431. portENTER_CRITICAL(&unit->spinlock);
  432. for (int i = 0; i < PCNT_LL_WATCH_EVENT_MAX; i++) {
  433. if (unit->watchers[i].event_id != PCNT_LL_WATCH_EVENT_INVALID && unit->watchers[i].watch_point_value == watch_point) {
  434. event_id = unit->watchers[i].event_id;
  435. unit->watchers[i].event_id = PCNT_LL_WATCH_EVENT_INVALID;
  436. break;
  437. }
  438. }
  439. switch (event_id) {
  440. case PCNT_LL_WATCH_EVENT_ZERO_CROSS:
  441. pcnt_ll_enable_zero_cross_event(group->hal.dev, unit->unit_id, false);
  442. break;
  443. case PCNT_LL_WATCH_EVENT_LOW_LIMIT:
  444. pcnt_ll_enable_low_limit_event(group->hal.dev, unit->unit_id, false);
  445. break;
  446. case PCNT_LL_WATCH_EVENT_HIGH_LIMIT:
  447. pcnt_ll_enable_high_limit_event(group->hal.dev, unit->unit_id, false);
  448. break;
  449. case PCNT_LL_WATCH_EVENT_THRES0:
  450. pcnt_ll_enable_thres_event(group->hal.dev, unit->unit_id, 0, false);
  451. break;
  452. case PCNT_LL_WATCH_EVENT_THRES1:
  453. pcnt_ll_enable_thres_event(group->hal.dev, unit->unit_id, 1, false);
  454. break;
  455. default:
  456. break;
  457. }
  458. portEXIT_CRITICAL(&unit->spinlock);
  459. ESP_RETURN_ON_FALSE(event_id != PCNT_LL_WATCH_EVENT_INVALID, ESP_ERR_INVALID_STATE, TAG, "watch point %d not added yet", watch_point);
  460. return ESP_OK;
  461. }
  462. esp_err_t pcnt_new_channel(pcnt_unit_handle_t unit, const pcnt_chan_config_t *config, pcnt_channel_handle_t *ret_chan)
  463. {
  464. esp_err_t ret = ESP_OK;
  465. pcnt_chan_t *channel = NULL;
  466. pcnt_group_t *group = NULL;
  467. ESP_GOTO_ON_FALSE(unit && config && ret_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  468. ESP_GOTO_ON_FALSE(unit->fsm == PCNT_UNIT_FSM_INIT, ESP_ERR_INVALID_STATE, err, TAG, "unit not in init state");
  469. group = unit->group;
  470. int group_id = group->group_id;
  471. int unit_id = unit->unit_id;
  472. channel = heap_caps_calloc(1, sizeof(pcnt_chan_t), PCNT_MEM_ALLOC_CAPS);
  473. ESP_GOTO_ON_FALSE(channel, ESP_ERR_NO_MEM, err, TAG, "no mem for channel");
  474. // search for a free channel
  475. int channel_id = -1;
  476. portENTER_CRITICAL(&unit->spinlock);
  477. for (int i = 0; i < SOC_PCNT_CHANNELS_PER_UNIT; i++) {
  478. if (!unit->channels[i]) {
  479. channel_id = i;
  480. unit->channels[channel_id] = channel;
  481. break;
  482. }
  483. }
  484. portEXIT_CRITICAL(&unit->spinlock);
  485. ESP_GOTO_ON_FALSE(channel_id != -1, ESP_ERR_NOT_FOUND, err, TAG, "no free channel in unit (%d,%d)", group_id, unit_id);
  486. // GPIO configuration
  487. gpio_config_t gpio_conf = {
  488. .intr_type = GPIO_INTR_DISABLE,
  489. .mode = GPIO_MODE_INPUT | (config->flags.io_loop_back ? GPIO_MODE_OUTPUT : 0), // also enable the output path if `io_loop_back` is enabled
  490. .pull_down_en = false,
  491. .pull_up_en = true,
  492. };
  493. if (config->edge_gpio_num >= 0) {
  494. gpio_conf.pin_bit_mask = 1ULL << config->edge_gpio_num;
  495. ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config edge GPIO failed");
  496. esp_rom_gpio_connect_in_signal(config->edge_gpio_num,
  497. pcnt_periph_signals.groups[group_id].units[unit_id].channels[channel_id].pulse_sig,
  498. config->flags.invert_edge_input);
  499. }
  500. if (config->level_gpio_num >= 0) {
  501. gpio_conf.pin_bit_mask = 1ULL << config->level_gpio_num;
  502. ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config level GPIO failed");
  503. esp_rom_gpio_connect_in_signal(config->level_gpio_num,
  504. pcnt_periph_signals.groups[group_id].units[unit_id].channels[channel_id].control_sig,
  505. config->flags.invert_level_input);
  506. }
  507. channel->channel_id = channel_id;
  508. channel->unit = unit;
  509. channel->edge_gpio_num = config->edge_gpio_num;
  510. channel->level_gpio_num = config->level_gpio_num;
  511. ESP_LOGD(TAG, "new pcnt channel(%d,%d,%d) at %p", group_id, unit_id, channel_id, channel);
  512. *ret_chan = channel;
  513. return ESP_OK;
  514. err:
  515. if (channel) {
  516. free(channel);
  517. }
  518. return ret;
  519. }
  520. esp_err_t pcnt_del_channel(pcnt_channel_handle_t chan)
  521. {
  522. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  523. pcnt_unit_t *unit = chan->unit;
  524. pcnt_group_t *group = unit->group;
  525. int group_id = group->group_id;
  526. int unit_id = unit->unit_id;
  527. int channel_id = chan->channel_id;
  528. portENTER_CRITICAL(&unit->spinlock);
  529. unit->channels[channel_id] = NULL;
  530. portEXIT_CRITICAL(&unit->spinlock);
  531. if (chan->level_gpio_num >= 0) {
  532. gpio_reset_pin(chan->level_gpio_num);
  533. }
  534. if (chan->edge_gpio_num >= 0) {
  535. gpio_reset_pin(chan->edge_gpio_num);
  536. }
  537. free(chan);
  538. ESP_LOGD(TAG, "del pcnt channel(%d,%d,%d)", group_id, unit_id, channel_id);
  539. return ESP_OK;
  540. }
  541. esp_err_t pcnt_channel_set_edge_action(pcnt_channel_handle_t chan, pcnt_channel_edge_action_t pos_act, pcnt_channel_edge_action_t neg_act)
  542. {
  543. pcnt_group_t *group = NULL;
  544. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  545. pcnt_unit_t *unit = chan->unit;
  546. group = unit->group;
  547. // mode control bits are mixed with other PCNT control bits in a same register
  548. portENTER_CRITICAL(&unit->spinlock);
  549. pcnt_ll_set_edge_action(group->hal.dev, unit->unit_id, chan->channel_id, pos_act, neg_act);
  550. portEXIT_CRITICAL(&unit->spinlock);
  551. return ESP_OK;
  552. }
  553. esp_err_t pcnt_channel_set_level_action(pcnt_channel_handle_t chan, pcnt_channel_level_action_t high_act, pcnt_channel_level_action_t low_act)
  554. {
  555. pcnt_group_t *group = NULL;
  556. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  557. pcnt_unit_t *unit = chan->unit;
  558. group = unit->group;
  559. // mode control bits are mixed with other PCNT control bits in a same register
  560. portENTER_CRITICAL(&unit->spinlock);
  561. pcnt_ll_set_level_action(group->hal.dev, unit->unit_id, chan->channel_id, high_act, low_act);
  562. portEXIT_CRITICAL(&unit->spinlock);
  563. return ESP_OK;
  564. }
  565. static pcnt_group_t *pcnt_acquire_group_handle(int group_id)
  566. {
  567. bool new_group = false;
  568. pcnt_group_t *group = NULL;
  569. // prevent install pcnt group concurrently
  570. _lock_acquire(&s_platform.mutex);
  571. if (!s_platform.groups[group_id]) {
  572. group = heap_caps_calloc(1, sizeof(pcnt_group_t), PCNT_MEM_ALLOC_CAPS);
  573. if (group) {
  574. new_group = true;
  575. s_platform.groups[group_id] = group; // register to platform
  576. // initialize pcnt group members
  577. group->group_id = group_id;
  578. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  579. // enable APB access pcnt registers
  580. periph_module_enable(pcnt_periph_signals.groups[group_id].module);
  581. periph_module_reset(pcnt_periph_signals.groups[group_id].module);
  582. // initialize HAL context
  583. pcnt_hal_init(&group->hal, group_id);
  584. }
  585. } else {
  586. group = s_platform.groups[group_id];
  587. }
  588. if (group) {
  589. // someone acquired the group handle means we have a new object that refer to this group
  590. s_platform.group_ref_counts[group_id]++;
  591. }
  592. _lock_release(&s_platform.mutex);
  593. if (new_group) {
  594. ESP_LOGD(TAG, "new group (%d) at %p", group_id, group);
  595. }
  596. return group;
  597. }
  598. static void pcnt_release_group_handle(pcnt_group_t *group)
  599. {
  600. int group_id = group->group_id;
  601. bool do_deinitialize = false;
  602. _lock_acquire(&s_platform.mutex);
  603. s_platform.group_ref_counts[group_id]--;
  604. if (s_platform.group_ref_counts[group_id] == 0) {
  605. assert(s_platform.groups[group_id]);
  606. do_deinitialize = true;
  607. s_platform.groups[group_id] = NULL; // deregister from platform
  608. periph_module_disable(pcnt_periph_signals.groups[group_id].module);
  609. }
  610. _lock_release(&s_platform.mutex);
  611. if (do_deinitialize) {
  612. free(group);
  613. ESP_LOGD(TAG, "del group (%d)", group_id);
  614. }
  615. }
  616. IRAM_ATTR static void pcnt_default_isr(void *args)
  617. {
  618. bool need_yield = false;
  619. pcnt_unit_t *unit = (pcnt_unit_t *)args;
  620. int unit_id = unit->unit_id;
  621. pcnt_group_t *group = unit->group;
  622. pcnt_watch_cb_t on_reach = unit->on_reach;
  623. uint32_t intr_status = pcnt_ll_get_intr_status(group->hal.dev);
  624. if (intr_status & PCNT_LL_UNIT_WATCH_EVENT(unit_id)) {
  625. pcnt_ll_clear_intr_status(group->hal.dev, PCNT_LL_UNIT_WATCH_EVENT(unit_id));
  626. uint32_t event_status = pcnt_ll_get_event_status(group->hal.dev, unit_id);
  627. // iter on each event_id
  628. while (event_status) {
  629. int event_id = __builtin_ffs(event_status) - 1;
  630. event_status &= (event_status - 1); // clear the right most bit
  631. // invoked user registered callback
  632. if (on_reach) {
  633. pcnt_watch_event_data_t edata = {
  634. .watch_point_value = unit->watchers[event_id].watch_point_value,
  635. .zero_cross_mode = pcnt_ll_get_zero_cross_mode(group->hal.dev, unit_id),
  636. };
  637. if (on_reach(unit, &edata, unit->user_data)) {
  638. // check if we need to yield for high priority task
  639. need_yield = true;
  640. }
  641. }
  642. }
  643. }
  644. if (need_yield) {
  645. portYIELD_FROM_ISR();
  646. }
  647. }