Add Shelly 3EM heartbeat

Add Shelly 3EM heartbeat to reinit if interrupts stopped (#13515)
This commit is contained in:
Theo Arends 2022-06-09 14:45:39 +02:00
parent f69421ced5
commit d43127ce58

View File

@ -41,6 +41,8 @@
//#define ADE7880_DEBUG //#define ADE7880_DEBUG
//#define ADE7880_PROFILING //#define ADE7880_PROFILING
#define ADE7880_HEARTBEAT 5 // Allow x seconds of missed interrupts before reinit
// Default calibration parameters can be overridden by a rule as documented above. // Default calibration parameters can be overridden by a rule as documented above.
#define ADE7880_FREQ_INIT 0 // Connected to networks with fundamental frequencies between 55 Hz and 66 Hz (1). Default 45 Hz to 55 Hz (0). #define ADE7880_FREQ_INIT 0 // Connected to networks with fundamental frequencies between 55 Hz and 66 Hz (1). Default 45 Hz to 55 Hz (0).
#define ADE7880_AIGAIN_INIT 3166385 // rms, current_a #define ADE7880_AIGAIN_INIT 3166385 // rms, current_a
@ -262,6 +264,7 @@ struct Ade7880 {
bool calib_frequency; bool calib_frequency;
bool irq0_state; bool irq0_state;
uint8_t cycle_count; uint8_t cycle_count;
uint8_t heartbeat;
} Ade7880; } Ade7880;
/*********************************************************************************************/ /*********************************************************************************************/
@ -460,6 +463,7 @@ bool Ade7880SetCalibrate(void) {
#endif // ADE7880_PROFILING #endif // ADE7880_PROFILING
if (Ade7880Init()) { if (Ade7880Init()) {
Ade7880.heartbeat = 0;
return true; return true;
} }
} }
@ -534,6 +538,7 @@ void Ade7880Service0(void) {
// Poll sequence // Poll sequence
SkipSleep(false); SkipSleep(false);
Ade7880Cycle(); Ade7880Cycle();
Ade7880.heartbeat = 0;
Ade7880.irq0_state = 0; Ade7880.irq0_state = 0;
} }
@ -545,6 +550,15 @@ void IRAM_ATTR Ade7880Isr0(void) {
} }
} }
void Ade7880Heartbeat(void) {
// Reinit if interrupt stopped
Ade7880.heartbeat++;
if (Ade7880.heartbeat > ADE7880_HEARTBEAT) {
AddLog(LOG_LEVEL_DEBUG, PSTR("A78: Reinit"));
Ade7880SetCalibrate();
}
}
/*********************************************************************************************/ /*********************************************************************************************/
bool Ade7880SetDefaults(const char* json) { bool Ade7880SetDefaults(const char* json) {
@ -747,6 +761,9 @@ bool Xnrg23(uint8_t function) {
case FUNC_LOOP: case FUNC_LOOP:
if (Ade7880.irq0_state) { Ade7880Service0(); } if (Ade7880.irq0_state) { Ade7880Service0(); }
break; break;
case FUNC_EVERY_SECOND:
Ade7880Heartbeat();
break;
#ifdef ADE7880_MORE_REGS #ifdef ADE7880_MORE_REGS
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
Ade7880Show(1); Ade7880Show(1);