|
@@ -7,21 +7,31 @@
|
|
|
#include "esp_log.h"
|
|
|
#include "config.h"
|
|
|
|
|
|
-/* Decode what PCNT's unit originated an interrupt
|
|
|
- * and pass this information together with the event type
|
|
|
- * the main program using a queue.
|
|
|
- */
|
|
|
+#include "soc/dport_reg.h"
|
|
|
+
|
|
|
+// Example-code: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/pcnt/pulse_count_event/main/pcnt_event_example_main.c
|
|
|
+
|
|
|
+// Counts full kWh
|
|
|
+static uint32_t kWh_Counter = 0;
|
|
|
+
|
|
|
+const int pcntUnit = PCNT_UNIT_0;
|
|
|
+
|
|
|
+float getkWh() {
|
|
|
+
|
|
|
+ pcnt_intr_disable(pcntUnit);
|
|
|
+ volatile int32_t count = DPORT_REG_READ(0x3FF57060);
|
|
|
+ volatile uint32_t kWh = kWh_Counter;
|
|
|
+ pcnt_intr_enable(pcntUnit);
|
|
|
+
|
|
|
+ return (float)kWh + ((float)count/PULSES_PER_KWH);
|
|
|
+}
|
|
|
+
|
|
|
static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
|
|
|
{
|
|
|
- int pcnt_unit = (int)arg;
|
|
|
+ kWh_Counter++;
|
|
|
}
|
|
|
|
|
|
-/* Initialize PCNT functions:
|
|
|
- * - configure and initialize PCNT
|
|
|
- * - set up the input filter
|
|
|
- * - set up the counter events to watch
|
|
|
- */
|
|
|
-void init_pcnt_unit(int unit)
|
|
|
+void init_pcnt_unit()
|
|
|
{
|
|
|
/* Prepare configuration for the PCNT unit */
|
|
|
pcnt_config_t pcnt_config = {
|
|
@@ -29,7 +39,7 @@ void init_pcnt_unit(int unit)
|
|
|
.pulse_gpio_num = PCNT_INPUT_SIG_IO,
|
|
|
.ctrl_gpio_num = PCNT_INPUT_CTRL_IO,
|
|
|
.channel = PCNT_CHANNEL_0,
|
|
|
- .unit = unit,
|
|
|
+ .unit = pcntUnit,
|
|
|
// What to do on the positive / negative edge of pulse input?
|
|
|
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
|
|
|
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
|
|
@@ -37,43 +47,31 @@ void init_pcnt_unit(int unit)
|
|
|
.lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
|
|
|
.hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
|
|
|
// Set the maximum and minimum limit values to watch
|
|
|
- .counter_h_lim = 0,
|
|
|
+ .counter_h_lim = PULSES_PER_KWH,
|
|
|
.counter_l_lim = 0,
|
|
|
};
|
|
|
/* Initialize PCNT unit */
|
|
|
pcnt_unit_config(&pcnt_config);
|
|
|
|
|
|
- pcnt_event_disable(unit, PCNT_EVT_H_LIM);
|
|
|
- pcnt_event_disable(unit, PCNT_EVT_L_LIM);
|
|
|
- pcnt_event_disable(unit, PCNT_EVT_ZERO);
|
|
|
- pcnt_filter_disable(unit);
|
|
|
-
|
|
|
+ // How to read a periphial-register:
|
|
|
//uint32_t c = p_pcnt_obj->hal.dev.hw.conf_unit[unit];
|
|
|
-
|
|
|
+ //printf("Reg: %08x\n",(uint32_t)DPORT_REG_READ(0x3FF57000));
|
|
|
|
|
|
/* Configure and enable the input filter */
|
|
|
+ pcnt_set_filter_value(pcntUnit, 100);
|
|
|
+ pcnt_filter_enable(pcntUnit);
|
|
|
|
|
|
- //pcnt_set_filter_value(unit, 100);
|
|
|
- //pcnt_filter_enable(unit);
|
|
|
-
|
|
|
- /* Set threshold 0 and 1 values and enable events to watch */
|
|
|
- //pcnt_set_event_value(unit, PCNT_EVT_THRES_1, PCNT_THRESH1_VAL);
|
|
|
- //pcnt_event_enable(unit, PCNT_EVT_THRES_1);
|
|
|
- //pcnt_set_event_value(unit, PCNT_EVT_THRES_0, PCNT_THRESH0_VAL);
|
|
|
- //pcnt_event_enable(unit, PCNT_EVT_THRES_0);
|
|
|
- /* Enable events on zero, maximum and minimum limit values */
|
|
|
- //pcnt_event_enable(unit, PCNT_EVT_ZERO);
|
|
|
- //pcnt_event_enable(unit, PCNT_EVT_H_LIM);
|
|
|
- //pcnt_event_enable(unit, PCNT_EVT_L_LIM);
|
|
|
+ /* Enable int on high count limit */
|
|
|
+ pcnt_event_enable(pcntUnit, PCNT_EVT_H_LIM);
|
|
|
|
|
|
/* Initialize PCNT's counter */
|
|
|
- pcnt_counter_pause(unit);
|
|
|
- pcnt_counter_clear(unit);
|
|
|
+ pcnt_counter_pause(pcntUnit);
|
|
|
+ pcnt_counter_clear(pcntUnit);
|
|
|
|
|
|
/* Install interrupt service and add isr callback handler */
|
|
|
- //pcnt_isr_service_install(0);
|
|
|
- //pcnt_isr_handler_add(unit, pcnt_example_intr_handler, (void *)unit);
|
|
|
+ pcnt_isr_service_install(0);
|
|
|
+ pcnt_isr_handler_add(pcntUnit, pcnt_example_intr_handler, (void *)pcntUnit);
|
|
|
|
|
|
/* Everything is set up, now go to counting */
|
|
|
- pcnt_counter_resume(unit);
|
|
|
+ pcnt_counter_resume(pcntUnit);
|
|
|
}
|