Skip to main content
Version: 6

How to Use GPIO with Torizon Visual Studio Extension (C++)

Introduction​

In this article, we will show how you can manipulate GPIO using Torizon C/C++ Application development Visual Studio Plugin.

Prerequisites​

How to Debug C/C++ Applications on Visual Studio with Torizon Extension​

To begin, see here a short video showing how easy is to debug C/C++ applications on Torizon devices using Torizon Extension with Visual Studio:

Then, let's go with our step-by-step guide with our sample application.

Instructions​

Create a new project​

Create a new Torizon Application

On Visual Studio 2019, create a new Torizon C/C++ Application. See the Visual Studio Extension For Torizon article for detailed instructions.

Add libgpiod library and the GPIO device to the application​

To add the libgpiod library to your project, right-click in your project on the Solution Explorer view and click on Properties.

Solution Properties

In Configuration Properties, click on Torizon C/C++ Application, right-click on the Configuration item and then <Properties...>.

Torizon Application Properties

Add the RUN usermod -a -G gpio torizon command to "buildcommands" property. This provides the container user the necessary privileges to access gpio devices.

Add the libgpiod-dev:arm64 libgpiod2:arm64 the "devpackages" property. If you plan to build a 32-bit application/container replace :arm64 with :armhf as suffix for the packages.

Add libgpiod2 gpiod to the "extrapackages" list. Please notice that in this case, you don’t need any suffix.

Also, add the corresponding gpiochip to the application. In this example, we will use /dev/gpiochip0. Set this value to the "device" property. Also include other gpio devices you may want to access.

Access the Torizon Configuration

Add the libgpiod library to the Linker command​

To use libgpiod in your C/C++ application, it is necessary to add the library to the Linker.

In Configuration Properties, click on "Linker" -> "Input" and add gpiod to the "Library Dependencies" field.

Add libgpiod to the Linker

Using the libgpiod API​

After setting the project properties, we are ready to start working with code. The instructions below are a subset of the comprehensive article How to Use GPIO on TorizonCore:

To make an easier integration to the IDE: in the examples above, instead of passing the GPIO bank and line as arguments, we suggest that you declare them directly in the source-code:

main.c
int bank = <bank-number>;
int line = <line-number>;


Send Feedback!