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

Flash Data Storage

hi nordic Have a code project sample shows the typical use Flash applications in data storage? I see

[code](infocenter.nordicsemi.com/index.jsp.
    my code
`
#define FILE_ID     0x1111
#define REC_KEY     0x2222

static void fds_evt_handler(ret_code_t       result,
                            fds_cmd_id_t     cmd,
                            fds_record_id_t  record_id,
                            fds_record_key_t record_key)
{
    // Handle events here
}

ret_code_t module_fds_register(void)
{
    ret_code_t retval = fds_register(fds_evt_handler);
    if(retval != NRF_SUCCESS)
    {
        // Registering of callback handler failed.
        return retval;
    }

    retval = fds_init();
    if (retval != NRF_SUCCESS)
    {
        // Error.
        return retval;
    }
    return retval;
}

ret_code_t module_fds_write(uint32_t *p_data)
{

    ret_code_t         retval;
    fds_record_key_t   key;
    fds_record_chunk_t chunk;
    fds_record_desc_t  descriptor;

    // Fill in the record keys.
    key.type     = REC_KEY;
    key.instance = FILE_ID;

    // Initialize the record chunk, pointing it to the data to be stored.
    chunk.p_data       = p_data;
    chunk.length_words = 1;

    // Write the record to flash.
    retval = fds_write(&descriptor,
                       key,
                       1 ,  /*number of chunks*/
                       &chunk);
    if (retval != NRF_SUCCESS)
    {
        // Error.
    }
    return  retval;
    // The command was queued. Wait for a callback signalling its completion.

}

ret_code_t module_fds_read(uint32_t p_data ,fds_length_t length_words)
{
    fds_type_id_t     type;
    fds_instance_id_t inst;
    fds_find_token_t  tok;
    fds_record_desc_t descriptor;
    fds_record_t      record;

    // Set the record keys.
    type = REC_KEY;
    inst = FILE_ID;

    //SEGGER_RTT_printf(0, " module_fds_read\n");
    // Loop until all records with the given key pair has been found.
    if(fds_find(type, inst, &descriptor, &tok) == NRF_SUCCESS)
    {
        // A record was found. Open the record to check its contents.
        fds_open(&descriptor, &record);

        // Cast the data.
        *p_data = (uint32_t)(*record.p_data); 

        // When you are done, close the record using its descriptor.
        fds_close(&descriptor);
    }

}
`

Writing down a record and Retrieving data is no SOME_VALUE.

  • Hi Libra,

    Here is a quick example on how to use the fds. The application will first search through the record and delete all record with ID =0x1111 and KEY = 0x2222? then will write a new record with that key and ID, and finish by a read and print out on UART.

    It's made with SDK v11.

    github.com/.../nRF52-fds-example

  • Hi Bui, I ref your code and copy into example "ble_app_hrs_rscs_relay" use. When it run this line: ret_code_t ret = fds_record_write(&record_desc, &record); there is a error show up and reset itself and I use DEBUG mode the error is: case PM_EVT_PEER_DATA_UPDATE_FAILED: // Assert. APP_ERROR_CHECK_BOOL(false); break;//PM_EVT_PEER_DATA_UPDATE_FAILED

    in static void pm_evt_handler(pm_evt_t const * p_evt) and what should I do for this error

    and I need 100 bytes data to store this is my project need.

    Thanks

  • Sorry for mention this I use nRF52 and example ble_app_hrs_rscs_relay in SDK 11.

  • @Vincent: Please dig a little bit deeper and check what is returned in p_evt->peer_data_update_failed when the PM_EVT_PEER_DATA_UPDATE_FAILED occurs. Also please make sure you have checked that all the call to init fds didn't return any error (APP_ERROR_CHECK(err_code);)

  • The example code these call first ble_stack_init(); peer_manager_init(erase_bonds); db_discovery_init();

    then I put the fds code (1). err_code =fds_test_init(); APP_ERROR_CHECK(err_code);

    Then I got static void my_fds_evt_handler(fds_evt_t const * const p_fds_evt) { switch (p_fds_evt->id) { case FDS_EVT_INIT: if (p_fds_evt->result != FDS_SUCCESS) { // Initialization failed. printf("fds init fail: %d\r\n", p_fds_evt->result);

                here I got p_fds_evt->result is 14                
    
    
            }
            break;
    

    .......

    (2). err_code = fds_test_find_and_delete(); APP_ERROR_CHECK(err_code);

    here I mask the fds_gc() and it seems no error

    (3). err_code =fds_test_write(); APP_ERROR_CHECK(err_code); while(write_flag==0);//wait until the write is finished.

    here I got the error code is 2 and the MCU stop no go any further.

    (4). fds_read();

Related