yes, that was using the default i2c Wire. i cant get the new SERCOM i2c to work. i wonder if the Wire.h library is written in a way that prevents the
slaveWire.onRequest(requestEvent); // Register the onRequest event handler
slaveWire.onReceive(receiveEvent); // register event
from working on other SERCOM?
I still have 3 unmodified modules to play with.
the communication speed desired is so slow i would think using ASCII is fine?
I tried removing serial.prints and transmitting a byte. worked fine on unmodified module (default wire). got nothing on one using sercom4.
SLAVE CODE FOR TEST:
#include <Wire.h>
#include <Adafruit_SleepyDog.h>
#include "wiring_private.h" // pinPeripheral() function
#define slaveSDA PIN_PA12 //22 PA12
#define slaveSCL PIN_PA13 //38 PA13
TwoWire slaveWire(&sercom4, slaveSDA, slaveSCL);
String responseToSend; // This will hold the response to be sent
String command = "";
//#define Serial SerialUSB
/*********************************************************************************************************************
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();
slaveWire.begin(0x20); // join i2c bus with address 0x20
// Assign pins to SERCOM functionality
pinPeripheral(slaveSDA, PIO_SERCOM_ALT);
pinPeripheral(slaveSCL, PIO_SERCOM_ALT);
slaveWire.onReceive(receiveEvent); // register event
}
int x = 0;
void loop() {
Watchdog.reset(); //resets the watchdog timer. basically, this makes sure the code doesnt lock up.
delay(500);
if (x != 0){
Serial.println("XXX");
x = 0;
}
}
/*********************************************************************************************************************
i2c Slave Request and Recieve functions
*********************************************************************************************************************/
// I2C communication function for receiving data
void receiveEvent(int howMany){
// while(1 < Wire.available()); // loop through all but the last
x = slaveWire.read(); // receive byte as an integer
}
MASTER CODE FOR TEST:
#include <Wire.h>
void setup()
{
pinModulesSetup();
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 1;
void loop()
{
selectModule(1);
Wire.beginTransmission(0x20); // transmit to device #4
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
delay(500);
}
/*********************************************************************************************************************
Pin Modules Setup Code
*********************************************************************************************************************/
//all four pin modules share an address. They are selected using a digital pin going "HIGH". This triggers an interupt in the selected slave and lets it recieve commands.
#define pinmoduleaddress 0x20
#define pinmodule3 49 //PA07 { PORTA, 7, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_7 },
#define pinmodule2 50 //PA15 { PORTA, 15, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_7 },
#define pinmodule1 51 //PB00 { PORTB, 0, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_7 },
#define pinmodule4 52 //PB01 { PORTB, 1, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_7 },
void pinModulesSetup(){
//Wire.begin(); // Start I2C
pinMode(pinmodule1,OUTPUT);
pinMode(pinmodule2,OUTPUT);
pinMode(pinmodule3,OUTPUT);
pinMode(pinmodule4,OUTPUT);
selectModule(0); //deselect all modules
}
/*********************************************************************************************************************
Module Select Function
*********************************************************************************************************************/
int selectModule(int selection) {
// Set all module pins to LOW initially
digitalWrite(pinmodule1, LOW);
digitalWrite(pinmodule2, LOW);
digitalWrite(pinmodule3, LOW);
digitalWrite(pinmodule4, LOW);
// Select the appropriate module based on the input
switch (selection) {
case 1: digitalWrite(pinmodule1, HIGH); break;
case 2: digitalWrite(pinmodule2, HIGH); break;
case 3: digitalWrite(pinmodule3, HIGH); break;
case 4: digitalWrite(pinmodule4, HIGH); break;
case 0: return 0; // Deselection
default: return 1; // Invalid selection, return error
}
return 0; // Successful selection
}