0

Grand Central M4 - Using SERCOM7 as an I2C port
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Grand Central M4 - Using SERCOM7 as an I2C port

by RCohenQubiq on Sat Sep 12, 2020 1:44 pm

Hi folks,

I had no problems configuring SERCOM3 and SERCOM6 as I2C ports. They are already defined in variants.h, where WIRE_INTERFACES_COUNT is defined as 2 and all of the necessary handlers are created/aliased.

When I tried to use SERCOM7 as an I2C port, with GCM4 pins 51 and 52 for SDA and SCL, I was unable to get it running. Even after using pinPeripheral() to change the mode of those pins to PIO_SERCOM, things were not running.

I was able to easily get things running by adding a 3rd Wire interface to variants.h. (I could also have replaced one of the other 2 defined Wire interfaces, as my project will not be using them.)

However I'm worried about maintaining this file across platform updates. Is there a better solution?

Thanks in advance.

RCohenQubiq
 
Posts: 12
Joined: Wed Jul 15, 2020 1:38 pm

Re: Grand Central M4 - Using SERCOM7 as an I2C port

by RCohenQubiq on Thu Sep 17, 2020 5:06 pm

bump ;)

RCohenQubiq
 
Posts: 12
Joined: Wed Jul 15, 2020 1:38 pm

Re: Grand Central M4 - Using SERCOM7 as an I2C port

by westfw on Thu Sep 17, 2020 6:01 pm

Did you notice that SERCOM7 is normally an SPI port?
I don't think that SPI is turned on UNLESS you actually load SPI code, but that could easily happen...

I would think that you should be able to define the necessary handlers and symbols in your sketch, instead of in variant.h, similar to what I did for Serial interfaces here: viewtopic.php?f=63&t=167139#p819295

Let me give it a try...

westfw
 
Posts: 1781
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Grand Central M4 - Using SERCOM7 as an I2C port

by westfw on Thu Sep 17, 2020 9:02 pm

You can try this. It compiles. I don't have i2C stuff lying around to actually try it out...

Code: Select all | TOGGLE FULL SIZE
  #define sercom_UseI2CPins(_sercom, _i2c, _sda, _scl) \
  TwoWire _i2c(&sercom##_sercom, _sda, _scl); \
  void SERCOM##_sercom##_0_Handler(void) {  _i2c.onService(); } \
  void SERCOM##_sercom##_1_Handler(void) {  _i2c.onService(); } \
  void SERCOM##_sercom##_2_Handler(void) {  _i2c.onService(); } \
  void SERCOM##_sercom##_3_Handler(void) {  _i2c.onService(); }

// Enable I2C on the SPI pins
//              SERCOM#, name,  sda, scl
sercom_UseI2CPins( 7,    Wire7,  82,  81)  // Wire7 on SPI pins.

void setup() {
  Wire7.begin();
  // etc
}

westfw
 
Posts: 1781
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Grand Central M4 - Using SERCOM7 as an I2C port

by RCohenQubiq on Fri Sep 18, 2020 1:57 pm

Hello westfw,

Thanks, this works!

I modified the SDA and SCL from your example to 66 and 65, in order to use the correct pins on the GCM4.

Now, I will not need to modify the variant.h file. (I will need to figure out how to modify my C++ class to use this sample code instead).

Take care, and have a great weekend.

RCohenQubiq
 
Posts: 12
Joined: Wed Jul 15, 2020 1:38 pm

Please be positive and constructive with your questions and comments.