1
0

pcnt_functions.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "freertos/queue.h"
  4. #include "driver/ledc.h"
  5. #include "driver/pcnt.h"
  6. #include "esp_attr.h"
  7. #include "esp_log.h"
  8. #include "config.h"
  9. /* Decode what PCNT's unit originated an interrupt
  10. * and pass this information together with the event type
  11. * the main program using a queue.
  12. */
  13. static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
  14. {
  15. int pcnt_unit = (int)arg;
  16. }
  17. /* Initialize PCNT functions:
  18. * - configure and initialize PCNT
  19. * - set up the input filter
  20. * - set up the counter events to watch
  21. */
  22. void init_pcnt_unit(int unit)
  23. {
  24. /* Prepare configuration for the PCNT unit */
  25. pcnt_config_t pcnt_config = {
  26. // Set PCNT input signal and control GPIOs
  27. .pulse_gpio_num = PCNT_INPUT_SIG_IO,
  28. .ctrl_gpio_num = PCNT_INPUT_CTRL_IO,
  29. .channel = PCNT_CHANNEL_0,
  30. .unit = unit,
  31. // What to do on the positive / negative edge of pulse input?
  32. .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
  33. .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
  34. // What to do when control input is low or high?
  35. .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
  36. .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
  37. // Set the maximum and minimum limit values to watch
  38. .counter_h_lim = 0,
  39. .counter_l_lim = 0,
  40. };
  41. /* Initialize PCNT unit */
  42. pcnt_unit_config(&pcnt_config);
  43. pcnt_event_disable(unit, PCNT_EVT_H_LIM);
  44. pcnt_event_disable(unit, PCNT_EVT_L_LIM);
  45. pcnt_event_disable(unit, PCNT_EVT_ZERO);
  46. pcnt_filter_disable(unit);
  47. //uint32_t c = p_pcnt_obj->hal.dev.hw.conf_unit[unit];
  48. /* Configure and enable the input filter */
  49. //pcnt_set_filter_value(unit, 100);
  50. //pcnt_filter_enable(unit);
  51. /* Set threshold 0 and 1 values and enable events to watch */
  52. //pcnt_set_event_value(unit, PCNT_EVT_THRES_1, PCNT_THRESH1_VAL);
  53. //pcnt_event_enable(unit, PCNT_EVT_THRES_1);
  54. //pcnt_set_event_value(unit, PCNT_EVT_THRES_0, PCNT_THRESH0_VAL);
  55. //pcnt_event_enable(unit, PCNT_EVT_THRES_0);
  56. /* Enable events on zero, maximum and minimum limit values */
  57. //pcnt_event_enable(unit, PCNT_EVT_ZERO);
  58. //pcnt_event_enable(unit, PCNT_EVT_H_LIM);
  59. //pcnt_event_enable(unit, PCNT_EVT_L_LIM);
  60. /* Initialize PCNT's counter */
  61. pcnt_counter_pause(unit);
  62. pcnt_counter_clear(unit);
  63. /* Install interrupt service and add isr callback handler */
  64. //pcnt_isr_service_install(0);
  65. //pcnt_isr_handler_add(unit, pcnt_example_intr_handler, (void *)unit);
  66. /* Everything is set up, now go to counting */
  67. pcnt_counter_resume(unit);
  68. }