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

Write and read several various records key in the same file of flash using FDS

Hi everybody,

I am currently trying to write in flash several records key in a same file using fds_write, fds_update, fds_records_find.

My page is FILE_ID = 0x1000 and my 3 record keys are: ITEM_INTRO = 0x01, ITEM_DATA = 0x02 and ITEM_TOKEN = 0x03.

These 3 record keys are associated to 3 tab respectively: learning_tab_intro, learning_tab_data and learning_tab token.

Whatever the records key called, it seems that the data is written or read at the same place in flash, that way when I shut down and next turn on my system, when I read the ITEM_INTRO records key I can find the data of record key  ITEM_DATA and ITEM_TOKEN in.

Here is the learning_tab_intro that I wrote :

Here is the learning_tab_data that I wrote :

Here i the learning_tab_token that I wrote :

Here is the learning_tab_intro that I read after shutting the system down:

We see clearly that the ITEM_TOKEN record has been written in the ITEM_INTRO record

Inspired by the solution of this link https://devzone.nordicsemi.com/f/nordic-q-a/32208/what-should-i-start-in-order-to-use-the-maximum-memory-space-in-fstorage I created these two functions :

Here is my write function :

ret_code_t fds_function_write(uint8_t item , uint8_t size,uint16_t tab[])
{		
	static uint32_t write_tab[LEARNING_TAB_SIZE/2] ={0};//max size

	memcpy(write_tab, tab, size*2);//in number of byte, 1 uint16 = 2 bytes
	
	fds_record_t        record;
	fds_record_desc_t   record_desc;

	// Set up record.
	record.file_id              = FILE_ID;
	record.key              		= item;
	record.data.p_data       		= &write_tab;
	record.data.length_words   	= (size+1)/2;//from uint16 to uint32, 1 uint16 = 0.5 uint32
 	
	fds_find_token_t    ftok ={0};//Important, make sure you zero init the ftok token
	if(fds_record_find(FILE_ID, item, &record_desc, &ftok) == FDS_SUCCESS)
	{
		ret_code_t ret = fds_record_update(&record_desc, &record);
		if (ret != FDS_SUCCESS)
		{
			return ret;
		}

		NRF_LOG_INFO("Updating Record ID = %d \r\n",record_desc.record_id);
		
		ret = fds_gc();
		if (ret != FDS_SUCCESS)
		{
			return ret;
		}
		NRF_LOG_INFO("FDS_GC = ok \r\n");
	}
	else
	{
		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;
}

And here my read function :

ret_code_t fds_function_read(uint8_t item , uint8_t size, uint16_t tab[])
{	
	static uint16_t read_tab[LEARNING_TAB_SIZE] = {0};//max size
		
	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
	uint16_t *data;
	uint32_t err_code;

	err_code = fds_record_find(FILE_ID, item, &record_desc, &ftok);
	if ( err_code == 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 = (uint16_t *) flash_record.p_data;

		for (uint8_t i=0;i<((flash_record.p_header->length_words)*2);i++)//1 word = 1 uint32 = 2 uint16
		{
			read_tab[i] = data[i];
		}
	    memcpy(tab, read_tab, (size));

		// Close the record when done.
		err_code = fds_record_close(&record_desc);
		if (err_code != FDS_SUCCESS)
		{
			return err_code;	
		}
	}
	else
	{
		return err_code;
	}
	return NRF_SUCCESS;	
}

Do you see any mistake or have you got an idea of how could I resolve my issue ?

I thanks you all in advance !

Related