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

Read issue in fds

Hello All,

I am working on the NRF52810 board with 15.3 sdk in segger IDE.

I have implemented fds with 2 key with 1 file id to store data.

I am storing 2 - 3  1 byte data in flash.

While updating the data in flash , it is working properly.

but after that if i call read function the it is reading previous data that is store .

If i reset the device then it is reading new data that is updated previously.

my sequence is like below for updating data...

      

      err_code = fds_test_find_and_delete2();
      APP_ERROR_CHECK(err_code);
      err_code =fds_test_write2();
      APP_ERROR_CHECK(err_code);

After update read new value...


     err_code = fds_read2();
     APP_ERROR_CHECK(err_code);

but here it is reading previous value, not updated. (here fds init is main)

It always  read previous value after update and after reset it is reading actual updated value.

How to solve this or what modification is required from my side...

Waiting for your response...

Regards,

Rohit

Parents
  • Hi Rohit,

    I cannot see clearly what you do from your code snippets, but there are a few typical reasons for not reading back the updated data:

    • Not waiting for the flash operation to finish before trying to read.
    • Not using FDS API to get updated data, just reading a pointer to the old location of the original data.

    Does one of these match what you do? If not, please post more of your code so that I can see how you use the FDS API and the logic where you wait for the update operation to finish before you read it back.

  • Hi,

    Thanks for reply.

    My fds read write code is below...

    static void my_fds_evt_handler(fds_evt_t const * const p_fds_evt)
    {
        switch (p_fds_evt->id)
        {
            case FDS_EVT_INIT:
                if (p_fds_evt->result != FDS_SUCCESS)
                {
                    // Initialization failed.
                }
                break;
            case FDS_EVT_WRITE:
                if (p_fds_evt->result == FDS_SUCCESS)
                {
                        write_flag_fds_test=1;
                }
                break;
            default:
                break;
        }
    }
    
    static ret_code_t fds_test_write(void)
    {
                    //static uint32_t const m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
                    //static uint8_t const m_deadbeef[8] = {0x00,0xA2,0xA3,0xA4,0x22,45,12,32};
    		fds_record_t        record;
    		fds_record_desc_t   record_desc;
    
    		// Set up data.
                    //for (int x = 0; x<8;x++)
                    //{
                      //m_deadbeef[0] = array_update[2];
                    //}
    
    		// Set up record.
    		record.file_id              = FILE_ID_FDS_TEST;
    		record.key                  = REC_KEY_FDS_TEST;
    
          temp_data = m_deadbeef[0];
    
    		record.data.p_data       = &temp_data;
    		//record.data.length_words   = sizeof(m_deadbeef)/sizeof(uint32_t);
    		record.data.length_words   = sizeof(m_deadbeef)/sizeof(uint8_t);
    				
                                    record_desc.record_id =1;
    		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 fds_read(void)
    {
                    //bsp_board_init(BSP_INIT_LEDS);
                    //bsp_board_led_off(ADVERTISING_LED);
    
    
    		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
    		//uint32_t *data;
    		uint8_t *data;
    		uint32_t err_code;
                    uint8_t i=0;
    		
                    write_flag_fds_test = 0;
    
    		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(FILE_ID_FDS_TEST, REC_KEY_FDS_TEST, &record_desc, &ftok) == FDS_SUCCESS)
                    //while (fds_record_find_in_file(FILE_ID_FDS_TEST, &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);
    				NRF_LOG_INFO("Data = ");
    				//data = (uint32_t *) flash_record.p_data;
    				data = (uint8_t *) flash_record.p_data;
    				for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
    				{
    					NRF_LOG_INFO("%d ",(data[i]));
    				}
    				NRF_LOG_INFO("\r\n");
                                    NRF_LOG_INFO("%d ",(data[i]));
                                    temp_name_id[0] = data[0];
    
    
    //                                data[2]= 45;
    //                                NRF_LOG_INFO("0x%8x ",data[2]);
    //                                NRF_LOG_INFO("0x%8x ",data[3]);
    //                                if(record_desc.record_id == 1)
    //                                {
    //                                  for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
    //                                  {
    //                                        NRF_LOG_INFO("%x",data[i]);
    //                                  }
    //                                  //NRF_LOG_INFO("0x%8x ",data[2]);
    //                                }
    //                                bsp_board_init(BSP_INIT_LEDS);
    //                                bsp_board_led_on(ADVERTISING_LED);
                                    //fds_record_find_in_file()
    				// 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;
    		
    }
    
    static ret_code_t fds_test_find_and_delete (void)
    {
    
    		fds_record_desc_t   record_desc;
    		fds_find_token_t    ftok;
    	
    		ftok.page=0;
    		ftok.p_addr=NULL;
    		// Loop and find records with same ID and rec key and mark them as deleted. 
    		while (fds_record_find(FILE_ID_FDS_TEST, REC_KEY_FDS_TEST, &record_desc, &ftok) == FDS_SUCCESS)
    		{
    			fds_record_delete(&record_desc);
    			NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
    		}
    		// call the garbage collector to empty them, don't need to do this all the time, this is just for demonstration
    		ret_code_t ret = fds_gc();
    		if (ret != FDS_SUCCESS)
    		{
    				return ret;
    		}
    		return NRF_SUCCESS;
    }
    
    static ret_code_t fds_test_init (void)
    {
    	
    		ret_code_t ret = fds_register(my_fds_evt_handler);
    		if (ret != FDS_SUCCESS)
    		{
    					return ret;
    				
    		}
    		ret = fds_init();
    		if (ret != FDS_SUCCESS)
    		{
    				return ret;
    		}
    		
    		return NRF_SUCCESS;
    		
    }
    

    Please provide your input or any modification...

    Regards,

    Rohit

  • Hi Rohit,

    I do not see any indication that you wait for the FDS write operation to complete before you try to read it back. If you try to read immediately after scheduling a write, the write will typically not have occurred yet, which is why you are not able to read the updated data (in fact, they are not updated yet).

    Please take note that FDS is asynchronous in nature, so you must wait for the operation to succeed first. You will get an event when the operation has completed.One simple way of implementing this in this test is to use a volatile variable that you wait for in your read function and set in the FDS event handler. Just remember that with this approach your read function should not run in an interrupt priority, since then it could prevent the FDS event handler from running, effectively resulting in a deadlock.

  • Hi Einar,

    Thanks for your detail explanation and the information.

    I do not see any indication that you wait for the FDS write operation to complete before you try to read it back.

    yes i will update as per your suggestion and update my code.

    Regards,

    Rohit

Reply Children
No Data
Related