SPI choices for D21-based Arduino boards

On Arduino models with the Atmel SAMD21G18A(D21 for short) processor such as the Tian and the M0 Pro, it is possible to implement Serial Peripheral Interface(SPI) protocol on any of several different combinations of Arduino pins. On those models, the SPIClass class in the Arduino libraries supports a SPI bus over the ICSP connector as it does on other Arduino models. But that processor has circuits to support several SPI buses. You can write a subclass of SPIClass. In that class, you can configure your own SPI bus to use a different set of digital pins for an SPI bus.

To do this, you need to learn a little about what is inside the D21

Hardware

The D21

Inside this ARM processor is a collection of configurable peripheral interface devices. The D21 Datasheet describes the chip in great detail. It is the source for information in this document.

Connecting to the D21

The signals follow this path from connected SPI device to program running on the D21: The sections below follow this path in more detail.

Processor Pins, Arduino Pins

D21 processor pins are labeled A00 through A31 and B00 through B31. On the Arduino boards, I/O pins D0 through D13 and A0 through A5, SDA and SCL are each connected to a processor pin.

Serial Communications Interfaces

A Serial Communications Interface(a SERCOM) is a circuit inside the D21 for serial communications. Each SERCOM can be configured to support one of several different serial protocols such as USART, SPI and I2C. There are six SERCOMs : SERCOM0, ..., SERCOM5. Each of them has four signal lines called PAD[0], PAD[1], PAD[2], PAD[3].

Some of the serial interfaces are already assigned to Serial, Serial5 and SPI classes. Those are SERCOM0, SERCOM5 and SERCOM4. This leaves SERCOM1, SERCOM2, and SERCOM3 available for SPI–as long as you don't need those serial interfaces to support other serial protocols.

A sketch can configure the mapping between the SPI signals: MISO, MOSI, SCK, _SS and the SERCOM lines: PAD[0], PAD[1], PAD[2], PAD[3] within limits.

Multiplexing

Each of the processor pins can be configured to perform one of many possible functions. A pin is assigned to a function named with a single letter A through H. These are described in the datasheet in the chapter on multiplexing. Functions C and D are of interest here because they are used to connect to SERCOMs. The table shows the connections for functions C and D for the processor pins that are connected to Arduino pins.

A program assigns one of these functions to a pin. When it assigns function D to pin D2, the pin becomes, effectively, a connection to the PAD[0] of SERCOM2 as you can see in this table.

ArduinoProcessorCD
PinPinSERCOMSERCOM_ALT
D0PA11SERCOM0PAD[3]SERCOM2PAD[3]
D1PA10SERCOM0PAD[2]SERCOM2PAD[2]
D2PA08SERCOM0PAD[0]SERCOM2PAD[0]
D3PA09SERCOM0PAD[1]SERCOM2PAD[1]
D4PA14SERCOM2PAD[2]SERCOM4PAD[2]
D5PA15SERCOM2PAD[3]SERCOM4PAD[3]
D6PA20SERCOM5PAD[2]SERCOM3PAD[2]
D7PA21SERCOM5PAD[3]SERCOM3PAD[3]
D8PA06SERCOM0PAD[2]
D9PA07SERCOM0PAD[3]
D10PA18SERCOM1PAD[2]SERCOM3PAD[2]
D11PA16SERCOM1PAD[0]SERCOM3PAD[0]
D12PA19SERCOM1PAD[3]SERCOM3PAD[3]
D13PA17SERCOM1PAD[1]SERCOM3PAD[1]
A0PA02
A1PB08SERCOM4PAD[0]
A2PB09SERCOM4PAD[1]
A3PA04SERCOM0PAD[0]
A4PA05SERCOM0PAD[1]
A5PB02SERCOM5PAD[0]
SDAPA22SERCOM3PAD[0]SERCOM5PAD[0]
SCLPA23SERCOM3PAD[1]SERCOM5PAD[1]

Software

Libraries

There are a few library files that you need to look at to follow how the example works: These paths are typical for Macintosh. Note that, in order to see the files in the finder, you need to right click on the application and select Show Package Contents.

Pin assignments

In the example, Arduino pins are assigned to custom SPI roles:

The choice of pins and signals is entirely arbitrary within limits. This figure shows the target configuration.

Multiplexers

In the multiplexing table above, we can see that our goal can be reached. In the row for Arduino pin D7, in the SERCOM-ALT column, D, is SERCOM3, PAD[3]. When software assigns function D to that pin, it makes a connection to the pad of SERCOM3.

DOPO and DIPO

Data out pinout and data in pinout. These are fields in hardware control registers. A sketch can set the values when initializing a SERCOM.

For SPI master, possible DOPO values are:

For SPI master, possible DIPO values are: Example sketch Sketches have been run on Arduino IDE 1.7.11. When this sketch is loaded, the user can enter a command to tell the sketch to send a short text message to the slave. The sketch displays the response from the slave in the Serial Monitor.

To send and receive a message, the sketch will:

Before running the sketch, the SPI pins should be connected to another SPI device configured as slave. Don't forget to attach GND as well.

Moving on to your own SPI bus

To build your own custom SPI connections on the Tian or other Arduino based on a SAMD21 processor: