Micro Python on STM32F4-Discovery

The Micro Python project is an effort to make the Python3 language fast and lean enough to run on micro controllers and if was successfully founded by this KickStarter campaign. It is intended to run on it’s own hardware, the pyboard. Unfortunately, this board is not widely availabel, and everyone who missed the KickStarter will not be able to get such a board until now. Luckily the firmware also offers support for more ARM-Coretex M4 based micros, like the widely available STM32F4-Discovery. Since I like the idea running Python on a micro, and due to the fact my STM32F4-Discovery arrived today, I decided to immediately give Micro Python a try. In the following article I will describe in short what needs to be done to make Micro Python running on the discovery by using Ubuntu.

STM32F4 Discovery

STM32F4 Discovery

Hardware needed

STM32F4-Discovery, that’s all you need since it includes already a stlink based debugger interface one could use for flashing.

Software needed on the host (Ubuntu)

To compile Micro Python, the arm-none-eabi cross-compiler is needed. On newer Ubuntu versions this could be easily installed through apt:

sudo apt-get install gcc-arm-none-eabi

Also you will need the stlink utility from texane. For more details on how to install stlink, please consult texane’s README.

To download the ELF firmware, you will also need the GDB for arm-none-eabi. In theory this could be done by the following apt statement:

sudo apt-get install gdb-arm-none-eabi libnewlib-arm-none-eabi

But unfortunately if you have installed any other gdb already (which is likely), this command will fail due a conflicting manpage! Thus, the following work-around is needed:

sudo apt-get install -d gdb-arm-none-eabi
sudo dpkg -i --force-overwrite /var/cache/apt/archives/gdb-arm-none-eabi_7.6.50.20131218-0ubuntu1+1_amd64.deb

Building Micro Python for the Discovery

Clone the Micro Python git repository:

git clone https://github.com/micropython/micropython.git

Now change into the micropython directory an build for the STM32F4-Discovery:

cd stmhal

This should create the firmware.elf in stmhal/build-STM32F4DISC.

Flash the Micro Python firmware to the Discovery

Connect the Discovery through USB (the one on with the Mini-B connector, don’t connect the USB-OTG), then start (in a separate terminal) the stlinkt utilit:


The st-util now waits for a connection from GDB. This is done like so (assuming you are still in the micropython/stmhal directory):

arm-none-eabi-gdb build-STM32F4DISC/firmware.elf

Within GDB connect to st-util by:

target extended localhost:4242

And flash the firmware with:


The multi-color COM-LED should blink while loading. After downloading finised, terminate GDB.

Now disconnect the USB Mini-B cable to power the board completely off.  Connect a USB-OTG on the opposite side, and reconnect the USB Mini-B cable for power. After a few seconds, the STM should be mounted as a storage device showing the files „boot.py“ and „main.py“. It is also possible to open a Python shell on the serial device /dev/ttyACM0:

screen /dev/ttyACM0

You now could follow the tutorial on the Micro Python web page (accelerometer is not supported on the STM).

Note: in theory it should be also possible to download the firmware through DFU as described in the Micro Python README. But to my understanding you need to enable DFU mode on the STM by removing some resistors to change boot-configuration. 


Micro Python looks very well done and promising. I think I should give it a try in some of my next projects.


Leave a Reply

You must be logged in to post a comment.