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.

Parents
  • 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

  • @Vincent: You should call fds_gc() when there is no space in flash.

    It's not easy to read the code when you paste it without code tag (the 101010 button on the bar). Anyway, it's only to post a small snippet not the full main.c code. It's very hard to read and follow.

    It's better that you attach the file instead of posting the code. You can create a new case to do that.

    I am still not quite sure I fully understand what your problem is. What do you mean by "got crash"? If you can explain it shortly with small step by step it's easier to understand.

    This case is already too long, please open a new question.

Reply
  • @Vincent: You should call fds_gc() when there is no space in flash.

    It's not easy to read the code when you paste it without code tag (the 101010 button on the bar). Anyway, it's only to post a small snippet not the full main.c code. It's very hard to read and follow.

    It's better that you attach the file instead of posting the code. You can create a new case to do that.

    I am still not quite sure I fully understand what your problem is. What do you mean by "got crash"? If you can explain it shortly with small step by step it's easier to understand.

    This case is already too long, please open a new question.

Children
No Data
Related