Fast GPIO with C++

After fiddling a few weeks with the Onion Omega2+ I wanted to see how I can use the GPIO pins of the Omega2. So for this weeks challenge I will try to pulse a LED with the help of C++ code.

If you don’t know how to compile C++ code for the Omega2 please read this post.

The bug

My first attempt failed because there is a bug in the Omega2 firmware. The bug still exist in the latest build (as of writing b156). The bug is fixed in the latest version 0.1.10.

The current version has the bug that it does not expose the /dev/mem file which we need to write directly to the registers of the Omega2.

So before we can start to code we need to fix our environment by downloading b137. Use the following command to downgrade if needed. Be sure to check if the latest version doesn’t already fix it, if the latest versions does fix it follow the instructions here to upgrade. After using the command wait for the Omega2 to stop blinking.

Using Fast-GPIO

The guys at Onion have done a great job with providing us a C++ library on GitHub here, this is actually the command line tool for controlling the pins. You can use the following command to set pin 1 to high.

We are going to use this code so we don’t have to figure out ourselves where the registers live in memory.

We can use the following command to pull the package to our PC, just remember where you put it we need it later on.

Setting up the IDE

For the IDE I use visual studio code, which is a free IDE from Microsoft, it is available here. To make it easy to work with c++ we are going to install the C/C++ extension for the IDE. Press the shortcut CTRL + P and enter following command to install the extension.

When your finished restart the IDE.

The code

You can find all the source code on the github repository here, but you can also type along.

First up we need a directory to work in, so setup a directory and create a file called main.cpp and put the following code in it. This will simply pull the pin high and low 15 times.

You’ll see a green squiggle line on the #include line, this happens because vscode doesn’t know where to find the header file. If you set the cursor on the line a lightbulb will appear, click on the lightbulb and choose Add include path to settings, it will then add the file c_cpp_properties.json to the .vscode folder. Point in the file to the include directory of  the fast gpio library downloaded earlier. Mine looks like this.

But you must replace the path to with the path where you placed it.


We programmers love automation because it enables us to do less 😀 luckily vscode allows us to do just that. Vscode has a feature that allows us to define tasks, we will define two tasks. One task for compiling our code and other for copying our executable to the Omega2.

Task 1

To create the first task we can use the shortcut CNTR + SHIFT + P and type task, select the entry Configure Task Runner. This creates a new file in the folder .vscode named tasks.json. Be sure to replace the paths with ones pointing to the location of the fast-gpio library on your PC.

By setting the property isBuildCommand to true it enables us to trigger this task with the shortcut CNTR + B.

Tip: When building the project with CNTR + B does not work after setting up the task, be sure you have configured the environment as shown in earlier in this post.

Task 2

The next task is enabling us to copy the executable to the Omega2. You can add the following task to the tasks array and of course replace the IP/username/password with your own.

To let this work we need to install sshpass in our ubuntu environment, we need this program so it can enter our password when asked. Use the following command to install it in our environment.

You can trigger the task by using the shortcut CNTR + P and type task copy. If everything is working, you can now use ssh to connect to the Omega2 and execute the application to let our LED flash.

One thought on “Fast GPIO with C++”

  1. Thank you dude, your posts are written in easy to understand instructions. Trying to find what you’re looking for in the forum, is like trying to traverse a minefield with a stick. Almost all of the posts refer to the Omega, as opposed to the Omega 2. As a total noob to embedded systems development, this is quite a daunting task.

Leave a Reply