cancel
Showing results for 
Search instead for 
Did you mean: 

EEPROM emulation in stm32f030

Arman Ilmak
Senior

Hello guys.

I have two int variables in my application and need to save them. Somebody told me that you can do this by software that is called EEPROM emulation. I searched what it is and I'm confused.

Can anyone help me do it step by step? (If you can tell me how to do it with HAL drivers I would appreciate that.)

17 REPLIES 17
Danish1
Lead II

By save them, do you mean that you want to remember their values over things like reset and loss-of-power?

Most (all?) stm32 do not have any EEPROM memory. But they do have FLASH memory.

EEPROM memory would allow you to simply write the value into the memory when you need to update it, and to read it back as and when you want.

FLASH memory allows you to read it back as and when you want, but programming is more complicated. You can only write individual bits from 1 to 0. In order to return bits from 0 to 1 you need to do an erase, which can only be done on a whole page (perhaps 16 kbytes) at a time. What's more, erasure can only be done a relative low number of times (perhaps 1000 or 10000) before the memory starts getting unreliable.

ST Application Notes AN3969 and AN4894 (and others, depending on which stm32 family) show a technique where you can store a sequence of address-value pairs in FLASH memory, so the need to erase is minimised, and you can read out the most-recently-written value.

Also, during programming or erasure, it is impossible to read any other FLASH memory, so program execution will freeze for the time the write/erase might take to happen. This could be a few milliseconds for a write, rising to a significant fraction of a second for an erase (a lifetime in microcontroller terms!).

From what I can see, examples are available for most ST demo boards from stm32cube.

Hope this helps,

Danish

Arman Ilmak
Senior

"By save them, do you mean that you want to remember their values over things like reset and loss-of-power?"Yeah I mean this exactly.

You said erase can be done to the hole page (16 KBytes)?The hole stm32f030f4 Flash memory is 16 KBytes. So I can't do the EEPROM emulation with this device or the pages can be 4 kBytes or others?

One other question is that I don't know what is page?

Arman Ilmak
Senior

"By save them, do you mean that you want to remember their values over things like reset and loss-of-power?"Yeah I mean this exactly.

You said erase can be done to the hole page (16 KBytes)?The hole stm32f030f4 Flash memory is 16 KBytes. So I can't do the EEPROM emulation with this device or the pages can be 4 kBytes or others?

One other question is that I don't know what is page?

Arman Ilmak
Senior

"By save them, do you mean that you want to remember their values over things like reset and loss-of-power?"Yeah I mean this exactly.

You said erase can be done to the hole page (16 KBytes)?The hole stm32f030f4 Flash memory is 16 KBytes. So I can't do the EEPROM emulation with this device or the pages can be 4 kBytes or others?

One other question is that I don't know what is page?

Page is a block/group of memory, smallest erase unit size in this case.

From Data Sheet

"Page size is 1KB for STM32F030x4/6/8 devices and 2KB for STM32F030xC devices"

The EEPROM emulation typically journals data across two flash pages to provide appearance of having EEPROM functionality. Ideally you could just understand how to read/write/erase FLASH and dispense with pretending you have EEPROM.

For things like configuration parameters , written a handful of times, one FLASH sector, or couple of bytes would suffice to store the settings. Permanent stuff like serial numbers or keys could be written to OTP once.

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

For just two int values, you can use RTC backup registers, if your chip has RTC.

A demo for this is available in CubeMX repository.

-- pa

Danish1
Lead II

A note of caution on what Pavel A. says about using the RTC backup registers:

These are only retained as long as there is sufficient voltage on VBat, coming from a backup battery (or e.g. supercapacitor) that you provide, outside the microcontroller.

If your board doesn't have this, or the power-outage is so long that this discharges, then any values you wrote to the RTC backup registers will be lost.

Arman Ilmak
Senior

I read the AN2594 that described emulation and I don't understand something.

What is the difference between the sector and page?

Why we have to use two pages in emulation?

Where the main code is programmed(I mean if I don't know where the main code is programmed, then I'm not able to understand which page is empty to use it as EEPROM).

Likely two pages to have the ability to erase one without loosing your content. It would be a journalling/management thing.

The .MAP describes where your code is extending too. The linker script or scatter file can describe to the linker what memory is available to use. If you want to stop the linker using your two pages, shrink the size of the FLASH reported to it, carving out space at the top/end of memory.

The "page" size in the part is 1 or 2KB, as established by the data sheet for the specific part.

>>What is the difference between the sector and page? Why we have to use two pages in emulation?

You're over thinking this. sector/block/page tend to be used somewhat interchangeably, think of it as the smallest erasable unit within the array.

If you used one page there would be a window where you erase the page, holding data in RAM, when the power could fail, or you reset the device, and your data would be lost. Using two allows you to hold the data safely while you erase. Basically when both pages fill, and you want to write more, it will erase the older of the two pages.

My personal view is that you should just understand how FLASH functions, and write your storage structure across the page(s)

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