SAM C21 SERCOM SPI Slave Example Project

Last modified by Microchip on 2023/11/10 11:08

Objective

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 the command and data. The SPI module operation is interrupt driven. The main processor routine is a simple state machine that performs 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:

Example 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.

ATSAMC21-XPRO board

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.

MCP2210 breakout module

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 Microchip Studio.

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

Back to top

Materials

Reference Materials

Back to top

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.

MCP2210 SPI Terminal Application

Information

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.

Back to top

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. Microchip Studio 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:

SPI slave demo connection diagram

The MCP2210 board signals may be connected to the SAM C21 Xplained Pro board header pins using female-female jumper cables as shown in the accompanying image.

SPI slave demo connection picture

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.

Back to top

Procedure

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 Microchip Studio.

SAM C21 Xplained PRO Studio window

Information

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.

Back to top.

Open the Solution

Select File > Open > Project/Solution…

File > Open > Project/Solution… menu

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

spi-slave-example.atsln file in the Open Project window

Select File > Open > Project/Solution…

File > Open > Project/Solution… menu

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

spi-slave-example.atsln file in the Open Project window

Back to top


Configure the Debugger

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

First, navigate to Project > Properties as shown:

Project > Properties menu

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

spi-slave-example window Tool settings

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

Save All button

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

First, navigate to Project > Properties as shown:

Project > Properties menu

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

spi-slave-example window Tool settings

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

Save All button

Back to top.


Rebuild/Program the Target

Click on the Start Without Debugging icon in Microchip 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.

Start Without Debugging button

If prompted, upgrade the EDBG firmware on the board:

Firmware Upgrade window

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:

SPI slave example results

Click on the Start Without Debugging icon in Microchip 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.

Start Without Debugging button

If prompted, upgrade the EDBG firmware on the board:

Firmware Upgrade window

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:

SPI slave example results

Back to top


Review the Command Packet Format

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

Commands

Command ByteCommand NameFunction
0x01ADDResult = Data0 + Data1
0x02SUBTRACTResult = Data0 – Data1
0x03MULTIPLYResult = Data0 * Data1
0x04INVERTResult =!((Data1 « 8) + Data0)
0x05ANDResult = Data0 & Data1
0x06ORResult = Data0 | Data1
0x07XORResult = Data0 ^ Data1

Packet Format

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

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

Commands

Command ByteCommand NameFunction
0x01ADDResult = Data0 + Data1
0x02SUBTRACTResult = Data0 – Data1
0x03MULTIPLYResult = Data0 * Data1
0x04INVERTResult =!((Data1 « 8) + Data0)
0x05ANDResult = Data0 & Data1
0x06ORResult = Data0 | Data1
0x07XORResult = Data0 ^ Data1

Packet Format

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

Back to top


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:

"MCP2210 Status: CONNECTED" dialog

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:

"MCP2210 Status: CONNECTED" dialog

Back to top


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:

SPI parameters pane

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:

'Hex Mode' display and 'Fill remaining with HEX' options

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:

SPI terminal pane

GP Settings

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

GP4 Settings pane

Back to top


Transfer SPI Data

Press the Transfer SPI Data button to send the packet

SPI terminal window

Press the Transfer SPI Data button to send the packet

SPI terminal window

Press the Transfer SPI Data button to send the packet

SPI terminal window

Press the Transfer SPI Data button to send the packet

SPI terminal window

Back to top


Results

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

Rx Data fields pane

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

Conclusions

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.