Search by Tags

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

 

Article updated at 17 Feb 2021
Compare with Revision




Select the version of your OS from the tabs below. If you don't know the version you are using, run the command cat /etc/os-release or cat /etc/issue on the board.



Remember that you can always refer to the Torizon Documentation, there you can find a lot of relevant articles that might help you in the application development.

Torizon 5.0.0

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

    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

    Solution Properties

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


  • Torizon Application 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

    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

    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>;

Torizon 4.0.0

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

    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

    Solution Properties

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


  • Torizon Application 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

    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

    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>;