Hey,
Just thought I’d let everyone know the secret to getting Wire1 to work on the Electron.
I’ve been trying to test an i2c slave device using the C5 and C4 i2c peripheral, which is mapped to the Wire1 object.
I kept getting to Wire1.endTransmission()
and encountering an SOS hard fault.
If I switched to using Wire instead of Wire1, everything worked properly, with no fault.
After a few hours of trying to figure out the problem, I tried simply including the following code at the beginning of the setup() function:
pinMode(C5, OUTPUT);
pinMode(C4, OUTPUT);
This got rid of the hard fault condition and allowed things to work properly.
Here is the program that results in a hard fault:
#include "Particle.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_TRACE, //Default logging level for non-application messages
{
{ "app", LOG_LEVEL_ALL }, //Logging level for logs from "app" log category
{ "app.main", LOG_LEVEL_ALL}, //Logging level for logs from "app.main" log category
});
Logger myLog("app.main"); //Logger object used in this "main" file
void setup(){
Serial.begin(115200);
delay(8000); //Time to open the serial monitor
myLog.trace("Now calling Wire1.begin");
Wire1.begin();
delay(1000);
myLog.trace("Now calling Wire1.beginTransmission()");
delay(10);
Wire1.beginTransmission(0b0110101);
delay(10);
myLog.trace("Now calling Wire1.write()");
delay(10);
Wire1.write(0xa8);
delay(10);
myLog.trace("Now calling Wire1.endTransmission()");
delay(10);
Wire1.endTransmission();
delay(10);
myLog.trace("Finished calling all Wire1 functions");
delay(1000);
myLog.trace("Now exiting setup()");
}
void loop(){
}
and here is the modified code that ends up working:
#include "Particle.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_TRACE, //Default logging level for non-application messages
{
{ "app", LOG_LEVEL_ALL }, //Logging level for logs from "app" log category
{ "app.main", LOG_LEVEL_ALL}, //Logging level for logs from "app.main" log category
});
Logger myLog("app.main"); //Logger object used in this "main" file
void setup(){
Serial.begin(115200);
pinMode(C5, OUTPUT);
pinMode(C4, OUTPUT);
delay(8000); //Time to open the serial monitor
myLog.trace("Now calling Wire1.begin");
Wire1.begin();
delay(1000);
myLog.trace("Now calling Wire1.beginTransmission()");
delay(10);
Wire1.beginTransmission(0b0110101);
delay(10);
myLog.trace("Now calling Wire1.write()");
delay(10);
Wire1.write(0xa8);
delay(10);
myLog.trace("Now calling Wire1.endTransmission()");
delay(10);
Wire1.endTransmission();
delay(10);
myLog.trace("Finished calling all Wire1 functions");
delay(1000);
myLog.trace("Now exiting setup()");
}
void loop(){
}