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?

  • Printing is done in print_data_cmd() in cli.c.

  • Thank you so much for your solution. But I have to store data into uint8_t array[10]. It means, I want to write "abcdefghij" via nus_data . I can do this shown as below, there is no problem.

    0> <info> app: save_data
    0> <info> app: Writing Record ID = 1 
    0>
    0> 61 62 63 64 65 66 67 68|abcdefgh
    0> 69 6A |ij

    Then I want to read "abcdefghij" and store it in uint8_t array [10].

    That's why I figure out your "convert_raw_data_to_human_readable" function as below:

    static void convert_raw_data_to_human_readable(uint8_t *out_data , uint32_t in_data)
    {
        for(uint8_t i = 0; i < 4; i++)
    	{
    		out_data[i] = (uint8_t)(in_data >> (8 * i));
        }
    }

    And I put the function inside kls_fds_read() function like this;

    uint32_t *data     = (uint32_t *) flash_record.p_data;
    uint32_t const len = flash_record.p_header->length_words * sizeof(uint32_t);
    				
    for(uint32_t i; i < len / 4; i++)
    {
        convert_raw_data_to_human_readable(read_data, *data);
        data++;
    }

    Finally, I'm trying to write on my destination array(dest_data_0[10]);

    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);
    }

    But I did not get the data I wrote.

    RTT Viewer output:

     0> <info> app: first_Setup Start

    0> <info> app: Found Record ID = 1
    0>
    0> <info> app: Data =
    0> <info> app: dest_data_0 :
    0> <info> app: 00 00 00 00 00 00 00 00|........
    0> <info> app: 00 00 |..

  • I'm still a little confused about the flow in your code. How about something like this:

    uint8_t * pointer_to_record_data;
    pointer_to_record_data = (uint8_t *)frec.p_data;
    
    uint8_t test_data_0[10];
    
    if(frec.p_header->record_key == A_RELEVANT_RECORD_KEY)
    {
        for(uint8_t i = 0; i < 10; i++)
        {
            test_data_0[i] = *pointer_to_record_data;
            pointer_to_record_data++;
        }
    }

    to get the record data stored in your array. 

  • How can I do data storing or printing without using CLI? Because I do not want to print data or anything else. I only want to store the data in an array that I write with fds_write. 

    I wil try to explain what I need;

    1. Write " uint8_t a[4] = { 'a', 'b', 'c', '\0' }; "  this array via FDS write
    2. Read "a[4]" from FDS 
    3. Store "a[4]" into uint8_t "b[4]" ; 
    4. The value of the b[4] (read_data) should print like this ;  NRF_LOG_HEXDUMP_INFO(b, sizeof(b));

    Result must be;

    0> <info> app:  61 62 63 00            |abc.   

    I want to do all of this without using cli.

  • Writing and reading operations using FDS are solved in the following link.

    Solution Link

Related