pcnt_functions.c 4.0 KB

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