Retaining variables with power off

I'm trying the possibility for the nRF52 in the Nano 33 BLE, to retain RAM contents even in a SYSTEMOFF condition.

However, I have a question regarding the C compiler behavior: Whenever an application starts, are the declared variables initialized to a known condition, even if you don't do it explicitly ?

If memory serves me well, then all variables that you do not explicitly initialize to a value are allocated to a section of RAM that used to be called zerovars - it probably still is!

During the very early stages of your code execution, there's a bit of code, usually hidden from you, that sets all that section of RAM to zero. That way, even if you do not explicitly set a value, the variable will be set to zero.

If course, it may all be different now!

Google "crt0"

Yes, global variables that aren't given an explicit initialisation value, are initialised to zero on startup.

If you do not want your variables initialized at startup/restart place them in the ".noinit" section...

uint8_t __attribute__ ((section (".noinit"))) flag;

Although note that if the memory location where the variable is placed by the compiler is used by your bootloader, it will get overwritten.

Do you think that by somehow inhibiting this process you will solve your first question? SRAM contents are NOT retained when power is not present. Any uninitialized variables will contain junk at startup. You will need to save your variables to EEPROM to preserve them over power outages.

pcbbc:
Do you think that by somehow inhibiting this process you will solve your first question? SRAM contents are NOT retained when power is not present. Any uninitialized variables will contain junk at startup. You will need to save your variables to EEPROM to preserve them over power outages.

Not sure if it's RAM (although it probably is), but I do know from specs and the Nordic forum, that nRF52 MCU used in the Nano 33 BLE, has the possibility to retain the contents of its 256K of RAM, when a System OFF condition. Of course that takes some power but at around 1.5 - 2 uA, I can endure it.

Sorry, that's a result of me not fully taking in the fact you were using the nRF52. My bad. It was right there in your original post, so no excuses.

I'm afraid I know next to nothing about that feature, or indeed if the ".noinit" section trick will work on the nRF52. I suppose, in the absence of any other information, you could test it?

I did some tests but it didn't worked out. And I don't know why, too many variables...

The little I could find seemed to indicate that System OFF is some very low power sleep mode. Probably very similar, if not identical to Arduino low power mode. I would have thought that the RAM would be preserved in this low power mode.

Or are you actually removing all power from the device?

My fist thought would be to save the variables to an SD card

Yes, system off is a ultra low power mode where you can choose whether or not to retain ram and how much of it.
And yes, I'm keeping power on and entering system off.