Using C# on embedded devices too – we show how it works and the benefits with nanoFramework.
Introduction
C# is currently used mainly for application development, but wouldn't a standardised programming language for different platforms – including embedded devices – simplify development? This is precisely the goal being pursued by the “nanoFramework” open source project, as the successor to the .NET Micro Framework. This makes it possible to run managed code such as C# on a microcontroller with limited resources.
CSA Engineering AG has ported nanoFramework for its own universal embedded platform and put it into operation. This platform is based on an STM32F4 microcontroller and is used by customers in industrial environments.
Benefits of standardising the programming language
Applications in the industrial sector often consist of an embedded device, e.g. a small sensor, an actuator or another electronic unit with a microcontroller as well as a central evaluation/control unit in the form of an industrial PC solution or cloud application. Normally, both areas use different programming languages, operating systems and development environments. Knowledge of different technologies and tools is therefore essential in order to develop these kinds of applications. The maintenance requirements of the various toolchains should not be underestimated. Reducing tools and programming languages through standardisation with the help of a framework would offer many advantages:
- Bundling expertise: employees can be utilised more flexibly in both areas.
- Speeding up development: the uniform toolchain, programming language and framework result in a standardised system configuration and a common structure across several teams.
- Increased reusability and stability: software modules no longer have to be created redundantly for both areas, but can be used universally.
- Simplifying maintenance: standardisation and the resulting reduction in tools and the code base lead to reduced maintenance requirements.
The nanoFramework design
The open source nanoFramework project implements the benefits mentioned using the C# programming language. This enables the programming, debugging and deployment of an embedded device directly from Microsoft Visual Studio. This is achieved through a Microsoft Visual Studio extension and by integrating a reduced version of the .NET Common Language Runtime (CLR), designed for embedded devices with limited resources, in nanoFramework. This provides the developer with a subset of the .NET standard and UWP classes as well as similar convenient features for development which are familiar from PC application development.
The benefits of nanoFramework are evident:
- Complete hardware abstraction and, consequently, platform-independent access to the usual interfaces such as UART, SPI, I2C, USB and Ethernet.
- Multithreading
- Automatic memory management using a garbage collector.
- Implementation of “unmanaged” code and thus the possibility of implementing optimised C or C++ code.
- The open-source strategy enables the expansion of .NET classes or the extension of in-house methods.
- The MIT licence allows unpublished code changes.
nanoFramework is constructed in various layers and using a real-time operating system is not absolutely necessary. nanoFramework can also be directly operated on the microcontroller. We have selected the option using a real-time operating system on the basis of the comprehensive HAL from ChibiOS optimised for STM32 microcontrollers. nanoFramework in turn consists of two parts: nanoBooter, which is responsible for the framework starting correctly and.NET Common Language Runtime – nanoCLR –, as previously mentioned. This division enables another feature of nanoFramework: both nanoCLR and the C# user application can be updated during operation. Extensions of the .NET classes or new features in the C# user application can therefore be continuously added. The HAL, RTOS and nanoFramework layers should only be edited during porting to the desired hardware platform or extensions to the .NET classes. The developer can then concentrate exclusively on the C# application – applied to the top layer – during the actual application development.
Summary
We believe that nanoFramework offers a comprehensive set of functions for applications in wired sensors, control and evaluation electronics or rapid prototyping. nanoFramework is also suitable as a gateway into the embedded world, as the developer does not first have to deal with microcontroller configurations, registers, memory accesses, etc. However, to port nanoFramework to your own platform successfully, in-depth embedded knowledge is absolutely essential. Due to the complex construct of the entire framework, it is also advisable to seek assistance from a partner with the relevant experience. We ported nanoFramework for our own M4MCU platform and we are now familiar with its challenges. nanoFramework is used on this M4MCU platform in combination with an Azure-based backend for communication and control of an LED matrix. The customer mentioned at the start uses nanoFramework in combination with the M4MCU platform as a smart gateway to analyse and control combined heat and power units.
Do you have any questions or a specific idea?
Then contact us so we can support your project too.