cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Wear Leveling for 3 Million Double Writes in Internal Flash (Keil MDK/uVision)

g__
Visitor

Hi everyone,

I'm working on an STM32f103 project using Keil MDK (uVision) and could use some advice and practical insights from those who've handled intensive flash write requirements before.

My use case:
I need to reliably store and retrieve a single `double` value (8 bytes) in the STM32's internal flash memory. The challenge is that I have to support at least 3 million write cycles to this value over the product's lifetime—well above the typical endurance of a single flash page (which is around 10,000 cycles per page on my device).

Constraints:
- No external memory: The hardware is fixed; I have to use internal flash only.
- Keil MDK: Development is in Keil uVision
- Memory available: I can dedicate about 30 of the 128 internal flash pages for this purpose, which should provide enough headroom for wear leveling.

Requirements:
- Core functions: 
- Write a double value to NV memory with wear leveling.
- Read the last stored value at startup and on demand.
- Robust wear leveling: The solution must ensure that flash wear is spread evenly, so no page exceeds its rated endurance before others are used.

My questions:
1. Are there any recommended libraries or code examples for this sort of wear-leveled, robust double-value storage in internal flash?
2. Has anyone implemented a similar solution (rolling log, circular buffer, etc.) on STM32, and can share best practices or gotchas?
3. Any tips for maximizing flash endurance and ensuring data integrity with frequent updates like this?

Thanks in advance for any advice or pointers!

 

 

5 REPLIES 5
Saket_Om
ST Employee

@g__ wrote:

My questions:
1. Are there any recommended libraries or code examples for this sort of wear-leveled, robust double-value storage in internal flash?


You can use FileX + LevelX library.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
Andrew Neil
Super User

Do you actually need to save all 3 million "writes" ?

Or could you just mostly write to RAM, and only update flash occasionally?

 

Have you looked at STSW-STM32010, EEPROM emulation in STM32F101xx and STM32F103xx microcontrollers (AN2594):

https://www.st.com/en/embedded-software/stsw-stm32010.html

 


@g__ wrote:

I need to reliably store and retrieve a single `double` value (8 bytes) in the STM32's internal flash memory. The challenge is that I have to support at least 3 million write cycles to this value over the product's lifetime—well above the typical endurance of a single flash page (which is around 10,000 cycles per page on my device).

:
- Memory available: I can dedicate about 30 of the 128 internal flash pages for this purpose, which should provide enough headroom for wear leveling.


A Flash page is 1K, I think (you didn't give a full part number).

So 128 of your values/page.

So you can write a value 128 times to a page before needing to erase.

With 30 pages, you can write 3840 times before needing to erase

So 10,000 cycles allows you over 38 million writes.

That's a bit of an over-estimate, as you'll need some housekeeping writes - but it sounds doable.

 

The STSW-STM32010 implementation has a huge overhead but, as you only have a single value, it should be easy to optimise...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

The device is doing a measure continuously. The application requires to have the latest value stored in non volatile memory. I limit the precision to one write every 3 minutes approx.

The challenge is that the device can loose it's power supply at anytime. I need to be sure the value is saved before being lost.

So in my understanding, it's one write every time I save the last value in flash.
My calculation are still valid then sadly.

The STSW-STM32010 code is only for 2 pages so I would need to modify and port from FileX + LevelX  to uVision as well.

If someone is aware of a small, simple and effective way to do such feature, it would be nice ;)


@g__ wrote:

The challenge is that the device can loose its power supply at anytime.;)


So you just need to make sure it has enough "hold-up" to be able to complete a write ...

 


@g__ wrote:

The STSW-STM32010 code is only for 2 pages ;)


Yes, but I wasn't suggesting you just use it as-is. Rather, look to see how it does it, then adapt that to your needs...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Or post in NVRAM (BKP)

One of the real problems with the STM32F1 FLASH would be the TIME and stalling related to SECTOR ERASE and WRITE

I don't think I'd complicate this with FILEX, you should just be able to journal through the available Pool of sectors, and then the periodic erase of sectors to free up resources.

I wouldn't bother with a count, as that would just consume resources quicker. You could maintain a sequence number on each sector

While I think this can be achieved with this part, there are probably BETTER choices, and THIS is the best time to make them before you commit too much to this path.

The F1 is a VERY OLD design, perhaps newer parts, with more options, lower power, and easy interface to QSPI NOR etc. 

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