diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index 84f3e82..99f6ef2 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS "app_main.c" "network_mngr.c" "gpio.c" - "timer.c" + "counter.c" INCLUDE_DIRS ".") diff --git a/src/main/app_main.c b/src/main/app_main.c index 064b4cd..4727a85 100644 --- a/src/main/app_main.c +++ b/src/main/app_main.c @@ -14,7 +14,7 @@ #include #include "gpio.h" -#include "timer.h" +#include "counter.h" @@ -39,7 +39,7 @@ void app_main(void) gpioInit(); networkInit(isGpioForceProv()); - timerInit(); + counterInit(); /* Start main application now */ while (1) { diff --git a/src/main/counter.c b/src/main/counter.c new file mode 100644 index 0000000..b4ede93 --- /dev/null +++ b/src/main/counter.c @@ -0,0 +1,70 @@ +#include + +#include "timer.h" + +#include +#include +#include +#include +#include +#include + +static const char *TAG = "cnt"; + + +static xQueueHandle zeroCrossingQueue = NULL; +static const uint64_t QUEUE_MARKER = UINT64_MAX; + + +static void counterTask(void *arg) { + while (1) { + vTaskDelay(1000 / portTICK_PERIOD_MS); + xQueueSend(zeroCrossingQueue, QUEUE_MARKER, portMAX_DELAY); + } +} + +static void counterZeroCrossingAveragerTask(void *arg) { + uint64_t counterCnt = 0; + uint64_t counterSum = 0; + while (1) { + uint64_t counterCurrentValue; + xQueueReceive(zeroCrossingQueue, &counterCurrentValue, portMAX_DELAY); + if (counterCurrentValue == QUEUE_MARKER) { + uint32_t counterSecondAverage = ((uint32_t)(counterSum)) / ((uint32_t)(counterCnt)); + ESP_LOGI(TAG, "second average is %lu", counterSecondAverage); + } else { + counterSum += 1; + counterCnt += counterValue; + } + } +} + +void IRAM_ATTR counterZeroCrossingISR(void *arg) { + uint64_t counterValue = 0; + + timer_spinlock_take(TIMER_GROUP_0); + counterValue = time_group_get_counter_value_in_isr(TIMER_GROUP_0, 0); + timer_spinlock_give(TIMER_GROUP_0); + + if (zeroCrossingQueue) { + xQueueSendFromISR(zeroCrossingQueue, counterValue, NULL); + } +} + +void counterInit() { + timer_config_t config = { + .divider = 80, + .counter_dir = TIMER_COUNT_UP, + .counter_en = TIMER_PAUSE, + .alarm_en = TIMER_ALARM_DIS, + .auto_reload = TIMER_AUTORELOAD_EN + }; + timer_init(TIMER_GROUP_0, 0, &config); + timer_set_counter_value(TIMER_GROUP_0, 0, 0); + timer_start(TIMER_GROUP_0, 0); + + zeroCrossingQueue = xQueueCreate(20, sizeof(uint64_t)); + + xTaskCreate(counterSecondTask, "counter_second_task", 2048, NULL, 5, NULL); + xTaskCreate(counterZeroCrossingAveragerTask, "counter_averager_task", 2048, NULL, 5, NULL); +} diff --git a/src/main/counter.h b/src/main/counter.h new file mode 100644 index 0000000..88346a8 --- /dev/null +++ b/src/main/counter.h @@ -0,0 +1,12 @@ +#ifndef _COUNTER_H_ +#define _COUNTER_H_ + + + + + +void counterInit(); +void counterZeroCrossingISR(void *arg); + + +#endif // _COUNTER_H_ \ No newline at end of file diff --git a/src/main/gpio.c b/src/main/gpio.c index 3e3bed8..b3416b8 100644 --- a/src/main/gpio.c +++ b/src/main/gpio.c @@ -1,6 +1,7 @@ #include #include "gpio.h" +#include "counter.h" #include #include @@ -17,6 +18,15 @@ void gpioInit() { io_conf.pull_down_en = 1; gpio_config(&io_conf); + io_conf.intr_type = GPIO_INTR_POSEDGE; + io_conf.pin_bit_mask = (1ULL << GPIO_ZERO_CROSSING); + io_conf.mode = GPIO_MODE_INPUT; + io_conf.pull_up_en = 0; + io_conf.pull_down_en = 0; + gpio_config(&i_conf); + + gpio_isr_handler_add(GPIO_ZERO_CROSSING, counterZeroCrossingISR, NULL); + ESP_LOGI(TAG, "gpios configured"); } diff --git a/src/main/gpio.h b/src/main/gpio.h index 09b5800..378194c 100644 --- a/src/main/gpio.h +++ b/src/main/gpio.h @@ -5,7 +5,7 @@ #define GPIO_FORCE_PROV 5 - +#define GPIO_ZERO_CROSSING 26 void gpioInit(); bool isGpioForceProv(); diff --git a/src/main/timer.c b/src/main/timer.c deleted file mode 100644 index b22fe61..0000000 --- a/src/main/timer.c +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#include "timer.h" - -#include -#include -#include -#include -#include -#include - -static const char *TAG = "tm"; - - - -static void timer_task(void *arg) { - while (1) { - uint64_t counterValue; - timer_get_counter_value(TIMER_GROUP_0, 0, &counterValue); - ESP_LOGI(TAG, "timer value: %08x %08x", (uint32_t)(counterValue >> 32), (uint32_t)(counterValue)); - vTaskDelay(1000 / portTICK_PERIOD_MS); - } -} - -void timerInit() { - timer_config_t config = { - .divider = 80, - .counter_dir = TIMER_COUNT_UP, - .counter_en = TIMER_PAUSE, - .alarm_en = TIMER_ALARM_DIS, - .auto_reload = TIMER_AUTORELOAD_EN - }; - timer_init(TIMER_GROUP_0, 0, &config); - timer_set_counter_value(TIMER_GROUP_0, 0, 0); - timer_start(TIMER_GROUP_0, 0); - - xTaskCreate(timer_task, "timer_task", 2048, NULL, 5, NULL); -} diff --git a/src/main/timer.h b/src/main/timer.h deleted file mode 100644 index ecc23e8..0000000 --- a/src/main/timer.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _PCNT_H_ -#define _PCNT_H_ - -void timerInit(); - - - -#endif // _PCNT_H_ \ No newline at end of file