|
@@ -26,10 +26,15 @@ const int pcntUnit = PCNT_UNIT_0;
|
|
|
|
|
|
uint32_t getkWh() {
|
|
|
|
|
|
- pcnt_intr_disable(pcntUnit);
|
|
|
- volatile int32_t count = DPORT_REG_READ(0x3FF57060);
|
|
|
- volatile uint32_t bigCounter = Counter_32K_Pulses;
|
|
|
- pcnt_intr_enable(pcntUnit);
|
|
|
+ volatile uint32_t firstRead, count, bigCounter;
|
|
|
+
|
|
|
+ // Loop until we have two identical reads of the interrupt-updated 32K-counter
|
|
|
+ // The 32K-counter is only updated maybe once per 24h, so should not be a problem
|
|
|
+ do {
|
|
|
+ firstRead = Counter_32K_Pulses;
|
|
|
+ count = DPORT_REG_READ(0x3FF57060);
|
|
|
+ bigCounter = Counter_32K_Pulses;
|
|
|
+ } while( firstRead != bigCounter );
|
|
|
|
|
|
return (bigCounter * 32000) + count;
|
|
|
}
|