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.
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:
Materials
Hardware Tools
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
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.
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:
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
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:
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.
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:
Next, under the project's 'Tool' settings, select your EDBG target from the pull-down. Select 'SWD' as the interface:
Save the tool setting by clicking on the Save All button:
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.
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 D21 Xplained PRO board toggle at a 1 second rate.
Every time the LED toggles, a new SPI transaction is initiated with the onboard serial flash:
- Enable Write to the Serial Flash
- Write 0x00 to the Status Register Byte 1
- Read the Status Register Byte 1
- Read the Manufacturer ID from the device
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:
- The SS (SPI-ENABLE) line is pulled low
- The SCK (SPI-CLOCK) line oscillates at 50kHz
- The MOSI (SPI-MOSI) line sends data to the onboard Serial Flash
- 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.:
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:
7
The following is a list of the first few commands sent to the serial flash memory with their use in parenthesis:
- 0x06 (Write Enable)
- 0x01 (Write Status Register Byte)
- 0x00 (0x00 sent to Status Register Byte)
- 0x05 (Read Status Register)
- 0xFF (0x10 is sent from the Serial Flash vis the MISO line)
- 0x9F (Read Manufacturer and Device ID)
- 0xFF (0x1F sent back to Master as part of Manufacturer and Device ID)
- 0xFF (0x45 sent back to Master as part of Manufacturer and Device ID)
- 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:
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.
Table of Contents
|