pcnt_functions.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "soc/dport_reg.h"
  10. #ifdef CCFG_PCNT
  11. // Example-code: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/pcnt/pulse_count_event/main/pcnt_event_example_main.c
  12. // Counts interrupts that occus every 32K Pulses
  13. // The counter register is 16 bit signed so we count to 32K before doing an interrupt
  14. // This equals to 32kWH consumed energy (at 1000 pulses per kWh)
  15. static uint32_t Counter_32K_Pulses = 0;
  16. const int pcntUnit = PCNT_UNIT_0;
  17. double getkWh(uint32_t *bigCnt, int32_t *cnt) {
  18. pcnt_intr_disable(pcntUnit);
  19. volatile int32_t count = DPORT_REG_READ(0x3FF57060);
  20. volatile uint32_t bigCounter = Counter_32K_Pulses;
  21. pcnt_intr_enable(pcntUnit);
  22. *bigCnt = bigCounter;
  23. *cnt = count;
  24. const double retVal = ((double)bigCounter*32)+((double)count / PULSES_PER_KWH);
  25. return retVal;
  26. }
  27. static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
  28. {
  29. Counter_32K_Pulses++;
  30. }
  31. void init_pcnt_unit()
  32. {
  33. /* Prepare configuration for the PCNT unit */
  34. pcnt_config_t pcnt_config = {
  35. // Set PCNT input signal and control GPIOs
  36. .pulse_gpio_num = PCNT_INPUT_SIG_IO,
  37. .ctrl_gpio_num = PCNT_PIN_NOT_USED;
  38. .channel = PCNT_CHANNEL_0,
  39. .unit = pcntUnit,
  40. // What to do on the positive / negative edge of pulse input?
  41. .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
  42. .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
  43. // What to do when control input is low or high?
  44. .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
  45. .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
  46. // Set the maximum and minimum limit values to watch
  47. .counter_h_lim = 32000,
  48. .counter_l_lim = 0,
  49. };
  50. /* Initialize PCNT unit */
  51. pcnt_unit_config(&pcnt_config);
  52. // How to read a periphial-register:
  53. //uint32_t c = p_pcnt_obj->hal.dev.hw.conf_unit[unit];
  54. //printf("Reg: %08x\n",(uint32_t)DPORT_REG_READ(0x3FF57000));
  55. //The length of ignored pulses is provided in APB_CLK clock cycles by calling pcnt_set_filter_value().
  56. //The current filter setting may be checked with pcnt_get_filter_value().
  57. //The APB_CLK clock is running at 80 MHz.
  58. /* Configure and enable the input filter */
  59. pcnt_set_filter_value(pcntUnit, 1023); // APB_CLK=80MHz * 1023 is filtered out = 0,0127875mS (????)
  60. pcnt_filter_enable(pcntUnit);
  61. /* Enable int on high count limit */
  62. pcnt_event_enable(pcntUnit, PCNT_EVT_H_LIM);
  63. /* Initialize PCNT's counter */
  64. pcnt_counter_pause(pcntUnit);
  65. pcnt_counter_clear(pcntUnit);
  66. /* Install interrupt service and add isr callback handler */
  67. pcnt_isr_service_install(0);
  68. pcnt_isr_handler_add(pcntUnit, pcnt_example_intr_handler, (void *)pcntUnit);
  69. /* Everything is set up, now go to counting */
  70. pcnt_counter_resume(pcntUnit);
  71. }
  72. #else
  73. void init_pcnt_unit() { }
  74. double getkWh() { return 0.0f; }
  75. #endif