0

FDS save under same id and key

MoritzB gravatar image

asked 2017-04-20 14:24:32 +0200

updated 2017-04-21 14:02:06 +0200

Hey, I want to save stuff to flash under the same id and key which should be no problem as shown in the documentation. But when I save it and retrieve the data only the last saved data is in both of the entries. I use SDK 13.

Here is the relevant code.

static ret_code_t fds_read(void)
{
        #define FILE_ID     0x1111
        #define REC_KEY     0x2222
        fds_flash_record_t  flash_record;
        fds_record_desc_t   record_desc;
        fds_find_token_t    ftok ={0};//Important, make sure you zero init the ftok token
        uint32_t *data;
        uint32_t err_code;

        NRF_LOG_INFO("Start searching... \r\n");
        // Loop until all records with the given key and file ID have been found.
        while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
        {
                err_code = fds_record_open(&record_desc, &flash_record);
                if ( err_code != FDS_SUCCESS)
                {
                    return err_code;        
                }

                NRF_LOG_INFO("Found Record ID = %d\r\n",record_desc.record_id);
                NRF_LOG_INFO("Data = ");
                data = (uint32_t *) flash_record.p_data;
                for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
                {
                    NRF_LOG_INFO("0x%x ",data[i]);

                }
                NRF_LOG_INFO("\r\n");
                // Access the record through the flash_record structure.
                // Close the record when done.
                err_code = fds_record_close(&record_desc);
                if (err_code != FDS_SUCCESS)
                {
                    return err_code;    
                }
        }
        return NRF_SUCCESS;

}

And here the write function:

static ret_code_t fds_test_write(void)
{
        #define FILE_ID     0x1111
        #define REC_KEY     0x2222
        static uint32_t m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
        fds_record_t        record;
        fds_record_desc_t   record_desc;
        fds_record_chunk_t  record_chunk;
        // Set up data.
        record_chunk.p_data         = m_deadbeef;
        record_chunk.length_words   = 2;
        // Set up record.
        record.file_id              = FILE_ID;
        record.key                      = REC_KEY;
        record.data.p_chunks       = &record_chunk;
        record.data.num_chunks   = 1;

                write_flag = 0;
        ret_code_t ret = fds_record_write(&record_desc, &record);
        if (ret != FDS_SUCCESS)
        {
                return ret;
        }
         NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);

                m_deadbeef[0] = 1;
                m_deadbeef[1] = 2;
                // Set up data.
        record_chunk.p_data         = m_deadbeef;
        record_chunk.length_words   = 2;
        // Set up record.
        record.file_id              = FILE_ID;
        record.key                      = REC_KEY;
        record.data.p_chunks       = &record_chunk;
        record.data.num_chunks   = 1;

        while (write_flag==0);  
                write_flag = 0;
        ret = fds_record_write(&record_desc, &record);
        if (ret != FDS_SUCCESS)
        {
                return ret;
        }
         NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
}

The relevant output is this.

APP:INFO:Writing Record ID = 7 
APP:INFO:Writing Record ID = 8 
APP:INFO:Start searching... 
APP:INFO:Found Record ID = 7
APP:INFO:Data = APP:INFO:0x1 APP:INFO:0x2 APP:INFO:
APP:INFO:Found Record ID = 8
APP:INFO:Data = APP:INFO:0x1 APP:INFO:0x2 APP:INFO:

Anyone knows whats wrong? Thanks in advance.

edit retag flag offensive close delete report spam

Comments

Could you share your complete project so I can test it here?

Petter Myhre ( 2017-04-21 13:37:02 +0200 )editconvert to answer

https://drive.google.com/open?id=0B7D...

here is the project. I programm in segger embeded studio, so i hope the Keil project still works. I can compile it but cant flash it because of the code limitation.

Moritz ( 2017-04-21 14:05:22 +0200 )editconvert to answer

2 answers

Sort by » oldest newest most voted
1
Petter gravatar image

answered 2017-04-24 14:17:26 +0200

You shouldn't change the data pointed to before you get the successful FDS_EVT_WRITE event.

So, do something like:

while (write_flag==0);  

m_deadbeef[0] = 1;
m_deadbeef[1] = 2;
edit flag offensive delete publish link more

Comments

works and makes sense. My idea was to speed things up and check for the write process when I already set the data. Thanks a lot.

Moritz ( 2017-04-24 14:32:39 +0200 )editconvert to answer

No problem :) If I answered your question please accept my answer by clicking the grey circle next to it. Thanks.

Petter Myhre ( 2017-04-24 14:46:48 +0200 )editconvert to answer
0
MoritzB gravatar image

answered 2017-04-21 11:49:19 +0200

I did a workaround now and just gave every set of data its own rec_key some more lines of code but it works now.

edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower

Stats

Asked: 2017-04-20 14:24:32 +0200

Seen: 39 times

Last updated: Apr 24