Friday, April 19, 2024

MicroPython: The Python For MCUs

- Advertisement -

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.

- Advertisement -

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