C# .NET Core IoT Error for ARM embedded board : Unhandled exception. System.IO.IOException: Device or resource busy

Viewed 648

I want to use .NET core IoT library in order to run C# code for my SAMA5D27 SOM1 EK1 ARM embedded board.

.NET core IoT github

I have build this .NET core project composed from project.cs source file :

using System;
using System.Device.Gpio;
using System.Threading;

namespace led_blink
{
    class Program
    {
        static void Main(string[] args)
        {
            var pin = 81;
            var lightTimeInMilliseconds = 1000;
            var dimTimeInMilliseconds = 200;
            
            Console.WriteLine($"Let's blink an LED!");
            using (GpioController controller = new GpioController())
            {
                controller.OpenPin(pin, PinMode.Output);
                Console.WriteLine($"GPIO pin enabled for use: {pin}");

                Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
                {
                    controller.Dispose();
                };

                while (true)
                {
                    Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.High);
                    Thread.Sleep(lightTimeInMilliseconds);
                    Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.Low);
                    Thread.Sleep(dimTimeInMilliseconds);
                }
            }
        }
    }
}

And this is .csproj file :

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Iot.Device.Bindings" Version="1.0.0" />
    <PackageReference Include="System.Device.Gpio" Version="1.0.0" />
  </ItemGroup>

</Project>

As you can see, the code is used for blinking Led which is situated on PIN 81 which corresponds to PortC pin 17 on my board. I build the project in order to use on arm-linux board.

First, to check if the pin is working well, I used libgpiod library and I turned on the led of pin81 using gpioset gpiochip0 81=1 and it is working well.

Furthermore, I have checked my GPIOs using gpioinfo command and this is the result of the desired pin :

 line  81:       "PC17"       unused   input  active-high

But when I try to run the C# code, it fails with this output message :

Let's blink an LED!
Unhandled exception. System.IO.IOException: Device or resource busy
   at System.IO.FileStream.WriteNative(ReadOnlySpan`1 source)
   at System.IO.FileStream.FlushWriteBuffer()
   at System.IO.FileStream.FlushInternalBuffer()
   at System.IO.FileStream.Flush(Boolean flushToDisk)
   at System.IO.FileStream.Flush()
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Dispose(Boolean disposing)
   at System.IO.TextWriter.Dispose()
   at System.IO.File.WriteAllText(String path, String contents)
   at System.Device.Gpio.Drivers.SysFsDriver.OpenPin(Int32 pinNumber)
   at System.Device.Gpio.GpioController.OpenPin(Int32 pinNumber)
   at System.Device.Gpio.GpioController.OpenPin(Int32 pinNumber, PinMode mode)
   at led_blink.Program.Main(String[] args) in /home/ubuntu/netcore/Program.cs:line 23
Aborted

This is my board device tree :

device_tree

PS : I have removed ISC node which is using PC17 GPIO from device tree in order to free the pin

ISC_DeviceTree_node

Why my code can't run ? any help please !

2 Answers

OK. Been working with embedded systems for 45 years. Initially I thought the error was coming from a PC connected to the board. I realize now that the device is console serial port.

The device is booting off eprom and first configures console so for debugging. Next the device is trying to mount the operating system from disk. You device has a smart card that is on a USB connection. I was reading your source code and thought the USB was the RS-232 but from device literature the USB is used for the smart card. The error looks like it may be from the startup code for the smart card. So either the card is not inserted, not formatted, or no operating system on card.

You may not need to smart card depending on your requirements. I'm using at work a Xilinx Embedded card and not using smart card so we have to boot option turned off to work with smart card. We also had to program the Xilinx before using. I assume Atmel has similar options.

Finaly i found the solution. You must use LibGpioD driver instead of SysFs interface in your Linux platform and your C# code.

Related