Skip to main content
Explorer II
June 23, 2025
Question

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

  • June 23, 2025
  • 2 replies
  • 858 views

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!

 

 

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    June 23, 2025

    @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.

    Super User
    June 23, 2025

    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...

    g__Author
    Explorer II
    June 23, 2025

    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 ;)

    Super User
    June 23, 2025

    @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...