Hi,
I am trying to read and write data from flahs using the fds library. For the moment my main problem is that when calling fds_record_find, it returns FDS_ERR_NOT_INITIALIZED (0x02). I tried to understand why, and I found that m_flags is set to 0x05 (FDS_FLAG_INITIALIZING | FDS_FLAG_PROCESSING) after the end of fds_init(). fds_record_find is checking if m_flags is set to FDS_FLAG_INITIALIZED (0x02), which returns this error. fds_init returns FDS_SUCCESS.
I am using SDK v11.0.0 and softdevice s130 v2.0.0. My target is an nRF51822 QFACA1
Here is my code concerning the fds module. flash_init is called after all the ble init functions excepted advertising_start :
static void fds_evt_handler(fds_evt_t const * const p_evt){
switch (p_evt->id)
{
case FDS_EVT_INIT:
if (p_evt->result != FDS_SUCCESS)
{
// Initialization failed.
}
break;
case FDS_EVT_WRITE:
if (p_evt->result == FDS_SUCCESS)
{
write_flag=1;
}
break;
default:
break;
}
}
/**@brief Function to read the data from the flash on initialization
*/
void flash_init(){
#ifdef TEST_FLASH
static fds_find_token_t test_token = {0};
static fds_flash_record_t test_record;
uint32_t ret_code = fds_register(fds_evt_handler);
if (ret_code == FDS_ERR_USER_LIMIT_REACHED)
SEGGER_RTT_WriteString(0, "fds_register : FDS_ERR_USER_LIMIT_REACHED.\n");
else if (ret_code == FDS_SUCCESS)
SEGGER_RTT_WriteString(0, "fds_register : FDS_SUCCESS.\n");
else
SEGGER_RTT_printf(0, "fds_register unknown error code : 0x%x.\n", ret_code);
ret_code = fds_init();
if (ret_code == FDS_ERR_NO_PAGES)
SEGGER_RTT_WriteString(0, "fds_init : FDS_ERR_NO_PAGES.\n");
else if (ret_code == FDS_SUCCESS)
SEGGER_RTT_WriteString(0, "fds_init : FDS_SUCCESS.\n");
else
SEGGER_RTT_printf(0, "fds_init unknown error code : 0x%x.\n", ret_code);
nrf_delay_ms(1000);
uint32_t record_find = fds_record_find(test_file_id, test_record_key, &test_descriptor, &test_token);
SEGGER_RTT_printf(0, "fds_record_find error code : 0x%x.\n", record_find);
while(record_find==FDS_SUCCESS){
ret_code = fds_record_open(&test_descriptor, &test_record);
if (ret_code == FDS_ERR_NULL_ARG)
SEGGER_RTT_WriteString(0, "fds_record_open : FDS_ERR_NULL_ARG.\n");
else if (ret_code == FDS_SUCCESS)
SEGGER_RTT_WriteString(0, "fds_record_open : FDS_SUCCESS.\n");
else if (ret_code == FDS_ERR_NOT_FOUND)
SEGGER_RTT_WriteString(0, "fds_record_open : FDS_ERR_NOT_FOUND.\n");
else
SEGGER_RTT_printf(0, "fds_record_open unknown error code : %d.\n", ret_code);
memcpy(&test, test_record.p_data, 4);
ret_code = fds_record_close(&test_descriptor);
if (ret_code == FDS_ERR_NULL_ARG)
SEGGER_RTT_WriteString(0, "fds_record_close : FDS_ERR_NULL_ARG.\n");
else if (ret_code == FDS_SUCCESS)
SEGGER_RTT_WriteString(0, "fds_record_close : FDS_SUCCESS.\n");
else if (ret_code == FDS_ERR_NOT_FOUND)
SEGGER_RTT_WriteString(0, "fds_record_close : FDS_ERR_NOT_FOUND.\n");
else if (ret_code == FDS_ERR_NO_OPEN_RECORDS)
SEGGER_RTT_WriteString(0, "fds_record_close : FDS_ERR_NO_OPEN_RECORDS.\n");
else
SEGGER_RTT_printf(0, "fds_record_close unknown error code : %d.\n", ret_code);
record_find = fds_record_find(test_file_id, test_record_key, &test_descriptor, &test_token);
if (record_find != FDS_SUCCESS)
SEGGER_RTT_printf(0, "fds_record_find error code : 0x%x.\n", record_find);
}
SEGGER_RTT_printf(0, "test 0x%#08x.\n", test);
SEGGER_RTT_WriteString(0, "Flash init terminated.\n");
}
static ret_code_t fds_test_find_and_delete (void)
{
fds_record_desc_t record_desc;
fds_find_token_t ftok;
ftok.page=0;
ftok.p_addr=NULL;
// Loop and find records with same ID and rec key and mark them as deleted.
while (fds_record_find(test_file_id, test_record_key, &record_desc, &ftok) == FDS_SUCCESS)
{
fds_record_delete(&record_desc);
SEGGER_RTT_printf(0,"Deleted record ID: %d \r\n",record_desc.record_id);
}
// call the garbage collector to empty them, don't need to do this all the time, this is just for demonstration
ret_code_t ret = fds_gc();
if (ret != FDS_SUCCESS)
{
return ret;
}
return NRF_SUCCESS;
}
void power_off(void){
test = 0x12345678;
static fds_record_chunk_t test_chunk;
fds_record_t test_record;
nrf_drv_gpiote_in_event_disable(RTC_NIRQ);
//Turn ON the LED display for power off
twi_enable();
displayOnOff();
nrf_delay_ms(2000);
displayOff();
/*if(nOFFindex<5){
nOFF[nOFFindex]=n;
nOFFindex++;
}*/
n++;
test_record.file_id = test_file_id;
test_record.key = test_record_key;
test_chunk.p_data = &test;
test_chunk.length_words = 1;
test_record.data.p_chunks = &test_chunk;
test_record.data.num_chunks = 1;
SEGGER_RTT_WriteString(0,"Flash writing begins : fds_test_find_and_delete(). \n");
uint32_t err_code = fds_test_find_and_delete();
if (err_code != FDS_SUCCESS)
SEGGER_RTT_printf(0, "fds_test_find_and_delete err_code : 0x%x. \n", err_code);
//uint32_t err_code = fds_record_update(&test_descriptor, &test_record);
SEGGER_RTT_WriteString(0,"fds_record_write. \n");
err_code = fds_record_write(&test_descriptor, &test_record);
if (err_code == FDS_SUCCESS)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_SUCCESS. \n");
else if (err_code == FDS_ERR_NOT_INITIALIZED)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_NOT_INITIALIZED. \n");
else if (err_code == FDS_ERR_INVALID_ARG)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_INVALID_ARG. \n");
else if (err_code == FDS_ERR_UNALIGNED_ADDR)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_UNALIGNED_ADDR. \n");
else if (err_code == FDS_ERR_RECORD_TOO_LARGE)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_RECORD_TOO_LARGE. \n");
else if (err_code == FDS_ERR_NO_SPACE_IN_QUEUES)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_NO_SPACE_IN_QUEUES. \n");
else if (err_code == FDS_ERR_NO_SPACE_IN_FLASH)
SEGGER_RTT_WriteString(0, "fds_record_update : FDS_ERR_NO_SPACE_IN_FLASH. \n");
else
SEGGER_RTT_printf(0, "fds_record_update unknown error code : 0x%x. \n", err_code);
SEGGER_RTT_WriteString(0,"wait until writing is finished. \n");
while (write_flag==0); //Wait until writing is finished
//APP_ERROR_CHECK(err_code);
SEGGER_RTT_WriteString(0,"Shutdown \n");
nrf_gpio_pin_clear(EN);
}
Best regards,
Guillaume