Skip to main content
Stastny.Petr
Associate III
June 7, 2022
Question

I have STM32G030J6 with 32kB Flash and I need emulate EEPROM. Is it possible?

  • June 7, 2022
  • 11 replies
  • 3762 views

I need permanently save configuration data (several bytes only) in STM32G030J6 in SO8N package. I tried to use EEPROM emulation SW but not working - very robust and complicated. Is there some easy way how to do this? There is no other EEPROM and no possible to connect.

This topic has been closed for replies.

11 replies

Andrew Neil
Super User
June 7, 2022

"I tried to use EEPROM emulation SW but not working"

So what, exactly, did you try; and how was it "not working" ?

What debugging have you done to find the problem(s)?

"very robust and complicated. Is there some easy way how to do this?"

Likely this is a case of, "do you want it robust, or do you want it easy?"

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.
waclawek.jan
Super User
June 7, 2022

The FLASH chapter in RM describes the procedures for erasing/programming FLASH, so you can write your own implementation to fit your purposes.

JW

Andrew Neil
Super User
June 7, 2022

... and make your own tradeoffs between "robust" and "easy" ...

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.
Stastny.Petr
Associate III
June 7, 2022

OK, I checked RM. I was not able to port the example from X-CUBE-EEPROM to my CPU. But I found that there are some option bytes and 2 bytes of "user data"... If 2 bytes are enough for me, is it possible to use this easily as data storage? Or these option bytes are used for something different? Thank you

waclawek.jan
Super User
June 7, 2022

I don't think in 'G030 there are any option bytes free for user usage.

JW

Stastny.Petr
Associate III
June 7, 2022

You are right. Anyway: Bits 7:0 RDP[7:0]: Read protection level

0xAA: Level 0, read protection not active

0xCC: Level 2, chip read protection active

Others: Level 1, memories read protection active

Means that at least this Byte would be possible to use. I do not need any protection so is this possible to use for storage?

Peter BENSCH
Technical Moderator
June 7, 2022

It does not matter if you need Read Protection - RDP is read by the hardware after every reset and leads to the described effect. So if you e.g. write 0xCC in there and perform a reset, you can no longer access the device, it's a one-way street. As @Community member​ already wrote, there are no free bytes for the user in the flash option bytes.

Regards

/Peter

Stastny.Petr
Associate III
June 7, 2022

So the only way is to emulate EEPROM with Flash? Or is some other small space there?

Peter BENSCH
Technical Moderator
June 7, 2022

Apart from the flash, there is no non-volatile area in the STM32G030 that the user could use as EEPROM.

In the LQFP48 package you would have 20 bytes of backup registers that could be buffered with e.g. a battery or capacitor to VBAT, but in the smaller packages VBAT is hardwired to VDD.

Regards

/Peter

Stastny.Petr
Associate III
June 13, 2022

I found a solution - I can use:

const uint32_t variable_X = 0xABCDDCBA;

But is it possible to say the compiler where should be this constant located? I do not need exact location, I only need to keep always the same the location in Flash when I make some pogram changes and recompile.

0693W00000NrhfZQAR.png 

waclawek.jan
Super User
June 13, 2022

> But is it possible to say the compiler where should be this constant located?

This is toolchain-dependent. If you use gcc-based toolchain, you should modify the linker script to include a custom section placed at a particular address, and then place the variable in question into that section.

I'm not sure whether there is some simple example out there, but this is a recurring question in this forum so you may want to search here.

JW

ne562
Associate III
June 19, 2022

For EEPROM emulation use array of structs, mapped to last FLASH page.

On power-on find last non-empty record (not equal to 0xFF), on every write increment record index, after last - erase whole page, and start from beginning.

According to my tests real number of page write cycles is 200000..400000 for STM32F030, so if record size is 20 bytes, you can get real 10..20 mln cycles - even better than many external EEPROMs...