Have been using the Wire library for over a year without problem. Now all of a sudden it doesn’t seem to work.
I have the following code:
cpp
#include "Particle.h" // Use this include for Particle Dev where everything is in one directory.
// Comment this out for normal operation
SYSTEM_MODE(SEMI_AUTOMATIC); // skip connecting to the cloud for (Electron) testing
SYSTEM_THREAD(ENABLED);
//Define log handler using serial monitor (dumps logs to serial monitor)
SerialLogHandler logHandler(LOG_LEVEL_ALL);
static Logger myLog("Electron"); //Logger object used in this "main.cpp" file
unsigned int last_i2c_transaction = 0;
const unsigned int time_between_i2c_transactions = 3000;
unsigned int last_led_blink = 0;
void setup(){
Serial.begin(115200);
delay(1000);
myLog.info("Initializing i2c peripheral");
Wire.begin(0X05); //Join the i2c bus with address of 0x05
pinMode(D7, OUTPUT);
digitalWrite(D7, LOW);
myLog.info("Waiting 3 seconds...");
delay(3000);
myLog.info("Setup finished!");
}
void loop(){
/*=============================================>>>>>
= Periodically send a hello world message on the i2c bus =
===============================================>>>>>*/
if((millis() - last_i2c_transaction) > time_between_i2c_transactions){
if(Wire.isEnabled()){
myLog.info("Sending a byte of data");
Serial.flush();
digitalWrite(D7, HIGH); //Trigger on-board LED (triggers capture on logic analyzer/oscilloscope)
Wire.beginTransmission(0X06);
Wire.write(0XF1); //Four ones, three zeroes, and a final one --> 0b11110001
byte writeResult = Wire.endTransmission(true);
myLog.info("Done sending... write result = %u", writeResult);
Serial.flush();
last_i2c_transaction = millis();
}
else{
myLog.info("Wire not enabled!");
Serial.flush();
}
}
}
And here is my logic analyzer output:
and here is my debug serial output:
0000001054 [Electron] INFO: Initializing i2c peripheral
0000001054 [Electron] INFO: Waiting 3 seconds...
0000004054 [Electron] INFO: Setup finished!
0000004055 [Electron] INFO: Sending a byte of data
0000001054 [Electron] INFO: Initializing i2c peripheral
0000001054 [Electron] INFO: Waiting 3 seconds...
0000004054 [Electron] INFO: Setup finished!
0000004055 [Electron] INFO: Sending a byte of data
At this point, the Electron stops running and just sits idle (no breathing RGB led even).
I have two Electrons and both behave the same.
Both are running Particle OS v 0.6.1
Am I crazy?
Note: Even if I remove the logic analyzer, oscilloscope probe, remove the pullup resistors etc etc until I just have an Electron sitting on my desk all alone, it still crashes without SOS error code
Update: Changed the sourcecode posted (above) to get rid of inconsistencies between comments and actual code