This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Save in flash a NO_INIT variable

Hi everyone!

After defining a variable as no_init , my intention is to update its value along the program to debug what was its last state before a random reset.

I am able to write this variable in flash but it never remains its last value after reset.

I am using softdevice so I think that although it is defined as no_init, it is reset by softdevice during ble_stack_init.

I have tested doing Hardware Pin Reset as well as NVIC_System_reset().

is there  any way to save its value before it is reset again?

  • Hi,

    Flash writes are time consuming operations that need to go through the SoftDevice API if the SoftDevice is enabled. Also note that flash is written by flipping bits from 1 to 0, and the only way to flip bits back from 0 to 1 again is to do a flash page erase (that is, flip all bits in the 4096 byte flash page back to 1.)

    There is no writing to flash as part of ble_stack_init(), so either the flash write operation did not finish before the reset, there is a flash erase performed elsewhere in the code, or the method used for flash writing is wrong.

    • How are you writing to flash?
    • How do you confirm the value is correctly written to flash before reset?
    • How are you checking the value after reset?

    Regards,
    Terje

  • Hi,

    Thank you for your answer.

    As you say, flash writes are time consuming  operations. For that reason, I don't try to do it during the program. My idea was to use a uint32_t variable to update state during the program and after reset, write it in flash before starting the main loop.

    I tryed to define that variable as no_init with the hope that after the reset it remained its value which I would write in the flash.

    However, that value always resets to 0. 

    I am using FDS functions and It works fine if I initialize the variable to a specific value, otherwise it writes 0. 

    After reading with fds_read function I store the value in an other variable which I check using BLE.

  • Hi,

    Ah, so you mean store the value in RAM across resets? That would be a whole different beast.

    There is some information in the reset behavior section in the power supply section of the Product Specification. RAM is not reset, but contents can nevertheless get corrupted (due to the nature of RAM.)

    What you could do, is use one of the general purpose retention registers (GPREGRET and GPREGRET2), depending on the application. (For instance our DFU bootloader uses GPREGRET for signaling from the application that the bootloader should enter DFU mode on reset, so then GPREGRET is already "taken" for that purpose.)

    Regards,
    Terje

  • Hi!

    Thank you for your answer. Finally, I tryed to define a no_init Section in RAM and associate it to a file( no main.c)  with a no_init variable. As you said, the value is sometimes corrupt ( when I mannually switchh off) . But I think that it is working when software reset happens. 

  • Hi,

    Yes, for CPU lockup and soft reset RAM content stays intact. For the other reset reasons RAM may get corrupted. (In the RAM column of the reset behavior table, "x" means the RAM may get corrupted.)

    Regards,
    Terje

Related