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

Parents Reply Children
No Data
Related