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

Check failed in nrf_fstorage_read()

Hi, 

I am trying to add nrf_storage to my project. The goal is to read-write-earse on flash. I have try exemple code name "flash_fstorage" and it work perfectly. 

So I decicde to add thoses functions in my project : 

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);

static const js_cfg_t g_def_cfg =
{
    .sof     = 0x00,
    .dev_eui = js_DEVICE_EUI,
    .app_eui = js_JOIN_EUI,
    .app_key = js_NWK_KEY,
    .dev_addr = js_DEVICE_ADDRESS,
    .nwkskey = js_NWK_S_ENC_KEY,
    .appskey = js_APP_S_KEY,
};

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 = 0x7F000,
    .end_addr   = 0x80000,
};

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt->result != NRF_SUCCESS)
    {
        NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
        NRF_LOG_FLUSH();
        return;
    }

    switch (p_evt->id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
            NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
                         p_evt->len, p_evt->addr);
                         NRF_LOG_FLUSH();
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
            NRF_LOG_INFO("--> Event received: erased %d page from address 0x%x.",
                         p_evt->len, p_evt->addr);
                         NRF_LOG_FLUSH();
        } break;

        default:
            break;
    }
}

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();
    }
}

void u_fs_init()
{
    ret_code_t rc;
    NRF_LOG_INFO("fstorage example started!");
    
    nrf_fstorage_api_t * p_fs_api;
    
    NRF_LOG_INFO("SoftDevice is present.");
    NRF_LOG_INFO("Initializing nrf_fstorage_sd implementation...");
    // Initialize an fstorage instance using the nrf_fstorage_sd backend.
    // nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
    // used whenever there is a SoftDevice, regardless of its status (enabled/disabled). 
    p_fs_api = &nrf_fstorage_sd;
    
    rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(rc);
    
}

void u_fs_check_js_cfg(js_cfg_t *cfg)
{
    printf("%x\r\n",cfg->sof);
    if(cfg->sof != 0x55)
    {
        memcpy((uint8_t*)cfg,&g_def_cfg,sizeof(g_def_cfg));
    }
}

void u_fs_read_js_cfg(js_cfg_t *cfg)
{
    ret_code_t rc;
    rc = nrf_fstorage_read(&fstorage, fstorage.start_addr, cfg, sizeof(js_cfg_t));
    //APP_ERROR_CHECK(rc);
}

void u_fs_write_js_cfg(js_cfg_t *cfg)
{
    ret_code_t rc;
    
    rc = nrf_fstorage_erase(&fstorage, fstorage.start_addr, 1, NULL);
    NRF_LOG_INFO("erase %d", rc);
    APP_ERROR_CHECK(rc);
    wait_for_flash_ready(&fstorage);

    cfg->sof = 0x55;
    rc = nrf_fstorage_write(&fstorage, fstorage.start_addr, cfg, sizeof(js_cfg_t), NULL);
    NRF_LOG_INFO("write %d", rc);
    APP_ERROR_CHECK(rc);
    wait_for_flash_ready(&fstorage);
    printf("JS parameters configured successfully\r\n");
}

And in my main : 

typedef struct {
        uint8_t sof;
        uint8_t dev_eui[8];
        uint8_t app_eui[8];
        uint8_t app_key[16];
        uint32_t dev_addr;
        uint8_t nwkskey[16];
        uint8_t appskey[16];
} lora_cfg_t;

extern js_cfg_t     g_js_cfg;

void nRF_Js_init()
{
    //load Js configuration
    u_fs_init();
    memset((uint8_t*)&g_js_cfg,0,sizeof(g_js_cfg));
    u_fs_read_js_cfg(&g_js_cfg);
    u_fs_check_js_cfg(&g_js_cfg);
    js_init(&g_js_cfg);
    printf("js init success.\r\n");
    NRF_LOG_FLUSH();
}

But each time I try to read flash with "u_fs_read_js_cfg(js_cfg_t *cfg)", I have this error : "<error> nrf_fstorage: p_fs->p_api check failed in nrf_fstorage_read() with value 0x8." and if I try to erase : "<error> nrf_fstorage: p_fs->p_api check failed in nrf_fstorage_erase() with value 0x8." etc... !

I don't really undertstand why, I have already try with differents flash addr like ".start_addr = 0x3e000, .end_addr = 0x3ffff,". But I have the same result. Maybe, I try to read-write-erase in a flash sector already use, but I don't know how to see that ?! (At this time I use a nRF52832-QFAA.

Thanks for your help

Sani300

Related