{"id":1065,"date":"2016-08-22T15:08:51","date_gmt":"2016-08-22T23:08:51","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?p=1065"},"modified":"2016-10-31T13:15:25","modified_gmt":"2016-10-31T21:15:25","slug":"gpio-with-zynq-and-petalinux","status":"publish","type":"post","link":"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/08\/gpio-with-zynq-and-petalinux\/","title":{"rendered":"GPIO with ZYNQ and PetaLinux"},"content":{"rendered":"<p>Accessing GPIO controllers is pretty straightforward with PetaLinux, but there are a few tricks you need to know.<\/p>\n<h1>Locating the GPIO controller<\/h1>\n<p>In the example FPGA I am using, there are two GPIO controllers in the programmable logic. These are at address 0x4120_0000 and 0x4121_0000. If you look in the <code>pl.dtsi<\/code> file in your PetaLinux project, in the directory <code>subsystems\/linux\/configs\/device-tree<\/code>, you will see entries for the GPIO devices. There&#8217;s no need to modify the entire device tree.<\/p>\n<p>If you make a PetaLinux build and boot it, you can look in the <code>\/sys\/class\/gpio<\/code> directory.<\/p>\n<pre>root@pz-7015-2016-2:~# ls \/sys\/class\/gpio\/                       \r\nexport       gpiochip901  gpiochip902  gpiochip906  unexport\r\n<\/pre>\n<p>You can see that there is a <code>gpiochip<\/code> directory for each GPIO controller. The <code>gpiochip901<\/code> and <code>gpiochip902<\/code> directories correspond to the PL controllers that I added in my design. The <code>gpiochip906<\/code> directory is for the GPIO controller in the PS.<\/p>\n<p>How will\u00a0you know which is which, though? Each directory contains a label file which tells you the device tree label for the controller. You can go ahead and look at the contents:<\/p>\n<pre>root@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip901\/label \r\n\/amba_pl\/gpio@41210000\r\nroot@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip902\/label \r\n\/amba_pl\/gpio@41200000\r\nroot@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip906\/label \r\nzynq_gpio\r\n<\/pre>\n<p>Looking at it, you&#8217;ll\u00a0see that gpiochip901 corresponds to my controller at 0x4120_0000 and gpiochip902 corresponds to the controller at 0x4121_0000. Gpiochip906 is different, and corresponds to the built-in controller on the ZYNQ. Why those numbers? In my FPGA, the first GPIO controller controls only a single GPIO bit, while the second controls four bits. We can tell how many bits each controller controls by looking in the <code>ngpio<\/code> file for the controller.<\/p>\n<pre>root@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip901\/ngpio \r\n1\r\nroot@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip902\/ngpio\r\n4\r\nroot@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpiochip906\/ngpio\r\n118\r\n<\/pre>\n<p>It looks to me like the numbering starts at 901. Since that controller has only a single GPIO bit, the next controller is 902. That one has four bits, so the ZYNQ PS controller goes at 906, which has 118 bits.<\/p>\n<h1>Enabling the GPIO bits<\/h1>\n<p>To access a GPIO bit, you need to enable the correct GPIO pin. You do that by writing to the <code>export<\/code> file in the <code>\/sys\/class\/gpio<\/code> directory. Here is an example of enabling the LSB of my second controller:<\/p>\n<pre>root@pz-7015-2016-2:~# echo -n 902 &gt; \/sys\/class\/gpio\/export \r\n<\/pre>\n<p>Now if you look in the <code>\/sys\/class\/gpio<\/code> directory, you will see a new directory created which allows you to control the individual GPIO pin.<\/p>\n<pre>root@pz-7015-2016-2:~# ls \/sys\/class\/gpio\r\nexport       gpio902      gpiochip901  gpiochip902  gpiochip906  unexport\r\n<\/pre>\n<p>If you look in that directory you see a number of controls:<\/p>\n<pre>root@pz-7015-2016-2:~# ls \/sys\/class\/gpio\/gpio902\r\nactive_low  direction   power       subsystem   uevent      value\r\n<\/pre>\n<h1>Accessing the GPIO bits<\/h1>\n<p>You can determine the GPIO direction by looking at the <code>direction<\/code> file. Since my GPIO pin is an output, it gives the value <em>out<\/em>.<\/p>\n<pre>root@pz-7015-2016-2:~# cat \/sys\/class\/gpio\/gpio902\/direction\r\nout\r\n<\/pre>\n<p>You can change the value to a 1 by writing to the value file.<\/p>\n<pre>root@pz-7015-2016-2:~# echo 1 &gt; \/sys\/class\/gpio\/gpio902\/value           \r\n<\/pre>\n<h1>Conclusion<\/h1>\n<p>So there you have it. The &#8220;official&#8221; way to access GPIO on PetaLinux.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Accessing GPIO controllers is pretty straightforward with PetaLinux, but there are a few tricks you need to know. Locating the GPIO controller In the example FPGA I am using, there are two GPIO controllers in the programmable logic. These are &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/08\/gpio-with-zynq-and-petalinux\/\">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-1065","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\/1065","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=1065"}],"version-history":[{"count":5,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/1065\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}