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
Pavel A.
Evangelist III

As a good example see the Silicon Labs "Persistent Store" API, it uses a similar approach.

https://www.silabs.com/documents/login/reference-manuals/Bluetooth_Smart_Software-BLE-1.6-API-RM.pdf from page 165.

It shouldn't be too hard to copy it. Key points - it allows to store limited number of pieces of limited size (up to 64 bytes) and automatically manages updating of the pieces and garbage collection.

-- pa

Arman Ilmak
Senior

I read the example provided for STM32F4-Discovery board.

There are some parts that I don't understand.

https://github.com/PaxInstruments/STM32CubeF4/blob/master/Projects/STM32F4-Discovery/Applications/EEPROM/EEPROM_Emulation/Src/main.c

In this example there are three variables:

uint16_t VirtAddVarTab[NB_OF_VAR] = {0x5555, 0x6666, 0x7777};

uint16_t VarDataTab[NB_OF_VAR] = {0, 0, 0};

uint16_t VarValue,VarDataTmp = 0;

The first one is the virtual address of 3 variables. When each word (32bit) of the flash memory has a specific address why do we have to define virtual address? I don't even know what is a virtual address?

The other variables I don't know is the VarDataTab and VarDataTmp ?What are Tab and Tmp stands for?

If you could use FLASH you wouldn't use EEPROM Emulation.

The emulation implements a journalling scheme, ie keeps writing in a circle, erasing one of the ping-pong buffers only as necessary.

It doesn't use physically addressing, but looks to a matching tag, the tag in this case is the Virtual Address that you're pretending to write a value too. When you do a read request it goes and grabs the most current tag from the journalled space.

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

OK thanks.

So if the virtual address is something symbolic ,it won't be important what virtual address we assign to each variable,will it?I mean the virtual address could be 0x0000 or 0x1256 or ... .The example has assigned 0x5555 and 0x6666 just for the ease of use ?

Correct

The example is purely illustrative, the patterns work well if you inspect the memory space.

My preference is to create a structure of values, with a CRC, and journal that across a FLASH sector.

I'd be wary of writing to the "EEPROM" in a continuous fashion, as the FLASH does have a finite life and gets slower over time.

For things with continuous requirements use on-board NVRAM/BKPRAM, or place an external EEPROM device, or external RTC with storage.

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

I downloaded the code sample for stm32f4 EEPROM emulation and used it with my stm32f217 MCU and it worked properly and I was so happy.But I added the same files to the stm32f0 project and there was a lot of errors :sad_but_relieved_face: .

I searched and I found EEPROM .c in st.com for stm32f0 and I was happy again, so I added the c file and oops lots of errors again.I found the the file is not generated for HAL drivers. So I tried just adding some variables and functions from flash.c to HAHAL_FLASH.c errors are gone, but the function write is not working. Just read function is working.

Can someone help me? I do not have time to start learning CMSIS. I can work with the CMSIS functions, but I don't know how to initialize the clock and other basic configurations that CUBE did it for me.

Arman Ilmak
Senior

eepromstexample is the st using CMSIS and the eepromemulation2 is my cube created project and the changes are in the stm32f0xx_HAL_Flash.c and stm32f0xx_HAL_Flash.h.

I coppied the FLASH_PageErase from stm32f0xx_Flash.c to stm32f0xx_HAL_Flash.c.And copied

typedef enum

{

 FLASH_BUSY = 1,

 FLASH_ERROR_WRP,

 FLASH_ERROR_PROGRAM,

 FLASH_COMPLETE,

 FLASH_TIMEOUT

}FLASH_Status;

from stm32f0xx_Flash.h to stm32f0xx_HAL_Flash.h.

Arman Ilmak
Senior