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

How to write, read and store uint8_t data_array via FDS

Hello everybody,

I used SDK14.2.0 , S132 .

I keep the data from nus_data in the uint8_t array. Then I am trying to write the array with use fds_write.

My fds_write function;

static ret_code_t kls_fds_write(uint32_t file_id, uint32_t record_key , uint8_t write_data[])
{		
	fds_record_t        record;
	fds_record_desc_t   record_desc;
	
	// Set up record.
	record.file_id           = file_id;
	record.key               = record_key;
	record.data.p_data       = &write_data;
	record.data.length_words = (sizeof(write_data) + 3) / 4;     //sizeof(write_data);   //(sizeof(write_data) + 3) / sizeof(uint32_t);
	
	ret_code_t rc;
	rc = fds_record_write(&record_desc, &record);
	if (rc != FDS_SUCCESS)
	{
			/* Handle error. */
			NRF_LOG_INFO("FDS Write Handle error.");
			return rc;
	}
	NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
	return NRF_SUCCESS;
	
}

I use my fds_write function by this way;

static void save_data( uint8_t *p_data)
{
	NRF_LOG_INFO("save_data");
	uint8_t source_data_0[10] ;		
	
	// assign source datas  ********************************	
	for (uint32_t i = 0; i < 10; i++)				
	{
		source_data_0[i] = p_data[i];
	}
	while (flag_write_fds == false);
	kls_fds_write(FILE_ID, RECORD_KEY, source_data_0);
	
	NRF_LOG_HEXDUMP_INFO(source_data_0, sizeof(source_data_0));
	
	allow_advertisement = false;

}

Then I am trying to read and store the data into another array.

My fds_read function;

static ret_code_t kls_fds_read(uint32_t file_id, uint32_t relevant_record_key , uint8_t read_data[])
{	
		fds_flash_record_t  flash_record;
		fds_record_desc_t   record_desc;
		fds_find_token_t    ftok;
		fds_record_t        record;
		/* It is required to zero the token before first use. */
		memset(&ftok, 0x00, sizeof(fds_find_token_t));
		
		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, relevant_record_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);
				
				uint8_t * pointer_to_record_data;
				pointer_to_record_data = (uint8_t *)record.data.p_data; 
				
				if(flash_record.p_header->record_key == relevant_record_key)
				{
					for(uint8_t i = 0; i < 10; i++)
					{
							read_data[i] = *pointer_to_record_data;
							pointer_to_record_data++;
					}
				}
				
				NRF_LOG_INFO("Data = ");
				NRF_LOG_HEXDUMP_INFO(read_data, sizeof(read_data));
				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;		
}

I use my fds_read function by this way;

static void first_Setup(void)
{
    NRF_LOG_INFO("first_Setup Start");
    uint8_t dest_data_0[10]  = {0};
	
    while (flag_write_fds == 0);
    kls_fds_read(FILE_ID, RECORD_KEY, dest_data_0);
    	
    NRF_LOG_INFO("dest_data_0 : ");
    NRF_LOG_HEXDUMP_INFO(dest_data_0, 10);
    
}

I can write and read something but these are not meaningful data. Or I can not read the data I wrote because of anything. I obviously could not figure out exactly where the problem was.

The steps I want to take are as follows;

  • Store particular nus_data into an array (uint8_t source_data_0[10] ) -> I'm already doing this. No problem.
  • Write the stored array(uint8_t source_data_0[10] ) via FDS -> I do that already, but I do not know exactly whether it is successful.
  • Read the stored array(uint8_t source_data_0[10] )via FDS and Store the read array(source_data_0[10] ) in another array (uint8_t dest_data_0[10]  = {0}).

Test Result;

I sent the nus data( "aaaaaaaaaa" ) and I can store it into source_data_0[] successfully. Then I use my fds_write function;

0> <info> app: save_data
0> <info> app: Writing Record ID = 1
0>
0> <info> app: 61 61 61 61 61 61 61 61|aaaaaaaa
0> <info> app: 61 61 |aa

Finally I tried to read and result is;

0> <info> app: first_Setup Start

0> <info> app: Start searching...
0>
0> <info> app: Found Record ID = 1
0>
0> <info> app: Data =
0> <info> app: 00 04 00 20 |...
0> <info> app:
0>
0> <info> app: dest_data_0 :
0> <info> app: 00 04 00 20 E9 08 00 00|... é...
0> <info> app: 7D 05 |}.

As a result; The data I read is not the same as the data I write . May be I read it in the wrong format, I do not know exactly. 

So, Do you have any suggestion ?

Parents
  • Hey Ugur,

    I believe I found a bug:

    fds_record_t record; // Uninitialized variable
    
    uint8_t * pointer_to_record_data;
    pointer_to_record_data = (uint8_t *)record.data.p_data; // points to an address with unknown data
    
    if(flash_record.p_header->record_key == relevant_record_key)
    {
    	for(uint8_t i = 0; i < 10; i++)
    	{
    		read_data[i] = *pointer_to_record_data; // record has not been used
    		pointer_to_record_data++;
    	}
    }

    You never used the variable 'record' and since it is uninitialized it's content is in an unknown state. The compiler has probably reused the memory of other parts of the program that is no longer in the scope and you will therefore read garbage data.

    Cheers,

    Håkon.

  • Hi Håkon, Thanks for answering I refresh my code and changed the name of "record "with "rec". 

    Test Result;

    0> <info> app: save_data
    0> <info> app: Writing Record ID = 1
    0>
    0> <info> app: 61 61 61 61 61 61 61 61|aaaaaaaa
    0> <info> app: 61 61 |aa

    tried to read and result is;

    0> <info> app: first_Setup Start
    0> <info> app: Start searching...
    0>
    0> <info> app: Found Record ID = 1
    0>
    0> <info> app: Data =
    0> <info> app: 00 04 00 20 |...
    0> <info> app:
    0>
    0> <info> app: dest_data_0 :
    0> <info> app: 00 04 00 20 E9 08 00 00|... é...
    0> <info> app: 7D 05 |}.

    Additionan Note; Interestingly, Whatever I write data is always the same result!

    For example;

    If I write different data ;

    0> <info> app: 78 68 6A 66 68 6B 67 67|xhjfhkgg
    0> <info> app: 6A 6A |jj

    And I read same result;

    0> <info> app: Data =
    0> <info> app: 00 04 00 20 |...
    0> <info> app:
    0>
    0> <info> app: dest_data_0 :
    0> <info> app: 00 04 00 20 E9 08 00 00|... é...
    0> <info> app: 7D 05 |}.

  • The problem is not the name of 'record/'rec', it's that it is not used at all! No data is ever stored in the 'record' instance of type fds_record_t. 

  • Before I wrote this post, I tried this;

    uint8_t * pointer_to_record_data;
    pointer_to_record_data = (uint8_t *)flash_record.p_data;
    
    if(flash_record.p_header->record_key == relevant_record_key)
    {
        for(uint8_t i = 0; i < 10; i++)
        {
            read_data[i] = *pointer_to_record_data;
            pointer_to_record_data++;
        }
    }
    
    NRF_LOG_INFO("Data = ");
    NRF_LOG_HEXDUMP_INFO(read_data, sizeof(read_data));
    NRF_LOG_INFO("\r\n");

    But it did not work either. Result like that;

    I wrote this

    0> <info> app: 41 30 31 31 30 30 30 34|A0110004
    0> <info> app: 30 00 |0.

    Result was

    0> <info> app: Data =
    0> <info> app: 31 97 00 00 |1—..
    0> <info> app:
    0>
    0> <info> app: dest_data_0 :
    0> <info> app: 31 97 00 00 C8 5C 00 20|1—..È\.
    0> <info> app: E8 5C |è\

  • Also I tried this one;

    uint32_t *data ;
    data = (uint32_t *) flash_record.p_data;
    for(uint8_t i =0; i< flash_record.p_header->length_words; i++)
    
        read_data[i] = data[i];
    }
    
    NRF_LOG_INFO("Data = ");
    NRF_LOG_HEXDUMP_INFO(read_data, sizeof(read_data));
    NRF_LOG_INFO("\r\n");

    Note: I defined uint8_t read_data[] . 

    I write ;

    0> <info> app: 41 30 31 31 30 30 30 34|A0110004
    0> <info> app: 30 00 |0.

    Result;

    0> <info> app: Data =
    0> <info> app: 31 B0 D0 1B |1°Ğ.
    0> <info> app:

  • Tried another experiment;

    uint32_t *data ;
    
    data = (uint32_t *) flash_record.p_data;
    NRF_LOG_HEXDUMP_INFO(data, sizeof(data));

    I write ;

    0> <info> app: 41 30 31 31 30 30 30 34|A0110004
    0> <info> app: 30 00 |0.

    Result;

     0> <info> app:  31 97 00 00            |1—..  

  • Another Experiment;

    uint32_t *data ;
    				
    data = (uint32_t *) flash_record.p_data;
    for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
    {
        NRF_LOG_INFO("0x%8x ",data[i]);
    }

    I wrote;

    0> <info> app: 41 30 31 31 30 30 30 34|A0110004
    0> <info> app: 30 00 |0.

    Result;

    0> <info> app: 0x 9731
    0> <info> app: 0x20005CB0
    0> <info> app: 0x20005CD0
    0> <info> app: 0x 971B

Reply Children
No Data
Related