pcnt_functions.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "pcnt_functions.h"
  10. #include "soc/dport_reg.h"
  11. #ifdef CCFG_PCNT
  12. /** Electric meter:
  13. * 10000 impulses per kWh
  14. * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
  15. *
  16. * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
  17. * So we need some other way of counting higher numbers that will last for one h
  18. * Probably we need to use interrupts !
  19. */
  20. // Example-code: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/pcnt/pulse_count_event/main/pcnt_event_example_main.c
  21. // Counts interrupts that occus every 32K Pulses
  22. // The counter register is 16 bit signed so we count to 32K before doing an interrupt
  23. // This equals to 32kWH consumed energy (at 1000 pulses per kWh)
  24. static uint32_t Counter_32K_Pulses = 0;
  25. static const int pcntUnit = PCNT_UNIT_0;
  26. static void pCntMonitorTask(void *pvParameters);
  27. static double getkWh(uint32_t *bigCnt, int32_t *cnt) {
  28. pcnt_intr_disable(pcntUnit);
  29. volatile int32_t count = DPORT_REG_READ(0x3FF57060);
  30. volatile uint32_t bigCounter = Counter_32K_Pulses;
  31. pcnt_intr_enable(pcntUnit);
  32. *bigCnt = bigCounter;
  33. *cnt = count;
  34. const double retVal = ((double)bigCounter*32)+((double)count / PULSES_PER_KWH);
  35. return retVal;
  36. }
  37. static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
  38. {
  39. Counter_32K_Pulses++;
  40. }
  41. void init_pcnt_unit()
  42. {
  43. /* Prepare configuration for the PCNT unit */
  44. pcnt_config_t pcnt_config = {
  45. // Set PCNT input signal and control GPIOs
  46. .pulse_gpio_num = PCNT_INPUT_SIG_IO,
  47. .ctrl_gpio_num = PCNT_PIN_NOT_USED;
  48. .channel = PCNT_CHANNEL_0,
  49. .unit = pcntUnit,
  50. // What to do on the positive / negative edge of pulse input?
  51. .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
  52. .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
  53. // What to do when control input is low or high?
  54. .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
  55. .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
  56. // Set the maximum and minimum limit values to watch
  57. .counter_h_lim = 32000,
  58. .counter_l_lim = 0,
  59. };
  60. /* Initialize PCNT unit */
  61. pcnt_unit_config(&pcnt_config);
  62. // How to read a periphial-register:
  63. //uint32_t c = p_pcnt_obj->hal.dev.hw.conf_unit[unit];
  64. //printf("Reg: %08x\n",(uint32_t)DPORT_REG_READ(0x3FF57000));
  65. //The length of ignored pulses is provided in APB_CLK clock cycles by calling pcnt_set_filter_value().
  66. //The current filter setting may be checked with pcnt_get_filter_value().
  67. //The APB_CLK clock is running at 80 MHz.
  68. /* Configure and enable the input filter */
  69. pcnt_set_filter_value(pcntUnit, 1023); // APB_CLK=80MHz * 1023 is filtered out = 0,0127875mS (????)
  70. pcnt_filter_enable(pcntUnit);
  71. /* Enable int on high count limit */
  72. pcnt_event_enable(pcntUnit, PCNT_EVT_H_LIM);
  73. /* Initialize PCNT's counter */
  74. pcnt_counter_pause(pcntUnit);
  75. pcnt_counter_clear(pcntUnit);
  76. /* Install interrupt service and add isr callback handler */
  77. pcnt_isr_service_install(0);
  78. pcnt_isr_handler_add(pcntUnit, pcnt_example_intr_handler, (void *)pcntUnit);
  79. /* Everything is set up, now go to counting */
  80. pcnt_counter_resume(pcntUnit);
  81. }
  82. static void pCntMonitorTask(void *pvParameters) {
  83. ESP_LOGI("PCNT", "pCntMonitorTask starting");
  84. /*
  85. char dataStr[100];
  86. double kWh = 0.0;
  87. uint32_t bigCnt = 0;
  88. int32_t cnt = 0;
  89. kWh = getkWh(&bigCnt, &cnt);
  90. sprintf(dataStr,"%.5f",kWh);
  91. // @TODO This is changed for testing
  92. #ifdef MQTT_ENABLED
  93. sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
  94. #endif
  95. ESP_LOGI("MAIN", "%.4f %u %d",kWh,bigCnt,cnt);
  96. */
  97. }
  98. void initPCNT() {
  99. init_pcnt_unit();
  100. ledc_init();
  101. xTaskCreate(pCntMonitorTask, "pCntMonitorTask", 1024*10, NULL, 2, NULL);
  102. }
  103. #endif