.NET nanoframework on an M5Core2

Every now and then I get the desire to do some electronics – I’m not very good at it, so it often ends up with me hitting a wall where I need to learn more electronics. Anyway, the devices available now are truly amazing. Before I used Raspberry Pi’s which are amazing, but now I’m using the Raspberry Pi Pico W and just got an even more (although ofcourse more expensive) amazing piece of kit in the M5Core2.

This thing has a touch screen, USB-C, the obligatory LED, vibration, microphone, speaker and more. But what’s really interesting is that it’s supported by .NET nanoframework.

Note: The pico also has a port for using with nanoframework, but for now I’m leaving that set-up with MicroPython.

Getting Started

Let’s start with the hardware

  • Plug your device into a USB port on your computer
  • If the device is not detected, got to Driver Installation and select the latest driver for your OS
  • If all went well it will appear on a COM port. In my case I’m on Windows I checked whether the COM PORT was added to your device manager, mine’s on COM9

The power button is on the left-hand side of the M5Core2 at the top, press this once and you should see a wonderful UI full of bits and bobs which detects sound and movement, sadly we’re going to get rid of that when we install nanoframework – so have a play now whilst you can.

Note: To turn the M5Core2 off, hold the power button for 6 seconds.

Getting nanoframework up and running

Follow the steps on the nanoframework Getting Started Guild – Managed (C#).

You’ll want to install the nano firmware flasher/nanoff using

dotnet tool install -g nanoff

You’re going to want to flash the M5Core2 using nanoff like this (remember this will clear the M5Core2 so if you want to play with the installed app first do so now – as you can tell I wish I’d played with the the default app a little more)

nanoff --target M5Core2 --update --serialport COM9

Replace the COM9 port name with whatever port your device is on.

Writing the LED Blink App.

I’m going be using Visual Studio 2022 to write my code. So, go to the extensions menu item and install the nanoframework extension. This will install project templates and other goodies.

Note: I found this didn’t work great first time, so had to try to remove and do again, but got there in the end, so persevere if it fails first time.

Although the M5Core2 has a screen, the ubiquitous first application for a microcontroller is the LED blinking application. Now the M5Core2 does have an LED but it’s the power LED, but that’s not an issue nanoframework has our back.

Once the extension is installed and working, creating a new application using the Blank Application (.NET nanoframework) template, you’ll see some code which looks like any other C# project. Actually, nanoframework it supports top level statements, so we’ll look at changing this code in a moment.

First off, we need to install this NuGet package

nanoframework.M5Core2

It will install quite a few extra packages.

Replace everything with Program.cs with the following

using nanoFramework.M5Stack;
using Console = nanoFramework.M5Stack.Console;

M5Core2.InitializeScreen();

Console.WriteLine("Led ON");
M5Core2.PowerLed = false;

In the above code I’m showing how we can write the M5Core2 LCD screen using Console (we need to initialize the screen to begin with) and I’m setting the PowerLed to false (umm yes, false seems to be ON, not sure if this is a bug or if this is some electronics things).

Before we can run anything, locate the Device Explorer view. In Visual Studio either type this into the Search (Ctrl+Q) textbox or go to View | Other Windows | Device Explorer. Ensure your hardware is plugged into a USB port and powered up and if need be, refresh the list in the Device Explorer until you see something like M5Core2 @ COM9. Feel free to ping the device using the ping button to check it’s running nanoCLR (this will be displayed in the Output Window of Visual Studio, .NET nanoFramework extension option).

Now press the run button (assuming .NET nanoFramework Device is selected). In the Output Window, Debug view, you’ll see the compiler run then the deployment process takes place, or in my case, it doesn’t work straight away. If yours works, jump ahead and ignore the bit on fixing the error I had.

If you get Error: a3000000, I found this was version issue, noticing that it also says Link failure: some assembly references cannot be resolved!!. If you check the Error List Window you may see it mentions a version issue, and suggests using AutoGenerateBindingRedirects. It appears nanoFramework does not support assembly binding, see this link.

So we need to check the Output Debug window, I found that after the Error: a3000000 I had this

Assembly: Iot.Device.Axp192 (1.2.0.0) needs assembly 'UnitsNet.Power' (4.145.0.0)

But before he Error: a3000000 I also have all these

Assembly: Blinky (1.0.0.0) needs assembly 'nanoFramework.M5Core2' (1.1.82.38397)
Assembly: nanoFramework.M5Core2 (1.1.82.38397) needs assembly 'Iot.Device.Mpu6886' (1.2.0.0)
Assembly: nanoFramework.M5Core2 (1.1.82.38397) needs assembly 'Iot.Device.Rtc' (1.2.0.0)
Assembly: nanoFramework.M5Core2 (1.1.82.38397) needs assembly 'Iot.Device.Axp192' (1.2.0.0)
Assembly: Iot.Device.Mpu6886 (1.2.0.0) needs assembly 'UnitsNet.Temperature' (4.145.0.0)
Assembly: Iot.Device.Rtc (1.2.0.0) needs assembly 'UnitsNet.Temperature' (4.145.0.0)
Assembly: Iot.Device.Axp192 (1.2.0.0) needs assembly 'UnitsNet.ElectricPotential' (4.145.0.0)
Assembly: Iot.Device.Axp192 (1.2.0.0) needs assembly 'UnitsNet.ElectricCurrent' (4.145.0.0)
Assembly: Iot.Device.Axp192 (1.2.0.0) needs assembly 'UnitsNet.Temperature' (4.145.0.0)

Obviously, there’s a versioning problem so we need to go into the NuGet Package manager and go through each of the items listed and change to the expected versions, either rolling back or forward out packages. I basically updated everything on the left hand side to of the list (above) to the latest versions and all, eventually started to work.

Once everything builds and deploys via the run button your M5Core2 should show the text “Led OFF” and the Power Led is off. Now switch the code to the following

Console.WriteLine("Led ON");
M5Core2.PowerLed = false;

and run again.

Once deployed and started on the device, the Power Led should be on and the text on the screen will say “Led ON”.

We’ve now got the bare bones of a .NET nanoFramework application running on the ESP32 M5Core2.

References

Nanoframework M5Stack