This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[PCA10040][SDK11] FDS unable to read [closed]

Hi

I have been able to successfully write using FDS but I'm unable to read back using the same keys. Here are my code snippets:

#define FILE_ID					0x1111
#define REC_START_ID			0x2222
uint32_t rec_count	= 0;

static void fds_timer_timeout_handler(void * p_context)
{
fds_record_t		record;
fds_record_desc_t	record_desc;
fds_record_chunk_t	record_chunk;

uint8_t temp[4] = {0,1,2,3};

record_chunk.length_words 	= 1;
record_chunk.p_data			= temp;

record.file_id		= FILE_ID;
record.key			= REC_START_ID+rec_count;
record.data.p_chunks = &record_chunk;
record.data.num_chunks = 1;

++rec_count;

ret_code_t	ret = fds_record_write(&record_desc, &record);

if(ret != FDS_SUCCESS)
{
	debug("[FDS] Write record fail, err_code: %d\n", ret);
	if(ret == FDS_ERR_NO_SPACE_IN_FLASH)
	{
		debug("max number of records %d\n", rec_count);
		nrf_delay_ms(100000);
	}
}

debug("[FDS] Writing Record key %d\n", REC_START_ID+rec_count-1);

if(rec_count == 10)
{
	app_timer_stop(m_fds_test_timer);
	debug("start reading data\n");
	for(int i=0; i<rec_count; i++)
	{
		fds_flash_record_t				flash_record;
		fds_record_desc_t				read_record_desc;
		fds_find_token_t				ftok;

		debug("[FDS] Looking for record id %d\n", REC_START_ID+i);
		ret_code_t	err_code = fds_record_find(FILE_ID, REC_START_ID+i, &read_record_desc, &ftok);
		if(err_code == FDS_SUCCESS)
		{
			if(fds_record_open(&read_record_desc, &flash_record) == FDS_SUCCESS)
			{
				uint8_t * data;
				data = (uint8_t *)flash_record.p_data;
				debug("[FDS] found record id %d\n", read_record_desc.record_id);
				for(int i=0; i<flash_record.p_header->tl.length_words; i++)
				{
					debug("[FDS] Data is 0x%8x \n", data[i]);
				}
			}
			else
			{
				debug("[FDS] Opening record failed\n");
			}

			if(fds_record_close(&read_record_desc) == FDS_SUCCESS)
			{
				debug("[FDS] Closing record success\n");
			}
			else
			{
				debug("[FDS] Closing record failed\n");
			}
		}
		else
		{
			debug("[FDS] Could not find the record, err_code: %d\n", err_code);
		}
	}
}
}

static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
switch(p_fds_evt->id)
{
	case FDS_EVT_INIT:
		if(p_fds_evt->result != FDS_SUCCESS)
		{
			debug("[FDS] Initialization Failed, error: %x\n", p_fds_evt->result);
		}
		debug("[FDS] Initialization Success\n");
		break;

	case FDS_EVT_WRITE:
		if(p_fds_evt->result != FDS_SUCCESS)
		{
			debug("[FDS] Write Failed, error: %x\n", p_fds_evt->result);
		}
		else
		{
			debug("[FDS] Write Successful and record id is %d\n",p_fds_evt->write.record_id);
		}
		break;

	case FDS_EVT_GC:
		break;

	case FDS_EVT_DEL_FILE:
		debug("[FDS] File deleted successfully\n");
		break;

	default:
		break;
}
}

Thanks in advance :)


[EDIT]

Here is the main file of code main.c


[EDIT]

I've removed the timer but still I'm getting the same problem. I've modified the main.c file to wait till the file is deleted. main.c

Here is my debug output:

start.....
[FDS] Initialization Success
[FDS] Deleting File
[FDS] Writing Record key 8738
[FDS] File deleted successfully
[FDS] Write Successful and record id is 4066 start reading data [FDS] Looking for record id 8738 [FDS] Could not find the record, err_code: 10

  • Yes, i have made sure that the fds is initialized only once. Also i think thewrite is done properly as in the FDS_EVT_WRITE I'm getting a valid record id. Is there any other method to check that the write is done properly? I have used the above code as a guide in writing my current code.

  • Providing the err_code is 10 (i.e., not able to find record) and not some other error code then it is a mystery. Perhaps avoid the increment to the record number in the fds_record_find() call itself but providing you have written successfully then that is call that should yield success.

  • I have checked it many times, the err_code is 10. In the fds_evt_handler I'm getting a valid record id on written event, so I guess it is safe to assume that the write is done successfully. I have also tried keeping the record.key same and searching with the same key, but with no success. One thing I'd lforgotten to mention is that I'm trying to read the data in a timer timeout handler. I've attached the main file as an edit to the question in case I might have missed something else that needs mentioning.

  • I'm a simple guy, so if I was you I would first check if I could find the record in main and then work up from there. Flash is asynchronous so to try and do things in an interrupt at the wrong priority is not going to be ideal.

  • I see you are calling fds_init when the peer manager does this. This can lead to strange behaviour, especially early on. Also most fds returns are call successes, not operational successes. So don't be fooled into believing something has happened because the return is nil.

Related