{"id":1066,"date":"2016-08-22T17:23:44","date_gmt":"2016-08-23T01:23:44","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?page_id=1066"},"modified":"2020-07-11T16:28:46","modified_gmt":"2020-07-12T00:28:46","slug":"tutorial26","status":"publish","type":"page","link":"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial26\/","title":{"rendered":"Tutorial 26: Controlling a SPI device using the ZYNQ SPI controller"},"content":{"rendered":"<p>In T<a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial24\/\">utorial 24<\/a>, I covered controlling a SPI device by just taking control of the memory mapped GPIO and bit-banging the SPI without a driver. In this tutorial, we&#8217;ll do things the &#8220;official&#8221; way, and use the one of the hard IP&nbsp;SPI controllers present on the ZYNQ chip. For this tutorial I am using Vivado 2016.2 and PetaLinux 2016.2.<\/p>\n<h1>Enabling the SPI controller<\/h1>\n<p>First you need to enable the SPI controller on the ZYNQ subsystem. Double-click on the ZYNQ processing subsystem in your Block Design in the IP Integrator window. This will bring up the IP configuration window. Click on the <em>Peripheral I\/O Pins<\/em> section of the <em>Page Navigator<\/em> and check the box next to <em>SPI 0<\/em>.&nbsp;Note that Quad SPI or QSPI is unrelated to this discussion.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1072\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0-300x169.png\" alt=\"tutorial26_enable_spi0\" width=\"300\" height=\"169\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0-300x169.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0-768x432.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0-500x281.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_enable_spi0.png 883w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Next, go to the <em>MIO Configuration<\/em> section of the <em>Page Navigator<\/em>. Expand the <em>I\/O Peripherals<\/em> section and scroll down to SPI 0. In the IO column make sure that EMIO is selected. This programs the SPI control signals to use the Programmable Logic rather than pins dedicated to the Processor Subsystem.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1074\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio-300x167.png\" alt=\"tutorial26_spi0_emio\" width=\"300\" height=\"167\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio-300x167.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio-768x427.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio-500x278.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_spi0_emio.png 878w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h1>Making the connections<\/h1>\n<p>Next, you&#8217;re going to need to make ports on the FPGA which will connect to the SPI controller. For a SPI master, you&#8217;ll need at least one chip select output, one clock output, and probably a data output to write to the SPI device. If you wish to read from the SPI device, you&#8217;ll also need a data input. In my case I am connecting to a MAX 5216 DAC which does not have read capability so I only have three output pins which I call <em>MAX5216_SCLK<\/em>, <em>MAX5216_MOSI<\/em>, and <em>MAX5216_SSN<\/em>. Wire them up to the corresponding pin on the SPI controller. Note: there will be many unused pins, since the controller can be used as a master, slave, or both. Also, the <em>SPI0_SS_I<\/em> pin must be connected to a logic 1 value due to a bug in the toolchain. To do this, instantiate a constant IP module and set the value to 1. Wire that to the <em>SPI0_SS_I<\/em> pin on the ARM&nbsp;processor. On my FPGA, I also connect to a <em>DS3231<\/em> realtime clock over I2C and have some GPIOs connected as well. Don&#8217;t worry about not having those in your design. My block design is shown below:<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1077\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design-300x167.png\" alt=\"tutorial26_block_design\" width=\"300\" height=\"167\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design-300x167.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design-768x428.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design-1024x571.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design-500x279.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_block_design.png 1216w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Note that I have also marked the pins for debug. This can be really useful for debugging your software using the embedded logic analyzer feature of Vivado.<\/p>\n<p>Now synthesize the design and use the I\/O Planner to place the outputs where you want on your FPGA. Generate a bitstream and export your design to SDK. These steps are covered in other tutorials, so I won&#8217;t go into details here.<\/p>\n<h1>Build PetaLinux<\/h1>\n<p>You need to create a PetaLinux project and extract the hardware description from your project SDK directory as discussed in <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial23\/\">tutorial 23<\/a>. Before you build PetaLinux, though, you need to modify the device tree to create the appropriate device file for your SPI device. This step is critical: without it, Linux won&#8217;t create the SPI device file to control your device.<\/p>\n<p>The first step after creating your project is to use petalinux-config to read the&nbsp;hardware description that you exported from Vivado.<\/p>\n<pre>petalinux-config --get-hw-description &lt;path to project.sdk directory&gt;\n<\/pre>\n<p>Then you need to enable SPI support for your kernel. To configure the kernel run the following command.<\/p>\n<pre>petalinux-config -c kernel\n<\/pre>\n<p><em>Navigate to Device Drivers<\/em>-&gt;<em>SPI support<\/em> and make sure that <em>Cadence SPI controller<\/em>, <em>Xilinx SPI controller command module<\/em>, <em>Xilinx Zynq QSPI controller<\/em>, and <em>User mode SPI device driver support<\/em> are all enabled. If you fail to enable the User mode SPI support then the SPI device files will not be created.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_config_kernel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1120\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_config_kernel-300x225.png\" alt=\"tutorial26_config_kernel\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_config_kernel-300x225.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_config_kernel-400x300.png 400w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/08\/tutorial26_config_kernel.png 760w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Edit the file&nbsp;<code>subsystems\/linux\/configs\/device-tree\/system-top.dts<\/code> in your PetaLinux project directory, then add the following lines.<\/p>\n<pre>&amp;spi0 {\n  is-decoded-cs = &lt;0&gt;;\n  num-cs = &lt;1&gt;;\n  status = \"okay\";\n  spidev@0x00 {\n    compatible = \"spidev\";\n    spi-max-frequency = &lt;1000000&gt;;\n    reg = &lt;0&gt;;\n  };\n};\n<\/pre>\n<p>Now you can run <code>petalinux-build<\/code> and <code>petalinux-package<\/code> to build and package Linux.<\/p>\n<h1>Verifying the SPI interface<\/h1>\n<p>Once you boot your board, you should be able to see the SPI device file in the <code>\/dev<\/code> directory.<\/p>\n<pre>root@pz-7015-2016-2:~# ls -l \/dev\/spidev*\ncrw-rw----    1 root     root      153,   0 Jan  1 00:00 \/dev\/spidev32766.0\n<\/pre>\n<p>You can compile the <code>spidev_test program<\/code> to exercise the SPI interface and view the results on the logic analyzer. To compile the <code>spidev_test<\/code> program, use the following command:<\/p>\n<pre>arm-xilinx-linux-gnueabi-gcc -o spidev_test \/tools\/xilinx\/petalinux-v2016.2-final\/components\/linux-kernel\/xlnx-4.4\/Documentation\/spi\/spidev_test.c\n<\/pre>\n<p>Then copy the program to your board using ssh. Run the command, and if you use the embedded logic analyzer, you should see activity on the SPI interface.<\/p>\n<pre>root@pz-7015-2016-2:~# .\/spidev_test -D \/dev\/spidev32766.0 --speed 10000000\nspi mode: 0x0\nbits per word: 8\nmax speed: 10000000 Hz (10000 KHz)\nRX | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  | ................................\n<\/pre>\n<p>In a future tutorial, I will cover the software side of controlling a SPI device in Linux.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Tutorial 24, I covered controlling a SPI device by just taking control of the memory mapped GPIO and bit-banging the SPI without a driver. In this tutorial, we&#8217;ll do things the &#8220;official&#8221; way, and use the one of the &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial26\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":226,"menu_order":26,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1066","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/1066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":11,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/1066\/revisions"}],"predecessor-version":[{"id":1335,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/1066\/revisions\/1335"}],"up":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/226"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}