1
0

kWhCounter.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. uint32_t kWh_cnt = 0;
  10. #ifdef ENABLE_KWH_COUNTER
  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. }
  22. if( state > 0 ) kWh_cnt++;
  23. if( ++logCnt == 10 ) {
  24. ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt);
  25. logCnt = 0;
  26. }
  27. state = 0;
  28. }
  29. }
  30. void kWhCounter_init()
  31. {
  32. ESP_LOGI("kWhCounter", "kWhCounter_init()");
  33. gpio_set_direction(KWH_COUNTER_INPUT_IO, GPIO_MODE_INPUT);
  34. xTaskCreate(counterControlTask, "counterControlTask", 1024*10, NULL, 2, NULL);
  35. }
  36. #endif