Skip to main content
Visitor II
September 6, 2018
Question

Firmware Upgrade using USB Key - How to get started? (CubeMX / F4Discovery / DFU?)

  • September 6, 2018
  • 14 replies
  • 7614 views

Setup:

(custom hardware based on STM32f373 with a USB A female port)

STM32F407 Discovery Board with additional micro-B to A cable for the USB drive

Keil uVision 5 and STM32CubeMX

What I want to do:

Plug in a USB thumb drive into the USB port to upgrade the firmware on the STM32f373.

This way, the STM32 acts as a USB host and pulls the firmware from the USB drive. I don't want to use a computer to program the microcontroller (using DfuSe).

I am using the STM32F4 discovery with a micro-B to A cable to test this feature before moving to my custom hardware.

What I found out so far:

  • DFU mode is only accessible in device mode (not host mode) in STM32CubeMX
  • There is AN3990 "Upgrading STM32F4DISCOVERY board firmware using a USB key"

My questions:

  • Where are the source code files referenced in AN3990?
  • Is it true that DFU only works with the microcontroller as a slave?
  • Are there other examples that show how to do firmware upgrades from a USB drive?

    This topic has been closed for replies.

    14 replies

    Super User
    September 6, 2018

    > Where are the source code files referenced in AN3990?

    [en.stsw-stm32068.zip]\STM32F4-Discovery_FW_V1.1.0\Project\FW_upgrade\

    https://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-library-expansion/stsw-stm32068.html

    Note that it's based on the older SPL "library", not on Cube.

    > Is it true that DFU only works with the microcontroller as a slave?

    Yes.

    JW

    Graduate II
    September 6, 2018

    MSC != DFU

    The MSC Host would permit you to read a DFU file off the media, and act on it. Read the docs on the .DFU format. Done it with DFU files on MicroSD cards here.

    HAL Example of reading from USB Flash Stick

    STM32Cube_FW_F4_V1.21.0\Projects\STM32F4-Discovery\Applications\FatFs\FatFs_USBDisk\readme.txt

    Graduate II
    September 6, 2018

    All examples I see for F3 are Device, not Host. Check if it supports Host/OTG operation at all.

    Super User
    September 6, 2018

    > The MSC Host would permit you to read a DFU file off the media, and act on it.

    What's the point, why would be the DFU file format more appropriate than e.g. intelhex?

    JW

    Graduate II
    September 6, 2018

    That I don't have to provide something in multiple formats? DFU also has a longitudinal CRC check, integrity can check before starting, and the binary data is easy to manage, addressed, no requirement to parse thousands of lines, with memory that my be sparse and out-of-order.

    I could use a ZIP file if I wanted too, got code for that.

    Super User
    September 6, 2018

    > That I don't have to provide something in multiple formats?

    Yes that might be a point in certain cases. The rest is mostly fulfilled by the more common hex or sfile.

    JW

    Super User
    September 6, 2018

    With Keil, you can easily get a .bin file.

    Enter the following in the project settings, post-make command:

    $K\ARM\ARMCC\bin\fromelf.exe --bin --output=$L@L.bin !L

    The output file is a plain binary image that begins from the starting flash address in project settings.

    A good smart student in our lab did this sort of updater for F4 recently, based on that AN3990 example.

    He also produced a python script that appends a header to that binary, with a CRC, version and so on (the updater discards this header after checking the update file).

    Then it took me some time to convert it to a normal CubeMX project & USB host & fatfs from the Cube repo.

    -- pa

    MnemocronAuthor
    Visitor II
    September 7, 2018

    > HAL Example of reading from USB Flash Stick

    > STM32Cube_FW_F4_V1.21.0\Projects\STM32F4-Discovery\Applications\FatFs\FatFs_USBDisk\readme.txt

    Where is this file? I am only able to find cloned stuff on some people's Github accounts.

    Super User
    September 7, 2018

    In your CubeMX repository.

    JW

    MnemocronAuthor
    Visitor II
    September 7, 2018

    Thanks JW I found the repository.

    > [en.stsw-stm32068.zip]\STM32F4-Discovery_FW_V1.1.0\Project\FW_upgrade\

    This example code does not compile with my MDK v5 because of missing files and an aborted migration from v4 to v5.

    Followup question

    How do I apply the code from the FatFs_USBDisk example into my own project?

    From what I see, the file structure is different (CubeMX vs. FatFs_USBDisk example).

    Simply copying the main code from the FatFs_USBDisk example

    /*##-1- Link the USB Host disk I/O driver ##################################*/
    if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
     {
     
    // code goes on till the empty endless loop

    into the /* USER CODE BEGIN 2 */ section of my newly generated Project from the CubeMX yields 4 errors of the type:

    ../Src/main.c(116): error: #20: identifier "USBDISKPath" is undefined
     if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
    ../Src/main.c(119): error: #20: identifier "hUsbHostFS" is undefined
     USBH_Init(&
    USB_Host, USBH_UserProcess, 0);
    ../Src/main.c(119): error: #20: identifier "USBH_UserProcess" is undefined
     USBH_Init(&
    USB_Host, USBH_UserProcess, 0);
    ../Src/main.c(134): error: #20: identifier "Appli_state" is undefined
     switch(Appli_state)

    Clearly I have never worked with USB before. For the start I would just like to replicate what the example does while understanding how it does it. Simple operations like creating and writing a file.

    What are the easiest and the recommended steps to learn how to correctly implement a simple file operation on an STM32 connected to a USB drive using CubeMX and MDK-ARM v5 ?

    On one hand I don't feel like reading a ton of specifications and application notes without a (working/applicable*) practical example. On the other hand I want to understand the important parts and do a proper implementation.

    (* the current example from the CubeMX repository does not look applicable to me because the projects file structure and code differs from the code generated by the CubeMX)