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 ?

Related