Thanks.
"Do you know if the SAMD21G18A processor can be a reliable I2C Slave when using Arduino ?" if all i wanted was slave only, i have left it running for hours without issue, so i assume it can.
yeah 1K is low, however it should work fine, especially considering i can have it work exclusively as a master or slave and never have an issue that i can notice.
so through some butchering, i broke out PA12 and PA13, sercom2, sercomalt4, cut the existing lines to the switch, and soldered the wires. i verified with an oscilloscope that it should be OK. I soldered 1K pullups directly to the microcontroller pins. i alsoe removed all extra code from the slave. it will now exclusively respond success. however, this new setup doesnt seem to work, the slave never enters receiveEvent or requestEvent.
doing this doesnt help either.
#define slaveSDA PIN_PA12 //22 PA12
#define slaveSCL PIN_PA13 //38 PA13
SIMPLIFIED SLAVE CODE:
#include <Wire.h>
#include <Adafruit_SleepyDog.h>
#include "wiring_private.h" // pinPeripheral() function
#define slaveSDA 22 //22 PA12
#define slaveSCL 38 //38 PA13
TwoWire slaveWire(&sercom4, slaveSDA, slaveSCL);
String responseToSend; // This will hold the response to be sent
String command = "";
/*********************************************************************************************************************
Setup and initilization function
*********************************************************************************************************************/
void setup() {
Watchdog.enable(10000); // turn on watchdog timer with 10 seconds
Serial.begin(9600);
while (!Serial) delay(10); // pause the serial port
Serial.println(F("testing"));
Wire.begin();
// Assign pins to SERCOM functionality
pinPeripheral(slaveSDA, PIO_SERCOM_ALT);
pinPeripheral(slaveSCL, PIO_SERCOM_ALT);
slaveWire.begin(0x20); // join i2c bus with address 0x20
slaveWire.onReceive(receiveEvent); // register event
slaveWire.onRequest(requestEvent); // Register the onRequest event handler
}
void loop() {
Watchdog.reset(); //resets the watchdog timer. basically, this makes sure the code doesnt lock up.
}
void switchI2CMode(bool asMaster) {
}
/*********************************************************************************************************************
i2c Slave Request and Recieve functions
*********************************************************************************************************************/
// Function to run when data requested from master
void requestEvent() {
Serial.println("In Request");
Serial.print("Command Sent: ");
Serial.println(responseToSend);
String responseWithTermination = responseToSend + "\n";
slaveWire.write(responseWithTermination.c_str(), responseWithTermination.length()); // Send the response
}
// I2C communication function for receiving data
void receiveEvent(int howMany) {
Serial.println("In Recieve");
if (slaveWire.available() > 0) {
command = "";
while (slaveWire.available()) {
char c = slaveWire.read();
command += c;
}
Serial.print("Command Reieved: ");
Serial.println(command);
responseToSend = processCommand(command); // Process the command and store the response
}
}
/*********************************************************************************************************************
i2c command handling functions
*********************************************************************************************************************/
// Process received command
String processCommand(String command) {
return "<success,1.0>";
}
