Introduction to developing MicroPython -- Background and Structure

Background

Most microcontroller users have heard/ used arduino IDE to certain degree as it is still a popular platform with many existing libraries and examples freely available online. However, using Arduino requires user to learn C++ language which is one of the most hard-to-learn programming language compared to other popular language such as Python. Luckily, there is MicroPython. As the name suggests, it uses the most popular and easy-to-learn Python language to controller Microcontrollers.

What is even better about MicroPython? It is an open-source project using MIT license which means it can be adopted for commercial uses free-of-charge. Plus, MicroPython is attracting more and more attention and tons of new features being added on daily basis by a group of elite programmers led by Dr. Damien P. George who is an Australian programmer and physicist.

Getting to know MicroPython Project

MicroPython by definition is “a lean and fast implementation of the Python programming language that is optimised to run on microcontrollers”.

Note that MicroPyhton uses Python 3 syntax thus should not be confused with Python 2.

MicroPython project is freely available on Github at

Once you are there, there are a number of folders which can be quite daunting to new comers, but fear not, here is a brief explanantion of the purposes and uses of each folder,

Major components in this repository:

:arrow_forward: py/ — the core Python implementation, including compiler, runtime, and core library.

:arrow_forward: mpy-cross/ — the MicroPython cross-compiler which is used to turn scripts into precompiled bytecode.

:arrow_forward: ports/ — MicroPython port to various hardware platforms. Most useful folder for feature development.

:arrow_forward: tests/ — test framework and test scripts.

:arrow_forward: extmod/ — additional (non-core) modules implemented in C.

:arrow_forward: tools/ — various tools, including the pyboard.py module.

:arrow_forward: examples/ — a few example Python scripts.

:arrow_forward: docs/ — user documentation in Sphinx reStructuredText format. Rendered HTML documentation is available at http://docs.micropython.org.

From the list above, it is clear that the most important folders are py/ and ports/ as they contains the actual implementation of micropython from software kernal to hardware level. While main functions are implemented in py/ (maintained by MicroPython team), hardware specific functions and modules are all inside ports/ directory, thus to start developing for a certain hardware, port/ is where you want to look at.

Due to the length of this article, MicroPython kernal will not be discussed in here, rather it will be mentioned and discussed together with other hardware specific implementations in the later posts.

MicroPython Port for RTL8722

Now let’s take a look at the ports/rtl8722 to better understand the hardware specific implementations.

Please nevigate to Realtek’s official repository for MicroPython RTL8722 port using the link above.

Once you are there, you would see this,

Let’s introduce them one by one,

:arrow_forward: amebad_tool/ — toolchain and post-build tools needed to produce and process the micropython firmware, also include download tool

:arrow_forward: amebad_vendor/ — vendor specific libraries provided by Realtek’s standard SDK, including header files and static archive libraries

:arrow_forward: mp_frozenmodules/ — python scripts that need to be pre-crosscompiled into micropython firmware for immediate availability and fast&efficient execution

:arrow_forward: mp_helper/ — hardware specific modules in C language for MicroPython, all peripharel controls are implemented in here

:arrow_forward: mp_scripts/ — python scripts to be stored in microntroller’s memory unit (flash / SD card) and can be edited and execute in MicroPython’s REPL (a interactive python prompt runs on UART)

:arrow_forward: Makefile –buld specific configurations and entry point for GNU make

:arrow_forward: amebad.mk — actual building recipes and commands

:arrow_forward: README.md — Documentation and guide to use this port

:arrow_forward: main.c — the main :wink:

:arrow_forward: mpconfigport.h — important macros for MicroPython kernal, this decides how kernals are tailored and compiled

:arrow_forward: mphalport.h — similar to moconfigport.h

:arrow_forward: pins.c — pin mapping

Summary

With this introduction, you now should have some basic ideas of how MicroPython project is structured and where to begin to look if you want to start developing for MicroPython. Next article we will dive deeper into actual implementations.

Stay tuned and happy coding! :slightly_smiling_face: