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

What should I start in order to use the maximum memory space in fstorage?

Hello everybody, 

I just started using fStorage. When I examine the example, it starts from write address 0x3e000.

err_code = nrf_fstorage_write(&fstorage, 0x3e000, m_hello_world, sizeof(m_hello_world), NULL);
APP_ERROR_CHECK(err_code);

Q-1 How is this write address determined?

Q-2 For example, when I write12 bytes data, what should I start with the next write address?

Thank you.

  • in fds_test_write function, it write 4 bytes not 10;
    "static uint8_t const m_deadbeef[4] = {0x1,0x2,0x3,0x4};" 

    The write function does not work when there are 8 or more bytes. For example I modified like this;

    static uint8_t m_deadbeef[8] = {0x41,0x30,0x31,0x31,0x30,0x30,0x30,0x34}; and it worked.

    But I tried this;

    static uint8_t m_deadbeef[10] = {0x41,0x30,0x31,0x31,0x30,0x30,0x30,0x34,0x30,0x24}; and it did not work. 

    It does not work if it is bigger than eight. 

  • FormerMember
    0 FormerMember in reply to purgoufr

    The example works fine with longer arrays as well, the variable holding the data has to be declared the following way:

    static uint8_t const m_deadbeef[xx ] = ...

  • Should it be Const?  Because I am trying to save the data from nus_data. The variable type here is uint8_t * p_data (Not const) as you know.

    Also I tried this one;

    static uint8_t const m_deadbeef[10] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10};

    and Writing has not occurred. 

    static uint8_t const m_deadbeef[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; is working

    static uint8_t const m_deadbeef[10] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10}; is not working

    I have not changed anything(except m_deadbeef size) . Pretty odd! . 

  • After Kristin's help, I finally get the solution.

    I do not know exactly why, If you want to record a sequence directly, it gives an error. So I have defined external an array( m_deadbeef[10] ) in the fds_write function. Then I copied the array that I want to save into m_deadbeef. And the problem is solved.

    static ret_code_t kls_fds_write(uint32_t write_file_id, uint32_t write_record_key , uint8_t write_data[])
    {		
    		static uint8_t m_deadbeef[10] = {0};
    		
    		memcpy(m_deadbeef, write_data, sizeof(m_deadbeef));
    		
    		fds_record_t        record;
    		fds_record_desc_t   record_desc;
    	
    		// Set up record.
    		record.file_id              = write_file_id;
    		record.key              		= write_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;
    	
    }
    	
    static ret_code_t kls_fds_read(uint32_t read_file_id, uint32_t relevant_record_key , uint8_t read_data[])
    {	
    		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;
    		
    		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(read_file_id, relevant_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_INFO("Found Record ID = %d\r\n",record_desc.record_id);
    
    				data = (uint8_t *) flash_record.p_data;
    				for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
    				{
    					read_data[i] = data[i];
    				}
    			
    		
    				NRF_LOG_HEXDUMP_INFO(read_data, sizeof(read_data));
    				// 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;	
    }
    
    

  • Hi Kristin, 

    Thanks for sharing this, it is very clear and helpful. 

    I am struggling to write uint8 or uint16 into the Flash using your fds_write function. I am getting this FDS error FDS_ERR_UNALIGNED_ADDR.

    Could you help me understand why please?

    Many thanks

Related