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

Use flash data storage to write and read back record of specific record_id

I am using this flash data storage as reference github.com/.../nRF52-fds-example

Running fds_test_write() below, it writes to a file specified by FILE_ID, REC_KEY but to different record_id all the time. So, when reading back, I will not know exactly which record_id to read back.

static ret_code_t fds_test_write(void)
{
		#define FILE_ID     0x1111
		#define REC_KEY     0x2222
		static uint32_t const m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
		fds_record_t        record;
		fds_record_desc_t   record_desc;
		fds_record_chunk_t  record_chunk;
		// Set up data.
		record_chunk.p_data = m_deadbeef;
		record_chunk.length_words = 2;
		// Set up record.
		record.file_id = FILE_ID;
		record.key = REC_KEY;
		record.data.p_chunks = &record_chunk;
		record.data.num_chunks = 1;
				
		ret_code_t ret = fds_record_write(&record_desc, &record);
		if (ret != FDS_SUCCESS)
		{
				return ret;
		}
		 NRF_LOG_PRINTF("Writing Record ID = %d \r\n",record_desc.record_id);
		return NRF_SUCCESS;
}

Below is the code to read back the record. It reads back all the records belonging to FILE_ID and REC_KEY. What I will prefer is to have a write function that writes to a specific record_id and a read function that reads from a specific record_id. Something like;

write_record(FILE_ID, REC_KEY, record_id);

read_record(FILE_ID, REC_KEY, record_id);

Is this possible with SDK_11 s130 on nRF51-dk?

static ret_code_t fds_read(void)
{
		#define FILE_ID     0x1111
		#define REC_KEY     0x2222
		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
		uint32_t *data;
		uint32_t err_code;
		
		NRF_LOG_PRINTF("Start searching... \r\n");
		// Loop until all records with the given key and file ID have been found.
		while (fds_record_find(FILE_ID, REC_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_PRINTF("Found Record ID = %d\r\n",record_desc.record_id);
				NRF_LOG_PRINTF("Data = ");
				data = (uint32_t *) flash_record.p_data;
				for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
				{
					NRF_LOG_PRINTF("0x%8x ",data[i]);
				}
				NRF_LOG_PRINTF("\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;
		
}
Related