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 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:
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 Microchip Studio.
Visit the following page to learn how to configure the SERCOM SPI peripheral registers for this application:
Materials
Reference Materials
- Part Number: ATSAMC21-XPRO - SAM C21 Xplained Pro evaluation kit
- Part Number: ADM00419 - MCP2210 Breakout Module
- Microchip 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.
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:
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.
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.
Open the Solution
Select File > Open > Project/Solution…
Navigate to the Solution folder and select the spi-slave-example.atsln solution file:
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 Microchip 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 setting, select your EDBG target from the pull-down. Select SWD as the interface:
Save the tool setting by clicking on the 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:
Next, under the project's Tool setting, 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 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.
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:
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.
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 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 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) |
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 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:
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
Press the Transfer SPI Data button to send the packet
Press the Transfer SPI Data button to send the packet
Press the Transfer SPI Data button to send the packet
Results
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.
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.