{"id":1057,"date":"2016-08-22T10:56:31","date_gmt":"2016-08-22T18:56:31","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?p=1057"},"modified":"2016-10-31T13:21:06","modified_gmt":"2016-10-31T21:21:06","slug":"spi-with-petalinux-on-zynq","status":"publish","type":"post","link":"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/08\/spi-with-petalinux-on-zynq\/","title":{"rendered":"SPI with PetaLinux on ZYNQ"},"content":{"rendered":"<p>Recently, I spent a lot of time trying to get SPI working on a PicoZed ZYNQ board under Linux. It was absolutely\u00a0shocking how complicated this ended up being. One issue, I think, is that the device tree options differ depending on which version of PetaLinux you&#8217;re using. In this post, I&#8217;m going to document here how to do it with PetaLinux 2016.2.<\/p>\n<h1>Modify the device tree<\/h1>\n<p>First, you need to modify the <code>system-top.dts<\/code> file located in your PetaLinux project&#8217;s <code>subsystems\/linux\/configs\/device-tree<\/code> directory. You need to add an entry that\u00a0extends the existing entry for the SPI device. In the example, I am using spi0 on the processor subsystem. You can see the base definition for the SPI interface in the <code>zynq-7000.dtsi<\/code> include file in the same directory.<\/p>\n<p>It&#8217;s important\u00a0to note that PetaLinux will create an entry for the SPI device when you configure Linux\u2013 however, you won&#8217;t get a device file unless you add the entry for your particular SPI device. The trick is to add the SPI\u00a0device information to the file system-top.dts. The device tree specification syntax allows you to make changes to the automatic entry for the SPI device by labeling a a node, then overlaying additional information onto the labeled node in other parts of the device tree specification.<\/p>\n<p>In our case, the processor built-in SPI devices are labeled <em>spi0<\/em> and <em>spi1<\/em>. I wanted to use <em>spi0<\/em>, so I added an entry in the system-top.dts file to add to the <em>spi0<\/em> definition. In the example below, I&#8217;ve added three devices.<\/p>\n<pre>&amp;spi0 {\r\n  is-decoded-cs = &lt;0&gt;;\r\n  num-cs = &lt;3&gt;;\r\n  status = \"okay\";\r\n  spidev@0x00 {\r\n    compatible = \"spidev\";\r\n    spi-max-frequency = &lt;1000000&gt;;\r\n    reg = &lt;0&gt;;\r\n  };\r\n  spidev@0x01 {\r\n    compatible = \"spidev\";\r\n    spi-max-frequency = &lt;1000000&gt;;\r\n    reg = &lt;1&gt;;\r\n  };\r\n  spidev@0x02 {\r\n    compatible = \"spidev\";\r\n    spi-max-frequency = &lt;1000000&gt;;\r\n    reg = &lt;2&gt;;\r\n  };\r\n};\r\n<\/pre>\n<p>Rebuild linux and reboot your PicoZed board and you can now see the device files.<\/p>\n<pre>root@pz-7015-2016-2:~# ls -l \/dev\/spi*\r\ncrw-rw----    1 root     root      153,   0 Jan  1 00:00 \/dev\/spidev32766.0\r\ncrw-rw----    1 root     root      153,   1 Jan  1 00:00 \/dev\/spidev32766.1\r\ncrw-rw----    1 root     root      153,   2 Jan  1 00:00 \/dev\/spidev32766.2\r\n<\/pre>\n<h1>Testing the SPI interface<\/h1>\n<p>In order to test the SPI interface, I built an FPGA with the SPI ports marked for debug. This allows me to use the embedded logic analyzer to view the pin activity from Vivado. PetaLinux ships with a program to test the SPI interface called spidev_test. I compiled it with 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\r\n<\/pre>\n<p>Then, I copied it to my board using ssh, configured the logic analyzer to capture SPI activity, and ran the following command:<\/p>\n<pre>root@pz-7015-2016-2:~# .\/spidev_test -D \/dev\/spidev32766.0 --speed 10000000\r\nspi mode: 0x0\r\nbits per word: 8\r\nmax speed: 10000000 Hz (10000 KHz)\r\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  | ................................\r\n<\/pre>\n<p>I could see the SPI pins wiggle in the logic analyzer view.<\/p>\n<h1>Conclusion<\/h1>\n<p>Anyway, I hope that this\u00a0you save some time getting SPI to work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I spent a lot of time trying to get SPI working on a PicoZed ZYNQ board under Linux. It was absolutely\u00a0shocking how complicated this ended up being. One issue, I think, is that the device tree options differ depending &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/08\/spi-with-petalinux-on-zynq\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,15,5,14],"tags":[],"class_list":["post-1057","post","type-post","status-publish","format-standard","hentry","category-fpga","category-petalinux","category-xilinx","category-zynq"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/1057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"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=1057"}],"version-history":[{"count":7,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/1057\/revisions"}],"predecessor-version":[{"id":1159,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/1057\/revisions\/1159"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}