cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Flash drive on USB MSC controller

JPesc
Associate II

I have a 4MB SPI Flash chip attached to an STM32F105 CPU that I would like to make into a Mass Storage Controller over USB. I made and built a Cube project.

At first, I thought I had to modify the functions in user_diskio.c. This did nothing. I then saw UM1734 and modified the corresponding usbd_storage_if functions.

Once I figured out that Windows wants 512-byte sectors, I could get Windows to see the drive. When I try to format it (FAT), Windows does its thing and then provides the unhelpful "Could not format drive" message.

I've checked my flash read/write functions several times, including code to verify every flash write by reading the sector back. My flash chip has 256-byte "pages", so I let the software treat two pages as a single "sector".

Internally, it all seems to work, but Windows won't format the drive. Is there anything I need to do besides modifying the functions in usbd_storage_if? Do I need to make changes to any of the other files that STCube put in my User file (usb_device.c, usbd.conf.c, usbd_desc.c, user_diskio.c)?

Also, if the modifications should be in usbd_storage_if.c, what is user_diskio.c for?

Documentation is either lacking or hard to find. I don't know what I need to do here.

4 REPLIES 4

Windows expects to get the same data back that it wrote, expects this to occur without errors, and that the capacity reported for the media is correct. Check the capacity first as Windows with attempt to write front and back. Instrument so you can observe it working, CRC the sectors written and read back so you can cross check in the logs you generate.

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

Ok, I've learned much since I sent this request. And yes, I did have a problem with my flash functions, but I don't know how to fix them within the limitations of the system.

My flash has 256-byte pages. I learned through experimentation that Windows doesn't like 256-byte sectors. It seems to like 512-byte sectors, so I wrote code to handle two flash pages as a single disk sector.

My flash bug was that my memory is only erasable in 16-page blocks - it is not page erasable.

What I don't know is, will Windows accept 4k sector sizes?

I made a working file system using on-chip RAM. I was able to make and use a 32kB drive with 512-byte sectors. However, any attempt to use larger sectors fails. I suspect this might be due to the small size of the drive - I can only get to about 56k on this chip.

So, if I can use 4k sectors, I should be able to do this with this flash chip. If I must use 512-byte sectors, then I will need some cheater code to handle the flash granularity.

Do you think the 4k sectors will work?

Possibly, or you can manage the deblocking yourself.

As I recall the "GET CAPACITY" request should return the size and number of blocks. 4K is more likely to work these days as many hard drives have moved to the larger units. Personally haven't tried it, I'd use eMMC over SPI FLASH.

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

I am dealing with legacy hardware and have no choice for the flash.

I rewrote the code to use 4k blocks. Now the driver is sending me calls to STORAGE_Write_FS with both blk_addr and blk_len equal to zero. So my code makes no writes and the formatting fails.

Why would it ask for writes with blk_len = 0?