cancel
Showing results for 
Search instead for 
Did you mean: 

How to write on memory and keep it when my card is off power

Vince18092001
Associate II

Hi everyone ! 

 I am new to the ST Community, so please forgive me in advance if my request lacks clarity or information. I am facing an issue: I want to store a data in my memory that will be updated approximately every second or a bit more, that will be determined later. To achieve this, I am using the following function:

void WriteDataToFlash(uint32_t data) {

    HAL_FLASH_Unlock();

    FLASH_Erase_Sector(FLASH_SECTOR_7, VOLTAGE_RANGE_3);

    HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_USER_START_ADDR, data);

    HAL_FLASH_Lock();
}

It works very well, my values change as I want them to. However, the problem I am encountering is that when I turn off my board and turn it back on, or even when I debug it, my memory seems to reset, even though I don't have a function that would cause my memory to reset. I believe there's something I'm missing, and since I'm new to working with memories, I'm having trouble identifying my mistake. Just to let you know, I am coding on an STM32F439VIT6 used on hardware other than Nucleo.

 

Thank you in advance to those who take the time to read.

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

Hi @Vince18092001  Assuming that FLASH_USER_START_ADDR is correct (within FLASH_SECTOR_7) and the erase and write are successful - a likely reason is that sector erase is a lengthy action (much longer than write) so if you remove power at random times, it is very probable that power drops in the middle of erase. So the following write does not occur.  Does this seem reasonable?

If yes - can you think of the following points?

- How you can avoid erasing the flash sector before every read? Why you want to do so (besides of saving time) ?

 - How to avoid switching the power off at unfortunate moments? If this cannot be avoided, how to keep at least the previous data?

 

 

View solution in original post

8 REPLIES 8
Sarra.S
ST Employee

Hello @Vince18092001 and welcome to ST Community, 

Since flash memory is non-volatile, try reading the content of the flash memory after you have written your data first, to it to make sure that it is being stored correctly before powering down/powering up 

 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Pavel A.
Evangelist III

Hi @Vince18092001  Assuming that FLASH_USER_START_ADDR is correct (within FLASH_SECTOR_7) and the erase and write are successful - a likely reason is that sector erase is a lengthy action (much longer than write) so if you remove power at random times, it is very probable that power drops in the middle of erase. So the following write does not occur.  Does this seem reasonable?

If yes - can you think of the following points?

- How you can avoid erasing the flash sector before every read? Why you want to do so (besides of saving time) ?

 - How to avoid switching the power off at unfortunate moments? If this cannot be avoided, how to keep at least the previous data?

 

 

ONadr.1
Senior III

And be aware that FLASH only has a limited number of writes/erases. So you will destroy it rather quickly if you delete and write it every second.

Maybe it would be better to store the data in backup SRAM. It depends on the volume of data and the size of the backup SRAM for a specific MCU. 4KB is available for STM32F439VIT6.
 

I understand the issue you're raising, and it seems plausible to me. If I use my function for erasing, my data remains intact. So, it appears that I need to delve deeper into this aspect.

Thank you for your response. I am now considering trying to store data in SRAM, as I had indeed overlooked the detail that FLASH has a limited number of read-write cycles.


limited number of read-write cycles.

No, the number of read operations is not limited. It has a limited number of erase-write cycles. 

There should be a region of RAM in the BKP / RTC you can use to store information retained by a battery / super-cap

With FLASH you should consider journalling across the entire sector before erasing, finding the last value when you read it later. The erase on large sectors on F4 parts can take several seconds.

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