cancel
Showing results for 
Search instead for 
Did you mean: 

USB Device MSC with External Flash

Shikamaru
Associate III

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

 

 

8 REPLIES 8

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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).

 

@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.  

Not detecting in Windows suggest the USB Device or descriptors aren't working.

Would suggest looking at the USB MSC Device examples under CubeF7 for EVAL and DISCO boards, and perhaps older versions that don't use an RTOS, but rather stand-alone STM32 USB implementation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Did you say, that running storage as SRAM and access it via USB MSC works?
If so, actually you use the same USB enumeration (descriptors), just change the media to QSPI flash.
USB MSC does not need a File System (FatFS) running, but it needs a properly formatted QSPI flash. USB MSC is like SCSI: it uses just sector read and write functions.

There should be a structure with function pointers for the sector read and write. Just assign those to the QSPI functions.

USB MSC will just read (and write) sectors. All the File System "interpretation" is done by the host computer. So, the host will read sectors via USB. It starts with reading the MBR, afterwards to find the FAT and then able to open a file and to read.

The QSPI must be formatted with a correct File System structure, including the MBR. Assuming USB enumeration works (not seen as an unknown USB device), if there is not a valid File System structure the host computer will not show it as an external memory device.

You can use FatFS to format QSPI, you can write a binary image via external flash loader (several sectors with the MBR, FATs) or: try to format via USB (even this one will be done by the host computer, via writing sectors).

BTW: the host computer will figure out the File System parameters like cluster size, number of clusters, start of FAT etc. via reading the MBR. If the MBR is not there or the information in it is wrong - the host will not see it as an external memory (even the USB enumeration works).

When you plug in USB as MSC - do you see the QSPI read functions called? Do they return reasonable sectors, with MBR and FAT entries?

Maybe, you should be "careful" if QSPI with DMA is used, or if QSPI is configured as memory mapped (works only for reads, for write it must be indirect, not memory mapped).

If you have data cache enabled (and DMA or memory mapped is used) - you would need cache maintenance functions (invalidate and clean), to make sure the DCache is "in sync" with the QSPI memory.


@tjaekel wrote:

Maybe, you should be "careful" if QSPI with DMA is used, or if QSPI is configured as memory mapped (works only for reads, for write it must be indirect, not memory mapped).

If you have data cache enabled (and DMA or memory mapped is used) - you would need cache maintenance functions (invalidate and clean), to make sure the DCache is "in sync" with the QSPI memory.


Yes for this reason only , i haven't enabled the DMA as well as Memory Mapped mode. QSPI is working in Indirect Mode only. Although i have enabled data cache. (faced issue while working with QSPI).

 

Did you say, that running storage as SRAM and access it via USB MSC works?

Yes it is working,  I did the same thing which you mentioned above , i kept the same USB enumeration as it is and updated the media with QSPI Mapped function. 

The QSPI must be formatted with a correct File System structure, including the MBR. Assuming USB enumeration works (not seen as an unknown USB device), if there is not a valid File System structure the host computer will not show it as an external memory device.

Even i am also assuming formatting the flash might have causing the issue, as of now i am formatting it with STMCube programmer , however i will try the methods of formatting as you mentioned above and let you know the result.  Thanks @tjaekel 

 

Shikamaru
Associate III

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.