I am trying to save and read a structure using FDS, how do I go about it?
I have the structure:
typedef struct { uint16_t total_runhours_flash; float total_energy_consumed_flash; }energy_struct; energy_struct energy_variables;
Is this correct by populating the record as such?:
fds_record_t record; fds_record_desc_t record_desc; record.file_id = FILE_ID; record.key = ENERGY_KEY; record.data.p_data = &energy_variables; record.data.length_words = sizeof(energy_variables); fds_record_desc_t desc = {0}; rc = fds_record_update(&desc, &record); APP_ERROR_CHECK(rc);
And to read is the main problem, I can't seem to match the types in which it would load data from the structure: (This is giving error)
energy_variables = (struct energy_struct *) config.p_data;
Hi,
The first two code snippets (defining a struct and writing it with FDS looks sensible). I am not sure what you want to convey in the last. What are you trying to do? What fails? If you need a simple example you can refer to this post. You can also refer to the FDS example, but it is a bit over-complicated.
In the last snip I am trying to read the structure I saved into a structure variable of the same kind but I dont believe it is working, maybe my data types are out of place?
So from the FDS example I kind of figured it out.
The write function would be like:
fds_record_t record; fds_record_desc_t record_desc; record.file_id = FILE_ID; record.key = ENERGY_KEY; record.data.p_data = &energy_variables; record.data.length_words = ( sizeof(energy_variables) + 3 ) / sizeof(uint32_t); fds_record_desc_t desc = {0}; rc = fds_record_update(&desc, &record); APP_ERROR_CHECK(rc);
And for reading:
rc = fds_record_open(&desc, &config); APP_ERROR_CHECK(rc); memcpy(&energy_variables, config.p_data, sizeof(energy_struct));
I am facing another problem with another structure saving. The structure I have is:
typedef struct { uint8_t led_blink; //was bool uint8_t initial_adv_disable; //was bool uint8_t enable_ibeacon_Packet; //was bool uint8_t enable_cbeacon1_Health_Packet; //was bool uint8_t enable_cbeacon2_Proximity_Packet; //was bool uint8_t enable_ibeacon_SOS_Packet; //was bool uint8_t enable_Mems; //was bool }beacon_packet_struct;
My custom function for saving is:
int fds_write_struct(uint16_t file_id, uint16_t flash_key) { ret_code_t ret; fds_record_t record; fds_record_desc_t record_desc; fds_record_desc_t desc = {0}; fds_find_token_t tok = {0}; if (flash_key == BEACON_STATE_KEY) { record.file_id = FILE_ID; record.key = BEACON_STATE_KEY; record.data.p_data = &beacon_flags; //&beacon_flags; record.data.length_words = ( sizeof(beacon_packet_struct) + 3 ) / sizeof(uint32_t); ret_code_t rc = fds_record_find(FILE_ID, BEACON_STATE_KEY , &desc, &tok); //Search for record. Update if found else write a new one if (rc == FDS_SUCCESS) { NRF_LOG_INFO("Similar BEACON STATE Variables Record was found thus it is being updated"); rc = fds_record_update(&desc, &record); APP_ERROR_CHECK(rc); } else { ret_code_t ret = fds_record_write(&record_desc, &record); APP_ERROR_CHECK(ret); NRF_LOG_INFO("First time writing BEACON STATE record"); } return 1; } }
I call the function using below:
fds_write_struct(FILE_ID,BEACON_STATE_KEY);
But I get error 0x03 at:
ret_code_t ret = fds_record_write(&record_desc, &record); APP_ERROR_CHECK(ret);
Suggestion would be greatly appreciated
I believe this solved the problem:
https://devzone.nordicsemi.com/f/nordic-q-a/55153/using-fds-to-save-a-struct-structure-and-also-reading-it