SAM C21 SERCOM SPI Slave Example Project

 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 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:

example-transaction.png

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_angle.png

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

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 >

 Materials

Hardware Tools

Tool About Purchase
atsamc21-xpro-50px.png
SAM C21 Xplained Pro
Evaluation Kit
ADM00419-50px.png
ADM00419
MCP2210 Breakout Module

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel Studio
Integrated Development Environment
swtool-28px.png
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.

mcp2210-spi-terminal-application.png

Exercise Files

File Download
Installation
Instructions
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:

spi-slave-demo-connection-diagram.png

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

spi-slave-demo-connection-picture.png

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.

 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 Atmel Studio as shown:

samc21-xplained-pro-enum-success.png

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.

1

Open the Solution

Select File > Open > Project/Solution…

as7-open-spi-slave-solution.png


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

as7-open-spi-slave-solution-detail.png

2

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:

as7-config-debugger-spi-slave-example-1.png

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

as7-config-debugger-spi-slave-example-2.png

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

as7-config-debugger-spi-slave-example-3.png

3

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.

as7-start-without-debugging-spi-slave-example.png

If prompted, upgrade the EDBG firmware on the board:

as7-edbg-firmware-upgrade.png

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

4

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.

Commands
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)

5

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

6

Configure Parameters

a

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:

mcp2210-terminal-spi-parameters.png

b

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:

mcp2210-terminal-enable-hex-display.png


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:

mcp2210-terminal-tx-data-parameters.png

c

GP Settings

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

mcp2210-terminal-gp-parameters.png

7

Transfer SPI Data

Press the Transfer SPI Data button to send the packet

mcp2210-spi-terminal-tx-data.png

 Results

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

mcp2210-spi-terminal-rx-data-result.png


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.

© 2024 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.