Writing files to Nucleo PYB Flash

After flashing MicroPython to your Nucleo board, and working with REPL, the next step is to write your Python program and transfer it to the board.  The STM Nucleo boards do not come with a USB-OTG (USB On-the-go) connector, so it is not possible to directly access the PYB flash directory where python scripts and programs are stored.  The STM Discover boards have this connector built in, and it is possible to mimic these boards’ USB-OTG circuit with the Nucleo through the GPIO Pins.  This process, however, requires a higher level understanding of electronics and requires additional hardware.

It is possible to access the PYB Flash directory using a serial interface through the ST-Link programmer’s USB port with a program called ampy – Adafruit MicroPython Tool. Install instructions are found on Adafruit’s website and their GitHub page.

Before using ampy, it is important to know the communication port your OS assigned to the Nucleo board.  See instructions under “Opening REPL – Testing your MicroPython flash” to determine COM port.

All Ampy Commands

Execute the command “ampy –help” in a terminal window to see a list of all available commands.

Display PYB Flash Contents

Execute the command “ampy -p COM_PORT ls /flash” (replace COM_PORT with your communication port)

Write File to PYB Flash

In your terminal window, make sure you are in the directory of the files you are trying to push to the Nucleo board.

Execute the command “ampy -p COM_PORT put FILE_NAME” (replace FILE_NAME with the file you are pushing).  Use the “ls” command as document above to ensure that the file was correctly sent.

Remove Files and Directories from PYB Flash

Use the command “ampy -p COM_PORT rm FILENAME” and “ampy -p COM_PORT rmdir DIRNAME” to remove files and directories, respectively, from PYB Flash.

Other Functions

Use “ampy –help” for a list of all functions.  Read the ampy GitHub Readme for even more information.