Skip to main content
Associate III
October 10, 2024
Solved

Want to detect the number of times a switch value is used through stm32

  • October 10, 2024
  • 3 replies
  • 2214 views

Hi 

Because power-off protection is required, the times or variables need to be stored in flash。
Is my solution reasonable?

Create a space inside the flash to define variables, and read the data after powering on the computer. Then there is a problem with data accumulation , inside the my program the define variables be accumulated, because the data is stored inside the flash. Does the change of data require special processing? HAL_FLASH_Unlock()
How to define variables inside flash, please give a program example。

Thank you

 

Best answer by Tesla DeLorean

NVRAM / BKPRAM might be more practical. 

FLASH sectors on the F4 are quite large and time consuming to erase

 You'd need to carve holes to use the smaller 16KB sectors.

You can journal writes across the sector, and the enumerate at next startup to find the last saved data.

3 replies

Andrew Neil
Super User
October 10, 2024
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.
zhaowhongAuthor
Associate III
October 10, 2024

To add, this variable of mine changes while the program is running. and needs to be stored in flash

Andrew Neil
Super User
October 10, 2024

Yes, that is addressed in the threads I linked.

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.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
October 10, 2024

NVRAM / BKPRAM might be more practical. 

FLASH sectors on the F4 are quite large and time consuming to erase

 You'd need to carve holes to use the smaller 16KB sectors.

You can journal writes across the sector, and the enumerate at next startup to find the last saved data.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
zhaowhongAuthor
Associate III
October 11, 2024

YES  

I read some documents that must be erased when writing. I originally planned to use RAM variables to record. And wanted the CPU to write to FLASH before powering off, but the demand is not sure when to power off, and powering off is uncontrollable.

It seems that methods such as external feram are better。

Andrew Neil
Super User
October 11, 2024

You haven't said what STM32 you're using.

Some have the facility for battery-backup of a portion of RAM...

 


@zhaowhong wrote:

, but the demand is not sure when to power off, and powering off is uncontrollable.


So what you'd need is

  1. A signal (interrupt?) to tell you when the power had been turned off, and
  2. Enough backup/reserve power to be able to complete a flash write.

 

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.