Retained variables during code flash

I am experimenting with using retained variables in my code to help the system recover from unexpected resets and crashes.

From what I have read in the forums, the SRAM is maintained during code flashes, but I’m wondering what happens if the new code being flashed has different retained variables than the code currently on the device. If I add, remove or reorder retained variables in my code, will the variables read from the wrong location in SRAM and cause problems?

Yes, you can run into problems when code changes the layout of retained variables.

I prefer to store the retained data in a struct, with a known identifier (magic bytes) and a version, to help prevent and recover from these situations.

Thanks for the quick response.

Is there an easy way to detect when new code is flashed to the device so I can reinitialize the retained variables? Saving a know value to EEPROM jumps to mind, but I’m wondering if there is some flag or function to check this.

Below is a link to a post by @rickkas7 about storing retained data in a struct.

There is an example of using a known identifier (magic bytes) stored within the struct as version control.

https://community.particle.io/t/reset-retained-variables-w-o-power-cycle/54253/6

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.