Technical Note
Technical Note
Contents
1 Changelog 2
3 Executive summary 2
4 Introduction 2
5 Port structure 2
5.1 docs/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.2 xng/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.3 micropython/ports/zedboard . . . . . . . . . . . . . . . . . . . . 3
7 Build system 5
7.1 Hello World example compilation . . . . . . . . . . . . . . . . . . 6
7.2 Test suite compilation . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3 How to modify the build system . . . . . . . . . . . . . . . . . . 6
7.3.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3.2 Test suite . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3.3 Micropython . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
1 Changelog
Version Date Author Comments
01 19/04/22 Sergio Pastor Pérez Initial document
3 Executive summary
This document summarizes the results obtained from the Micropython porting
to XNG.
4 Introduction
Micropython is an implementation of Python 3 programming language for
microcontrollers. It is mostly compilant with the specification of CPython3.4.
The port support most of the funcionalty provided by Micropython with the
exception of optimisation oriented features, such as, the native assembly com-
piler and the viper compiler. Other features out of the scope are the VFS, the
GPIO and the implementation of drivers APIs for specific peripherals.
5 Port structure
The port is divided in two mayor components, the hypervisor folder named
xng and the Micropython folder which contains the Zedboard port. From the
Micropython folder, the port is located in ./ports/zedboard.
2
5.1 docs/
Holds all the documentation created for the port.
Subdirectories
• note_files: holds all the images and extra files needed to build the docu-
mentation.
5.2 xng/
Holds the example and test suite source files along with the necessary makefiles
to build them.
Subdirectories
• drivers: necessary source files to control de UART and enable the REPL
serial input.
• xml: configurations related to the hypervisor and the partitions.
• lds: linker scripts for the build system.
• mkfiles: the extra rules and paths used by the makefiles.
5.3 micropython/ports/zedboard
Holds the necessary libraries and configuration files needed by the python build
for the Zedboard.
Subdirectories
• tools: holds the tool needed to generate the test suite C file.
• mkfiles: the extra rules and paths used by the makefiles.
3
6 Python code execution on the Micropython VM
port
6.1 Python injection from C
Micropython allows to write python code from the C files used to program the
partition running over XRE. This can be done as follows:
int py_code_injection()
{
int stack_dummy;
stack_top = (char *)&stack_dummy;
#if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap));
#endif
mp_init();
#if MICROPY_ENABLE_COMPILER
pyexec_friendly_repl();
mp_deinit();
return 0;
}
First, the user has to initialize the Micropython enviroment, after that the
function do_str(. . . ) can be called and the python code passed as a string will
be execute thanks to the JIT compiler. When the Micropython enviroment is
no longer needed it should be discarded with mp_deinit().
4
6.2 Load precompiled Python binaries
Precompiled python binaries can be loaded from the C files used to program
the partition running over XRE. This can be done as follow:
int uPython_exec_frozen_test(const char *const test)
{
int stack_dummy;
stack_top = (char *)&stack_dummy;
mp_init();
pyexec_friendly_repl();
pyexec_frozen_module(test);
mp_deinit();
return 0;
}
First, the user has to initialize the Micropython environment, after that the
function pyexec_frozen_module(. . . ) can be called with a string holding the
.py file name that wants to be loaded.
To allow this loading, first the file has to be acknowledged and compiled by the
build system. This is done through the XNG makefile. For each partition, a
call to the makefile macro def_part(. . . ) will create the apropiate rules to build
the system. The ${1} argument should hold the partition name, ${2} should
hold all the partition source C files, ${3} holds all the extra object files that
are ready to be linked (if any), ${4} is reseved for a .py file that wants to be
frozen. ${4} can also accept __test__ as a parameter to start the build of the
test suite.
7 Build system
The build system will also generate the binaries of each partition separately, for
this example the generated partition file is hello_world.armv7a-vmsa-tz.elf,
which is stripped to a raw binary for the elfbdr tool. The raw binary is
hello_world.armv7a-vmsa-tz.bin.
5
7.1 Hello World example compilation
In order to build the hello_world example, execute make in the terminal. This
will generate the file sys_img.elf, this executable will run the example over
XRE in a partition of XNG.
6
7.3.3 Micropython
The only modifications that the user should do to the Micropython side of the
build system are in the mpconfigport.h, here are the different macro definitions
that change how the Micropython firmware is built. For more information the
user should check the official documentation of the project from: Micropython
docs