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

How to erase record from flash in nRF52832??

Hello,

I am use nRF52832 chip in my project.

i am write data in flash by crating record id. For write record in flash I use below code,

ret_code_t fds_write(uint16_t FILE_ID, uint16_t REC_KEY, uint8_t indication)
{
        ret_code_t err_code;
        uint8_t i = 0;
	
        fds_record_t        record;
        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

        err_code = fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok);
	if (err_code == FDS_SUCCESS)
	{
//            fds_test_find_and_delete(FILE_ID, REC_KEY);

            err_code = fds_record_open(&record_desc, &flash_record);
            if ( err_code != FDS_SUCCESS)
            {
                    return err_code;		
            }
                            
            memcpy(&m_res_cfg, flash_record.p_data, sizeof(res_configuration_t));

            m_res_cfg.adc_max = reg_adc_max_value;
            m_res_cfg.adc_min = reg_adc_min_value;

            NRF_LOG_INFO("\r\n");
            // 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;	
            }
            err_code = fds_record_update(&record_desc, &rec_res);
            APP_ERROR_CHECK(err_code);

            NRF_LOG_INFO("SETTINGS WRITE");NRF_LOG_FLUSH();
	}
        else
        {

           ret_code_t ret = fds_record_write(&record_desc, &rec_res);
           if (ret != FDS_SUCCESS)
           {
              return ret;
           }
           NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
        }
        return NRF_SUCCESS;
}

For read flash I use below code,

ret_code_t fds_read(uint16_t FILE_ID, uint16_t REC_KEY, uint8_t indication)
{
        ret_code_t err_code;
        uint8_t i = 0;

	fds_flash_record_t  flash_record;
	fds_record_desc_t   record_desc ={0};
	fds_find_token_t    ftok ={0};//Important, make sure you zero init the ftok token
		
	NRF_LOG_INFO("Start searching... \r\n");
//        NRF_LOG_INFO("File ID = %d Rec key = %d\r\n",FILE_ID,REC_KEY);

	// Loop until all records with the given key and file ID have been found.
	if(fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
	{
		err_code = fds_record_open(&record_desc, &flash_record);
		if ( err_code != FDS_SUCCESS)
		{
			return err_code;		
		}

                memcpy(&m_res_cfg, flash_record.p_data, sizeof(res_configuration_t));

                reg_adc_max_value = m_res_cfg.adc_max;
                reg_adc_min_value = m_res_cfg.adc_min;

		NRF_LOG_INFO("\r\n");
		// 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;	
		}
                
	}
        else
        {
                    reg_adc_max_value = m_res_cfg.adc_max;
                    reg_adc_min_value = m_res_cfg.adc_min;

                    ret_code_t ret = fds_record_write(&record_desc, &rec_res);
                    if (ret != FDS_SUCCESS)
                    {
                            return ret;
                    }
        }
	return NRF_SUCCESS;	
}

For erase record I use below code,

ret_code_t fds_test_find_and_delete (uint16_t FILE_ID, uint16_t RES_KEY)
{
	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, RES_KEY, &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;
}

My problem is when I erase specific record for that I use FILE_ID and RES_KEY which is same use in flash write function. but usinf erase function I can not able to erase specific record but erase full flash.

So how can i erase specific record?

Give me suggestion for that as soon as possible.

Thanks & regards,

 Urvisha Andani

  • Hi,

    fds_record_delete() is the correct function to delete a single record. Note that since you are calling this in a while-loop that will only exit when fds_record_find() does not return NRF_SUCCESS, you will delete all records with the given FILE_ID and RES_KEY. The records are not actually deleted from the flash when you call this function, they are only invalidated. The fds_gc() call is what erases the records from flash, by moving valid records to a SWAP page, before the old DATA page is erased. Flash can only be erased whole pages at a time.

    Best regards,
    Jørgen

  • The records are not actually deleted from the flash when you call this function, they are only invalidated.

    What can i do for delete the record?

    The fds_gc() call is what erases the records from flash, by moving valid records to a SWAP page, before the old DATA page is erased. Flash can only be erased whole pages at a time.

    can i erase SWAP page? if yes then How can I erase SWAP page.

  • Urvisha Andani said:
    What can i do for delete the record?

    With FDS, the only way to fully delete a record from flash is to call the delete function and then do garbage collection. The point of using FDS is to simplify the writing and updating of records, without having to keep track of the currently used location at all times. Delaying the actual erase until the flash page is getting close to full and spreading the writes over the full page will reduce flash wearing. Each flash word can only be written/erased 10000 times before it starts failing. Like I said before, you need to erase the full page in order to delete a record, it is not possible to erase a single record from flash. This means that you need to either limit each flash page to a single record or move all other records that you want to keep to another page, when you want to erase a record.

    Urvisha Andani said:
    can i erase SWAP page? if yes then How can I erase SWAP page.

    No, the SWAP page is erased before it is marked as a SWAP page. FDS consists of a minimum of one DATA page and one SWAP page. When you run garbage collection, the valid records in the DATA page is written to the SWAP page. The old DATA page is then erased and promoted to a SWAP page, while the old SWAP page that now contains valid records is promoted to a DATA page.

  • With FDS, the only way to fully delete a record from flash is to call the delete function and then do garbage collection. The point of using FDS is to simplify the writing and updating of records, without having to keep track of the currently used location at all times. Delaying the actual erase until the flash page is getting close to full and spreading the writes over the full page will reduce flash wearing. Each flash word can only be written/erased 10000 times before it starts failing. Like I said before, you need to erase the full page in order to delete a record, it is not possible to erase a single record from flash. This means that you need to either limit each flash page to a single record or move all other records that you want to keep to another page, when you want to erase a record.

    Thanks for suggestion.

    This reply is useful for me.

Related