Here is that shorter example code:
#include "application.h"
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
SerialLogHandler logHandler(LOG_LEVEL_ALL);
#define SENSOR_FAIL 2
#define SENSOR_SUCCESS 3
int dataPin = D3;
int powerPin = C0;
int status;
uint32_t timerStart;
uint32_t timerStop;
uint16_t rpm;
uint32_t delayTimer = 3000;
volatile unsigned long rpmCount;
void getRpm();
void interrupt();
void setup() {
Serial.begin(9600);
pinMode(dataPin, INPUT);
pinMode(powerPin, OUTPUT);
}
void loop() {
getRpm();
Log.info("RPM: %u", rpm);
delay(1000);
}
void getRpm() {
digitalWrite(powerPin, LOW);
delay(50);
Log.trace("TACH0");
rpm = 0;
rpmCount = 0;
attachInterrupt(dataPin, interrupt, RISING);
timerStart = millis();
Log.trace("TACH1");
while(millis() - timerStart < delayTimer) {
Particle.process();
}
Log.trace("TACH2");
timerStop = millis();
Log.trace("TACH3");
//Disable interrupt when calculating
detachInterrupt(dataPin);
Log.trace("TACHDET");
Serial.flush();
Log.trace("TACH4");
Serial.flush();
rpm = (uint16_t)(rpmCount / ((timerStop - timerStart) / 1000));
Log.trace("TACH5");
Serial.flush();
rpmCount = 0;
Log.trace("TACH6");
Serial.flush();
if(rpm >= 0 && rpm < 5000) {
status = SENSOR_SUCCESS;
} else {
status = SENSOR_FAIL;
rpm = 0;
}
digitalWrite(powerPin, HIGH);
return;
}
void interrupt() {
rpmCount++;
}
This crashed my Electron after 29 minutes. The electron running this code is on 0.7.0. The RPM pulse signal is about 3700 pulses per second. A link to the log file is here: https://pastebin.com/DFVM65CL
In parallel I ran a version of the code turning interrupts on and off before and after detachInterrupt, but in my main application. That too crashed that Electron. I will try this on the simpler code above and report back.
Both of these happened quicker than I would have expected, but I don’t think this bug is related to time, so it might be more random.
One obvious item to remove would be Particle.process, although I’m not sure why this would cause this crash?