Arduino M0 slave on secondary I2C sercom

Hi,

Here is the (simple) idea.
I get 2 Arduino M0, they should be able to communicate through I2C each other.

The master can use the main I2C
The slave has to be set on a secondary I2C SERCOM

I first give a try with the 2 M0 on main I2C and everything is fine.
As soon as I use another sercom (“ExchWire”) on my slave Arduino everything goes wrong, I don’t receive anything on the slave and the master seems to be stuck.

I tried different SERCOM with no luck.

If anyone has an idea, I’m very interested !

(I already used SERCOM with serial on another project and everything was fine)

Here are the 2 files
SLAVE :

#define DEBUG_PORT SERIAL_PORT_USBVIRTUAL
#include <Wire.h>
#include "wiring_private.h"  // pinPeripheral() function

TwoWire ExchWire(&sercom1, 11, 13);

void setup()
{
  ExchWire.begin(18);

  pinPeripheral(11, PIO_SERCOM);
  pinPeripheral(13, PIO_SERCOM);

  ExchWire.onReceive(receiveEvent); // data slave recieved
}

void receiveEvent(int howMany) {
  while (1 < ExchWire.available()) { // loop through all but the last
    char c = ExchWire.read(); // receive byte as a character
    DEBUG_PORT.print(c);         // print the character
  }
  int x = ExchWire.read();    // receive byte as an integer
  DEBUG_PORT.println(x);         // print the integer
}

void loop() {
  delay(100);
}

MASTER :

#define DEBUG_PORT SERIAL_PORT_USBVIRTUAL
#include <Wire.h>

void setup()
{
  Wire.begin();
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(18); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
  x++;
  DEBUG_PORT.println(x);
  delay(500);
}

Here is the solution :slight_smile:

Just add this at the end of the “SLAVE” code :

void SERCOM1_Handler(void) {
  ExchWire.onService();
}

Tutorial SamdSercom
is used to allow the real I2C communication since the Wire library relies on interrupts