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

Saving words in FDS memory ?

I was able to use the fds with the help of good people here , I am trying now to save a word into memory.

As I see in the docs, the only way to save data is by uint32_t chunks .

So I guess I am doing it wrong but I am trying this without success :

to save with this :

        char data[]="lola"; 
        uint32_t save;
        memcpy(&save, data, 4);


        #define FILE_ID     0x1111
	#define REC_KEY     0x2222

	fds_record_t        record;
	fds_record_desc_t   record_desc;
	fds_record_chunk_t  record_chunk;
	// Set up data.
	record_chunk.p_data         = &save;
       record_chunk.length_words   = 1;
	// Set up record.
	record.file_id              = FILE_ID;
	record.key              		= REC_KEY;
	record.data.p_chunks       = &record_chunk;
	record.data.num_chunks   = 1;
	
    ....

Then to read with :

... //some code that works , then
			 data = (uint32_t *) flash_record.p_data;
			for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
			{
                                   char bytes[6];
                                   char n =  data[i];
                                   bytes[0] = (n >> 24) & 0xFF;
                                   bytes[1] = (n >> 16) & 0xFF;
                                   bytes[2] = (n >> 8) & 0xFF;
                                   bytes[3] = n & 0xFF;
                                   for (int k=0;k<4;k++)
                                       NRF_LOG_INFO("message is: %c ",bytes[k]);

				//NRF_LOG_INFO("record ID: %d and data: %x ",record_desc.record_id,data[i]);

			
			}
		
	

Which will print record 1 and data - means empty data

  1. Is this the way to save a word ?
  2. what about saving multiple words like this ? is it a overhead ?

Thanks !

  • FormerMember
    0 FormerMember

    The below code shows how to store and retrieve a string using FDS:

    static ret_code_t fds_test_write(void)
    {
    	#define FILE_ID     0x1111
    	#define REC_KEY     0x2222
    	
    	static char const my_text[]    = "What a day, what a day!";
    	
    	fds_record_t        record;
    	fds_record_desc_t   record_desc;
    	fds_record_chunk_t  record_chunk;
    	// Set up data.
    	record_chunk.p_data         =  my_text; 
    	record_chunk.length_words   =  (sizeof(my_text)+3)/4;  // When calculating the length in words, using integer division, the result will always be "rounded" down to the whole integer. A data array of 21 bytes, is strictly speaking 5 words + 1 byte. However, the length should be in words, and thus 6 words are needed to store all the data. 21/4 = 5, (21+3)/4 = 6. For 20 bytes, 5 words are needed, in integer division 20/4 = (20+3)/4 =5.
    	// Set up record.
    	record.file_id              = FILE_ID;
    	record.key              		= REC_KEY;
    	record.data.p_chunks       = &record_chunk;
    	record.data.num_chunks   = 1;
    			
    	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);
    	return NRF_SUCCESS;
    }
    
    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
    	
    	char     *p_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 = ");
    			
    			// Access the record through the flash_record structure.
    			p_data = (char *) flash_record.p_data;
    			NRF_LOG_INFO(" %s \r\n", (uint32_t)p_data);
    		
    			
    			// Close the record when done.
    			err_code = fds_record_close(&record_desc);
    			if (err_code != FDS_SUCCESS)
    			{
    				return err_code;	
    			}
    	}
    	return NRF_SUCCESS;
    	
    }
    
  • Hi, Kristen thanks a lot, but it wasn't my question - that I already know. The question was how to save strings(char arrays), and read them. You can see in the first 3 lines of the question I am trying to save a string into memory as a set of Uint32 .

  • FormerMember
    0 FormerMember in reply to FormerMember

    I have updated the code to show how to store and retrieve a string.

  • Thanks a lot, did you mean record_chunk.p_data= &my_text (with the address sign) ? it's a pointer that should get an address no ?

    Also, why calculating the size with the 3/4 ?

  • By the way, I found out that the word you are about to save must be static, or global otherwise the system just lose it while it's writing, and you read garbage. So If I drop the static I get garbage, if I add it it's good. Also, if I set the my_text to be global and not static it's also work.

Related