kWhCounter.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "kWhCounter.h"
  9. #ifdef ENABLE_KWH_COUNTER
  10. uint32_t kWh_cnt = 0;
  11. void counterControlTask(void *pvParameters) {
  12. ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID());
  13. uint32_t logCnt = 0;
  14. uint32_t state = 0;
  15. TickType_t vLastWakeTime = xTaskGetTickCount();
  16. const TickType_t xFreq = 100 / portTICK_PERIOD_MS;
  17. while( 1 ) {
  18. for(uint8_t i=10; i>0; i-- ) {
  19. vTaskDelayUntil( &vLastWakeTime, xFreq );
  20. if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) state++;
  21. vTaskDelay(100 / portTICK_PERIOD_MS);
  22. }
  23. if( state > 0 ) kWh_cnt++;
  24. if( ++logCnt == 10 ) {
  25. ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt);
  26. logCnt = 0;
  27. }
  28. state = 0;
  29. }
  30. }
  31. void kWhCounter_init()
  32. {
  33. ESP_LOGI("kWhCounter", "kWhCounter_init()");
  34. gpio_set_direction(KWH_COUNTER_INPUT_IO, GPIO_MODE_INPUT);
  35. xTaskCreate(counterControlTask, "counterControlTask", 1024*10, NULL, 2, NULL);
  36. }
  37. #endif