Third try to fix counter interrupt storm

This commit is contained in:
Theo Arends 2020-07-20 11:20:58 +02:00
parent f40c186e79
commit eeed6768d0
3 changed files with 13 additions and 42 deletions

View File

@ -969,6 +969,9 @@ void Every250mSeconds(void)
SettingsSave(1); // Free flash for OTA update SettingsSave(1); // Free flash for OTA update
} }
if (ota_state_flag <= 0) { if (ota_state_flag <= 0) {
#ifdef USE_COUNTER
CounterInterruptDisable(true); // Prevent OTA failures on 100Hz counter interrupts
#endif // USE_COUNTER
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
if (Settings.webserver) StopWebserver(); if (Settings.webserver) StopWebserver();
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
@ -1000,15 +1003,6 @@ void Every250mSeconds(void)
char *bch = strrchr(mqtt_data, '/'); // Only consider filename after last backslash prevent change of urls having "-" in it char *bch = strrchr(mqtt_data, '/'); // Only consider filename after last backslash prevent change of urls having "-" in it
if (bch == nullptr) { bch = mqtt_data; } // No path found so use filename only if (bch == nullptr) { bch = mqtt_data; } // No path found so use filename only
/*
char *ech = strrchr(bch, '.'); // Find file type in filename (none, .bin or .gz)
if ((ech != nullptr) && (0 == strncasecmp_P(ech, PSTR(".GZ"), 3))) {
char *fch = ech;
*fch = '\0';
ech = strrchr(bch, '.'); // Find file type .bin.gz
*fch = '.';
}
*/
char *ech = strchr(bch, '.'); // Find file type in filename (none, .ino.bin, .ino.bin.gz, .bin, .bin.gz or .gz) char *ech = strchr(bch, '.'); // Find file type in filename (none, .ino.bin, .ino.bin.gz, .bin, .bin.gz or .gz)
if (ech == nullptr) { ech = mqtt_data + strlen(mqtt_data); } // Point to '/0' at end of mqtt_data becoming an empty string if (ech == nullptr) { ech = mqtt_data + strlen(mqtt_data); } // Point to '/0' at end of mqtt_data becoming an empty string
@ -1055,6 +1049,9 @@ void Every250mSeconds(void)
ResponseAppend_P(PSTR("\"}")); ResponseAppend_P(PSTR("\"}"));
// restart_flag = 2; // Restart anyway to keep memory clean webserver // restart_flag = 2; // Restart anyway to keep memory clean webserver
MqttPublishPrefixTopic_P(STAT, PSTR(D_CMND_UPGRADE)); MqttPublishPrefixTopic_P(STAT, PSTR(D_CMND_UPGRADE));
#ifdef USE_COUNTER
CounterInterruptDisable(false);
#endif // USE_COUNTER
} }
} }
break; break;

View File

@ -2600,7 +2600,7 @@ void HandleUploadDone(void)
MqttRetryCounter(0); MqttRetryCounter(0);
#ifdef USE_COUNTER #ifdef USE_COUNTER
CounterInterruptDisable(false); CounterInterruptDisable(false);
#endif #endif // USE_COUNTER
WSContentStart_P(S_INFORMATION); WSContentStart_P(S_INFORMATION);
if (!Web.upload_error) { if (!Web.upload_error) {
@ -2677,8 +2677,8 @@ void HandleUploadLoop(void)
} else { } else {
MqttRetryCounter(60); MqttRetryCounter(60);
#ifdef USE_COUNTER #ifdef USE_COUNTER
CounterInterruptDisable(true); CounterInterruptDisable(true); // Prevent OTA failures on 100Hz counter interrupts
#endif #endif // USE_COUNTER
#ifdef USE_EMULATION #ifdef USE_EMULATION
UdpDisconnect(); UdpDisconnect();
#endif // USE_EMULATION #endif // USE_EMULATION
@ -2876,7 +2876,7 @@ void HandleUploadLoop(void)
MqttRetryCounter(0); MqttRetryCounter(0);
#ifdef USE_COUNTER #ifdef USE_COUNTER
CounterInterruptDisable(false); CounterInterruptDisable(false);
#endif #endif // USE_COUNTER
Web.upload_error = 7; // Upload aborted Web.upload_error = 7; // Upload aborted
if (UPL_TASMOTA == Web.upload_file_type) { Update.end(); } if (UPL_TASMOTA == Web.upload_file_type) { Update.end(); }
} }

View File

@ -49,7 +49,6 @@ struct COUNTER {
uint32_t last_cycle; uint32_t last_cycle;
uint32_t cycle_time; uint32_t cycle_time;
//void ICACHE_RAM_ATTR CounterUpdate(uint8_t index) {
void ICACHE_RAM_ATTR CounterIsrArg(void *arg) { void ICACHE_RAM_ATTR CounterIsrArg(void *arg) {
uint32_t index = *static_cast<uint8_t*>(arg); uint32_t index = *static_cast<uint8_t*>(arg);
@ -94,15 +93,15 @@ void ICACHE_RAM_ATTR CounterIsrArg(void *arg) {
// add 100.000 cpu ticks to ensure right step calculation // add 100.000 cpu ticks to ensure right step calculation
uint32_t steps = (current_cycle-last_cycle+100000)/(clockCyclesPerMicrosecond() * 10000); uint32_t steps = (current_cycle-last_cycle+100000)/(clockCyclesPerMicrosecond() * 10000);
cycle_time = (current_cycle-last_cycle)/steps; cycle_time = (current_cycle-last_cycle)/steps;
#ifdef ESP8266 #ifdef ESP8266
pinMode(Pin(GPIO_PWM1, index), OUTPUT); pinMode(Pin(GPIO_PWM1, index), OUTPUT);
uint32_t high = (cycle_time * 5) / 1023; uint32_t high = (cycle_time * 5) / 1023;
uint32_t low = cycle_time - high; uint32_t low = cycle_time - high;
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t // Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t
startWaveformClockCycles(Pin(GPIO_PWM1, index), high, low, 0, -1, 0, true); startWaveformClockCycles(Pin(GPIO_PWM1, index), high, low, 0, -1, 0, true);
#else #else // ESP32
analogWrite(Pin(GPIO_PWM1, index), 5); analogWrite(Pin(GPIO_PWM1, index), 5);
#endif #endif // ESP8266 - ESP32
} }
last_cycle = current_cycle; last_cycle = current_cycle;
} }
@ -120,27 +119,7 @@ void ICACHE_RAM_ATTR CounterIsrArg(void *arg) {
} }
} }
} }
/*
void ICACHE_RAM_ATTR CounterUpdate1(void)
{
CounterUpdate(0);
}
void ICACHE_RAM_ATTR CounterUpdate2(void)
{
CounterUpdate(1);
}
void ICACHE_RAM_ATTR CounterUpdate3(void)
{
CounterUpdate(2);
}
void ICACHE_RAM_ATTR CounterUpdate4(void)
{
CounterUpdate(3);
}
*/
/********************************************************************************************/ /********************************************************************************************/
void CounterInterruptDisable(bool state) { void CounterInterruptDisable(bool state) {
@ -172,20 +151,15 @@ bool CounterPinState(void)
void CounterInit(void) void CounterInit(void)
{ {
// typedef void (*function) () ;
// function counter_callbacks[] = { CounterUpdate1, CounterUpdate2, CounterUpdate3, CounterUpdate4 };
for (uint32_t i = 0; i < MAX_COUNTERS; i++) { for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
if (PinUsed(GPIO_CNTR1, i)) { if (PinUsed(GPIO_CNTR1, i)) {
Counter.any_counter = true; Counter.any_counter = true;
pinMode(Pin(GPIO_CNTR1, i), bitRead(Counter.no_pullup, i) ? INPUT : INPUT_PULLUP); pinMode(Pin(GPIO_CNTR1, i), bitRead(Counter.no_pullup, i) ? INPUT : INPUT_PULLUP);
if ((0 == Settings.pulse_counter_debounce_low) && (0 == Settings.pulse_counter_debounce_high) && !Settings.flag4.zerocross_dimmer) { if ((0 == Settings.pulse_counter_debounce_low) && (0 == Settings.pulse_counter_debounce_high) && !Settings.flag4.zerocross_dimmer) {
Counter.pin_state = 0; Counter.pin_state = 0;
// attachInterrupt(Pin(GPIO_CNTR1, i), counter_callbacks[i], FALLING);
attachInterruptArg(Pin(GPIO_CNTR1, i), CounterIsrArg, &ctr_index[i], FALLING); attachInterruptArg(Pin(GPIO_CNTR1, i), CounterIsrArg, &ctr_index[i], FALLING);
} else { } else {
Counter.pin_state = 0x8f; Counter.pin_state = 0x8f;
// attachInterrupt(Pin(GPIO_CNTR1, i), counter_callbacks[i], CHANGE);
attachInterruptArg(Pin(GPIO_CNTR1, i), CounterIsrArg, &ctr_index[i], CHANGE); attachInterruptArg(Pin(GPIO_CNTR1, i), CounterIsrArg, &ctr_index[i], CHANGE);
} }
} }