Electronic – FATFS porting on STM32F103 SPI Flash

flashfreertosspistm32f10x

I have ported FATFS for Free RTOS on STM32F103 SPI Flash of 32 Mbit. In a demo Application I have successfully created a file, written a file, and read back from the file. My requirement is like I have to store multiple files (images) in SPI flash and read it back when required.

I have the following conditions/queries.

  1. I have set the Sector Size to 512 bytes, and block erase size for SPI flash is 4K. As in the SPI Flash, block needs to be erased before written. Do I need to keep track on whether a particular block is erased or not or its the file System who is managing this?

  2. How can I verify that the sector, which I am writing in erased or not? What I am currently doing is, Erase the Complete Block for the sector, which I am going to write?

  3. How can I make sure, The Block for SPI flash I am going to erase will not affect any Sector containing useful data?

Best Answer

FATFS is a bad choice to use directly on a flash chip without any intermediate translation layer, as writing the FAT will overwrite the same sector again and again, causing these cells to wear out quickly.

  1. You cannot reliably determine whether a certain block has been erased. If it reads back as only ones, the chance is fairly high that this is an empty, usable block, but it could also be a block that was in the process of being erased when the power went out, so some of the bits might be unstable. A flash file system should be aware of this, and explicitly mark blocks it has successfully erased with a specific marker.

  2. The only way to be sure is to either perform an erase immediately before, or to have a note somewhere that a certain block has been erased.

  3. You will erase the entire block, which contains 8 sectors. If any of these sectors already contains data, it will be erased as well, which you want to avoid.

As said, FAT is not suitable for use in this way.

Related Topic