how to use on-board SPI

123 views
Skip to first unread message

Fustercluck

unread,
Nov 22, 2017, 3:23:24 PM11/22/17
to Mechaduino

Hi,

TL;DR: How can the external SPI interface be used? Arduino SPI seems to be connected to an separate, internal bus.

I'm trying to interface the Mechaduino to an external processor via SPI, with the external processor (AVR) acting as a slave.

I connect from header/hole for SCK/D13 and MOSI/D11 (and of course gnd) to the corresponding pins on the AVR. Also added a new slave select pin, to select the AVR--as opposed to the angle sensor--when doing SPI transfers.

I think wrote a bit of test code, using the standard Arduino spi lib--i.e. Spi.Transfer, etc.

Unfortunately, I didn't see any activity on the SPI lines (on my scope). I was confused by this, since clearly the standard mechaduino code--still running--is clearly reading from the position sensor successfully, and it uses the standard SPI lib.

Then I looked at the Mechaduino schematic, and noticed that the lines connecting to the angle sensor are *not* the ones exposed on the board. This would explain why I don't see any activity on the external SPI pins.

So, if that's right, then what are the external SPI for and how can they be used?

Thanks,
Dan

Tropical Labs

unread,
Nov 23, 2017, 5:18:18 PM11/23/17
to Mechaduino
You can configure multiple SPI interfaces on the SAMD21/Arduino Zero.  This Adafruit tutorial shows how to configure pins 11,12,13:
https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports/creating-a-new-spi

I've verified that the code near the end of this tutorial does work when added into the Mechaduino firmware, and I see the expected SPI packets and clock on pins 11 & 13 using a logic analyzer.  I haven't done much testing beyond this though.

-Joe

Fustercluck

unread,
Nov 24, 2017, 2:07:50 PM11/24/17
to Mechaduino

Terrific. I'll give this a try and report back.

Thanks much.

Dan

Fustercluck

unread,
Nov 24, 2017, 4:11:21 PM11/24/17
to Mechaduino


Hmm--well, the code shown below didn't work for me; I don't see any activity on SCK (D13) or MOSI (D11) on the scope.

I confirmed that avrSpiTest() is executing by seeing the chip select line toggle.

Any ideas? Could you share the code you used for your test?

Thanks.
Dan

SPIClass SPI1 (&sercom1, 12, 13, 11, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_3);

// called from setup (not shown here)
void setupAVRSpi()
{
  SPI1.begin();
}


void loop() 
{
  serialCheck();             
  avrSpiTest();
}

   
void avrSpiTest()
{
  static byte count = 0;
  digitalWrite(avrChipSelectPin, LOW);
  SPI1.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  SPI1.transfer(count++);
  SPI1.endTransaction();
  digitalWrite(avrChipSelectPin, HIGH);
}

Tropical Labs

unread,
Nov 24, 2017, 6:31:06 PM11/24/17
to Mechaduino
If you read a little further down in that tutorial it explains that you have to change the pin MUX type by adding this code:

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

The example code in the tutorial that contains this works.  Adding this code into the appropriate places in the Mechaduino firmware also works (see attached).

-Joe
Mechaduino_SPI.ino

Fustercluck

unread,
Nov 24, 2017, 7:08:02 PM11/24/17
to Mechaduino

Duh--missed the "OK ... that wont work" comment.

In any event, your sample code works perfectly for me.

Thanks a ton for the prompt help.
Reply all
Reply to author
Forward
0 new messages