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

FDS not saving variables

NRF52832 on SDK 16.0.0, softdevice s132 (using SES)
When writing constant value with FDS it saves normally, but when I try to write variable it saves some garbage. Here is my write function:

static ret_code_t fds_write_U32(uint32_t FILE_ID, uint32_t REC_KEY, uint32_t data)
{
        uint32_t m_data[1] = {data};
		fds_record_t        record;
		fds_record_desc_t   record_desc;
		// Set up data.
		record.data.p_data         = &m_data;
		record.data.length_words   = 1;
		// Set up record. 
		record.file_id             = FILE_ID;
		record.key                 = REC_KEY;
                				
		ret_code_t ret = fds_record_write(&record_desc, &record);
		if (ret != NRF_SUCCESS)
		{
				return ret;
		}
		NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
        NRF_LOG_INFO("Writing val = %d \r\n",m_data[0]);
		return NRF_SUCCESS;
}

Any ideas how to fix it? 

Parents
  • How do you read the records?

    When fds_record_write() was called twice, you will end up whith two identical (in id and key) records with possibly different data.

    That is why there is a fds_record_update() function...

    Edit: Your m_data[] buffer must not be on the stack, as fds_record_write() is a function with delayed execution (using softdevice). Due to m_data being on the stack it will be overwritten by the time the softdevice actually writes to flash.

  • Thanks for your reply! I solved this issue initializing static variable out of write function and now it works. Here is the code

    #define FILE_ID     0x1111
    #define FILE_KEY     0x2222
    
    static uint32_t data_to_write[3] = {0, 0, 0};
    
    static fds_record_t const m_dummy_record =
    {
        .file_id           = FILE_ID,
        .key               = FILE_KEY,
        .data.p_data       = &data_to_write,
        /* The length of a record is always expressed in 4-byte units (words). */
        .data.length_words = (sizeof(data_to_write) + 3) / sizeof(uint32_t),
    };
    
    static ret_code_t fds_write_U32()
    {
            data_to_write[0] = var1;
            data_to_write[1] = var2;
            data_to_write[2] = var3;
            
    		fds_record_desc_t   record_desc;
                    				
    		ret_code_t ret = fds_record_write(&record_desc, &m_dummy_record);
    		if (ret != NRF_SUCCESS)
    		{
    				return ret;
    		}
    		NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
    		return NRF_SUCCESS;
    }

Reply
  • Thanks for your reply! I solved this issue initializing static variable out of write function and now it works. Here is the code

    #define FILE_ID     0x1111
    #define FILE_KEY     0x2222
    
    static uint32_t data_to_write[3] = {0, 0, 0};
    
    static fds_record_t const m_dummy_record =
    {
        .file_id           = FILE_ID,
        .key               = FILE_KEY,
        .data.p_data       = &data_to_write,
        /* The length of a record is always expressed in 4-byte units (words). */
        .data.length_words = (sizeof(data_to_write) + 3) / sizeof(uint32_t),
    };
    
    static ret_code_t fds_write_U32()
    {
            data_to_write[0] = var1;
            data_to_write[1] = var2;
            data_to_write[2] = var3;
            
    		fds_record_desc_t   record_desc;
                    				
    		ret_code_t ret = fds_record_write(&record_desc, &m_dummy_record);
    		if (ret != NRF_SUCCESS)
    		{
    				return ret;
    		}
    		NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
    		return NRF_SUCCESS;
    }

Children
No Data
Related