Dynamic Languages on Embedded
At ThingForward, we’re looking at embedded programming from a multilingual perspective, inspired by and with the background of multiple years of web development. Programming for embedded is truly different: Very close to the hardware, with very limited capabilities, with no operating system or very slim OS functionality. If you want to use dynamic languages on small devices, where could you look?
C and C++
First, let’s look at some features of C with regards to embedded programming. Its code is of course compiled and thus quite compact - Constrained devices often come with very limited amount of flash memory, where program code can reside, so having smaller code (in terms of compiled code size) equals to using a smaller (and cheaper) MCU.
Most recent programming languages have dynamic memory management. That is, if you need memory, say 1KB for a buffer to process a HTTP Request, you allocate it, use it and later deallocate it. Many languages come with garbage collection, so you don’t even need to deallocate, it’s automatically done for you.
This is not the case with C. Developers have to make sure to
free everything they’d
Embedded firmware in C can even go further. If the embedded development framework or OS does not come with a library for dynamic allocation of memory, the developer is left with a static memory model. This means that all memory that is needed has to be statically allocated at compile time in arrays, structs and so on. Which has cons but also pros, since you cannot run out of memory :)
C as a programming language can be hard - many frameworks for embedded now rely on C++. Examples of this are the Arduino Development Framework, or ARM mbed OS. Coding with these is more fun, since C++ can offer a lot of abstractions which make code look cleaner. Though C++ also comes with a cost. Features such as inheritance with virtual function need internal function tables, which need additional memory, and dynamic memory can be challenging in embedded environments. As MCUs grow in capabilities regarding processing power and memory, using C++ with a well-tested RTOS is a good choice.
Scripting for IoT?
MicroPython is essentially “python for embedded“, a stripped-down version of Python’s standard library that is able to run on devices with at least 256k of code space and 16k of RAM. This limits the number of microcontrollers that can be used for running MicroPython, typically 32bit MCUs with ARM-Cortex M or Espressif Systems’ ESP8266/ESP32. The MicroPython firmware is coded in C and includes the compiler and runtime. It also has the well-known REPL-Shell (for read, evaluate, print - loop), so that you can dynamically type in python commands to be executed. But most important are the built-in libraries and the functionality it supports out-of-the box. First, it’s a subset of Python 3, so you cannot expect to have the full extent of functions available as on your desktop or server installation of python. Many regular libraries have „micro“-replacements, which offer a subset of functionality (which makes sense for embedded devices). The ability to handle json data is really nice for exchanging data with other devices and services. Some libraries are specific to MicroPython, especially where they make sense for embedded boards. One example of this is „machine“, a library to deal with GPIO, interrupts and sleep mode. Vendors such as Zerynth offer additional libraries on top of that, for example regarding (industrial) sensors, cloud backends or protocols such as MQTT.
We think that as of now, most professional development on constrained devices will be done with C or C++, as the tooling support is great and the knowledge about building robust firmware is widespread. With IoT, more demands are coming up: Connectivity, device intercommunication, dynamic device behaviour just to name a few. And that’s where in our opinion a look at other languages and development frameworks can be helpful, because they already ship with these modern features.
We’re looking forward to our next blog post, where we look at Zerynth and their Python-based solutions for embedded development. Stay tuned!
Your ThingForward team