cancel
Showing results for 
Search instead for 
Did you mean: 

how to retain global variable after NVIC_reset() in STM32F302R8

KE.1
Associate II

Hi Team,

I want to retain a value of variable after software reset

i tried __no_init but variable was resetting to zero

Is there any way i can write into Flash during runtime and fetch it back after reset

Thanks,

Krupashankar

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

It works with gcc like this:

in .c:

int no_init __attribute__ ((section (".no_init")));

in linker description file .ld add a section before heap+stack:

.no_init :
  {
    *(.no_init)
  } > RAM

For testing:

printf("no_init: %d\n", no_init );
  	no_init++;
 
	HAL_Delay(100);
	NVIC_SystemReset();

Redirect printf to an UART or such and watch no_init variable being incremented after each reset.

hth

KnarfB

View solution in original post

3 REPLIES 3

The processor doesn't clear the memory.

Check what your startup.s code is doing, and perhaps shrink the memory visible to the linker if you need to place it outside that scope.

Yes you can write data into FLASH, there are examples in the HAL trees for assorted boards. Memory will need to be erased prior to writing, so often better to journal the variable, or structure holding retained data, across the flash page/sector.

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

It works with gcc like this:

in .c:

int no_init __attribute__ ((section (".no_init")));

in linker description file .ld add a section before heap+stack:

.no_init :
  {
    *(.no_init)
  } > RAM

For testing:

printf("no_init: %d\n", no_init );
  	no_init++;
 
	HAL_Delay(100);
	NVIC_SystemReset();

Redirect printf to an UART or such and watch no_init variable being incremented after each reset.

hth

KnarfB

KE.1
Associate II

Thank you:smiling_face_with_smiling_eyes: