{"id":1885,"date":"2022-05-14T16:54:56","date_gmt":"2022-05-14T16:54:56","guid":{"rendered":"http:\/\/retroramblings.net\/?p=1885"},"modified":"2025-09-30T10:26:00","modified_gmt":"2025-09-30T10:26:00","slug":"ports-pmods-and-pin-numbering","status":"publish","type":"post","link":"https:\/\/retroramblings.net\/?p=1885","title":{"rendered":"Ports, PMODs and Pin Numbering"},"content":{"rendered":"\n<p><strong>2022-05-14<\/strong><\/p>\n\n\n\n<p>Continuing my experiments with the IceSugar-Pro board, building cores using Yosys and friends, I now have EightThirtyTwoDemos running, using PMODs to provide VGA out, I2S audio out, PS\/2 keyboard and mouse, and SD card.  (The latter isn&#8217;t strictly necessary since the FPGA board already has an SD card slot &#8211; but it&#8217;s on the underside of the board and impossible to access while the IceSugar-Pro is inserted into the carrier board.)<\/p>\n\n\n\n<p>So how did I go about wiring up the PMODs?<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>There&#8217;s comprehensive pin documentation for the connectors on the carrier board at <a href=\"https:\/\/github.com\/wuxx\/icesugar-pro\/tree\/master\/schematic\">https:\/\/github.com\/wuxx\/icesugar-pro\/tree\/master\/schematic<\/a> &#8211; but I was very keen not to have to refer to that, and go through a tedious, error-prone pin mapping process every time I changed my mind about which connector should carry a particular peripheral.  At first glance we appear to have six identical connectors, labelled P1 through P6, so I wanted to be able to allocate peripherals by connector name, and not have to worry about the individual pins.<\/p>\n\n\n\n<p>There are a few complications with that plan:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>P1 doesn&#8217;t carry any useful signals, and if populated, prevents the USB-C cable from being connected!<\/li><li>P2, P3 and P6 each carry two sets of two-row PMOD-compatible pins, as well as four extra GPIOs and +5v supply in between.<\/li><li>P4 has two pins unconnected, but otherwise matches P2, P3 and P6.<\/li><li>P5 is fully connected, but the DAPLink on the carrier board interferes with four pins.  P4 and P5, therefore, each provide only one usable set of PMOD pins.<\/li><\/ul>\n\n\n\n<p>So we have eight usable two-row PMODs and some random extra GPIOs.<\/p>\n\n\n\n<p>Since we want to be able to reference the eight PMODs individually, what I&#8217;ve done is split each of the connectors into a &#8220;high&#8221; part, a &#8220;low&#8221; part and a &#8220;gpio&#8221; part for the extra signals in the middle, like so:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"416\" height=\"276\" src=\"http:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/P2.jpg\" alt=\"\" class=\"wp-image-1886\" srcset=\"https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/P2.jpg 416w, https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/P2-300x199.jpg 300w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/figure>\n\n\n\n<p>The (VHDL) toplevel for the board, declares the ports thusly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>P2_pmod_high : inout std_logic_vector(7 downto 0);<\/code><br><code>P2_gpio : inout std_logic_vector(3 downto 0);<\/code><br><code>P2_pmod_low : inout std_logic_vector(7 downto 0);<\/code><br><code>P3_pmod_high : inout std_logic_vector(7 downto 0);<\/code><br>...<\/pre>\n\n\n\n<p>I then assign a peripheral to the appropriate ports like so:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>alias i2s_pmod is P2_pmod_high;<\/code><\/pre>\n\n\n\n<p>It&#8217;s hard to imagine any way that could be easier!<\/p>\n\n\n\n<p>I created a package to define the PMOD pin numberings, like so:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>-- Pin numberings for PS\/2 PMOD<\/code><br><code>constant PMOD_PS2_KDAT : integer := 0;<\/code><br><code>constant PMOD_PS2_MDAT : integer := 1;<\/code><br><code>constant PMOD_PS2_KCLK : integer := 2;<\/code><br><code>constant PMOD_PS2_MCLK : integer := 3;<\/code><\/pre>\n\n\n\n<p>The PS\/2 PMOD is a special case, because it only has one row of pins, so only needs to use half the allocated PMOD.  Which half?<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>-- PS\/2 keyboard and mouse<\/code><br><code>constant ps2_pmod_offset : integer := 4;<\/code><br><code>alias ps2_pmod is P6_pmod_high;<\/code><\/pre>\n\n\n\n<p>By defining an offset, I can select between the top or bottom row of pins &#8211; an offset which is taken into account when instantiating the drivers.<\/p>\n\n\n\n<p>That leads me to the other complication with this scheme.  For maximum flexibility I wanted to declare all the PMOD IOs as &#8220;inout&#8221;, and let their subsequent usage determine the effective data direction.  This is where Yosys throws a bit of a spanner into the works, because it has only the most rudimentary support for tristate signals, and I&#8217;m told it&#8217;s best to instantiate pin drivers manually.  For ECP5 devices, the component we need for this is TRELLIS_IO.  I declare the component, complete with generic { DIR : string := &#8220;BIDIR&#8221; }.  The actual wiring to the nominated connector, bringing together the multiple threads of tri-state IO, the aliased connector name and the pin offset, looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ps2kd : component TRELLIS_IO port map ( B => ps2_pmod(PMOD_PS2_KDAT+ps2_pmod_offset), I => '0', T => ps2k_dat_out, O => ps2k_dat_in );<br>ps2kc : component TRELLIS_IO port map ( B => ps2_pmod(PMOD_PS2_KCLK+ps2_pmod_offset), I => '0', T => ps2k_clk_out, O => ps2k_clk_in );<br>ps2md : component TRELLIS_IO port map ( B => ps2_pmod(PMOD_PS2_MDAT+ps2_pmod_offset), I => '0', T => ps2m_dat_out, O => ps2m_dat_in );<br>ps2mc : component TRELLIS_IO port map ( B => ps2_pmod(PMOD_PS2_MCLK+ps2_pmod_offset), I => '0', T => ps2m_clk_out, O => ps2m_clk_in );<\/pre>\n\n\n\n<p>Notice that the PS\/2 out signals are connected to &#8216;T&#8217;, the tristate signal &#8211; and not to &#8216;I&#8217;, which is tied to &#8216;0&#8217;.  This gives us open-collector semantics, where the port is either high-Z or driven low.<\/p>\n\n\n\n<p>The complete port mapping for the carrier board looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"763\" src=\"http:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1-1024x763.jpg\" alt=\"\" class=\"wp-image-1888\" srcset=\"https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1-1024x763.jpg 1024w, https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1-300x224.jpg 300w, https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1-768x572.jpg 768w, https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1-402x300.jpg 402w, https:\/\/retroramblings.net\/wp-content\/uploads\/2022\/05\/PMODs-1.jpg 1257w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>2022-05-14 Continuing my experiments with the IceSugar-Pro board, building cores using Yosys and friends, I now have EightThirtyTwoDemos running, using PMODs to provide VGA out, I2S audio out, PS\/2 keyboard and mouse, and SD card. (The latter isn&#8217;t strictly necessary &hellip; <a href=\"https:\/\/retroramblings.net\/?p=1885\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9,8],"tags":[],"class_list":["post-1885","post","type-post","status-publish","format-standard","hentry","category-fpga","category-geekery","category-hardware"],"_links":{"self":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1885"}],"version-history":[{"count":3,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1885\/revisions"}],"predecessor-version":[{"id":1891,"href":"https:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/1885\/revisions\/1891"}],"wp:attachment":[{"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}