Grant Goldsmith
Grant Goldsmith

Reputation: 1

Storing and writing to text file in STM32 F4 internal flash memory

I need to store a text file into the STM32 F446RE internal flash memory.  This text file will contain log data that needs to be written to and updated consistently.  I know there are a couple ways of writing to it including embedding the text as constant string/data into the source code or implementing a file system like fatfs (Not suitable for STM32 F4 flash due to its sector orientation). It has total 7 sectors that vary in size.  Sectors 0-3 each contain 16 kB, 4 contains 64 kB, and 5-7 each contain 128 kB.  This translates to a total of 512 kB of Flash memory.   These are are not sufficient for what I'm looking for, and was wondering if anyone has ideas?  I'm using the STM32CubeIDE. 

Upvotes: 0

Views: 2333

Answers (1)

JaffaMicrobrain
JaffaMicrobrain

Reputation: 71

Writing to FLASH memory requires an erase operation first. It seems that you already know that erase operations must be performed on whole sectors. Note also that FLASH memory wears out with repeated erase/write cycles.

I suggest one of three approaches depending upon how much data you must store and your coding abilities.

An "in-chip" approach is to implement a circular buffer in RAM and maintain your log there. If power is lost then you need code to commit that RAM buffer to FLASH. On power-up you need code to restore the RAM buffer from FLASH. This implies that your design does not suffer frequent power cycles and that you can maintain power to the microcontroller long enough to save the buffer from RAM to FLASH.

Next option is to use an external memory chip. EEPROMs are not terribly fast and are also subject to wear. FRAM is fast and suffers trillions of writes before wear issues. It is available as I2C or SPI so you can implement a number of chips to provide a reasonable buffer size and handle the chip to memory mapping in your handler code. FRAM is not cheap though.

Finally, there is an option to add an SSD drive. These devices include "wear levelling" to maintain their active lives. However, you need a suitable interface such as USB or PCI.

HTH

Upvotes: 0

Related Questions