Skip to main content
Alexey Kondakov
Associate
February 1, 2017
Question

STM32F4 mass storage class with internal flash

  • February 1, 2017
  • 3 replies
  • 3814 views
Posted on February 01, 2017 at 10:10

Hi! I am using stm32f4-discovery to start working with mass storage clas and internal flash.

I have tried different ways but every time I've had the same resault. I can see storage device

in My Computer but can't use it because I can't format it. I've seen another topics with the same

problem but I have not found any solution yet. Can somebody help me?

I'd like to use the last sector of internal flash (128 Kb) to store my device's settings. There is my code in attach...and thanks a lot!

P.S. I'm sorry for my poor English.

#stm32f407-msc-format #stm32f4-discovery #msc
This topic has been closed for replies.

3 replies

MARTIN LAZZARI
Associate
April 20, 2017
Posted on April 20, 2017 at 06:13

Hi, I'm also trying to do the same, with an STM32F433. It was easy to implement the drive in RAM, just defined a static array, and memcpy'd to and from it. However, I'm having problems when trying to implement it in internal flash. Have you had any luck with this? The 'F433 has a different Flash structure (size, locations, etc) and it seems to have a different set of functions in the HAL driver for the Flash (compared to the processor in the F4 discovery). By looking at your code, something that pops up is that in your write operation you are calling

HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);

but 

EraseInitStruct

has been filled only in STORAGE_Init_FS and it is pointing to your whole Sector 11, and therefore, every time you're writing to a Flash Drive sector, you're erasing your whole user memory. The rest seems to make sense for me (although I am not yet familiar with the Flash functions in this micro)

Alexey Kondakov
Associate
April 20, 2017
Posted on April 20, 2017 at 14:35

Hi, Martin!

I' ve succeedded in programming STM with USB MSC & internal FLASH (great thanks to dang_son.hai for his help). But I've still have unsolved problem.

I can successfully create, format, write to and read from a mass storage. In my case it's a 32K mass storage located in the last 11'th FLASH sector.

 I told you I can format my mass storage. It's true. But I can do it with Windows 7 (only as quick format) and Linux (Windows XP can't format device). I don't know about Windows 7 & XP differeces so I can only conjecture.

The second problem is speed. All procedure (format, change & save data) is very slow. Format to FAT from Linux lasts about 2 minutes, from Windows lasts about 1 minutes.

Saving 1K *.txt file lasts about 30 sec. I gebugged my project and found out that saving one simbol change in *.txt file leads to calling 'STORAGE_Write_FS()' procedure in project about 10 times!

In another words it leads to reading data and erasing flash secter 10 times per one simbol! It's curiously. I don't understand why USB HAL cal my procedure many times... It waste time and demage flash memory.

Mr. Dang_son.hai supposed I was wrong with USB initial. May be. But I have workable project with mass storage & SD card. And it works better!

Martin, you wrote '...every time you're writting to a Flash Drive sector, you're erasing your whole user memory...'. Yeah. You can erase whole Flash memory or whole sector. I erase whole 11'th sector.

Tesla DeLorean
Guru
April 20, 2017
Posted on April 20, 2017 at 15:15

The PC expects to be able to write 512 byte blocks randomly, you would need to implement a caching scheme and a lazy writer so that you only do the erase/write once, or highly infrequently. The erase speed of the 128KB sectors is very slow, the smaller sectors would be faster, so if you can carve some space early in flash that would help that aspect.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Alexey Kondakov
Associate
April 20, 2017
Posted on April 20, 2017 at 14:38

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6ru&d=%2Fa%2F0X0000000bwx%2F9XOKP2OC1W.3oSumD4yHMiONBo.LlkOBEogboaoajcI&asPdf=false
MARTIN LAZZARI
Associate
April 21, 2017
Posted on April 21, 2017 at 03:08

Hi, Alexey

Thank you for sharing your code. I just checked and saw that the processor you're using does not support erasing less than 128K in the area you're using. Then erasing the whole 128k 'makes sense' since it is the only way to go. As Clive One mentioned, that's the main reason why you're getting such low speeds (erasing 128K for every sector write operation). Keep in mind that for overwriting a 1KB file, you will need at least 4 write operations (probably more). When you overwrite the file, you are actually re-writing the whole file. So at least you have to: 1) Write the first sector of the file (first 512 bytes) 2) write the 2nd sector of the file to complete the 1KB 3) update the sector that contains the directory entry with the new time stamp and a new pointer to the first FAT entry in the FAT 4) Update the FAT to point to the new sectors that the file is using. I'm probably missing some operations, but the number of writes that you see are probably due to the way FAT works, and not related to the implementation of the USB Mass Storage Device in the ST libraries. I'll make some more tries with my code to see if I can get it to work on the L433.

Tesla DeLorean
Guru
April 21, 2017
Posted on April 21, 2017 at 05:10

FAT is non-ideal, with enough RAM you can mask the issue, but here we are obviously resource constrained. A file system like UDF as used on write-once media like CD-R/DVD-R might work better, but does eat a lot in file system structures, and on flash would need some garbage collection to recover discarded blocks periodically.

I did build a MSC RAM-DRIVE using the SDRAM of an STM32F429I-DISCO

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