{"id":933,"date":"2016-06-02T10:32:29","date_gmt":"2016-06-02T18:32:29","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?page_id=933"},"modified":"2016-09-29T10:14:46","modified_gmt":"2016-09-29T18:14:46","slug":"tutorial22","status":"publish","type":"page","link":"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial22\/","title":{"rendered":"Tutorial 22: Embedded Linux\u2013 Hardware"},"content":{"rendered":"<p>In this tutorial, we&#8217;re going to take a diversion from the I2S audio and look at embedded Linux. Up until now, we&#8217;ve been using bare metal programming for the ARM. This is the easiest method of programming the ARM for simple tasks, but once we want to start doing things which are complicated, it&#8217;s good to get an operating system involved. In this tutorial, we will be using the Petalinux tools recommended by Xilinx.<\/p>\n<h1>Requirements<\/h1>\n<p>Before proceeding further you need to make sure that you have installed Xilinx Vivado 2015.4 and the Xilinx Petalinux 2015.4 tools. It is helpful if the Vivado and Petalinux versions match, and 2015.4 seems to be the latest stable release as I write this. As time moves on, you may want to try this with other newer releases. If so, you can expect some subtle differences between the tools you use and this tutorial.<\/p>\n<p>I install the Petalinux tools and Vivado tools into the same directory on my machine. This is not a requirement, but it keeps things organized. I also use the path <code>\/tools\/xilinx<\/code>, so my Vivado install is in <code>\/tools\/xilinx\/Vivado\/2015.4<\/code> and my petalinux install is in <code>\/tools\/xilinx\/petalinux-v2015.4-final<\/code>.<\/p>\n<p>Note that the Petalinux tools only run under Linux, so if you have been using Windows for Vivado, you need to switch to Linux, or you&#8217;ll need to adapt these instructions for building on two different machines.<\/p>\n<h1>Building the\u00a0FPGA Design<\/h1>\n<p>We first need to build an FPGA design to program into the ZYNQ part. Don&#8217;t worry if you have not followed along with the previous tutorials. In this tutorial, you will not need to write any Verilog code. Start Vivado\u2013 you may need to source the appropriate settings file to set the environment for your shell. Once Vivado is started click <em>Create New Project.<\/em> This starts the Create New Project Wizard. Click <em>Next<\/em> and enter the project name <em>zedboard_spi<\/em> and then click <em>Next<\/em> again. Click <em>Next<\/em> again to create an RTL project, and then click <em>Next<\/em> again because we don&#8217;t need to add any sources. Click <em>Next<\/em>\u00a0once again, since we&#8217;re not adding existing IP, and click <em>Next<\/em> because we are adding no constraints. Finally on the <strong>Default Part<\/strong> page, click <strong>Boards<\/strong> and select the ZedBoard, then click <em>Next<\/em>.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-937 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard.png\" alt=\"NewProjectBoard\" width=\"783\" height=\"570\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard.png 783w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard-300x218.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard-768x559.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/NewProjectBoard-412x300.png 412w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/a><\/p>\n<p>Finally, click <em>Finish<\/em> to create the project.<\/p>\n<p>In the <em>Flow Navigator<\/em> panel, click <em>Create Block Design<\/em> to start a new block design. Name the design <em>system<\/em> and click <em>OK<\/em>. The design will be empty, so click the add IP button to start adding IP. Note that you can mouse over the buttons to see what they are named.\u00a0Select\u00a0the\u00a0ZYNQ7 Processing System and press Enter. This will add the processing system to the FPGA. Click the add IP button again, and add an AXI GPIO block. Add three more AXI GPIO blocks for four total. Click the Regenerate Layout button to organize the diagram.<\/p>\n<p>Now, click the <em>Run Block Automation<\/em> link at the top of the window. This will configure the ARM processors for use on the ZedBoard FPGA board. Click OK. Notice that connections for the DDR memory and FIXED_IO have been added.<\/p>\n<p>Now click the <em>Run Connection Automation<\/em> link. Select the S_AXI connections for each of the four GPIO blocks, then click OK.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-938 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation.png\" alt=\"GPIO_S_AXI_ConnectionAutomation\" width=\"849\" height=\"392\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation.png 849w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation-300x139.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation-768x355.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/GPIO_S_AXI_ConnectionAutomation-500x231.png 500w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/><\/a><\/p>\n<p>Now\u00a0click the <em>Regenerate Layout<\/em> button to clean up the diagram. Notice that a Processor Reset System unit and an AXI Interconnect unit were automatically added and connected. This logic allows the ARM processors to talk to the FPGA GPIO units. We&#8217;re still going to need to connect the GPIO units to some external pins, however.<\/p>\n<p>We will connect one GPIO unit to the eight LED outputs, one to the eight switch inputs, one to the five pushbutton inputs, and finally, one to the JA1 Pmod connector, which we will use as a SPI interface.<\/p>\n<p>Click the Run Connection Automation link again. Notice how the AXI options have gone away but we can still select the GPIO connections. Select the axi_gpio_0 unit to connect to btns_5bits, the axi_gpio_1 to leds_8bits, the axi_gpio_2 to sws_8bits, and the axi_gpio_3 to connect to Custom.<\/p>\n<p>You should have a diagram that looks something like this:<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-940 size-large\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1-1024x555.png\" alt=\"T22-Diagram1\" width=\"584\" height=\"317\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1-1024x555.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1-300x163.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1-768x416.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1-500x271.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-Diagram1.png 1174w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>Double-click on the axi_gpio_0 block. Then select the IP Configuration tab. Notice that the width has been set to 5 bits and the All Inputs box has been checked. The connection automation knew that the buttons were five bits and that they could only be inputs. The leds GPIO has been set to eight outputs, and the switches have been set to eight inputs. Go ahead and click <em>Cancel<\/em>. Double-click on the axi_gpio_3, which is connected to gpio_rtl, and you can see that it is configured for 32 bits and bidirectional operation. Go ahead and set the width to eight, then click OK.<\/p>\n<p>Change the name from gpio_rtl to pmod_a, and change the names of the axi_gpio_0 block to btns_gpio. Likewise, change the other names as well. Your final diagram should look like this:<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-942 size-large\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram-1024x559.png\" alt=\"T22-FinalDiagram\" width=\"584\" height=\"319\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram-1024x559.png 1024w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram-300x164.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram-768x419.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram-500x273.png 500w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FinalDiagram.png 1132w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>Click on the Address Editor tab and drill down to the address mapping for the FPGA. It should look like this:<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-943 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap.png\" alt=\"T22-AddressMap\" width=\"854\" height=\"151\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap.png 854w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap-300x53.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap-768x136.png 768w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-AddressMap-500x88.png 500w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/a><\/p>\n<p>Go back to the Diagram tab and click the Validate Design button. The design should validate successfully.<\/p>\n<p>Now go ahead and click on the Generate Block Design task in the IP Integrator section of the Flow Navigator (way to the left). The Generate Output Products dialog will pop up. Click the <em>Generate<\/em> button. It may take a few seconds for this dialog to pop up, so be patient. This process builds the RTL code for the block design that you have just entered.<\/p>\n<p>Now, we&#8217;ll leave the <strong>IP Integrator<\/strong> and select the <strong>Project Manager<\/strong>. In the Design Sources section you can see the block diagram that we created. Right clock <em>system<\/em> and select Create HDL Wrapper&#8230; This will build the top level of the FPGA design for us. Make sure to let Vivado manage the update.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-CreateHDLWrapper.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-944 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-CreateHDLWrapper.png\" alt=\"T22-CreateHDLWrapper\" width=\"715\" height=\"572\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-CreateHDLWrapper.png 715w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-CreateHDLWrapper-300x240.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-CreateHDLWrapper-375x300.png 375w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/a><\/p>\n<h1>Creating Constraints<\/h1>\n<p>The FPGA design itself is almost done. We now need to tell Vivado about the Pmod pins. It already knows about the buttons and LEDs from a board description, but we need to tell Vivado which FPGA pins connect to the Pmod port. From the\u00a0Project Manager section of the Flow Manager, click Add Sources. Select the radio button for <strong>Add or create constraints\u00a0<\/strong>and click <em>Next<\/em>. Then click <em>Create File<\/em> and enter the file name <strong>system<\/strong>. The file type should be XDC and the location should be local to project. Click <em>OK<\/em>. Then click <em>Finish<\/em> to dismiss the <em>Add Sources<\/em> dialog. If you drill down to the Constraints section of the sources panel, you will see a new constraint file called system.xdc. Double click that file to edit it.<\/p>\n<p>The constraint file will let us map a pin on the FPGA to a port in the design. This will let us connect that port in the design to a pin on the Pmod connector. We first need the <a href=\"http:\/\/zedboard.org\/sites\/default\/files\/documentations\/ZedBoard_RevD.2_Schematic_130516.pdf\" target=\"_blank\">ZedBoard schematic<\/a>. You can see the Pmod connectors on page 3 and the signals are named JA1, JA2, JA3, JA4, JA7, JA8, JA9, JA10 for Pmod connector JA1.<\/p>\n<p><a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/JA1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-949 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/JA1.png\" alt=\"JA1\" width=\"608\" height=\"174\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/JA1.png 608w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/JA1-300x86.png 300w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/JA1-500x143.png 500w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-950 size-full\" src=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FPGA-JA1.png\" alt=\"T22-FPGA-JA1\" width=\"267\" height=\"722\" srcset=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FPGA-JA1.png 267w, https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/2016\/06\/T22-FPGA-JA1-111x300.png 111w\" sizes=\"auto, (max-width: 267px) 100vw, 267px\" \/>We now need to find where those signals names are connected to the FPGA. The figure has JA2 highlighted, and you can see that it connected to the FPGA pad AA11.<\/p>\n<p>All in all we have the following mapping:<\/p>\n<div class=\"table-responsive\"><table  style=\"width:100%; \"  class=\"easy-table easy-table-default \" border=\"0\">\n<thead>\r\n<tr><th >Signal<\/th>\n<th >Pad<\/th>\n<\/tr>\n<\/thead>\n<tbody>\r\n<tr><td >JA1<\/td>\n<td >Y11<\/td>\n<\/tr>\n\r\n<tr><td >JA2<\/td>\n<td >AA11<\/td>\n<\/tr>\n\r\n<tr><td >JA3<\/td>\n<td >Y10<\/td>\n<\/tr>\n\r\n<tr><td >JA4<\/td>\n<td >AA9<\/td>\n<\/tr>\n\r\n<tr><td >JA7<\/td>\n<td >AB11<\/td>\n<\/tr>\n\r\n<tr><td >JA8<\/td>\n<td >AB10<\/td>\n<\/tr>\n\r\n<tr><td >JA9<\/td>\n<td >AB9<\/td>\n<\/tr>\n\r\n<tr><td >JA10<\/td>\n<td >AA8<\/td>\n<\/tr>\n<\/tbody><\/table><\/div>\n<pre>set_property PACKAGE_PIN Y11 [get_ports {pmod_a_tri_io[0]}]\r\nset_property PACKAGE_PIN AA11 [get_ports {pmod_a_tri_io[1]}]\r\nset_property PACKAGE_PIN Y10 [get_ports {pmod_a_tri_io[2]}]\r\nset_property PACKAGE_PIN AA9 [get_ports {pmod_a_tri_io[3]}]\r\nset_property PACKAGE_PIN AB11 [get_ports {pmod_a_tri_io[4]}]\r\nset_property PACKAGE_PIN AB10 [get_ports {pmod_a_tri_io[5]}]\r\nset_property PACKAGE_PIN AB9 [get_ports {pmod_a_tri_io[6]}]\r\nset_property PACKAGE_PIN AA8 [get_ports {pmod_a_tri_io[7]}]\r\nset_property IOSTANDARD LVCMOS33 [get_ports pmod_a_tri_io]\r\n<\/pre>\n<p>The first eight constraints set the package pin for each bit of the pmod_a port. The final constraint sets the IO type on the port to 3.3V CMOS. Note that this can apply to the entire 8 bits of the port. Also note that, for reasons which likely aren&#8217;t very good, Vivado has added a suffix of _tri_io to the port name. It doesn&#8217;t do this for inputs or outputs, but it feels compelled to do this for bidirectional pins.<\/p>\n<h1>Building the FPGA<\/h1>\n<p>Save your constraints file and click Generate Bitstream, located in the Program and Debug section of the Flow Navigator. After a few minutes, you should receive a dialog indicating that the build has completed. Elect to open the implemented design.<\/p>\n<h1>Exporting the Design<\/h1>\n<p>At this point you can go to the File menu and select Export-&gt;Export Hardware. In the dialog, make sure Include bitstream is checked, and that you are exporting local to project. This export saves the design and related files in a location where the Software Design Kit (SDK) can find them.<\/p>\n<p>In the <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial23\/\">next tutorial<\/a> we build a custom Linux distribution using the FPGA we built in this tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;re going to take a diversion from the I2S audio and look at embedded Linux. Up until now, we&#8217;ve been using bare metal programming for the ARM. This is the easiest method of programming the ARM for &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial22\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":226,"menu_order":22,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-933","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/933","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=933"}],"version-history":[{"count":21,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/933\/revisions"}],"predecessor-version":[{"id":1105,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/933\/revisions\/1105"}],"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=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}