SAMD21 second I2C via SERCOM

Everything ARM and LPC

Moderator: phalanx

Post Reply
Sennert
Posts: 1
Joined: Tue Jan 30, 2018 6:03 pm

SAMD21 second I2C via SERCOM

Post by Sennert » Tue Jan 30, 2018 6:19 pm

Hello Community,

I've spent several days trying to get a second I2C Bus running on the Sparkfun SAMD21 DEV Board.
Has anybody managed to make sense of the free SERCOMs? And maybe even used it as a second I2C?

As I couldn't find that much about how to do it i was following Lady Ada's Instruction on how to use the SERCOMs on Arduino Zero or their variant.

I tried Several Combinations of Pins (4+3, 11+13) with the SERCOM1 or SERCOM2 etc.
Used an I2C Scanner Sketch and it uploaded without errors.

Unfortunately it stops working after the first printout of "Scanning...". So the Problem probably comes at line

Code: Select all

myWire.beginTransmission(address);
I hope somebody can point me to my the right address. Do I have to change the variant File?

Code: Select all

/* 

 Demonstrates use of the Wire library and how to instatiate another Wire
 Sends data as an I2C/TWI slave device
 Refer to the "Wire Master Reader" example for use with this

 Created 20 Jun 2016
 by 
 Arturo Guadalupi <a.guadalupi@arduino.cc>
 Sandeep Mistry <s.mistry@arduino.cc>
*/

#include <Wire.h>
#include "wiring_private.h"

TwoWire myWire(&sercom1, 11, 13);   // Create the new wire instance assigning it to pin 0 and 1

void setup()
{
  myWire.begin(2);                // join i2c bus with address #2
  SerialUSB.begin(9600);
  while (!SerialUSB);             // Leonardo: wait for serial monitor
  SerialUSB.println("\nI2C Scanner");
  
  pinPeripheral(11, PIO_SERCOM);   //Assign SDA function to pin 0
  pinPeripheral(13, PIO_SERCOM);   //Assign SCL function to pin 1

  myWire.onRequest(requestEvent); // register event
}

void loop()
{
  byte error, address;
  int nDevices;
 
  SerialUSB.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    myWire.beginTransmission(address);
    error = myWire.endTransmission();
 
    if (error == 0)
    {
      SerialUSB.print("I2C device found at address 0x");
      if (address<16)
        SerialUSB.print("0");
      SerialUSB.print(address,HEX);
      SerialUSB.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      SerialUSB.print("Unknown error at address 0x");
      if (address<16)
        SerialUSB.print("0");
      SerialUSB.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    SerialUSB.println("No I2C devices found\n");
  else
    SerialUSB.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
  
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  myWire.write("hello "); // respond with message of 6 bytes
                          // as expected by master
}

// Attach the interrupt handler to the SERCOM
extern "C" {
  void SERCOM1_Handler(void);

  void SERCOM1_Handler(void) {
    myWire.onService();
  }
}
Thanks in advance ;)
steffen

Post Reply