Search by Tags

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

 

Article updated at 17 Sep 2020
Compare with Revision



Subscribe for this article updates

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

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

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 = ; int line = ;

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

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 = ; int line = ;