SDK 14.0,
IC:NRF52832
I want to save some data in the flash via the FDS. firstly i write 3 records then the next I want to read them one by one according to the record key.
Code for me is(I add the fds code used nRF5_SDK_14.0.0_3bcc1f7\examples\ble_peripheral\ble_app_blinky):
//============================================================================================
#include "fds.h"
uint8_t init_flag = 0;
uint8_t write_flag = 0;
uint8_t delete_flag= 0;
#define afile_id 0x0001
//---------------------------------------------------------------------------------
static void fds_evt_handler(fds_evt_t const * p_fds_evt)
{
switch (p_fds_evt->id)
{
case FDS_EVT_INIT:
if (p_fds_evt->result != FDS_SUCCESS)
{
// Initialization failed.
}
init_flag = 1;
NRF_LOG_PRINTF("FDS Init ok\n");
break;
case FDS_EVT_GC:
{
NRF_LOG_PRINTF("GC completed\n");
}
break;
case FDS_EVT_WRITE:
if (p_fds_evt->result == FDS_SUCCESS)
{
write_flag=1;
NRF_LOG_PRINTF("FDS Write ok\n");
}
break;
case FDS_EVT_DEL_RECORD:
if (p_fds_evt->result == FDS_SUCCESS)
{
delete_flag=1;
NRF_LOG_PRINTF("FDS Delete Rec ok\n");
}
break;
default:
break;
}
}
//--------------------------------------------------------------------------------------------
ret_code_t fds_test_init (void)
{
ret_code_t ret = fds_register(fds_evt_handler);
if (ret != FDS_SUCCESS)
{
return ret;
}
ret = fds_init();
if (ret != FDS_SUCCESS)
{
return ret;
}
NRF_LOG_PRINTF("fds_test_init return ok\n");
return NRF_SUCCESS;
}
//======================================================================================
uint32_t zk_fds_write(uint16_t file_id, uint16_t record_key, uint8_t wrt_data[])
{
static uint8_t m_deadbeef[10] = {0};
memcpy(m_deadbeef, wrt_data, sizeof(m_deadbeef));
fds_record_t record;
fds_record_desc_t record_desc;
// Set up record.
record.file_id = file_id;
record.key = record_key;
record.data.p_data = &m_deadbeef;
record.data.length_words = sizeof(m_deadbeef)/sizeof(uint8_t);
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;
}
//---------------------------------------------------------------------------------------------
uint32_t zk_fds_read(uint16_t file_id, uint16_t record_key)
{
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
uint8_t *data;
uint32_t err_code;
//memset(&ftok, 0x00, sizeof(fds_find_token_t));
NRF_LOG_INFO("Start searching... \r\n");
// Loop until all records with the given key and file ID have been found.
while (fds_record_find(file_id, record_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 = (uint8_t *) flash_record.p_data;
for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
{
NRF_LOG_PRINTF("0x%2x ",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;
}
//------------------------------------------------------------------------------------------------
int main(void)
{
uint32_t err_code = NRF_SUCCESS;
// Initialize.
leds_init();
timers_init();
log_init();
buttons_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
NRF_LOG_PRINTF("Blinky example started0.\n");
//---------------------------------------------
err_code = fds_test_init();
while(err_code != NRF_SUCCESS);
#if 1 //tag0 ---write 3 records
uint8_t a_buf[10]={1,2,3,4,5,6,7,8,9,0};
write_flag = 0;
err_code = zk_fds_write(afile_id, 1, a_buf); //write record 1 -a_buf[10], record key as 1
while(err_code != NRF_SUCCESS);
//while(write_flag==0);
uint8_t b_buf[10]= {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x10};
err_code = zk_fds_write(afile_id, 2, b_buf);//write record 2 -b_buf[10], record key as 2
while(err_code != NRF_SUCCESS);
uint8_t c_buf[10]= {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x20};
err_code = zk_fds_write(afile_id, 3, c_buf);//write record 3 -c_buf[10], record key as 3
while(err_code != NRF_SUCCESS);
#endif
NRF_LOG_INFO("Read record:."); //Read the 3 records
err_code = zk_fds_read(afile_id, 1); //Read record 1 by record key ==1
while(err_code != NRF_SUCCESS);
err_code = zk_fds_read(afile_id, 2);//Read record 2 by record key ==2
while(err_code != NRF_SUCCESS);
err_code = zk_fds_read(afile_id, 3);//Read record 3 by record key ==3
while(err_code != NRF_SUCCESS);
//----------------------------------------------
// Start execution.
NRF_LOG_INFO("Blinky example started1.");
advertising_start();
// Enter main loop.
for (;;)
{
if (NRF_LOG_PROCESS() == false)
{
power_manage();
}
}
}
this time the app exec result:
next step: I trun off the write function, code as following:
int main(void)
{
uint32_t err_code = NRF_SUCCESS;
// Initialize.
leds_init();
timers_init();
log_init();
buttons_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
NRF_LOG_PRINTF("Blinky example started0.\n");
//---------------------------------------------
err_code = fds_test_init();
while(err_code != NRF_SUCCESS);
//delete the write record code here. only read.
NRF_LOG_INFO("Read record:.");
err_code = zk_fds_read(afile_id, 1);
while(err_code != NRF_SUCCESS);
err_code = zk_fds_read(afile_id, 2);
while(err_code != NRF_SUCCESS);
err_code = zk_fds_read(afile_id, 3);
while(err_code != NRF_SUCCESS);
//----------------------------------------------
// Start execution.
NRF_LOG_INFO("Blinky example started1.");
advertising_start();
// Enter main loop.
for (;;)
{
if (NRF_LOG_PROCESS() == false)
{
power_manage();
}
}
}
now the result is:
The result does not match my expectations. why return the same record, the last record writed?
I want it can return 1,2,3,4,5,6,7,8,9,0 when I transfer the record key equal 1,
return 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x10 when record key is 2.
return 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x20 when record key is 3.
So I am wonder how to get the function my expected?