Thursday, April 25, 2024

MicroPython: The Python For MCUs

- Advertisement -

The power of Python is well-known and widely accepted. Easy to learn and systematic to script, the effortless way to express complex behaviour is what makes this language tick. Here we explore the adaptation of this popular language to a popular branch of electronics, the embedded systems. Presented below is the software review for MicroPython.

Introducing MicroPython

The Pyboard
The Pyboard

Whether it is to make a light emitting diode (LED) blink, read voltages, make motors and servos move, communicate wirelessly or control robots, MicroPython comes to one’s aid. The software is built to exploit the powers of Python programming to create efficient embedded systems. A brainchild of Damien George and funded by a Kickstarter campaign, this is a selected sub-set and a lean implementation of Python 3.x, optimised to run on microcontrollers (MCUs) and in constrained environments.

With full Python programming language at your disposal, you can write functions and classes, make lists and dictionaries, do string processing, read and write files, and have more sophisticated features such as generators, closures, list comprehension and exception handling. The possibilities for programming are endless!

- Advertisement -

Stretch to accommodate

To extend the capabilities of the MCU-MicroPython combination, exclusive drivers in pure Python have been implemented. Creating an Internet of Things (IoT) application is easy, thanks to CC3000 Wi-Fi module. If the Ethernet is the protocol supported, use WIZ820io module, and low-power implementation is not far with support for nRF24L01+ wireless module. The serial peripheral interface (SPI) port of the MCU board can be exploited to include the above modules.

Getting better by the day

What started off as a project to develop faster and better embedded systems was continuously developed to include support for the latest trends. George, the developer, has joined hands with Paul Sokolovsky and Viktoriya for a second MicroPython Kickstarter campaign that intends to run MicroPython like clockwork on ESP8266, a compact and little system-on-chip (SoC) having built-in Wi-Fi and some nice general-purpose input/output (GPIO).

In another collaboration with Nicholas Tollervey and Python Software Foundation, and help from volunteers, running MicroPython on BBC micro:bit is being targeted. This being the official way of running Python on micro:bit, it has grown to a well-constructed teaching tool with a child-friendly set of modules for doing things like making animations and playing music.
The developer also highlights a latest attempt at taking MicroPython to Space as one of his most exciting happenings. In coordination with European Space Agency, the project involves continuous monitoring and development to make MicroPython more deterministic, robust and efficient, to port it to scalable processor architecture (SPARC) and evaluate its use in Space based applications.

Interestingly, some of the development work coming out of this project is already available in the official repository, providing a stepping-stone for use in critical systems and industrial settings.

A mini Python OS

By using the compiler to compile Python script to byte code, the software can also be run on personal computers. The developer claims that this version runs faster than CPython for various benchmarks, and the native compilation option makes MicroPython run significantly faster even. The boot-up process of this operating system (OS) can be done via device firmware upgrade mode, which allows for upgrading MicroPython library or downloading your C/C++ program. The OS can also go into boot mode, booting from flash or creating one on its own.Overview of Pyboard

MicroPython supports 32-bit ARM processors like the Cortex range that runs on Thumb v2 instruction set. An exclusive circuit board called Pyboard or MicroPython board has been designed by Jaltec Systems, a UK based company, to run MicroPython on the bare metal, providing you with a low-level Python OS that can be used to control all kinds of electronics projects.

Decomposing the structural functionality

Re-written in C, MicroPython includes a complete parser, compiler, virtual machine, runtime system and garbage collector, and supports libraries to run on an MCU. Of course, in order to get programs working on an MCU, you need to convert the code into a machine-understandable format.

Choosing options using a function decorator lets the compiler compile the code to either byte code or native machine code. The compressed code then runs on the built-in virtual machine.

A nativity that is powerful. Native machine code takes more memory but runs at twice the speed of byte code. It uses native machine integers instead of Python objects, but can still be called from Python or call Python. This kind of scripting comes to one’s advantage in time-critical procedures like interrupt functioning.Three projects based on MicroPython

Complex implementation, simplified. The structure of MicroPython is built to not only allow for high-level control operations, but also lower-level execution. Be it to optimise one’s code, access a processor-specific instruction or execute a system call, one can feel free to use low-level assembly instructions and completely access the underlying machine. The software’s inline assembler then takes care of these. From Python’s perspective, these inline assembler functions are like any other regular function.

The tool provides a command-line interface that works on the principle of a read-evaluate-print loop. The grammar is interpreted on-the-fly when parsing, referencing compact tables encoded using Extended Backus-Naur Form (EBNF).

All syntactic elements and most built-in functions of Python are supported. Micro versions of array, struct, sys, io, math and other libraries are implemented in the core; additional libraries are available as pure Python code.

Storing, coding and processing. The designer has worked smartly to minimise memory usage to as minimum as possible, intended for MCUs. Minimal random access memory (RAM) usage, minimal code and quick execution, in this order, are the driving factors behind the design of MicroPython. A simple and fast mark-sweep garbage collector manages memory, taking less than four minutes to perform a full collection.

Objects are stored efficiently and many functions can be written to use no heap memory, thus requiring no garbage collection either. This is well-illustrated in the case of integers that fit in 31 bits, which do not allocate an object on the heap and require memory only on the stack. Even exceptions are handled with minimum memory using a variant of setjmp/longjmp instructions. A cut-down build of MicroPython can work with around 75kB of read-only memory (ROM) and 8kB of RAM.

Putting it to maximum use

MicroPython is provided under MIT open source licence. The software package, apart from the files for use with Pyboard, includes a Unix version, one each for ARM, Teensy 3.1, 16-bit PIC and Texas Instuments’ CC3200 MCUs.

In addition to the above, there is also a minimal port that acts as a starting point for porting MicroPython to any MCU chosen by you.

Do you like this article? You may also like other software reviews


Priya Ravindran is M.Sc (electronics) from VIT University, Vellore, Tamil Nadu. She loves to explore new avenues and is passionate about writing

1 COMMENT

SHARE YOUR THOUGHTS & COMMENTS

Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators