cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L073 store data after power off

waiyang93
Associate II
Posted on July 12, 2016 at 11:19

Hi,

I am working with an external flash and I wanted to store the lastest memory address after the power cut off and fetch back the stored memory address when power on. So, how should I do it ?  Is it done by write into the EEPROM of the chip or is there any ways to do it?

Is there any EEPROM example code provided by ST? Because I am using STM32CubeL0 to learn, but I did not find any example code related to EEPROM.
12 REPLIES 12
slimen
Senior
Posted on July 12, 2016 at 12:26

Hi,

You can refer to the EEPROM examples in other firmware package which can help you as an implementation example and get inspiration to achieve you goal.

For example, you find under the 

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubel4.html

package an EEPROM application:

STM32Cube_FW_L4_V1.5.0\Projects\STM32L476G_EVAL\Applications\EEPROM\EEPROM_Emulation

You can take this example as start point to make required updates in the project generated and look at this manual 

http://www.st.com/content/ccc/resource/technical/document/reference_manual/21/bd/0f/bd/1c/88/40/f0/DM00108282.pdf/files/DM00108282.pdf/jcr:content/translations/en.DM00108282.pdf

 in the section 3 ''Flash program memory and data EEPROM (FLASH) '' and code examples.

Regards

matic
Associate III
Posted on July 12, 2016 at 20:38

You can enable PVD (Programmable Voltage Detector) and set it to the highest value - 2.9 V. At power off, when Vdd drops below 2.9 V, PVD interrupt occurs and then you have still some time to program a half-word or two into the embedded flash. You have time until Vdd drops below 2 V. If you want to extend this time, you could also give one larger capacitor (e.g. 100 uF) to the power supply. 

waiyang93
Associate II
Posted on July 13, 2016 at 06:03

Thanks for the reply. I found the eeprom function inside the STM32CubeL0 hal_flash_ex.c file but without the read function.For now I need to figure out how to read.

Besides, is there any method to let the code know where was the lastest eeprom address to read when restart?

waiyang93
Associate II
Posted on July 13, 2016 at 06:07

Thanks for reply. Is the whole process can complete writing before the power totally cut off? For now I still working on write and read on the eeprom and probably with a push button to save before power off. Will try this out afterward. Thanks for the suggestions!

matic
Associate III
Posted on July 13, 2016 at 06:37

Yes, it works. I used that. But it works for small amount of data (maybe up to few programmings of 16-bits), not for the whole page. Also, page should be already erased (the cell where you want to write data should be erased). I work with custom functions (directly with registers), don't use SPL or HAL functions, because they are often too large.

You have to find your last written data in a certain page after power on. This is the last not equal to 0xFFFF. So, after next power off you already know the address where to store data - on the first address in the page which is equal to 0xFFFF.

waiyang93
Associate II
Posted on July 13, 2016 at 07:03

I see. But I did not understand how did u find the last written data by looking at last not equal to 0xFFFF. Do you mean using a for-loop to check all the eeprom address data during the power on until it find out the unwritten slot?

matic
Associate III
Posted on July 13, 2016 at 19:52

Yes. If a page of flash has 2048 bytes, then you have to check 1024 half-words from the start forward. But there is a quicker way -->

https://en.wikipedia.org/wiki/Bisection_method

 

You simply check the element in the middle od a page. If this is 0xFFFF (erased), then you know you have to look only to the first half of a page. And so on...

Posted on July 14, 2016 at 01:02

I think the mindset with the L series parts is that they are constantly powered by a battery, or if you have a power supply you fork that with a battery and a pair of diodes, and recognize when the primary power is removed and enter a very low power state where you can hold values in RAM for prolonged periods.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waiyang93
Associate II
Posted on July 14, 2016 at 04:20

I think the check half method, it is fast and easy to implement for me. But another question will be when will you erase the old data?

The next thing is currently I will record down some data into an external Flash and to access back the last written address in Flash after restart,I will store the last Flash address in eeprom and do the empty check to find back the last written Flash address. Is this the correct way to doing it?Because directly do a for-loop check in the Flash is very time consuming if the last written address is at the very last address.