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

How to read data by using fstorage

Hii all,

I am working on a project where I need to store data to persistent storage and retrieve back when the board is restart. but now we are migrated to simultaneous central and peripheral role and I found some thing like fstorage and I read this is just like pstorage but efficient than pstorage
So I would like to use this for my operation but its something wondering like when I look at the doc of fstorage we only have fs_store() and fs_erase() but nothing like fs_load(). Can any one suggest me is there any way to do that

I'm using nRF5_SDK_11.0.0_89a8197 Thank you

  • After calling fs_store() you will receive the FS_EVT_STORE event in the fs_evt_handler() when the flash operation is finished. Within the fs_evt_t structure you'll find the pointer to the data that was written to flash.

    To retrieve the data, you must create a pointer variable that should be used to store the pointer that points to the data in flash, i.e.

    const uint32_t * stored_data;
    

    and when you get the FS_EVT_STORE, simply set the pointer to point to the same address as the pointer that is returned in the fs_evt_handler(), i.e.

    static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
    {
        switch(evt->id)
        {
            case FS_EVT_STORE:
                if(result == FS_SUCCESS)
                {
                    stored_data = evt->store.p_data;
                    SEGGER_RTT_printf(0,"Stored Data: %X \n",*stored_data);
                }
                break;
            
            case FS_EVT_ERASE:
                // Handle erase event
                break;
            
            default:
                //Do nothing
                break;
        }
    }
    

    This is provided that you run the following code snippet in main()

    FS_REGISTER_CFG(fs_config_t fs_config) =
        {
            .callback  = fs_evt_handler,    // Function for event callbacks.
            .num_pages = 1,                 // Number of physical flash pages required.
            .priority  = 0xFE               // Priority for flash usage.
        };
    
    fs_ret_t ret = fs_init();
    if (ret != FS_SUCCESS)
    {
        SEGGER_RTT_printf(0,"FS ERROR OCCURED\n");
        // An error occurred.
    }
    
    //Data to be written to flash
    static uint32_t data = 0x12345678;
    
    ret = fs_store(&fs_config, fs_config.p_start_addr, &data, 1);
    if (ret != FS_SUCCESS)
    {
         SEGGER_RTT_printf(0,"FS ERROR OCCURED\n");
        // An error occurred.
    }    
    

    and that you have added the fs_sys_event_handler() function in sys_evt_dispatch(), i.e.

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
        ble_advertising_on_sys_evt(sys_evt);
        fs_sys_event_handler(sys_evt);
    }
    

    -Bjørn

  • Thanks Bjorn for replying. I already come to know that I can use the data by using pointer but my question is just like in pstorage we have any call that can return the data required

    Thanks&regards Pavan

  • There is no load API call, you have to keep track of the pointer after you store the data.

  • OOh ok I think its better if you have any. Any how that's fine and thanks for ans

  • Dear Bjorn,

    I understand your way to read data. But this way you are forced to overwrite your data before being able to read it, right? How can you access the pointer without modifying the saved data? Thank you

Related