SAM D21 SERCOM SPI Master Example Project

Last modified by Microchip on 2023/11/09 09:09

Objective

This page provides a code example that configures SERCOM5 as an SPI Master to read or write the onboard Serial Flash memory device.

This example targets the SAM D21 family of devices. The application is designed to work using the SAM D21 Xplained PRO Evaluation Kit (ATSAMD21-XPRO) which contains the ATSAMD21J18A Arm® Cortex®-M0+ MCU.

SAM D21 Xplained Pro Evaluation Kit

This application uses:

  • SERCOM5 in SPI Master mode
  • PB23 (SCK), PB22 (MOSI), PB16 (MISO), and PA13 (Software-driven SS line)
  • PB30 (User LED, LED0)

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

Visit the following page to see a detailed description of how the SERCOM SPI peripheral registers are configured for this application: 

Back to Top

Reference Materials

Exercise Files

​We recommend extracting the ZIP file to your C:\ folder. You should see the folder C:\MTT\32arm\samd21\code-examples-gcc\sercom\spi-master-demo containing the solution spi-master-demo.atsln.

Back to Top

Connection Diagram

The SAM D21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMD21J18A 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 D21 GPIOs. Atmel Studio 7 is used as a front-end for the EDBG.

The user LED and LED0, is driven by this application and is connected to port PB30, while the SPI pin functions SCK, MOSI, and MISO are connected to pins PB23, PB22, and PB16 respectively. SPI pin function SS is driven by application software (not the SERCOM SPI peripheral), using pin PA13.

SPI master demo connection diagram

Refer to the SAM D21 Xplained Pro User Guide for detailed circuit connections.

To see the operation of the SERCOM in SPI mode, an oscilloscope or a logic analyzer is needed. Expansion port EXT3 can be used with a logic analyzer to see the transfers. Make sure a 0 Ω resistor (or solder jumper) is added to the board so SS can be used as a trigger:

Serial Flash Connections

The following diagram identifies the key areas of the Xplained Pro board being used in this application:

  • A: EDGB (Program/Debug Connection)
  • B: SPI Flash Memory
  • C: Resistor location to connect SS to EXT3 (Default: not connected)
  • D: EXT3 pins used to monitor communication with a logic analyzer

Xplained pro key areas

​Refer to the "SAM D21 Xplained Pro User Guide" for a detailed description of the board and connections.

Back to Top

Procedure

Attach the SAM D21 Xplained PRO board to your computer using a USB A-to-MicroB cable.

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

SAM D21 Xplained pro enum success

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

Back to Top


Open the Solution

Select File > Open > Project/Solution…

Open the Project from the file drop down

Navigate to the Solution folder and select the spi-master-demo.atsln solution file:

Open SPI master solution detail

Back to Top


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:

Open the project properties

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

Select the EDBG target from the pulldown and select SWD for the interface

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

Save the tool settings

Back to Top


Rebuild/Program the Target

Finally, 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.

Click on Start with Debugging icon from the ribbon

If prompted, upgrade the EDBG firmware on the board:

Upgrade the EDBG firmware by clicking upgrade

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 D21 Xplained PRO board toggle at a 1 second rate.

Amber LED toggles at a rate of 1 second

Every time the LED toggles, a new SPI transaction is initiated with the onboard serial flash:

  1. Enable Write to the Serial Flash
  2. Write 0x00 to the Status Register Byte 1
  3. Read the Status Register Byte 1
  4. Read the Manufacturer ID from the device

Back to Top


Add the 0 Ω resistor (or solder-jumper) to the SAMD21 Xpained Pro board to the location "C" indicated in the diagram:

Add the resistor

Back to Top


Connect a logic analyzer to the EXT3 connections in location "D" indicated in the diagram above.

Connect a logic lead to SS (PA13)

  • This will be used to trigger the data acquisition on a low going pules.

Connect a logic lead to PB23 (SCK)

  • The clock signal will be present on this input, for input and output clocking.

Connect a logic lead to PB22 (MOSI)

  • The Master commands and transmissions will be monitored with this data line

Connect a logic lead to PB16 (MISO)

  • The data returning to the SPI interface from the onboard Serial Flash will be on this data line.

Don’t forget to connect the ground between the logic analyzer and the SAMD21XplainedPro board

Here is an example of the typical transactions seen with a logic analyzer:

Typical transactions seen with a logic analyzer

  1. The SS (SPI-ENABLE) line is pulled low
  2. The SCK (SPI-CLOCK) line oscillates at 50kHz
  3. The MOSI (SPI-MOSI) line sends data to the onboard Serial Flash
  4. THE MISO (SPI-MISO) line receives data from the onboard Serial Flash

SPI data transactions can also be verified via single-stepping through the example project in debug mode in Atmel Studio.

Back to Top


The data sent and received can be viewed from the logic analyzer and should look like the following screen capture:

SPI master trace capture

The Serial EEPROM datasheet allows us to understand the communication to the device. The following table is from the datasheet allows us to confirm the commands being sent to the device:

Command table

Back to Top


The following is a list of the first few commands sent to the serial Flash memory with their use in parenthesis:
  1. 0x06 (Write Enable)
  2. 0x01 (Write Status Register Byte)
  3. 0x00 (0x00 sent to Status Register Byte)
  4. 0x05 (Read Status Register)
  5. 0xFF (0x10 is sent from the Serial Flash vis the MISO line)
  6. 0x9F (Read Manufacturer and Device ID)
  7. 0xFF (0x1F sent back to Master as part of Manufacturer and Device ID)
  8. 0xFF (0x45 sent back to Master as part of Manufacturer and Device ID)
  9. 0xFF (0x01 sent back to Master as part of Manufacturer and Device ID)

From the above transaction we can see from items seven, eight and nine that the Manufacturer and Device ID match the expected values from the datasheet as shown:

Manufacturer and Device ID Information

Back to Top

Conclusions

This application example demonstrated a simple initialization for the SAM D21 SERCOM SPI peripheral in Master mode, as well as an example on how to collect and evaluate the data transactions.

Learn More

Back to Top