nrf_fstorage_erase hardfault after use nordic flash loader

Hi,

We use nrf52840 and SDK 17.0.2 with Softdevice S340

   We have a very strange problem and need help from Nordic. Below is my source code.

uint32_t bootloader_start_addr = 0xF2000;

static void power_manage(void)
{
#ifdef SOFTDEVICE_PRESENT
    (void) sd_app_evt_wait();
#else
    __WFE();
#endif
}

void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
{
    /* While fstorage is busy, sleep and wait for an event. */
    while (nrf_fstorage_is_busy(p_fstorage))
    {
        power_manage();
    }
}

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt->result != NRF_SUCCESS)
    {
        return;
    }

    switch (p_evt->id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
        } break;

        default:
            break;
    }
}

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = 0x31000,
    .end_addr   = 0xFFFFF,
};

void update_bootloader_func()
{
    volatile uint8_t iap_update_flag = 0;
    uint32_t err_code = 0;
    uint8_t i = 0;
    uint32_t data = 0xF2000;
    

    nrf_fstorage_api_t * p_fs_api;
        
    p_fs_api = &nrf_fstorage_sd;
    err_code = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(err_code);
            
    nrf_fstorage_erase(&fstorage,bootloader_start_addr,1,NULL);
    wait_for_flash_ready(&fstorage);
}

int main(void)
{
    ret_code_t err_code;
    uint32_t temp = 0;
    
    update_bootloader_func();
    
    while(1);
}

I don't have any Bootloader and My application flash range is 0x31000 - 0xF1ffff

After I build and download by IAR. Everything is OK. I can erase internal flash 1 block form 0xF2000

But If I click option -> Debugger -> Use flash loader and Override default .board file 

I go to run the same source code. Erase action will cause Hardfault in below function

/* Erase flash page(s). */
static uint32_t erase_execute(nrf_fstorage_sd_op_t const * p_op)
{
return sd_flash_page_erase(p_op->erase.page + p_op->erase.progress);
}

Why? We don't have any idea how to debug this problem. Can you help me to try this problem using my source code?

Thank you.

John.

  • Hi,

    I'm not sure why the flash loader was changed or what differences there are compared to using the default programming configuration. However, I believe a good starting point to troubleshoot this issue would be to check if IAR is loading the data differently when using the flash loader. You can use nrfjprog to read out the flash on the chip and compare the flash content.

    // Read out flash after programming the chip using the default programmer settings
    $ nrfjprog --memrd 0x0 --n 0x100000 > default.txt 
    
    // Read out flash after having used the flash loader
    $ nrfjprog --memrd 0x0 --n 0x100000 > flash_loader.txt 
    
    // Compare flash content using 'diff' or similar tools
    $ diff -y default.txt flash_loader.txt
    

    Best regards,

    Vidar

  • Hi Vidar,

            Can you help to find out why the difference between the two will cause the ersae problem. This problem has troubled us for a long time, and we have a function that cannot be completed as scheduled.

    Thank you.

    John.

  • Hi John,

    I don't have the setup to reproduce the issue on my end. Please compare the flash content as I suggested. It would also be nice if you could explain the reason why you want to override the default settings.

  • Hi Vidar,

        OK. We are comparing. Just wanted to know from your side to speed us up.

          We have some read only data(font/picture) that will be written to the external flash during the IAR download stage. Because the internal flash is completely smaller than the size we want. So we have own settings.

    Thank you.

    John.

  • Hi Vadar,

           We have solved this problem. It seems that there is something wrong with our own loader. But we have another problem.

    If I use programmer to burn S340 softdevice only then I plug out my battery. At this time, my system is completely out of power. Then I use IAR to download my application image. We found that erase will Hardfault again at this time. What may be the reason?

    Thank you.

    John.

Related