February 26, 2015


Microsoft C# as a New Language for Cross Device Hardware Programming

C# is developed by Microsoft as a simple, powerful, type-safe and object-oriented programming language. It is widely accepted as a high level programming language for web and desktop programming on the Windows platform. By utilizing the rich set of libraries and API, it is possible to build applications rapidly.

Although C# is known for programming on the Windows platform, at its core it is not tied to any platform due to its intermediate compilation and runtime execution. C# gets compiled into byte code which gets compiled to the native machine instructions by using the JIT compiler. This permits the C# program to execute on any platform which provides an execution environment such as other cross platform programming languages, for e.g. Java.

Device hardware programming

A device’s hardware programming typically requires programming of its microcontroller (MCU) which is an intelligent unit or device brain that enables the device to perform intelligent functions like decision making, external communication, etc.

There are generally two categories of devices:

  • Standalone device hardware (like an embedded system)
  • Device connected to the PC via some port (serial, parallel, USB, etc.)

Programming a device’s microcontroller involves cross-compilation. Code for the device is written in some (high-level) language on a host PC, compiled and converted to a form suitable for downloading onto the target device (microcontroller).

Device programming requires the program to perform well on resource constraint devices which have limitations such as:

  • Slow CPU (Microprocessor)
  • Very small available memory (RAM)
  • Small disk space (ROM)
  • Few or no external connectivity (Interfaces)

A device’ program also needs to execute in real time without any pauses or delays. This requires a sort of contract that the program will perform as specified and any delay or nonconformance will defy the purpose of programming the device.

Need for a new language

C and C++ are the current industry de-facto standards for native hardware programming, as programs written in these languages perform well in the device ecosystem. Most of the device manufactures release development SDK and IDE environments targeting these languages.

Though C and C++ are suited for devices, they still suffer from limitations, such as:

  • Long development cycles
  • Unpredictable results during runtime such as memory leaks/device crashes
  • Difficult to maintain/upgrade
  • Require specialized development resources

C and C++ are not the only options for programing Microcontrollers, as it all depends on the support of the compiler, linker and assembler, which convert the code in C and C++ or any other language into machine readable instructions for processing.

How C # fits in

With its proven performance and rapid development, C# can be a viable alternate to the conventional programming languages for device hardware programming.

With the features like managed code execution and runtime code optimization, it can overcome most of the limitations of conventional device programming languages while providing the required performance and reliability.

Presently there are many options for leveraging C# for device programming that include:

  • Dot net compact framework – This is a compact version of the .Net framework for executing C# programs on devices that support Windows embedded applications. E.g. Windows phone, Windows CE devices.
  • Dot net Micro framework – This is a complete runtime environment for executing C# programs. It does not require Windows embedded on devices and is supported by some microcontroller boards like Netdrino.
  • In scenarios where the Dot net compact framework/Micro framework is not an option, devices can still be programed by utilizing third party frameworks like Mono.
  • Time ahead compilation using Mono AOT – AOT enables the compilation of C# programs for the target platform directly instead of the CLR. This enables C# programs to run on target devices not supporting the .net framework.
  • Cross-compiler platform – The cross-compiler platform compiles and replaces the unsupported C# executable portions to the specific device instructions. This enables porting C# on a wide array of devices. E.g. Pigmeo.

Still a long way to go…

C# involves managed programming, while C and C++ are unmanaged. This means that there is no automatic memory allocation or garbage collection, although garbage collector (GC) is one of the most exciting and useful features of C#. Still, it has a cost and overhead that is too much for memory and CPU constrained embedded devices. Memory optimization and a better utilization of CPU cycles can increase C# programming adaptation to other hardware devices.