MicroPython on STM32L475 Disco
The STM32L475 Discovery board (B-L475E-IOT01A) is one of the more versatile development boards for embedded developers or makers. Not only does it support traditional C/C++ developments, it also supports MicroPython and Javascript using 3rd party tools.
We will cover Javascript development on B-L475E-IOT01A with MakeCode in another post. In this post we will first look at how to set up the MicroPython development environment for STM32.
By following our posts, you will be able to learn all the three most popular coding languages – C/C++, Python and Javascript with just one embedded platform!
Build MicroPython Firmware
Many open-source development tools for embedded software traditionally run on Linux. Today a lots of them offer corresponding Windows versions. You may also run the Linux versions on Windows through Windows Subsystem for Linux (WSL) or Virtual Machine (VM). In this post, we assume you have access to a Linux build environment using either a native Linux machine, VM or WSL.
First we need to build the MicroPython firmware which contains the MicroPython interpreter along with all the built-in modules. Refer to the official MicroPython git repository for details: github.com/micropython/micropython
Checkout the MicroPython source code from git to a local folder. We are going to place it under ~/Projects/git. We then build the MicroPython cross-compiler.
cd ~/Projects/git git clone https://github.com/micropython/micropython.git cd micropython make -j16 -C mpy-cross
Prepare to build MicroPython for STM32. You would need to have the gcc-arm-none-eabi toolchain installed. If you have not installed it already, you may download it from: GNU Arm Embedded Toolchain Downloads. You may pick one of the recent versions, but note there might be issues with certain versions (e.g. memory leak in snprintf() in version gcc-arm-none-eabi-10.3-2021.10). In this post, we will use the version gcc-arm-none-eabi-9-2020-q2-update.
Download the file for 64-bit x86 Linux host named gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2 .
Decompress the file to a folder with the default name and move the folder to/usr/local. Create a symbolic link to the folder.
bunzip2 gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2 tar xvf gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar sudo mv gcc-arm-none-eabi-9-2020-q2-update /usr/local ln -s /usr/local/gcc-arm-none-eabi-9-2020-q2-update /usr/local/gcc-arm-none-eabi
Add the path /usr/local/gcc-arm-none-eabi to the $PATH environment variable with a shell script named ~/Projects/git/setup.sh, which contains:
export PATH=/usr/local/gcc-arm-none-eabi/bin:$PATH
Run the setup script and verify the compiler version is correct:
cd ~/Projects/git . ./setup.sh arm-none-eabi-g++ --version
Build firmware for STM32L475 Discovery board:
cd ports/stm32 make submodules make -j16 BOARD=B_L475E_IOT01A
If the build is successful, you will see the following output. It tells us the output firmware files are located under the subfolder build-B_L475E_IOT01A:
LINK build-B_L475E_IOT01A/firmware.elf text data bss dec hex filename 339160 136 27560 366856 59908 build-B_L475E_IOT01A/firmware.elf GEN build-B_L475E_IOT01A/firmware.bin GEN build-B_L475E_IOT01A/firmware.hex GEN build-B_L475E_IOT01A/firmware.dfu
Download MicroPython Firmware
The STM32L475 Discovery board supports mbed Flash update. You can simply copy the MicroPython firmware hex file (firmware.hex) to the USB mass-storage drive which is automatically mounted to your host machine (Windows or Linux) when the board is plugged in.
The following screenshot shows the file firmware.hex has been copied to the USB drive named DIS_L4IOT. Do not unplug the board while the internal Flash is being updated. The board will automatically reboot when the update is completed. The USB drive will be remounted without the file firmware.hex shown anymore.
The update process should not take more than one minute. If the USB drive is not remounted automatically after a minute (i.e. the file firmware.hex remains), please check the ST-Link version of your board by opening the file DETAILS.TXT.This is the version of my board:
Version: V2J39M27 Build: Oct 22 2021 16:50:45
If the version of your board is lower than this, please upgrade the ST-Link firmware using the ST-Link upgrade utility (STSW-LINK007 – STMicroelectronics) or through STM32CubeIDE.
Trying out REPL
After the MicroPython firmware has been downloaded to the STM32L475 Discovery board, we can verify it is running normally by trying out the REPL interface (read-evaluate-print-loop).
Using the same USB connection used to download the firmware, we connect to the virtual COM port from a serial terminal program such as minicom. To allow access to the COM port without root privilege, we need to add the current user to the dialout group:
sudo usermod -a -G dialout $USER
Type “minicom -s” to bring up the setup menu of minicom. Configure it to use “/dev/ttyACM0” as the serial device with the settings of 115200 N81.
Press the reset button of the discovery board, you should see something similar to the following on the terminal:
... MicroPython v1.18-99-g203ec8ca7 on 2022-02-19; B-L475E-IOT01A with STM32L475 Type "help()" for more information. >>>