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

how can I retain RAM after watchdog reset and bootloader present

Im using nrf51822 (16KB RAM), I'm trying to retain some data (RAM) after watchdog reset. when I load only the softdevice and my app, the RAM retention workings, but when I used it with bootloader, the data is always reset. I use this customized scatter file :

RW_IRAM2 0x20002000 UNINIT 0x20  {
   main.o (rtc_ram)
  }
  
  RW_IRAM1 0x20002020 0x3FE0{  ; RW data
   .ANY (+RW +ZI)
  }

I can see the data on the map file are set in the region 0x20002000 to 0x20002080, but each time the system reset, the data is reset.

I configured RAM retention on BLOCK1 after advertising start function

if(sd_power_ramon_set(0x00020003) != NRF_SUCCESS)
        {
        NVIC_SystemReset();
        }

I configured the desired RAM retention data this way

static uint8_t session_start_flag	__attribute__((section("rtc_ram"),zero_init));
static uint8_t reset_after_hang		__attribute__((section("rtc_ram"),zero_init));
static uint16_t steps_store_index	__attribute__((section("rtc_ram"),zero_init));
static uint16_t last_read_block		__attribute__((section("rtc_ram"),zero_init));
static uint16_t last_offset			  __attribute__((section("rtc_ram"),zero_init));

what could be the issue? I thought that when the app is valid, it's launched automatically on watchdog reset and the bootloader code is just flash code???, I mean the RAM is not affected by the bootlader

  • I am not sure, but additionally to things you implemented (modified .sct file and attribute) I also have "NoInit" for IRAM2 ticked in "target" tab in target options in keil. Maybe you could try that.

  • Please refer to this answer to a very similar question.

    The suggested solution for that case does not work for you, but the initial part of the answer is: the reset behavior table found in section 12.1.19 of the nRF51 RM states that RAM content may be corrupted for all reset sources but CPU lockup and softreset. This means that even if you do everything by the book with regard to RAM retention, you still risk the data being corrupted after a watchdog reset. So what you ask is not possible on the nRF51 HW, as you cannot be confident that RAM data is valid after a watchdog reset.

    Your updated question asks about differenced using bootloader or not. The bootloader is always run at startup if it is flashed, though it will not do much in the normal case where there is no upgrade ongoing. It will do some basic configuration (configure LED's, enable timer and GPIO for button etc) and check whether it is in the process of upgrading or not, and if there is a valid application. Then it will start the application. This means that you risk losing data that is stored in the RAM area for the bootloader, also when not upgrading.

    So in your case you have two separate issues that can make your data corrup:

    1. RAM content may be corrupted after watchdog reset
    2. Bootloader will use some of it's RAM at startup even if no upgrade is ongoing.
  • .. comment removed as I now see two different loads of documentation about what 'zero_init' means.

  • I noticed that when I dont use boot loader, it's working (RAM retention working) but when I flash the bootloader, it's not working. what could be the problem

  • I noticed that when I keep above variable out of bootloader RAM range it works fine. is that mean that the RAM is affected by the bootlader even when the bootloader is not running???

    bootloader RAM configuration

    image description

Related