MicroPython Firmware Development Guides

RT-Thread IoT OS
4 min readJun 18, 2020

You’ll need to build a MicroPython firmware if you don’t have a development board officially supported by RT-Thread. RT-Thread provides the MicroPython software package, and the MicroPython package has been adapted to the RT-Thread driver framework when bound to the underlying hardware, so it’s easy to add MicroPython to a board that runs RT-Thread.

Note: RT-Thread MicroPython requires RT-Thread 3.0 and above.

Find the suitable BSP platform

RT-Thread MicroPython mini version occupies less than

ROM: 190KB

RAM: 20KB

As long as system resources meet the above requirements, many common development boards can run MicroPython, such as the boards supported by STM32 Series BSP.

Let’s take the MDK project on rt-thread\bsp\stm32\stm32f407-atk-explorer as an example of how to build MicroPython firmware on a BSP basis.

Obtain Micropython software package

Update the package list with the pkgs --upgrade command, then select the MicroPython package with the RT-Thread Env tool and finally pull the package locally using the pkgs --update command.

Increase the main thread stack

To subsequently enable the MicroPython runtime environment in the main thread, we need to increase the stack size of the main thread. We increase the stack size to 8k here.

Configure the size of the MicroPython run environment heap

Next, allocate memory to the MicroPython running environment based on the remaining memory, and the higher the number filled in here, the larger the amount of code you can run the Python program. However, if the value written here exceeds the actual allocatable memory, there may be an error caused by memory allocation. Therefore, before you configure this project, you need to have some understanding of the allocation of system RAM resources.

View the remaining memory of the system

Rebuild the project, compile and download, then view the memory usage via the free command in the msh command.

Configure the system

By checking the memory allocation in the last step, we get to know the system RAM resources. In this example, we allocate 20k memory for the MicroPython runtime environment. If you want to run more MicroPython code later, you can allocate more memories, as shown in the following illustration:

Mount the file system at the root of the system

Finally, make sure that the file system is mounted on the root directory (‘/’). With the file system, you can then use the MicroPython development environment to synchronize Python code files to the board to run.

  1. Open MicroPython file-sync option.

2. For the development board used in this example, the file system is stored on SPI Flash and the BSP support for the storage device is already done, you only need to enable the elm-fat file system, and configure the system as follows:

After the configuration is completed, remember to regenerate the project with the scons --target=mkd5 command so that the configuration takes effect in the project.

When you run MicroPython on your board, you are free to choose the storage media for the file system, but remember that the file system needs to be mounted to the root directory (‘/’) so that you can ensure that there are no errors in subsequent file transfers using MicroPython IDE.

MicroPythonStart MicroPython in the main thread

Start MicroPython in the main thread, the code modification is shown as follow:

Recompile the project and download the program to the board, then MicroPython is run in the main thread, and the RT-Thread MicroPython IDE is available for application development. By connecting the development environment to the board, you can see MicroPython’s interactive environment REPL, as shown in the following image:

RT-Thread Contact Info:

Website | Github | Twitter | Facebook | Youtube

--

--

RT-Thread IoT OS

An Open-Source Community-Powered Real-Time Operating System (RTOS) Project! Let’s develop, DIY, create, share, and explore this new IoT World together!