How to Analyze SPI

Last updated last month

How to Analyze SPI

Serial Peripheral Interface (SPI) is a communication protocol often used between processors and devices that require a relatively fast interface, such as shift registers, flash memory, and some sensors. It is a synchronous protocol, which means it requires a separate clock line so that the transmitter can tell the receiver when to sample the data line.

Interestingly, SPI connections often rely on two data lines: MISO and MOSI. Because of this, data can be sent between the master and device at the same time (full duplex).

Example code is provided below for Arduino, mbed, or AC6 System Workbench for STM32 (SW4STM32).

Required Materials

Connect Hardware

Even though the Nucleo-F446RE has an onboard analog-to-digital converter (ADC), the MCP3002 is an easy-to-use and inexpensive ADC that communicates over SPI, which makes it perfect for demonstrating the protocol.

Connect the Nucleo to the MCP3002 and the MCP3002 to a 10k potentiometer as shown. Note that the SPI pins are also broken out to the male pins just to the right of the female Arduino headers on the Nucleo. This allows us to attach the Saleae Logic Analyzer wires.

Run Demo Application

Download the example code for your IDE:

Open the demo in your chosen IDE. Compile the program, and upload it to the Nucleo-F446RE development board.

Open the serial terminal program that you downloaded from the UART example.

Connect to the Nucleo board over the assigned serial port with a baud rate of 115200, 8 data bits, no parity bit, and 1 stop bit (8-N-1). Open the connection, and you should see the voltage from the MCP3002 being reported to you in regular intervals. Try turning the knob on the potentiometer to see the voltage change.

Measure the Signal

Open the Logic software with the Logic Analyzer plugged in. Click on the Device Settings Button.

In the device settings window, set the speed to at least 50 MS/s and the duration to 1 second. Click both Clear buttons to disable all channels, leaving only the digital Channel 0 enabled. Click digital Channels 1-3 to enable them.

We can set up the analyzer before we capture so that we can choose the appropriate signal as the trigger. Click on the plus button (+) next to Analyzers on the right side. Select SPI to bring up the settings window. Leave everything as default and click Save.

The Enable line (also known as CS or SS) is often the first signal to change prior to data being transmitted on a SPI bus. As a result, we’ll set our trigger on that line. Click on the Trigger Button next to Channel 3 (SPI - ENABLE), and select the Trigger on Falling Edge option.

Click on the Trigger Button again to close the pop-up. Click Start to begin collecting data. Because our code is set to send SPI data every 0.2 seconds, data collection should begin almost immediately.

Zoom in around the 0 s : 0 ms : 0 μs mark, and you should see the waveforms that traveled across the SPI bus at that moment. Click on the gear icon next to SPIunder Analyzers. Click on Bin under Display Radix to show the interpreted data as binary.

Above Channel 0 (MOSI), you should see the data that we are transmitting out from the Nucleo board: 0b11000000 for the first byte, and all 0s for the second byte. At the same time, data comes back in from the MCP3002 on Channel 1 (MISO). In this instance, only bits 1-10 carry information about the measured voltage.

Adjust the potentiometer and see if you get different data on the MISO line when you sample again.