Search by Tags

.NET Core Development and Debugging on Torizon Using Visual Studio Code

 

Article updated at 21 Jul 2020
Compare with Revision


Subscribe for this article updates

Introduction

This article explains how to run and debug .NET Applications on TorizonCore using the Visual Studio Code extension provided by Toradex.

The goal is to show a simple LED blink using .NET Core Console template and being able to debug the code with breakpoints using the Torizon Visual Studio Code Extension.

This article complies to the Typographic Conventions for Torizon Documentation.

Prerequisites

Create a New Project

Press F1 on Visual Studio Code and then type Torizon/.NET: Create .NET Core Application to create an application:


  • Creating a .NET Core Application

    Creating a .NET Core Application

After creating it write the application name on the same command bar:


  • Naming your .NET Core Project

    Naming your .NET Core Project

Select a folder on system to store the project files:


  • Selecting project folder

    Selecting project folder

After the folder selection two options will appear to select the right target architecture of your board:


  • Selecting target architecture

    Selecting target architecture

Type torizon on the username label:


  • Set Username

    Set Username

A .NET Core project with the following files will be created:


  • .NET Core project

    .NET Core project

Configure Project

Understand how to add Nuget packages, library dependencies and more in the next subsections.

For this article we will set up a project to make a simple blink LED as Hello World example.

Add Nuget Packages

For add .NET Core dependencies packages on .NET Core Torizon project there are no differences between a standard .NET Core project. Open a new terminal using the Visual Studio Code and enter the command:

dotnet add package System.Device.Gpio

  • Add Nuget Package to Project

    Add Nuget Package to Project

This will add the dotnet/iot library to our project, which will have the APIs for access GPIO through .NET Core using C#.

Add Package Dependencies

Some Nuget packages may depend on other packages and shared libraries. Open the Torizon configuration and edit the extrapackages configuration, just need to click on the pen icon and add libgpiod2 libgpiod-dev:


  • Edit extrapackages configutation

    Edit extrapackages configutation

This is done to add support to LibGpiodDriver from System.Device.GPIO.

Add Build Configuration Commands

In some cases it will be necessary to run some commands to add settings to the image generated by the extension. For these cases there is the buildcommands configuration. Open the Torizon configuration and edit the buildcommands value with:

RUN usermod -a -G gpio torizon

This will add our user torizon to the gpio group to have access to GPIO management.

Source Code

The example code used in this article comes from the sample https://github.com/toradex/torizon-samples/tree/master/gpio/dotnet/console/

Hardware Setup

For the purposes of example in this article, the Apalis iMX6Q and Ixora carrier board will be used.

For the LED connection, the SODIMM_5 of the Apalis family will be used, which in the case corresponds to the Ixora Extension Header x27 pin 15:


  • LED Connection

    LED Connection

Which for Linux corresponds to the GPIO bank /dev/gpiochip1 line 6. See our article How to Use GPIO on TorizonCore to know more.

Add Devices

For projects that need to access hardware devices, listed in TorizonCore /dev/, it is necessary to add the device path in the settings for the application to have access to it. Open Torizon configuration and add a /dev/gpiochip1 device on devices list:


  • Add device entry

    Add device entry

Update GPIO Bank & Line

Edit https://github.com/toradex/torizon-samples/tree/master/gpio/dotnet/console/Program.cs lines 19, 20 with the content below:

sharp
            int gpioBank = 1;
            int gpioLine = 6;

And remove lines 12, 13, 14, 15, 16 and 17.

Warning: In this article we are using /dev/gpiochip1 (GPIO bank 1) and line 6 for example purposes. Remember to enter the /dev/gpiochip and line number device for the pin that will be used in your project

Deploy and Debug

After the setup of hardware and replacing the GPIO bank and line on source code, add a breakpoint on line 25 by clicking on the left side of the line, it will make a red dot appear.

Warning: You may have to enable setting breakpoints anywhere in a file via File → Preferences → Settings → `Debug: Allow Breakpoints Everywhere.

Let the debug process begin. Press F5 and the Torizon VS Code Extension will build and deploy the debug container image to the Toradex computer on module. This process will take some time for the first time or whenever you edit the project settings.

The application runtime will stop on line 25:


  • Debug LED blink step by step

    Debug LED blink step by step

So we can use the VS Code debug tools to follow the code step by step, and check the LED turning on and off at each step.

Deploy and Release

After going on the process of deploying and debugging the application, you can now deploy and release it .

Build Release Image

Press F1 in VS Code to open the command bar and write the command Torizon/.NET: Build release container for Python application.


  • Build Release image command

    Build Release image command

Warning: Just note that the building process can take some time.

Your terminal output should be like the following:

Building release container (this may take some time)...
Publishing application...
Building container image...
Release image has been built successfully.

Deploy Release Image

Now that your container was successfully built, you can press F1 again and enter the Torizon: Deploy release container, which deploys your container.


  • Deploy Image Torizon Command

    Deploy Image Torizon Command

Deploying release container...
Device 05204061 selected.
Deploying image to target (this may take a few minutes).
Container has been deployed.

You can confirm if the image was actually deployed to your Torizon device by running the following command on the module:

# docker images

To get more information about Docker Containers and their commands, please refer to Docker Oficial Documentation.