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 ?

  • my 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);   //(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;
    	
    }

    my 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;
    		/* 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 *)flash_record.p_data;
    
    				if(flash_record.p_header->record_key == relevant_record_key)
    				{
    					for(uint8_t i = 0; i <flash_record.p_header->length_words; 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 write function in save function

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

    save function used in nus_data_handler

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
    	memset(nus_rx_data, 0, sizeof(nus_rx_data));
    	
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
            NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
    			
            for (uint8_t i = 0; i < p_evt->params.rx_data.length; i++)
            {
    			nus_rx_data[i]= p_evt->params.rx_data.p_data[i];
    		}
    
    		save_data(nus_rx_data);
    
        }
    }

    So I want to;

    1- write "aaaaaaaaaa" in nRF Toolbox-> UART

    2- store the "aaaaaaaaaa" in static uint8_t nus_rx_data[16]; 

    3- write this nus_rx_data array with save function; "save_ssn(nus_rx_data);"

    4- Read the saved data so that you can use the data again when the power of the device is turned off;

    uint8_t dest_data_0[16] = {0};
    while (flag_write_fds == 0);
    kls_fds_read(FILE_ID, RECORD_KEY, dest_data_0);

  • I tried the memcpy 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;
    	/* 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);
    
    		memcpy(read_data, flash_record.p_data, flash_record.p_header->length_words * sizeof(uint32_t));
    				
    		NRF_LOG_INFO("Data: %u, Length: %u", read_data, flash_record.p_header->length_words * sizeof(uint32_t));
    				
    		// 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 sent nus_data"A01100040" to write in fds

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

    Result;

    0> <info> app: Found Record ID = 1
    0>
    0> <info> app: Data: 536895120, Length: 4

    Unfortunately, I still could not write the data I wanted, and then I could not get the data itself (in uint8_t format). 

  • Hi Haakonsh,

    The write function does not work when there are 8 or more bytes. For example I modified like this;

    static uint8_t m_deadbeef[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; and it worked.

    static ret_code_t kls_fds_write(uint32_t write_file_id, uint32_t write_record_key , uint8_t write_data[])
    {		
    	//static uint32_t const m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
    		static uint8_t m_deadbeef[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
    		fds_record_t        record;
    		fds_record_desc_t   record_desc;
    	
    		// Set up record.
    		record.file_id              = write_file_id;
    		record.key              		= write_record_key;
    		record.data.p_data       		= &m_deadbeef;
    		record.data.length_words   	= sizeof(m_deadbeef)/sizeof(uint8_t);
    				
    		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 kls_fds_read(uint32_t read_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 ={0};//Important, make sure you zero init the ftok token
    		uint8_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(read_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);
    
    				data = (uint8_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_HEXDUMP_INFO(read_data, sizeof(read_data));
    				// 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;	
    }

    But I tried this;

    static uint8_t m_deadbeef[10] = {0x41,0x30,0x31,0x31,0x30,0x30,0x30,0x34,0x30,0x24}; and it did not work. 

    It does not work if it is bigger than eight. 

    Do you have any suggestion for that?

  • Hi Haakonsh,

    Thank you very much for your interest. I reach the solution through another question. But I learned a lot from your answers. So I wanted to share the situation with you. 

    Solution Link

  • Hey purgoufr,

    This is strange. Maybe a compiler optimization issue or something. Have you tried declaring the m_deadbeef array as a 'const'?

    Anyway I'm glad that you got to move on with your development. I hate it when I get stuck on issues like these.

    Cheers,

    Håkon.

Related