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β
- Windows 10 Pro / Enterprise / Education
- Visual Studio 2019 and Torizon Visual Studio Plugin installed. For instructions ,see the Visual Studio Extension For Torizon article
- Toradex Hardware with TorizonCore installed.
- Reading of the How to Use GPIO on TorizonCore article is highly recommended for more information and examples of libgpiod library.
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β
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.
In Configuration Properties, click on Torizon C/C++ Application
, right-click on the Configuration
item and then <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.
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.
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:
int bank = <bank-number>;
int line = <line-number>;