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

How to use FDS for reading and writing data?

Hello everybody,

I used SDK14.2 S132

I defined fds_write and read function like that;

static ret_code_t kls_fds_write(uint32_t file_id, uint32_t record_key , uint8_t write_data[], uint16_t length)
{		
	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 = length;
	
	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;
	

	
}
	
static ret_code_t kls_fds_read(uint32_t file_id, uint32_t record_key , uint8_t read_data[])
{	
		fds_flash_record_t  flash_record;
		fds_record_desc_t   record_desc;
		fds_find_token_t    ftok;
		/* It is required to zero the token before first use. */
		memset(&ftok, 0x00, sizeof(fds_find_token_t));
		
		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, 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);
				NRF_LOG_INFO("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]);
					read_data[i] = 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;		
}

Also, I wrote a function to register particular data;

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, 10);
	
	NRF_LOG_RAW_HEXDUMP_INFO(source_data_0, 10);
	
	allow_advertisement = false;

}

Then I used save_data like this;

save_data(nus_data_array);


Then I read data via fds_read

while (flag_write_fds == 0);
kls_fds_read(FILE_ID_SSN, RECORD_KEY_SSN, dest_data_0);
	
NRF_LOG_INFO("dest_data_0 : ");
NRF_LOG_HEXDUMP_INFO(dest_data_0, 10);

As a result;

0> <info> app: save_data
0> <info> app: Writing Record ID = 1 
0> 
0> 41 30 31 31 30 30 30 34|A0110004
0> 30 00 |0.

This is exactly what I want it to be. There is no problem, But when I want to read save_data array;

Result is;

0> <info> app: Start searching...
0> <info> app: Found Record ID = 1
0> 
0> <info> app: Data = 
0> <info> app: 0x20005CC8 
0> <info> app: 0x 9731 
0> <info> app: 0x20005CC8 
0> <info> app: 0x20005CE8 
0> <info> app: 0x 971B 
0> <info> app: 0x 29 
0> <info> app: 0x 0 
0> <info> app: 0x200027B0 
0> <info> app: 0x 2 
0> <info> app: 0x 12A3 
0> <info> app: 
0> 
0> <info> app: dest_data_0 : 
0> <info> app: C8 31 C8 E8 1B 29 00 B0|È1Èè.).°
0> <info> app: 02 A3 |.£

It's a meaningless result.

Q1- Is the problem reading data or writing data or all of them?

Q2- What do I need to do to correctly write and read the data?

Parents
  • Can you show us how you print out your data? And maybe post more of your code?

    Values like 0x20005CC8 look like addresses in RAM, so it might be that you are simply printing out the location of your data, and not the data itself. 

  • Hi Martin,

    I printed like this;

    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_SSN, RECORD_KEY_SSN, dest_data_0);
    	
    	NRF_LOG_INFO("dest_data_0 : ");
    	NRF_LOG_HEXDUMP_INFO(dest_data_0, 10);
    }

    I wrote the function "kls_fds_read" at the top, you can look at it from there. In a part of "Kls_fds_read" function I used this code to print out;

    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->length_words;i++)
    {
    	NRF_LOG_INFO("0x%8x ",data[i]);
    	read_data[i] = data[i] ; 
    }
    NRF_LOG_INFO("\r\n");

    data = (uint32_t *) flash_record.p_data;  ------>>>>  Does not that mean the data itself?

     

    I try to read data with use "uint8_t read_data"  but fds used "uint32_t *data;" , and I try to copy the data into read_data via  "read_data[i] = data[i] ; ". Although I do not get any errors, maybe it is not logical to choose two different variable types.

Reply
  • Hi Martin,

    I printed like this;

    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_SSN, RECORD_KEY_SSN, dest_data_0);
    	
    	NRF_LOG_INFO("dest_data_0 : ");
    	NRF_LOG_HEXDUMP_INFO(dest_data_0, 10);
    }

    I wrote the function "kls_fds_read" at the top, you can look at it from there. In a part of "Kls_fds_read" function I used this code to print out;

    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->length_words;i++)
    {
    	NRF_LOG_INFO("0x%8x ",data[i]);
    	read_data[i] = data[i] ; 
    }
    NRF_LOG_INFO("\r\n");

    data = (uint32_t *) flash_record.p_data;  ------>>>>  Does not that mean the data itself?

     

    I try to read data with use "uint8_t read_data"  but fds used "uint32_t *data;" , and I try to copy the data into read_data via  "read_data[i] = data[i] ; ". Although I do not get any errors, maybe it is not logical to choose two different variable types.

Children
Related