I was introduced to PlatformIO by my friend Steven, as we were planning to do an ESP8266 project at a hackathon. Despite being a regular hackaday reader and someone who thinks he’s got his finger on the pulse of the embedded maker community, I’d never heard of it before. It’s a cross-platform environment that supports development across a wide range of microcontrollers and platforms, like ESP, Arduino, and others. It’s got a sweet Atom-based IDE, which is what the focus of this article will be.
I’m interested in it primarily for AVR and Tiva (TI’s ARM chips) parts, as I’m moving away from using Arduino in my projects to improve my firmware skills. Of course, these parts can be programmed using the manufacturer’s solutions, like Code Composer for TI and AVR Studio. Being a mac user however, embedded development has been a hassle, as I’d normally have to virtualize Windows to run Code Composer or AVR Studio. To run under native OSX, I’ve set up the AVR toolchain under eclipse, which was not a pleasant experience. PlatformIO appears to streamline the toolchain setup process by integrating a bunch of open source packages and simplifying project configuration. In short, it appears that they’ve fixed the nightmare that is an embedded toolchain, and they’ve even integrated a serial monitor in the IDE! Let’s go through the installation process and get to programming some AVRs and Tivas.
The first thing you’ll need to do is install the PlatformIO package using pip, Python’s package installer.
sudo easy_install pip
sudo pip install platformio
Assuming everything went well, you should have PlatformIO now, and you can proceed to installing the IDE. Nowhere in my quick glance of the IDE’s documentation did it mention that you need to install PlatformIO and the IDE separately. Maybe to some people it’s obvious, but I think it’s a pretty big documentation oversight that’ll catch a lot of beginners. I caught on quickly, as the IDE installer threw me an error that caused me to remember installing PlatformIO previously via the command line on my laptop. Since the IDE installed smoothly on my laptop, I figured lack of PlatformIO was the issue, and I tracked down the documentation I’d used previously, which is here.
Now you can download the IDE here.
I had to install the latest version of clang, which does code completion.
“sudo clang” did the trick, after accepting some license agreements. This is OS dependent, so look at the link provided in the error message.
Now it looks like we’re good to go. Head over to the PlatformIO menu and select “Initialize new PlatformIO project or update existing.” Specify a directory and select the correct board (I picked my launchpad).
Finding the correct documentation and sample code was a little confusing the first time around. This seems to be a consistent theme throughout PlatformIO, I think their documentation and example code needs to be a little more cohesive. You end up clicking links and poking around GitHub pages, and the readme files aren’t very well fleshed out.
Eventually I ran across the PlatformIO GitHub page, and there is example code in the “examples/titiva” directory. Within, there are three projects; energia, native, and opencm3. I went for the native example. Turns out, it uses the same TivaWare functions that we’re used to seeing in my other tutorials.
To get this going, I grabbed the code out of the main.c file right off GitHub, and pasted it into a main.c file I already created in my project in the PlatformIO IDE. That goes in the /src folder, naturally. You will also want to ensure that your platformio.ini file matches that of the example, or at least has the section covering your specific microcontroller. Under the PlatformIO menu, click build, cross your fingers, and it should all build successfully. If not, I’ve found the error messages to be quite specific and detailed. Nice! Plug in your LaunchPad and click “upload” under the same menu. Hopefully, after a couple of seconds you’ll have a blinking LED!
Well, I had a blinking LED and I didn’t need to touch flash compiler installation, dependencies, build variables, or anything else that needs to be set up when doing the same process under Code Composer (CC). Given that CC comes from TI and PlatformIO comes from the open source community, I’d call that a huge success. Hands down, it was the fastest I’ve ever gotten an LED blinking, with the exception of Arduino. While looking for example code and documentation will lead you along a twisting path of GitHub and PlatformIO pages, the reference material I’ve found worked the first time. That almost never happens, so that’s pretty good!
Is this Energia?
Energia is TI’s environment for the LaunchPad boards that lets them use standard Arduino syntax and libraries. Two things led me to wonder if there is some element of Energia hanging around in what is supposedly a native development platform.
First, there is a setup() and loop() function in the main.c demo code. This is classic arduino stuff. When I tried pasting in some code I’d written under CC that only had a main() function with a while loop in it, I got an error upon building. The error is below, and says that main() is already defined in energia.a.
This seems weird, since according to the example, we’re developing with the native framework. Interestingly, the TivaWare functions are working correctly. So is this Energia or not?
The answer to this can be found in the platformio.ini file. Here, we see “framework = energia.” Code completion tells us that “native” is definitely not an option here, but “libopencm3” certainly is.
So we can conclude that we’re developing with the vestiges of Energia, but TivaWare functions work as expected. I think it’s pretty strange, and I’m definitely going to figure out how to get a native framework working without Energia, so that code can work with the standard main() and not the arduino-style setup() and loop(), however convenient they may be. The issue here is keeping compatibility with standard embedded practices so that code is portable. If PlatformIO is really as “professional” as they claim, I’m of the opinion that standard professional practices should be upheld, and hobby style code should be wiped out.
In the meantime, before a true native framework is figured out, I will use the energia framework as if it were native. That is, nothing will go in setup(), and loop() will be structured with inits at the top, followed by a while(1), as would normally appear in main(). This way, code that works under this framework will adhere to standard embedded practice by simply renaming loop() to main().
Man, the IDE is slick! I’ve barely scratched the surface of what it can do, but already, the console window, code highlighting and file navigation are impressive. Tabs work with the keyboard shortcuts you’d expect from a web browser (I think there’s some Chrome behind this, it seems very browser-y). I also love the dark theme, and the collapsible panels have obvious buttons and collapse/expand properly. Very nice UI, it absolutely destroys the awkward Eclipse-based stuff out there.
As I mentioned, this has been the smoothest toolchain installation I’ve ever done. That’s one of the main features of PlatformIO, and it delivers. The documentation is a little convoluted and needs to be a bit more cohesive, but that should improve as the community develops and more people start adopting the platform. I plan to offer a series of videos about it. At the time of this writing, there is only one video, and it doesn’t involve the IDE.
I can’t stress enough how nice it is to have the libraries and toolchain taken care of automatically. It feels more robust than CC, for example, where it feels like the entire toolchain could implode on itself with the wrong click of some box buried deep in a preference pane. However, it is nice to know where things like the TivaWare library are actually located, since you put them there. This is a sense you don’t get with PlatformIO abstracting everything away from you. I certainly plan to dig around in the environment config files to get an understanding of where it is pulling libraries from. That sense of knowing where my files are is important to me, and it is a little weird developing and including things without that. Do I have this library? I don’t know, because I didn’t put it there.
Overall though, I look forward to giving PlatformIO a further test run. I think my nervousness about where it keeps the framework files will be alleviated after digging around, and the issue of energia vs. native development is something I’ll be able to fix and ideally bring back to the community. I’m excited to be using an open source tool chain, and this really feels like we’re on the cusp of something that’s going to be the new standard in the maker community. Hopefully it’ll bleed over into startups and into the professional space. With that kind of adoption, I can see a lot less wrangling with toolchain issues, and a lot of happy firmware devs!