I am able to initialize the module and get the callback , but when I try to write I will not get the callback after the write, and no errors.
If I simply try to read after write he will not find anything to read.
Please help, I'v been trying 2 days without any clue. I guess again there is some hidden parameter that nobody wrote on the docs.
This is the output :
:INFO:STARTED
:INFO:fds_evt_handler with : 0
:INFO: init good!
:INFO:Writing Record ID = 1
:INFO:Writing Record ID = 2
This is the program :
static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
// ****this line will log ONLY on init event, not on write event
NRF_LOG_INFO("fds_evt_handler with : %d\r\n",p_fds_evt->id);
switch (p_fds_evt->id)
{
case FDS_EVT_INIT:
if (p_fds_evt->result != FDS_SUCCESS)
{
NRF_LOG_INFO("failed initializing\r\n");
}
else
{
NRF_LOG_INFO(" init good!\r\n");
init_flag=1;
}
break;
case FDS_EVT_WRITE:
write_flag=1;
if (p_fds_evt->result == FDS_SUCCESS)
{ NRF_LOG_INFO("finish write\r\n"); }
else
{ NRF_LOG_INFO("failed write with %d \r\n",p_fds_evt->result); }
break;
case FDS_EVT_UPDATE:
write_flag=1;
if (p_fds_evt->result == FDS_SUCCESS)
{NRF_LOG_INFO("finish write\r\n"); }
else
{ NRF_LOG_INFO("failed updated with %d \r\n",p_fds_evt->result); }
break;
default:
break;
}
}
void initStorage()
{
ret_code_t ret = fds_register(fds_evt_handler);
if (ret != FDS_SUCCESS)
{
NRF_LOG_INFO("FAILED TO INIT STORAGE\r\n");
}
ret = fds_init();
if (ret != FDS_SUCCESS)
{
NRF_LOG_INFO("ERROR INIT\r\n");
}
}
void write()
{
#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)
{
NRF_LOG_INFO("failed write with %d \r\n",ret);
}
NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
ret = fds_record_write(&record_desc, &record);
if (ret != FDS_SUCCESS)
{
NRF_LOG_INFO("ERROR WROTE WITH %d\r\n",ret);
}
NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
}
void read ()
{
NRF_LOG_INFO("START READING\r\n");
#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;
// 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)
{
NRF_LOG_INFO("error read: %d\r\n",err_code);
}
NRF_LOG_INFO("Found Record ID = %d\r\n",record_desc.record_id);
NRF_LOG_INFO("Data = ");
data = (uint32_t *) flash_record.p_data;
for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
{
NRF_LOG_INFO("0x%8x ",data[i]);
}
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)
{
NRF_LOG_INFO("error read: %d\r\n",err_code);
}
}
}
void find_and_delete (void)
{
#define FILE_ID 0x1111
#define REC_KEY 0x2222
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(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
{
fds_record_delete(&record_desc);
NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
}
ret_code_t ret = fds_gc();
if (ret != FDS_SUCCESS)
{
NRF_LOG_INFO("error find: %d\r\n",ret);
}
}
static void sys_evt_dispatch(uint32_t sys_evt)
{
fs_sys_event_handler(sys_evt);
}
int main(void)
{
log_init();
NRF_LOG_INFO("STARTED\r\n");
setupBluetoothWithDelegate(BluetoothDelegate);
// fds
initStorage();
find_and_delete();
write();
//wait until the write is finished.
while (write_flag==0);
read();
advertise();
for (;;)
{
power_manage();
}
}