counter
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
idf_component_register(SRCS "app_main.c"
|
||||
"network_mngr.c"
|
||||
"gpio.c"
|
||||
"timer.c"
|
||||
"counter.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <nvs_flash.h>
|
||||
|
||||
#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) {
|
||||
|
70
src/main/counter.c
Normal file
70
src/main/counter.c
Normal file
@ -0,0 +1,70 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#include <driver/timer.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_types.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
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);
|
||||
}
|
12
src/main/counter.h
Normal file
12
src/main/counter.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef _COUNTER_H_
|
||||
#define _COUNTER_H_
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void counterInit();
|
||||
void counterZeroCrossingISR(void *arg);
|
||||
|
||||
|
||||
#endif // _COUNTER_H_
|
@ -1,6 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "gpio.h"
|
||||
#include "counter.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
@ -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");
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
#define GPIO_FORCE_PROV 5
|
||||
|
||||
#define GPIO_ZERO_CROSSING 26
|
||||
|
||||
void gpioInit();
|
||||
bool isGpioForceProv();
|
||||
|
@ -1,38 +0,0 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#include <driver/timer.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_types.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
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);
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
#ifndef _PCNT_H_
|
||||
#define _PCNT_H_
|
||||
|
||||
void timerInit();
|
||||
|
||||
|
||||
|
||||
#endif // _PCNT_H_
|
Reference in New Issue
Block a user