Ill answer here, in case somebody else bumps into this.
First, find in your reference manual a region of SRAM that remains power through a reset. In my case (mx rt 595), thats SRAM partition 0.

It's address range is:

First up, modify your linker script.
1. Add a memory region. In our case, m_persistent. I placed it at the beginning of the above-mentioned partition.
MEMORY
{
m_flash (RX) : ORIGIN = 0x08000000, LENGTH = 0x00300000
m_interrupts (RX) : ORIGIN = 0x00080000, LENGTH = 0x00000180
m_persistent (RW) : ORIGIN = 0x20000000, LENGTH = 0x1000
m_text (RX) : ORIGIN = 0x00080180, LENGTH = 0x7FE80
m_data (RW) : ORIGIN = 0x200C0000, LENGTH = 0x400000
m_usb_sram (RW) : ORIGIN = 0x40140000, LENGTH = 0x00004000
}
2. Add a section in the memory region.
SECTIONS
{
...
.persistent_data (NOLOAD) :
{
. = ALIGN(4);
__persistent_data_start__ = .;
KEEP(*(.persistent_data))
KEEP(*(.persistent_data*))
. = ALIGN(4);
__persistent_data_end__ = .;
} > m_persistent
...
}
And place your variables in the newly created section. Here's a quick code to test the functionality. It should print the value, increment it, and reset after 3 seconds.
__attribute__((section(".persistent_data"))) int persistent_var;
int main(void)
{
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
SDK_DelayAtLeastUs(1000*2500, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
printf("Hello World %u\n", persistent_var.PersistentValue++);
NVIC_SystemReset();
}
Sample output:
