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
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
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.
- from Arduino pins to processor pins
- from processor pins to multiplexors
- from multiplexors to SERCOM pads
- from SERCOM pads to SPI roles
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
Each of them has four signal lines called
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:
and the SERCOM lines:
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
D are of interest here because they are used to
connect to SERCOMs. The table shows the connections for functions
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 of SERCOM2 as you can see in this table.
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.
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
- SDA - MOSI
- SCL - SCK
- D10 - SS
- D7 - MISO
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
PAD. When software assigns
function D to that pin, it makes a connection to the pad of
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:
SPI_PAD_0_SCK_1 MOSI=PAD, SCK=PAD
SPI_PAD_2_SCK_3 MOSI=PAD, SCK=PAD
SPI_PAD_3_SCK_1 MOSI=PAD, SCK=PAD
SPI_PAD_0_SCK_3 MOSI=PAD, SCK=PAD
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:
- turn on SPI
- set _SS to LOW(active)
- using a library function send and receive bytes one at a time
- shut down SPI
- set _SS to HIGH(inactive)/li>
- write the data received from the slave onto the serial port
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:
- Decide which pins are available.
- Choose a SERCOM that connects to three of those pins for
- Choose a pin for
- Decide which of the SERCOM connections will be used for which SPI connection
- Once you have determined a configuration that works, you can replace the values in the sample sketch
to run it.
Consider the other requirements for your sketch. Which pins do you need? Which pins are take by
other uses such as serial for the console?
Choose a SERCOM
Given the available pins, search the table in the multiplexing section above for a SERCOM device.
It should be one that isn't already in use and connects to at least 3 of the pins that are
available. Use this information to setup a
MySpiPadSet structure like the one in the
sample sketch. The
MySpiPadSet and sercom device are parameters for a
A pin for
It is your master mode program that will control the
MySpiPadSet structure in
MySpi.h includes an member for slave select.
This is not used in this example but included for future expansion of
The digital pin that you use for
_SS does not need to be connected to a SERCOM.
Connections between SERCOM and SPI signals
The choices for which of the SERCOM connections,
listed in the section above on
DIPI. These values are
passed as parameters to
The software configuration is complete ready to run.