Electrical – STM32 firmware upgrade (In-System Programming) over USB

firmwarenxpstm32

There are many ways to program an STM32 chip (in my case STM32L4R5):

  1. USB DFU mode (using DFuSe utility from ST)
  2. UART Serial Port (through something like FTDI chip)
  3. JTAG or SWO debug ports
  4. SD Card bootloader: https://github.com/akospasztor/stm32-bootloader

But often omitted in discussions is something similar to NXP's In-System Programming (ISP), see note AN10986 (PDF)

It is the easiest (avoids using a proprietary ST DFuSE utility), cheapest (avoids SD card receptacle BOM cost, or USB-to-Serial chip) of all but I couldn't find a solution anywhere on the internet. For example, the Teenage Engineering OP-Z firmware upgrade process is similar to NXP's ISP – i.e., connect the device to a computer over USB, it shows up as a Mass Storage Device and drag and drop the .bin/.hex file, eject and voila! the firmware upgrade process automatically starts. How can one implement such a way to upgrade firmware on STM32 devices?

I want the user to upload firmware on my device in the simplest, easiest way. It is OK for prototyping phase to use aforementioned methods (1-4), but it is unacceptable in a real production device.

Any insight into this matter would be greatly appreciated!

Best Answer

It's possible if there is enough memory (RAM or FLASH) to be used as a backing store, holding the filesystem image consisting of filesystem structures (FAT tables, directory entries etc) and the new firmware, in addition to the flash where the firware is to be written.

Check the STM32Cube library distribution for your controller series, it might contain an example application called USB_Device/MSC_Standalone, which emulates an usb mass storage device (flash drive) using some onboard storage. If there is none for your controller, find one for a similar MCU, it's quite portable.

The first step is to adapt it to your device, to use whatever onboard storage your board has. Easiest if the firmware fits in internal or external RAM.

The memory area should contain an empty (formatted) filesystem image at the USB connect event, you can create it with FatFS.

After the USB disconnect there should be a filesystem image with the firmware in it.

Use the FatFS library to access the firmware (.bin) file stored in the image, and copy the contents to the right address in the internal flash where it can be executed.


It is also possible to turn this around (when the MCU has USB host capability), read the firmware file from an USB flash drive using FatFS, and write it directly into the internal flash without using intermediate storage. Again, there are example programs showing how to read a file from a flash drive using FatFS.

This would work with an SD card too, using SDIO (if present) or SPI interface.

Related Topic