Possibility to leave variables uninitialized on boot / reset

I’m trying if I can have some variables values unaffected during a reset (but always powered), using the noinit “directive”. So far no luck. Board is a Nano 33 BLE and the code is below:

int __attribute__ ((section (".noinit"))) i;
int j;

void setup() {

  i++;
  pinMode(LED_PWR, OUTPUT);

}

void loop() {

  for (j = 0; j < i; j++) {
    digitalWrite(LED_PWR, HIGH);
    delay(200);
    digitalWrite(LED_PWR, LOW);
    delay(200);
  }

  delay(3000);
}

Any ideas ?

Any ideas ?

Don't expect RAM content to survive a hardware reset. Write values you need after a reset to non-volatile memory.

How do you know it doesn't work? I see no output or other reporting of variable 'i' in your test sketch.

aarg:
How do you know it doesn’t work? I see no output or other reporting of variable ‘i’ in your test sketch.

No need. The led blinks according to the value of the variable

Bootloader is overwriting it.

This doesn't work for me...

int __attribute__ ((section (".noinit"))) i[0];

void setup()
{
  Serial.begin(57600);
  Serial.println(i[0]);
  i[0]++;
}

void loop()
{
}

This does...

int __attribute__ ((section (".noinit"))) i[100];

void setup()
{
  Serial.begin(57600);
  Serial.println(i[99]);
  i[99]++;
}

void loop()
{
}

YMMV depending on which bootloader you are using (and how many global variables it uses and where it places them in RAM) and your device. Tested with Pro Mini 168 and whatever bootloader it came with.

What is actually resetting the device? Why reset the device at all? If you want to restart your program and count the number of resets, just create your own setup() and loop() aliases inside loop() and assign a GPIO to be your "reset button".

Sees like a follow-on from this post.

In my opinion this is a duplicate not a follow on.

Sorry, it's a follow on.

As explained on the other post, i'm trying to keep some vars alive through a systemoff (very low power standby) followed by a reset to wakeup.

The nRF52 allows it but my tests fail, I suppose, because of the bootloader getting in the way.

pjrebordao:
The nRF52 allows it but my tests fail, I suppose, because of the bootloader getting in the way.

That’s my assumption, and I provided an example that demonstrates that only later elements in the array are preserved (because lower elements are stored in lower memory locations, which are where globals for the boot loader are stored).

I did mention this in your previous thread that this could happen.

Also this test was on a “regular” 8-bit AVR device. The architecture of the nRF52840 may make things quite different again. Sorry, but I do not have one to test with.