This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

FDS: Saving my records

NRF52 DK on SDK 12.2 (using armGCC)

I have the FDS example from GITHUB working - it shows it is writing and reading the example data (0xDEADBEEF and 0xBAADF00D) fine. I want to save my own data as a record instead of the data in the example and I need assistance. Below are the variables that I need to save so I can recover them after a power outage or reboot.

uint8_t		currentSetting;
int16_t		photocellThreshold;
uint16_t	pwmValue;
bool		powerState;
uint32_t	remoteMode1;
uint32_t	remoteMode2;
uint32_t	remoteMode3;

I am not sure if I should have a separate record for each variable or if I should write all as 1 "chunk" of data. Which is better?

My problem is how do I save those different data types into FDS?

How do I recover the settings after reboot?

Also, If the user changes the setting or PWM value for instace, I will use a timer and after 20 seconds of no setting changes, I will update the data to FDS. How can I update that changed information back into FDS?

Can someone assist me? I appreciate any help!

Thanks, Bryan

  • The link I gave you is directly to the page that explains how FDS works and how to use it. You need read through to understand how it works. I know it's hard but there are just too much info that can be explained here.

  • Bryan

    You don't need to typedef the struct (as you only have one instance of it)

    So you can just do

    struct
    {
        uint8_t     currentSetting;
        int16_t     photocellThreshold;
        uint16_t    pwmValue;
        bool        powerState;
        uint32_t    remoteMode1;
        uint32_t    remoteMode2;
        uint32_t    remoteMode3;
    } myData;
    
    main()
    {
        	myData.currentSetting=12;// assign some initial value
    }
    

    I've not used the FDS, but you should just need to do something like

    record_chunk.p_data = &myData;
    record_chunk.length_words = sizeof(myData)/4; // NOTE. I DONT KNOW WHAT SIZE IS A "WORD" 
    

    Please check if a word is 4 bytes, but it appears to be as int32 (4 bytes) uses length_words = 1 infocenter.nordicsemi.com/index.jsp

  • Roger Thank you so much! I am able now to put my variables into FDS!!!! Now when I read them back using the example code,:

    while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
    {
    	fds_record_open(&record_desc, &flash_record);
    	printf("Found Record ID = %lu\r\n",record_desc.record_id);
    	printf("Data = ");
    	data = (uint32_t *) flash_record.p_data;
    	for (uint8_t i=0;i<flash_record.p_header->tl.length_words;i++)
    	{
    		printf("%lu ",data[i]);
    	}
    	printf("\r\n");
    	fds_record_close(&record_desc);
    }
    

    I get in my terminal window: Data = 262144010 65556 4401217 12345678 77777777

    My actual data was:

    myData.currentSetting=10; myData.photocellThreshold=4000; myData.powerState=1; myData.pwmValue=20; myData.remoteMode1=4401217; myData.remoteMode2=12345678; myData.remoteMode3=77777777;

    How can I read the data from FDS back into the struct format above? Thanks!!!!

  • Bryan

    You should just be able to pass the pointer to your struct to the fs function, and get it to write the raw data back into the struct.

    once its in the struct, you can access the elements like you normal, using the dot syntax.

    Or if the fs function does not simply take a pointer to the destination memory, then you would need to use memcpy to copy it to your struct

    Edit.

    It looks like you need to use memcpy to transfer the data pointed to by p_data to your struct Because the docs say that for the flash_record that

    "However, you should never modify it or use any of its fields."

    So you can't set the data pointer to your struct

    You then need to release the Record, otherwise its wasting valuable RAM

Related