projects github about
.Net Core on Raspberry Pi 3

I thought i could get away from using a debian-based Docker image for .Net Core… *sigh…

For now an Alpine-based Docker image is available for .NET Core on amd64. But the runtime-deps can also be installed to an arm32v6 architecture which Alpine-Linux support so the possibility is almost there.

To use the latest build preview get it from here: .Net Core SDKfile_download

I’m using Ubuntu 17.10 on my RPi3 and as of typing this… there’s no v17.12 available Docker so i use one from the xenial.
1
2
3
wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/arm64/docker-ce_17.12.0~ce-0~ubuntu_arm64.deb

sudo dpkg -i docker-ce_17.12.0~ce-0~ubuntu_arm64.deb

Docker-ce deps:

1
sudo apt-get install libltdl7
Run below cmd if there’s still some missing dependencies
1
sudo apt-get install -f install

Then just run docker -v and add your user to docker group.

Get the Dockerfile where we will create our image with the latest .Net Core.

1
wget https://raw.githubusercontent.com/dotnet/dotnet-docker-nightly/master/2.1/runtime/stretch/arm32v7/Dockerfile

Then let’s build it.

1
docker build -t dotnet .

For a test app… let’s get one…

1
git clone https://github.com/dotnet/dotnet-docker-samples/

We’ll use linux-arm as the target runtime for arm32.

1
2
cd dotnetapp-selfcontained
dotnet publish -c release -r linux-arm -o selfcontained-linux-arm
dotnet run while your inside dotnetapp-selfcontained to see the App in action.
I use Windows to Build this test app then use winSCP to transfer the selfcontained-linux-arm folder on to the Raspberry Pi 3
1
docker run -it --rm -v /home/pi3:/home/share dotnet

And to execute the test app… run this command inside the docker environment.

1
dotnet /home/share/selfcontained-linux-arm/dotnetapp.dll



I wan't to try .Net Core on linux-arm64... but unfortunately, there's no available yet except Win-arm64 and linux-arm32. For this one we'll use a bcm2835 library but later a C# version of this library i had which is on-hold until today.

Prerequisites

bcm2835 libraryfile_download
Be sure gcc make are installed
Extract bcm2835-1.55.tar.gz anywhere you like, because where going to remove it once we have `libbcm2835.so`.
1
2
3
4
tar zxvf bcm2835-1.55.tar.gz
cd bcm2835-1.52/src
gcc -fPIC -c bcm2835.c
gcc -shared bcm2835.o -o libbcm2835.so
You can now remove the bcm2835-1.55 folder and be sure to put libbcm2835.so inside /lib/aarch-~ folder. We'll create a simple console app using these command dotnet new console -o testapp. Then modify Program.cs with these codes. What it does is we use libbcm2835.so C internal commands into our C# app using PInvoke. Take note of the PIN Numbering: bcm2835 library use physical numbering so RPI_GPIO_P1_07 is equal to GPIO4 of our RPi3.
// Program.cs


using System;
using System.Runtime.InteropServices;

namespace testapp
{
    class Program
    {
        private static uint HIGH = 0x1;
        private static uint LOW  = 0x0;
        private static byte RPI_GPIO_P1_07 = 4;
        private static byte BCM2835_GPIO_FSEL_OUTP = 0x01;

        [DllImport("libbcm2835.so")]
        public static extern int bcm2835_init();

        [DllImport("libbcm2835.so")]
        public static extern void bcm2835_close();

        [DllImport("libbcm2835.so")]
        public static extern void bcm2835_delay(uint millis);

        [DllImport("libbcm2835.so")]
        public static extern void bcm2835_gpio_fsel(byte pin, byte mode);

        [DllImport("libbcm2835.so")]
        public static extern void bcm2835_gpio_write(byte pin, uint on);

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            if (bcm2835_init() == 1)
            {
                
                    bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_OUTP);
                    
                    // Blink

                    while(true)
                    {
                        // Turn it on

                        bcm2835_gpio_write(RPI_GPIO_P1_07, HIGH);
                        // wait

                        bcm2835_delay(500);
                        // turn it off

                        bcm2835_gpio_write(RPI_GPIO_P1_07, LOW);
                        // wait 

                        bcm2835_delay(500);
                    }
                
            }
            bcm2835_close();
        }
    }
}
Then issue the command while inside the testapp folder. And copy testapp-linux-arm folder to your Raspberry Pi 3.
1
dotnet publish -c release -r linux-arm -o testapp-linux-arm
Assuming you know how to and already hook up our test circuit: a led attach to our GPIO4 or rather PIN7 on the RPi3 physical Pin header. Please be careful and you should know how to properly hook up our test led to avoid breaking your RPi3. In RPi3, let us run the docker image then test our testapp on it.
1
docker run -it --privileged --rm -v /home/pi3:/home/share dotnet
--privileged - To give docker access rights to /dev/gpiomem. --rm - the container is removed when it exits or when the daemon exits
Make sure that you have libbcm2835.so inside the /lib/aarch64-~ folder or it will complain about running it in SDK. Then run our testapp like so...
1
dotnet /home/share/testapp-linux-arm/testapp.dll
Your led should be blinking right now....

For revision...