SAM D21 SERCOM SPI Master Example Project

 Objective

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

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.

samd21-xplained-pro.png


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

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

 
SERCOM SPI Master Configuration
Learn more >

 Materials

Hardware Tools

Tool About Purchase
ATSAMD21-XPRO-50px.png
SAM D21 Xplained Pro
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel Studio
Integrated Development Environment

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\samd21\code-examples-gcc\sercom\spi-master-demo containing the solution spi-master-demo.atsln.

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

Refer to the SAM D21 Xplained Pro Schematic 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:

table4-8.png

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 (AT25DF081A)
  • 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.png

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

 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:

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

1

Open the Solution

Select File > Open > Project/Solution…

as7-open-systick-solution.png


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

as7-open-spi-master-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-master-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-master-example-2.png

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

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

3

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.

as7-start-without-debugging-spi-master-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 D21 Xplained PRO board toggle at a 1 second rate.

systick-initialization-example-results.png

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

4

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

xplained-pro-key-areas.png

5

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:

spi-master-trace-example.png
  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.

6

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

spi-master-trace-capture.png

The AT25DF081A data sheet allows us to understand the communication to the device. The following table is from the data sheet allows us to confirm the commands being sent to the device:

at25df081a-command-table.png

7

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:

at25df081a-devid-table.png

 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.

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