Skip to main content
Graduate II
May 14, 2024
Question

USB Device MSC with External Flash

  • May 14, 2024
  • 3 replies
  • 5494 views

Hi team,

            I am working on stm32F7 discovery board, I am working on USB MSC Device however i am not getting , how to configure external flash of this discovery board as Mass Storage. My main goal here is as soon as i connect the controller to PC, the external flash has to detect as mass storage device (predefined size).

 

 

regards,

Shikamaru

 

 

    This topic has been closed for replies.

    3 replies

    Graduate II
    May 14, 2024

    Probably want to use 4KB sectors report capacity in that form. You can preformat the space using FATFS f_mkfs(), which you presumably use on the STM32 side to access files later.

    As MSC you'll need to furnish the read/write block routines to map to read and erase/write* routines on the QSPI memory. I'd use the direct command methods, memory-mapped is of little use.

    There's probably board examples of how to make USB MSC SDRAM-DRIVE, see how those map

     

    * 4KB block erase, wait for completion, then write 4K as multiple 256-byte pages, waiting for each to complete. NOR Flash is not a fast medium for writes.

    Visitor II
    May 15, 2024

    Yes, try to use SRAM (or SDRAM, if available and configured) as the storage.
    USB MSC works this way:

    • it assumes that the storage media is formatted (e.g. as FAT32): there is a MBR, FAT table etc.
      Without having a formatted media - USB MSC cannot work
    • USB MSC is like SCSI, sending commands to read sectors (512Bytes):
      USB MSC reads the entire FAT, directory structure itself: it does not need a FatFS, just a sector based (512Bytes) device and transfer. USB MSC interprets the formatting itself (therefore it must be formatted properly first)

    There are also examples how to use an SD Card as storage media.
    You can debug via trace or breakpoints what USB MSC tries to read (which sector, what is the next sector it wants to read, is the data from reading a sector "reasonable", e.g. is it part of a FAT, ...).

    If anywhere it goes wrong, e.g. the Host (doing all these USB MSC commands) sees a discrepancy in the "format" (e.g. not a valid MBR, not a valid FAT record), it will stop working.

    You have to bind the USB MSC "primitives" (e.g. reading a sector X, as 512bytes) to your external flash "read a sector". And you must "format" this external flash first (how: no idea, with your own code in FW).

    Writing is a bit more tricky: you have to flash a sector (512Bytes) in external flash. But this chip has a "page size", e.g. every "page" in flash chip is 4KB. So, you need a caching for writing (Read-Modify-Write). And often you have to erase a page first (e.g. 4K) before you write all back (with updating a 512KB "sector" in a page).

    There are some implementations for using a File System on Flash devices, e.g. Azure RTOS LevelX NAND.
    But not a problem to implement yourself, if you know what the relation between "sectors" (on USB MSC) and "pages" in a flash chips is (and how much to cache/buffer to translate and handle "chunks of data" properly).

     

    ShikamaruAuthor
    Graduate II
    June 7, 2024

    @tjaekel  adn @Tesla DeLorean ,

                                   Sorry for delayed response. As per your suggestion i tried USB MSC on SRAM, it is straight forward and i could play with it by allocating some 100KB of space. It is working fine. 

                                  Now I try to do the same thing with External flash (MX25L51245G which is of 512 Mb). This flash is communicating through QSPI. Before I started with USB , tried QSPI communication on board standalone. It is also working fine. 

    #define STORAGE_LUN_NBR 1
    #define STORAGE_BLK_NBR 16384 //total no of sectors present
    #define STORAGE_BLK_SIZ 4096 // sector size is 4K

    This snippet is from usbd_storage_if.c file, here for STORAGE_BLK_NBK  is 16384(number of sectors) and BLK_SIZ is 1096 (sector size).  Even i tried the smaller size as i did for RAM (100KB) still no luck.

    sector_size.png

    The above pic is from datasheet. And mapping of QSPI API's with Storage Class i have done. However, it is not detecting in windows, couldn't see the disk there. Kindly guide me here.

     

    Note : i am manually formatting the disk space through STM Programmer before flashing the code.  

    ShikamaruAuthor
    Graduate II
    June 12, 2024

    Hi @tjaekel , @Tesla DeLorean ,

                         I can see my external flash in windows like below image,

    Shikamaru_0-1718200969973.png

    before accessing it windows asks for formatting the disk. as soon as i hit format , it will hit STORAGE_Write_FS() (while debugging) this function  contains mapped QSPI  functions are there. I could see erase and write both are successful. However windows is not able to format it completely. It will through the error like "Windows was unable to format the disk". I am not getting the actual cause for the failure. 

    Shikamaru_1-1718201002700.png

    Note : FATfs is not yet enabled in firmware. I am formatting the external flash in STM Cube programmer. 

     

    Explorer II
    July 7, 2025

    Hi @Shikamaru,
    did you find a solution on this task?

     

    I reached the same point of problem like you and i dont know how i should solve it.
    Are there any examples with QSPI in Indrect Mode with USB Device Mass Storage Class?
    After two weeks of  try, error and searching, it only worked with the SRAM over memcpy().

    But i would like to Storage Data directly over USB into the QSPI Flash and not over SRAM.
    Also i could format the QSPI Flash with the indirect mode with FAT.

    Maybe you have some suggestions?

     

    I look forward to your reply

    RSheetz

    Explorer
    August 25, 2025

    Hi @Shikamaru and @RSheetz 

    Did you ever find a solution to this?  I have got to the same point as you both and am confused why it's not working. 

     

    Kind Regards

    Andrew

    Visitor II
    August 25, 2025

    OK, you want to see your STM32 board behaving like a USB memory device?

    First of all: do you have memory (RAM) on STM32 board which you want to see on PC host?

    It works this way:

    You configure and build you STM32 board FW so that it will setup an USB device USB memory (USB class MSC).
    You have to add this USB stack as MSC device to your STM32 FW project.

    This USB MSC works like a SCSI device: it reads the memory via low level primitives like reading and writing sectors on a memory device. The filesystem "interpretation" is done by the host (PC).

    You have to implement the read and write functions for sectors on STM32 board, e.g. using internal SRAM as the storage device.
    When the USB stack as MSC device on STM32 is running - the PC will see it as an external USB memory device.
    It can format, write and read your STM32 like a USB stick.

    If you use on STM32 board an external flash (e.g. eMMC), you have to provide the read, write sector functions which goes to you eMMC memory. Bear in mind: if it is a flash (e.g. eMMC), for a write, you have to erase a sector first, whereby the write data is 'cached' (stored somewhere), after erasing this sector you can write the sector.

    So, it means:

    • besides adding USB FW stack as MSC to your STM322 project
    • you have to provide also the correct Read, Write functions for handling sectors
    • on flash devices - it is a bit more 'complicated' because you have to erase sectors first before you can write a new sector
    • better, to consider to use a STM32 board which supports also your FW as Azure OS: it comes also with support for flash devices, filesystems on flash memory

    BTW: just to connect STM32 board via USB and the 'wish' to see it as memory device does NOT work without an appropriate FW for the board built and running (USB MSC device, mapping the SCSI commands to a physical memory).

    It is possible (I did it several times on different STM32 boards). It just needs to get familiar how to add USB MSC Device to your FW project.

    Visitor II
    August 25, 2025

    BTW: if you are asking how to flash an external flash memory connected on a STM32 board (not to see it as USB memory device, but to flash the content of this memory)...

    check out how to install/enable 'external loaders' in STM32 debugger tool (e.g. STM32 Programmer or STM32 ST-Link Utility): it is possible to add and enable an external loader for a flash memory device connected on a STM32 board: just make sure you use a flash device which is supported by the STM32 Programming/Debug tool (you have to specify which loader for which device).

    This can see your flash memory device, read and write the content of it. But it does not behave like a USB stick ('just' possible to write content also for this flash memory, not just the STM32 MCU internal flash itself).

    ==> so, what do you want to implement/accomplish? (USB memory device seen on host PC or flashing memory connected on STM32 board?)