Good Afternoon,
I'm writing some config data on an empty record the first time I turn on my board and updating that same record every next time I turn on the board.
While the writing of the first data is correctly written with fds_record_write() - when I check the data with fds_record_open() -, the data is totally wrongly written when I update the record with new config data, with fds_record_update().
When the board initialises I initialise FDS and call flash_write_record_setup(&DEVICE_struct_w_config2write) after initialising the softdevice.
By looking into the code below, do you have any cue why the data is not correctly written when I update the record?
Thank you,
João Oliveira
/* Declarations in flash.h */
/* File ID and Key used for the configuration record. */
#define CONFIG_FILE (0xF010)
#define CONFIG_REC_KEY (0x7010)
typedef struct
{
uint8_t enable;
uint8_t time_adv;
}ADVERTISE;
typedef struct
{
uint8_t state;
ADVERTISE adv2;
}DEVICE;
/* flash_write_record_setup function in flash.c */
void flash_write_record_setup(DEVICE * config2write)
{
ret_code_t rc;
fds_record_desc_t desc = {0};
fds_find_token_t tok = {0};
//CONFIG_FILE and CONFIG_REC_KEY defined in flash.h
rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);
if (rc == FDS_SUCCESS) //record exists => update it
{
/* A config file is in flash. Let's update it. */
fds_flash_record_t config = {0};
/* Open the record and read its contents. */
rc = fds_record_open(&desc, &config);
APP_ERROR_CHECK(rc);
/* Close the record when done reading. */
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
//compare existing config in flash with new one
uint8_t ret = memcmp(config2write, config.p_data, sizeof(DEVICE));
if(ret != 0) //if different update config in flash
{
/* Write the updated record to flash. */
fds_record_t const rec =
{
.file_id = CONFIG_FILE,
.key = CONFIG_REC_KEY,
.data.p_data = config2write,
.data.length_words = (sizeof(DEVICE) + 3) / sizeof(uint32_t)
};
rc = fds_record_update(&desc, &rec);
APP_ERROR_CHECK(rc);
m_write_finished = false;
}
else
{
/* Same config data as previous, nothing to be done */
}
}
else /* System config not found; write a new one. */
{
fds_record_t const rec =
{
.file_id = CONFIG_FILE,
.key = CONFIG_REC_KEY,
.data.p_data = config2write,
.data.length_words = (sizeof(DEVICE) + 3) / sizeof(uint32_t)
};
rc = fds_record_write(&desc, &rec);
APP_ERROR_CHECK(rc);
m_write_finished = false;
}
}