{"id":25445,"date":"2019-04-24T13:10:36","date_gmt":"2019-04-24T20:10:36","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=25445"},"modified":"2024-12-28T11:34:55","modified_gmt":"2024-12-28T19:34:55","slug":"what-is-spi","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/what-is-spi\/","title":{"rendered":"What Is Serial Peripheral Interface (SPI)?"},"content":{"rendered":"<p>What is SPI? That is a question I asked myself a couple of months ago that I am glad to answer now! After an introduction to SPI protocol, I will show an example of SPI protocol signals using the <a href=\"https:\/\/digilent.com\/shop\/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply\/\">Analog Discovery 2<\/a>, <a href=\"https:\/\/digilent.com\/shop\/waveforms-previously-waveforms-2015\/\">WaveForms<\/a>, and a <a href=\"https:\/\/digilent.com\/shop\/pmod-als-ambient-light-sensor\/\">PmodALS<\/a>.<\/p>\n<p>SPI is an acronym for <strong>Serial Peripheral Interface<\/strong>. Okay, so what is that? It is one form of electronics communication protocol: how something like a microcontroller (or some \u201cmaster\u201d device) can send\/receive data from external electronics such as sensors, shift registers, and SD cards (called \u201cslave\u201d devices) over a short distance.<\/p>\n<p>A serial communication sends one bit of data at a time over one wire, whereas a parallel communication sends all bits of data at once on individual wires. SPI also sends data in a continuous stream, which means that the transfer itself does not interrupt the acquisition. This is awesome because the data will be of higher quality than if sampling had to be interrupted for acquisition.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25630\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/240072417_e23874194e_b.jpg\" alt=\"\" width=\"1024\" height=\"681\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/240072417_e23874194e_b.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/240072417_e23874194e_b-600x399.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/240072417_e23874194e_b-768x511.jpg 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/240072417_e23874194e_b-800x532.jpg 800w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Your data: the continuous stream. Photo from <a href=\"https:\/\/www.flickr.com\/photos\/makelessnoise\/240072417\">here<\/a>.<\/p>\n<p>SPI is a 4-wire Bus system:<strong> Master-In-Slave-Out<\/strong> (MISO, SDO on some slave devices), <strong>Master-Out-Slave-In<\/strong> (MOSI), <strong>Slave-Select\/Chip Select<\/strong> (SS, SSN, or CS), and <strong>Serial-Clock<\/strong> (SCLK or CLK). However, power and ground wires are usually present as well.<\/p>\n<p>The MOSI line transmits data bits from the microcontroller to the peripheral device, the MISO transmits data bits from the peripheral device to the microcontroller, the SS\/CS is controlled by the master device and is used to select the peripheral device for data transfer (\u201cstart\/stop\u201d if connected to only one peripheral or to select between multiple peripherals). Multiple slave devices can be connected, but each slave device will require its own SS\/CS wire to the master device. For example, here is a system with one master and four slave devices.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25637\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI-diagram.png\" alt=\"\" width=\"600\" height=\"600\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI-diagram.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI-diagram-150x150.png 150w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI-diagram-300x300.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>A SPI connection diagram with multiple slave devices. Photo from <a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:SPI_Stern.svg\">here<\/a>.<\/p>\n<p>Be aware though that SPI has not been standardized and that depending on the system architecture, implementation of the data transmission may vary. There are four general modes of SPI communication that the master device must configure. This is to compliment the bus design of the slave device and is based on the <strong>Serial Clock Polarity<\/strong> (CPOL) and <strong>Phase<\/strong> (CPHA). Both parameters can be expressed as low and high or 0 and 1. CPOL defines whether the serial clock idles (when data transmission is not occurring) at a low or high level logic (0 or 1, respectively) and CPHA defines whether data is sampled on the leading or trailing edge (0 or 1, respectively) of the CPOL.<\/p>\n<p>The diagram below shows what each signal would look like given the variations to each. Image from <a href=\"https:\/\/de.wikipedia.org\/wiki\/Datei:SPI_timing_diagram.svg\">here<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25548\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_.png\" alt=\"\" width=\"2000\" height=\"1163\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_.png 2000w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_-600x349.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_-768x447.png 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_-1024x595.png 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_-800x465.png 800w\" sizes=\"auto, (max-width: 2000px) 100vw, 2000px\" \/><\/p>\n<p>Furthermore, transmission can begin with either the <strong>Least-Significant-Bit<\/strong> (LSB) or the <strong>Most-Significant-Bit<\/strong> (MSB). There is also a choice of sampling frequency, which is equally important and is a choice dependent on the limitations of the devices you are using and the frequency or resolution of the data you are trying to sample. Always check the datasheets for the system you are working with to verify defaults, modes of operation, and limitations.<\/p>\n<p>Now, let&#8217;s take a look at an example of SPI protocol using the <a href=\"https:\/\/digilent.com\/shop\/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply\/\">Analog Discovery 2<\/a>\u00a0and <a href=\"https:\/\/digilent.com\/shop\/waveforms-previously-waveforms-2015\/\">WaveForms<\/a>. I used the <a href=\"https:\/\/digilent.com\/shop\/pmod-als-ambient-light-sensor\/\">Pmod ALS<\/a> (Ambient Light Sensor) as my slave device, an <a href=\"https:\/\/store-usa.arduino.cc\/products\/arduino-uno-rev3?selectedStore=us\">Arduino Uno<\/a> as the master device, and the code from the hackster project <a href=\"https:\/\/www.hackster.io\/58085\/using-the-pmod-als-with-arduino-uno-e86dca\">here<\/a> to display the SPI protocol.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25675\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window.png\" alt=\"\" width=\"1600\" height=\"608\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window.png 1600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window-600x228.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window-768x292.png 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window-1024x389.png 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-24-12_58_44-Window-800x304.png 800w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/p>\n<p>Right-click the picture and open in a new window\/tab to see a larger version of this image.\u00a0The <strong>Logic Analyzer<\/strong> in <a href=\"https:\/\/digilent.com\/shop\/waveforms-previously-waveforms-2015\/\">WaveForms<\/a> is an especially handy tool when debugging SPI and other protocol. For example, when setting up this demo, I accidentally used a damaged <a href=\"https:\/\/digilent.com\/shop\/pmod-tph-6-pin-test-point-header\/\">PmodTPH<\/a>. Through the use of the Logic Analyzer, I was able to determine that the TPH was damaged and not the Pmod ALS. Once I removed that Pmod and adjusted the trigger settings, I saw the signals operating correctly!<\/p>\n<p>The <a href=\"https:\/\/digilent.com\/shop\/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply\/\">Analog Discovery 2<\/a>&#8216;s <strong>Protocol Analyzer<\/strong> is another tool that can help determine if your slave device is sending data appropriately. Here is a screen shot of the Protocol Analyzer showing a dynamic output (in hexadecimal) as I change the amount of light that the <a href=\"https:\/\/digilent.com\/shop\/pmod-als-ambient-light-sensor\/\">PmodALS<\/a> is experiencing (disregard the 7F value, that was from me touching and shorting the leads).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25684\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/2018-09-10-10_46_25-WaveForms-SPI_Protocol_LogicAn.png\" alt=\"\" width=\"159\" height=\"363\" \/><\/p>\n<p>Now you know enough about SPI, and some valuable debugging tools, to be able to start experimenting and getting your hands into some SPI protocol projects. Happy trails!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-25445 jlk' data-task='like' data-post_id='25445' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-25445 lc'>+3<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-25445 jlk' data-task='unlike' data-post_id='25445' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-25445 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-25445 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Ian delves into SPI and other valuable debugging tools!<\/p>\n","protected":false},"author":49,"featured_media":25548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4325,1563],"tags":[],"ppma_author":[4459],"class_list":["post-25445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debug-validation-test","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2018\/09\/SPI_timing_diagram.svg_.png","authors":[{"term_id":4459,"user_id":49,"is_guest":0,"slug":"ian","display_name":"Ian Etheridge","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/8a19055c49586a33bb17b9a5823ac103?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Etheridge","last_name_2":"","first_name":"Ian","first_name_2":"","job_title":"","description":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/25445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/49"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=25445"}],"version-history":[{"count":3,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/25445\/revisions"}],"predecessor-version":[{"id":31140,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/25445\/revisions\/31140"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/25548"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=25445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=25445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=25445"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=25445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}