SAM C21 SERCOM SPI Slave Example Project


This page provides a code example that configures SERCOM5 as an SPI Slave using the additional SPI Addressing functionality. The SERCOM module receives a command followed by two data bytes and then returns an arithmetic or logical operation based on command and data. The SPI module operation is interrupt driven. The main processor routine is a simple state machine that performs the mathematical calculations. The SERCOM module uses the slave-select (SS) interrupt to detect the start of an SPI transaction and enable the response. Below is a screenshot of a data transaction:


This example targets the SAM C21 family of devices. The application is designed to work using the SAM C21 Xplained PRO evaluation kit (ATSAMC21-XPRO) which contains the ATSAMC21J18A Arm® Cortex®-M0+ MCU.


This application uses:

  • SERCOM5 in SPI Slave mode, with Address Range slave addressing mode enabled
  • Pins PB01 (SCK), PB00 (MOSI), PB02 (MISO), PB03 (SS)
  • Pin PA15 (User LED - LED0)

To run this demo, an SPI Master host must be present to issue the commands necessary for the communication. In this demo, we use the MCP2210 USB-SPI Breakout Module (ADM00419), which can be conveniently connected to a PC where SPI transactions can be issued using the provided SPI terminal application.


This code example uses a register-direct C-coding style (i.e., no software framework) and is built using the Arm GCC compiler toolchain, which is installed along with the Atmel Studio 7 IDE.

Visit the following page to learn how to configure the SERCOM SPI peripheral registers for this application:

SERCOM SPI Slave Configuration
Learn more >


Hardware Tools

Tool About Purchase
SAM C21 Xplained Pro
Evaluation Kit
MCP2210 Breakout Module

Software Tools

Tool About Installers
Windows Linux Mac OSX
Atmel Studio
Integrated Development Environment
MCP2210 SPI Terminal
Communications Program

MCP2210 SPI Terminal Application

This PC application simulates an SPI Master device and interacts with our SPI Slave application using the MCP2210 USB-SPI Breakout Module.


Exercise Files

File Download
Windows Linux Mac OSX
Example Project

We recommend extracting the ZIP file to your C:\ folder.

You should see the folder C:\MTT\32arm\samc21\code-examples-gcc\sercom\spi-slave-example containing the solution spi_slave_example.atsln.

 Connection Diagram

SAM C21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMC21J18A using the Serial Wire Debug (SWD) interface. The EDBG also includes a Virtual Com port interface over UART, a Data Gateway Interface (DGI) over SPI and TWI, and it monitors four of the SAM C21 GPIOs. Atmel Studio 7 is used as a front-end for the EDBG.

LED0 is driven by this application and is connected to port PA15, while the SPI pins MISO, MOSI, SCK and SS are connected to pins PB02, PB00, PB01, and PB03 respectively via extension header 2:


The MCP2210 board signals may be connected to the SAM C21 Xplained Pro board header pins using female-female jumper cables as shown below:


When making the circuit connections, ensure that the supply voltage setting jumpers on each board are matched, as both the MCP2210 breakout board and SAM C21 Xplained Pro can operate at either 3.3 V or 5 V.


Attach the SAM C21 Xplained PRO board to your computer using a USB A-to-MicroB cable. Attach the MCP2210 breakout board to your computer using a USB A-to-MiniB cable. Connect the MCP2210 breakout board pins to the Xplained PRO pins as shown above.

If the SAM C21 Xplained PRO board has been successfully enumerated, you should see the board image come up in Atmel Studio as shown:


The board is identified by the last four digits of its serial number (see the sticker on the bottom of the board). In this example, the last four digits are 3514.


Open the Solution

Select File > Open > Project/Solution…


Navigate to the Solution folder and select the spi-slave-example.atsln solution file:



Configure the Debugger

Next, you need to configure the debugger in Atmel Studio to discover and connect to the target EDBG IC on your Xplained Pro board.

First, navigate to Project > Properties as shown:


Next, under the project's 'Tool' settings, select your EDBG target from the pull-down. Select 'SWD' as the interface:


Save the tool setting by clicking on the Save All button:



Rebuild/Program the Target

Click on the Start Without Debugging icon in Atmel Studio which re-builds the HEX file from the project source code, downloads/programs the HEX file onto the target MCU, and releases the target MCU Reset pin, allowing the program to execute.


If prompted, upgrade the EDBG firmware on the board:


You need to click on Start Without Debugging again after an EDBG firmware upgrade in order to rebuild/program the target.

After the programming is complete, you should see the Amber LED LED0 on the SAM C21 Xplained PRO turn on solid:



Review Command Packet Format

The SPI Master is to transmit a formatted, 6-byte packet, consisting of a command byte, 2 input operand bytes, a dummy byte, then clocking out 2 result bytes.

Command Byte Command Name Function
0x01 ADD Result = Data0 + Data1
0x02 SUBTRACT Result = Data0 – Data1
0x03 MULTIPLY Result = Data0 * Data1
0x04 INVERT Result =!((Data1 « 8) + Data0)
0x05 AND Result = Data0 & Data1
0x06 OR Result = Data0 | Data1
0x07 XOR Result = Data0 ^ Data1

Packet Format
Byte Number Function
1 Command
2 Input Data 0 (byte)
3 Input Data 1 (byte)
4 Dummy Byte used for data turnaround
5 Output Data 0 (low byte)
6 Output Data 1 (high byte)


Open MCP2210 SPI Terminal Application

Open the MCP2210 SPI Terminal Application. If the MCP2210 board is recognized, you should see the "MCP2210 Status: CONNECTED" dialog displayed on the lower-right-side of the application as shown:



Configure Parameters


SPI Parameters

Enter the following parameters under SPI Parameters:

  • Bit-rate: 100,000
  • SPI Mode: 0 (if a change to the SPI mode is desired, the SPI configuration in the SAM C21 must be changed as well)
  • Number of bytes to transfer: 6 (MUST be 6)
  • CS-to-Data Delay: 1
  • Data-to-Data Delay: 1
  • Data-to-CS Delay: 1

The completed values are as shown:



Tx Data Parameters

We issue an 'ADD' command packet 0x01 by adding 2 bytes, 0xC3 and 0xA5, and returning the result 0x0168.

First, enable 'Hex Mode' display and 'Fill remaining with HEX' as shown:


Enter the following parameters under Tx Data Parameters:

  • Byte 1: 0x01 (ADD Command)
  • Byte 2: 0xC3 (Operand 1)
  • Byte 3: 0xA5 (Operand 2)
  • Byte 4: 0xFF (Dummy Byte used for data turn around)
  • Byte 5: 0x00 (Dummy byte used to clock in result low byte)
  • Byte 6: 0x00 (Dummy byte used to clock in result high byte)

The completed values are as shown:



GP Settings

Select the MCP2210 GP4 output signal as the active-low SS signal for SPI communications. GP Settings should appear as shown:



Transfer SPI Data

Press the Transfer SPI Data button to send the packet



A successful result is indicated in the Rx Data fields with the correct arithmetic result 0x0168 returned by the SAM C21 board:


Additionally, the LED0 on the SAM C21 Xplained Pro board will toggle every time a command is successfully executed.


This example project demonstrated how to initialize the SAM C21 SERCOM SPI peripheral in Slave mode, as well as how to handle the SPI interrupts generated by the different sources.

© 2019 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.