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.

Related