{"id":4079,"date":"2020-06-13T15:20:08","date_gmt":"2020-06-13T20:20:08","guid":{"rendered":"https:\/\/dev.iachieved.it\/iachievedit\/?p=4079"},"modified":"2020-06-13T15:20:08","modified_gmt":"2020-06-13T20:20:08","slug":"exploring-hifive1-rev-b-gpios-with-platformio","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/exploring-hifive1-rev-b-gpios-with-platformio\/","title":{"rendered":"Exploring HiFive1 Rev B GPIOs with PlatformIO"},"content":{"rendered":"<p>In our <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/hifive1-rev-b-gpio-pins\/\">last post<\/a> we looked at the GPIO pins of the <a href=\"https:\/\/www.sifive.com\/boards\/hifive1-rev-b\">SiFive HiFive1 Rev B board<\/a>, and in this one we will continue doing so, but let&#8217;s take a look at <a href=\"https:\/\/platformio.org\">PlatformIO<\/a> on that journey.  PlatformIO bills itself as &#8220;A new generation ecosystem for embedded development&#8221; and aims to really simplify pulling together all of the tools needed for embedded development.  In our <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/an-introduction-to-the-hifive-rev-b-and-risc-v\/\">introduction to the HiFive1 Rev B<\/a> we outlined all of the packages you needed to install and configure to get started with the board.  Cross-compilers, JTAG daemons, remote debug tools, SDKs, and so on.  What if we could skip all that, not just for one microcontroller platform, but for hundreds of them?  Enter PlatformIO.<\/p>\n<h2>Installing PlatformIO<\/h2>\n<p>PlatformIO, while compatible with a number of popular editors (Sublime Text, Atom, etc.), really shines with <a href=\"https:\/\/code.visualstudio.com\">Visual Studio Code<\/a>.  While VSCode has features I don&#8217;t find appealing (horizontal bars on my code), it is very configurable and I can turn them off and enable Emacs keybindings.  After some time with VSCode and PlatformIO, I really began enjoying how straightforward it was to work with the HiFive1 Rev B.<\/p>\n<p>Installing PlatformIO is simple with Visual Studio Code.  Open the <em>Extensions<\/em> panel and search:<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/platformio_install.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/platformio_install.png\" alt=\"\" width=\"604\" height=\"574\" class=\"aligncenter size-full wp-image-4085\" \/><\/a><\/p>\n<p>Click on <em>Install<\/em>.  You&#8217;ll see a &#8220;landing page&#8221; of sorts for PlatformIO and a window open up on the bottom indicating it is being installed and configured.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/please_dont_close.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/please_dont_close.png\" alt=\"\" width=\"1504\" height=\"1014\" class=\"aligncenter size-full wp-image-4087\" \/><\/a><\/p>\n<p>Once PlatformIO is installed we&#8217;ll create a project for use with our HiFive1 Rev B board.  Click on the PlatformIO <em>Projects<\/em> button and then <em>Create New Project<\/em>.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/hifive1_gpio.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/hifive1_gpio.png\" alt=\"\" width=\"1184\" height=\"936\" class=\"aligncenter size-full wp-image-4096\" \/><\/a><\/p>\n<p>This is where PlatformIO really shines.  There are over 800 boards to choose from, and depending on the board, multiple frameworks to work with.  For example, with the HiFive1 Rev B you can use either the <a href=\"https:\/\/sifive.github.io\/freedom-e-sdk-docs\/index.html\">Freedom E SDK<\/a> (which we use), or the <a href=\"https:\/\/www.zephyrproject.org\">Zephyr RTOS<\/a>.<\/p>\n<h2>Creating a New File<\/h2>\n<p>Obviously we&#8217;ll need to write some code, so in your project right-click on <em>src<\/em> and select <em>New File<\/em>.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/new_file_2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/new_file_2.png\" alt=\"\" width=\"776\" height=\"880\" class=\"aligncenter size-full wp-image-4092\" \/><\/a><\/p>\n<p>Visual Studio Code will prompt you to name the file in the project explorer; ours is named simply <code>main.c<\/code>.  We&#8217;ll do the GPIO equivalent of a Hello World by blinking the onboard green LED:<\/p>\n<pre class=\"theme:sublime-text toolbar-overlay:false tab-size:2 lang:c decode:true \" >#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n#include &lt;metal\/gpio.h&gt;\n\nvoid delayForSeconds(unsigned seconds) {\n    time_t timeout;\n    timeout = time(NULL) + seconds;\n  while (timeout &gt; time(NULL));\n}\n\nint main(void) {\n  struct metal_gpio* gpio_device = metal_gpio_get_device(0);\n  if (!gpio_device) {\n    printf(\"Unable to obtain GPIO device\\n\");\n    return -1;\n  }\n\n  while (1) {\n    metal_gpio_enable_output(gpio_device, 19);  \/\/ Turn on onboard green LED\n    delayForSeconds(1);\n    metal_gpio_disable_output(gpio_device, 19); \/\/ Turn off\n    delayForSeconds(1);\n  }\n\n  return 0;\n}<\/pre>\n<h2>Building and Uploading<\/h2>\n<p>PlatformIO automates and streamlines installation of the correct toolchain, debugger, SDK and so on.  To build your project there is a checkmark on the bottom of the editor window; if you hover over it you&#8217;ll see the label <em>PlatformIO: Build<\/em>.  Click on it and scroll through the output to see what PlatformIO is doing; I&#8217;ve found it very helpful to read everything that is going on.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/build.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/build.png\" alt=\"\" width=\"706\" height=\"138\" class=\"aligncenter size-full wp-image-4091\" \/><\/a><\/p>\n<p>Uploading your project to the HiFive1 Rev B is as simple as clicking the right arrow that is labeled <em>PlatformIO: Upload<\/em>.  Again, if this is your first time using PlatformIO on a given environment you can see it&#8217;s installing what&#8217;s necessary to upload to your board.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/upload.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/upload.png\" alt=\"\" width=\"540\" height=\"40\" class=\"aligncenter size-full wp-image-4102\" \/><\/a><\/p>\n<p>Here is a great example of what PlatformIO is doing in the background:<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/download_everything.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/download_everything.png\" alt=\"\" width=\"1346\" height=\"416\" class=\"aligncenter size-full wp-image-4093\" \/><\/a><\/p>\n<p>While it&#8217;s possible to do this by hand (and we have!), how nice it is to have it done automatically for you.<\/p>\n<h3>Serial Output<\/h3>\n<p>PlatformIO does have the ability to open a serial monitor to your HiFive1 Rev B board, though you may have to do a bit of configuration here.  Open the project&#8217;s <code>platformio.ini<\/code> and ensure that your environment configuration has <code>monitor_speed=115200<\/code> set.  This will tell PlatformIO to use 115200 baud to communicate on the serial port.  Then open the serial monitor with the &#8220;electric plug&#8221; icon and choose the first <code>usbmodem<\/code> port you see.<\/p>\n<p>I typically prefer to use <code>screen<\/code> on macOS for this &#8211; the console output in Visual Studio Code is nice, but I&#8217;m used to <code>screen<\/code> and like having it in a separate terminal window altogether.  For example, typing <code>screen \/dev\/cu.usbmodemIDENTIFIER 115200<\/code> (where <code>IDENTIFIER<\/code> will be specific to your machine) will bring up the serial console to the board.<\/p>\n<h2>Testing the GPIO Pins<\/h2>\n<p>Okay, PlatformIO is ready to go and we&#8217;re going to take a look at the GPIO pins on the HiFive1 Rev B again.  I&#8217;ve learned a bit since writing about them in <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/hifive1-rev-b-gpio-pins\/\">this post<\/a>.  For starters, digital pin 14 on the header is <b>not connected<\/b> and there is no mapping of a pin from the GPIO device.<\/p>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/pins_lit.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/pins_lit.jpg\" alt=\"\" width=\"240\" height=\"320\" class=\"aligncenter size-full wp-image-4099\" \/><\/a><\/p>\n<p>In addition, pins 11, 12, and 13 on the header correspond to the MOSI, MISO, and SCK signals for the SPI device; since they are tied to SPI writing a digital one to them has no effect.  On the other hand, you can write a 1 to pin 10 (SPI SS) and get an output.<\/p>\n<p>That said, you <em>can<\/em> use the I2C pins (digital pins 18 and 19) and write a digital one or zero to them as long as we&#8217;re willing to give them up as I2C pins.<\/p>\n<p>Pins 15 and 16 are interesting.  If you look at the HiFive1 Rev B schematic for J4 (the Arduino 6 pin block on the bottom left of the ESP32) you see GPIO 9 and GPIO10 map to DIG15 and DIG16.  But then going over here we see that:<\/p>\n<ul>\n<li>GPIO9 is tied to a line labeled SPI_CS2 which comes from the ESP-SOLO-1<\/li>\n<li>GPIO10 is tied to a line labeled WF_INT which comes from the ESP-SOLO-1<\/li>\n<\/ul>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/gpio9_gpio10.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/gpio9_gpio10.png\" alt=\"\" width=\"988\" height=\"344\" class=\"aligncenter size-full wp-image-4100\" \/><\/a><\/p>\n<p>The schematic notes &#8220;Solder across SJ1 to connect GPIO_9 to SPI_CA2&#8221; (I think that is a typo).  On the physical board I cannot find SJ1 or SJ2, so I&#8217;m assuming these connections are present.<\/p>\n<p>So, what does that gives us in the end?<\/p>\n<ul>\n<li>DIG2 &#8211; easy to use to drive a digital output<\/li>\n<li>DIG3 &#8211; easy to use to drive a digital output, but doing so interferes with the onboard LED<\/li>\n<li>DIG4 &#8211; easy to use to drive a digital output<\/li>\n<li>DIG5 &#8211; easy to use to drive a digital output, but doing so interferes with the onboard LED<\/li>\n<li>DIG6 &#8211; easy to use to drive a digital output, but doing so interferes with the onboard LED<\/li>\n<li>DIG7 &#8211; easy to use to drive a digital output<\/li>\n<li>DIG9 &#8211; easy to use to drive a digital output<\/li>\n<li>DIG10 &#8211; easy to use to drive a digital output, but doing so interferes with using SPI<\/li>\n<li>DIG17 &#8211; easy to use to drive a digital output<\/li>\n<li>DIG18 &#8211; easy to use to drive a digital output, but interferes with I2C<\/li>\n<li>DIG19 &#8211; easy to use to drive a digital output, but interferes with I2C<\/li>\n<\/ul>\n<p>In the next post I&#8217;m going to be building an I2C circuit that contains 4 EEPROMs, each of which will have the same address.  To do so I want to use two digital output pins to specify which I2C chip to work with, so to make life easy I want to choose pins that won&#8217;t interfere with anything else.  DIG2, DIG4, DIG7, DIG9, and DIG17 are my best bests, and since DIG2 and DIG4 are on the same side of the board I&#8217;ll start there.<\/p>\n<h2>Conclusion<\/h2>\n<p><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/platformio_logo.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2020\/06\/platformio_logo.png\" alt=\"\" width=\"220\" height=\"220\" class=\"aligncenter size-full wp-image-4104\" \/><\/a><\/p>\n<p><a href=\"https:\/\/platformio.org\">PlatformIO<\/a> is pretty damn slick if you ask me and worth checking out if you are just starting with the HiFive1 Rev B.  While I still recommend that you understand cross-compilers and the underlying tools that go into supporting such a platform, it really does take the hassle out of downloading and configuring everything!<\/p>\n<p>If you have a HiFive1 Rev B board, try checking out our <a href=\"https:\/\/github.com\/iachievedit\/hifive1_gpio\">GPIO project<\/a>.  I have no doubt that you&#8217;ll be able to build and upload it with minimal fuss using PlatformIO.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last post we looked at the GPIO pins of the SiFive HiFive1 Rev B board, and in this one we will continue doing so, but let&#8217;s take a look at PlatformIO on that journey. PlatformIO bills itself as &#8220;A new generation ecosystem for embedded development&#8221; and aims to really simplify pulling together all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,102,101],"tags":[],"class_list":["post-4079","post","type-post","status-publish","format-standard","hentry","category-hacking","category-hifive","category-risc-v"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4079"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=4079"}],"version-history":[{"count":19,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4079\/revisions"}],"predecessor-version":[{"id":4113,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4079\/revisions\/4113"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=4079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=4079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=4079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}