Flashing MicroPython to STM32 Nucleo

In Windows

MicroPython can easily be run on any of the compatible STM32 Nucleo boards listed by flashing the newest version to the board using the ST-Link program.  A list of compatible boards can be found at the bottom of MicroPython’s download page under “Firmware for other boards”.  However, the dfu files listed on this page cannot be natively flashed to the Nucleo board; rather, a hex file must be built and compiled from the MicroPython source code.  The process to do this is quick and easy, and should take only a few moments.  For this tutorial, a new Nucleo-L476RG will be flashed with the newest version of MicroPython.

In order to simplify the process, we will be using the Windows version of the ST-Link software to flash the built MicroPython image to the Nucleo board; however, it is tremendously easier to build the hex file using a unix-based operating system.  For the purpose of this tutorial, we will use a Debian virtual machine to do this; however, any unix environment will work (unix servers, Raspberry Pi, etc.)   Note:  It is recommended to use either Debian or Ubuntu as the linux distro.  Other version will work but the procedure may differ from this tutorial.

Create and configure a Debian VirtualBox virtual machine

  1. Install the most recent version of Debian Linux to a VirtualBox virtual machine.  There are countless online resources documenting this procedure.  For further info, read here.
  2. Log into unix.  Open a terminal window.  Enter the following commands one by one into the terminal window.  Press enter/return after each command, and wait for the {your-user-name}@debian or root@debian prompt between entering each command.  Replace {your-user-name} with the username you chose during installation.  Note:  Instructions preceded with >>> are notes, not commands to be entered.

Building MicroPython for your Nucleo board

All pre-requisite packages and software should now be installed, and you can clone the MicroPython source.  Enter the following commands into the terminal. When entering the last make command, a list of supported boards to replace {your-board-model-here} can be found here, the directory names are all valid board types.  Use no spaces and all caps.  See images below.

Upon success, the terminal should display the following “LINK build-NUCLEO…” message:

If you have this message, you have successfully built MicroPython for your Nucleo board!

Copying files from VirtualBox virtual machine to host

We need the firmware.hex file that was built in unix on a Windows PC.  If you built MicroPython on a dedicated unix computer or did not use a VirtualBox virtual machine, the following steps do not apply and you must find a way to copy firmware.hex to your Windows PC.  The following procedure documents copying the firmware.hex file from a VirtualBox virtual machine to the Windows host.

  1. Right click the network icon on the bottom right corner of the VirtualBox window and select “Network Settings”.  See highlighted image below.
  2. Select “Host-only Adapter” from the drop down menu next to “Attached to”.  Click OK.
  3. Open a terminal window in Debian.  Type “ip address”.  Write down the IP address under “link/ether” after “inet”.  See image below.  IP address from image below is 192.168.25.101
  4. Minimize (but do not close) VirtualBox.  In Windows, open your favorite FTP client (I recommend FileZilla Client).  In the FTP Client, enter the IP address from the previous step as the host, your unix username and password as the FTP account, and use port 22.  Note:  If your FTP client does not allow you to specify port number, enter sftp://{your-ip-address-here} into the host field.
  5. In the FTP client, navigate to micropython/ports/stm32.  You should see a directory named “build-{your-board-model-here}.  Open this directory.  Browse for “firmware.hex”.  Copy this file to your Windows desktop.
  6. You can now close VirtualBox.  We no longer need Unix.  In fact, you can delete your virtual machine if you wish.  IMPORTANT:  If you plan to use your virtual machine again, repeat steps 1 and 2 but change “Host-only Adapter” back to “Bridged Adapter”.

Flashing MicroPython firmware HEX file using ST-Link

The hard part is done, and now we must use the ST-Link software from ST to flash MicroPython to our board.

  1. Download and install the STM32 ST-Link Utility software.  It can be found on ST’s website here.
  2. Connect your Nucleo board to your computer using a mini USB cable through the STLink programmer port.  Allow drivers to install in Windows.
  3. Open the ST-Link software.  From the top bar, Select File>Open.  Browse for your firmware.hex file.  Click OK.
  4. From the top bar, select Target>Connect.
  5. From the top bar, select Target>Program & Verify.  Leave default settings, click Start.  If successful, you will see “Verification… OK” at the bottom status window.  You can now close ST-Link Utility.

You have successfully flashed MicroPython onto your Nucleo board!!!

Opening REPL – Testing your MicroPython flash

We can communicate with MicroPython through the ST-Link programmer mini-USB port using serial communication.  To do so, we will need to determine the COM port that Windows has assigned to the board, and we will need a serial client for Windows.  I recommend and will be using PuTTY as the serial client.

  1. Download and install a serial communication client such as PuTTY.
  2. Open the Windows Device Manager.  (WIN + R, type “devmgmt.msc”, press Enter)
  3. Expand “Ports (COM & LPT).  Look for the STLink Virtual COM Port entry and record the COM port.  (COM9 port below).  Close Device Manager.
  4. Open your serial client (PuTTY).  Select “Serial” under Connection Type.  Enter the COM port you recorded into the Serial Line field.  Enter 115200 for speed.  Click Open.
  5. Click inside the empty serial terminal window.  Press the RESET button on the board.  You should see the REPL display.  Try entering the command print(“SUCCESS”).

Congratulations!  You have successfully flashed MicroPython onto your Nucleo microcontroller board.

23 thoughts on “Flashing MicroPython to STM32 Nucleo

  1. Hi,
    Thanks your effort for sharing how to use micropython in Nucleo-L476.
    I’ve successfully followed your step to build the hex file, flash into the board and test the ADC.
    Here are two problems I encountered:
    1. I found that the I2C doesn’t work at all. Have you tried yet?
    I use the I2C1. As mpconfigboard.h shows, PB6 and PB7 are SCL and SDA respectively.
    I deploy two board that communicate with each other through I2C; one as master, the other as slave.
    I’ve tried the same code on PYB, which works well.
    With the aid of the Logic Analyzer, I found that after executing the following lines of code:

    from pyb import I2C
    i2c=I2C(1, I2C.MASTER, baudrate=100000)

    PB6 and PB7 are not pulled high.

    2. I can’t see a PYB Flash, that is I can not see a main.py/boot.py.
    How to flash the code into the board once I complete developing?

    Any thoughts are welcomed. Thanks for your reply in advance.

    1. Hello there!

      I have documented how to flash code to any Nucleo board here without the need of any additional hardware.

      To fix your I2C problem, sounds like you are missing an external pullup resistor. Add a 4.7k resistor between SDA and Vpp and another between SCL and Vpp.

      Best of luck!

Leave a Reply

Your email address will not be published. Required fields are marked *