<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/113792/ble-fds-problem-to-read-write-flash-data-err</link><description>sdk：17.1 example: ble_app_uart+ secure_bootloader. Our device is charged with power-off reset function. 
 When I call fds_record_update(&amp;amp;desc, &amp;amp;gtPara_record),if returns FDS_ERR_NO_SPACE_IN_FLASH, which means I need to call to clean up the garbage.So</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 25 Sep 2024 10:53:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/113792/ble-fds-problem-to-read-write-flash-data-err" /><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/503728?ContentTypeID=1</link><pubDate>Wed, 25 Sep 2024 10:53:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00195252-2895-4e16-9dd5-dbdb364743c9</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Zicongkuang,&amp;nbsp;&lt;br /&gt;I don&amp;#39;t think there is a workaround to avoid assert&amp;amp;restart except for that you make sure there shouldn&amp;#39;t be a problem.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One thing you can do though, is that if the err_code returns FDS_ERR_BUSY then you can wait and try again after a while instead of reset. But make sure you don&amp;#39;t end up in a dead loop. You should trigger a reset if the same error returned for a while.&amp;nbsp;This make sure the product can recover with a reset.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/503490?ContentTypeID=1</link><pubDate>Tue, 24 Sep 2024 04:27:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a80424e6-0f06-4d00-9d28-8f53edc5266c</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;br /&gt;Thank you very much for your advice. After the one-month test, our products have no major problems.&lt;br /&gt;But I have one last question, to avoid APP_ERROR_CHECK(err_code); Cause the device to restart.&lt;br /&gt;So I would like to know if there are any errors in the following functions that need to happen easily and need to be handled in time？&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;err_code = fds_init();&lt;/li&gt;
&lt;li&gt;err_code = fds_stat(&amp;amp;stat);&lt;/li&gt;
&lt;li&gt;err_code = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok);&lt;/li&gt;
&lt;li&gt;err_code = fds_record_open(&amp;amp;desc, &amp;amp;config);&lt;/li&gt;
&lt;li&gt;err_code = fds_record_close(&amp;amp;desc);&lt;/li&gt;
&lt;li&gt;err_code = fds_record_delete(&amp;amp;desc);&lt;/li&gt;
&lt;li&gt;err_code = fds_gc();&lt;/li&gt;
&lt;li&gt;err_code = fds_record_update(&amp;amp;desc, &amp;amp;gtPara_record);&lt;br /&gt;&lt;br /&gt;Thanks！&lt;br /&gt;Best Regard!&lt;br /&gt;Kenyon&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498980?ContentTypeID=1</link><pubDate>Tue, 20 Aug 2024 08:24:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:374f13d2-56b4-4a2d-9a5a-43cf9e9b1a80</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Zicongkuang,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Yes it&amp;#39;s the case. You can take a look at this similar ticket here:&amp;nbsp;&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/100891/flash-data-storage-fds-fds_record_update---reset---two-valid-records-possible"&gt;Flash Data Storage (FDS) fds_record_update() -&amp;gt; Reset -&amp;gt; Two valid records possible?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Please follow the suggestion from tesc to use the record_id to check which one is the latest one. If you find duplicated records, just delete the ones with smaller record_id.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498937?ContentTypeID=1</link><pubDate>Tue, 20 Aug 2024 02:51:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9f3e5a7-7583-4152-9713-2a95b8923967</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/113792/ble-fds-problem-to-read-write-flash-data-err/498761"]Could you give a brief info on how to test it with your code ?&amp;nbsp;&lt;br /&gt;Could you elaborate more on &amp;quot;&lt;span&gt;&amp;nbsp;call collision of fds_update&amp;nbsp;&amp;quot; ?&amp;nbsp;&lt;/span&gt;[/quote]
&lt;p&gt;The code is very simple,flash_store is invoked by&amp;nbsp;fleetly pressing keys repeatedly to create fds_update conflicts with the scheduled flash_store.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/113792/ble-fds-problem-to-read-write-flash-data-err/498761"]The way it works is that&amp;nbsp;fds_update will write the content of the new record to a new location in flash and mark the old one as invalid. There could be a chance that if the reset occurs right after the new data written in flash but the old record has not been marked as dirty then it will cause duplicated records.&amp;nbsp;[/quote]
&lt;p&gt;I think that might be the case.&lt;br /&gt;&lt;br /&gt;I look forward to your verification.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498761?ContentTypeID=1</link><pubDate>Mon, 19 Aug 2024 08:07:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11500755-578e-4a27-ba5a-86b793d09a51</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;br /&gt;Thanks for the code. I will take a look.&lt;br /&gt;Could you give a brief info on how to test it with your code ?&amp;nbsp;&lt;br /&gt;Could you elaborate more on &amp;quot;&lt;span&gt;&amp;nbsp;call collision of fds_update&amp;nbsp;&amp;quot; ?&amp;nbsp;&lt;br /&gt;The way it works is that&amp;nbsp;fds_update will write the content of the new record to a new location in flash and mark the old one as invalid. There could be a chance that if the reset occurs right after the new data written in flash but the old record has not been marked as dirty then it will cause duplicated records.&amp;nbsp;&lt;br /&gt;I will check internally to see if it&amp;#39;s the case and if we have a solution.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498493?ContentTypeID=1</link><pubDate>Thu, 15 Aug 2024 16:23:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96359e18-477a-445c-a926-f1c948ac97af</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;I made a DK and was able to reproduce the problem on the development board (52832).&lt;br /&gt;I found that the call collision of fds_update caused two valid data to appear instead of fds_write. If there are any errors in the code, please correct, thank you very much.&lt;br /&gt;&lt;br /&gt;If fds_update does cause two valid pieces of data, how can we avoid it?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/TestFlash_5F00_sdk17.7z"&gt;devzone.nordicsemi.com/.../TestFlash_5F00_sdk17.7z&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498326?ContentTypeID=1</link><pubDate>Wed, 14 Aug 2024 14:31:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7c3a51d1-de95-43e6-bdde-c524a731565c</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi again,&amp;nbsp;&lt;br /&gt;How long did it take to reproduce the issue ?&amp;nbsp;&lt;br /&gt;Can you reproduce it on a DK ? If you can send a minimal sample that I can test here I can try to investigate.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498249?ContentTypeID=1</link><pubDate>Wed, 14 Aug 2024 11:21:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6cae898-b91f-4b24-bece-cd4b8b5eafaf</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi&lt;br /&gt;In the test I did just now, I didn&amp;#39;t find that they called fds_write the second time, but I did find two more valid data in flash.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498245?ContentTypeID=1</link><pubDate>Wed, 14 Aug 2024 11:00:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3a327e0e-c23f-4fcf-a92d-11ff92af6bb5</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think turning that on would prevent the duplicate write issue. The setting is to check if the data writen has been modified when writing. If you have a reset in the middle of the writing process, in theory the data should just be marked as non-valid (not marked as valid).&amp;nbsp;&lt;br /&gt;When you turn on&amp;nbsp;FDS_CRC_CHECK_ON_READ&amp;nbsp; a corrupted record will still can be found but not open. Have you make sure you can open the duplicated records ?&amp;nbsp;&lt;br /&gt;As I mentioned, please try to check and make sure you are not calling write&amp;nbsp;fds_record_write() twice for the same record.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498222?ContentTypeID=1</link><pubDate>Wed, 14 Aug 2024 09:20:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ac0ac4f-6036-4b79-8cb4-806f2c5e9743</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;/p&gt;
[quote userid="2121" url="~/f/nordic-q-a/113792/ble-fds-problem-to-read-write-flash-data-err/498063"]To make sure you are not calling write twice, you should have a semaphore to wait for the event FDS_EVT_WRITE in fds_evt_handler() to be success before you call write again.&amp;nbsp;[/quote]
&lt;p&gt;This, in my opinion, only prevents two consecutive calls to fds_write caused by faulty software logic. Because the act of charging RESET is uncontrollable and random.Is it&amp;nbsp;right？&lt;br /&gt;&lt;br /&gt;#ifndef FDS_CRC_CHECK_ON_READ&lt;br /&gt;#define FDS_CRC_CHECK_ON_READ 1&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;#ifndef FDS_CRC_CHECK_ON_WRITE&lt;br /&gt;#define FDS_CRC_CHECK_ON_WRITE 0&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;I only turned on the read CRC，I may need to turn on write crc as well.&lt;br /&gt;But I have a question: if there is no CRC write check, will fds_update also&amp;nbsp;probably result in two sets of valid data in the same KEY &amp;amp;&amp;amp;FILE ID?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498063?ContentTypeID=1</link><pubDate>Tue, 13 Aug 2024 13:06:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9007e84c-b920-4845-b3eb-56b8f840f985</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;Kenyon,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;To make sure you are not calling write twice, you should have a semaphore to wait for the event FDS_EVT_WRITE in fds_evt_handler() to be success before you call write again.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think doing reset multiple times when writing can cause the problem. If the record is written correctly before the reset, then the next&amp;nbsp;fds_record_find() should be able to find it. If the writing is not finished, the CRC will not match.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/498027?ContentTypeID=1</link><pubDate>Tue, 13 Aug 2024 12:11:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:258c2db3-44c0-4774-ae10-ebd1d0aaeb63</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;br /&gt;I can reproduce the issue，maybe need a long time to reproduce.&lt;br /&gt;Because the charging port of our device is equipped with hardware reset function，I &lt;span&gt;reproduce this&amp;nbsp;&lt;/span&gt;&lt;span&gt;issue&amp;nbsp;&lt;/span&gt;&amp;nbsp;by continuously charging reset(write flash) + key_longPress() (write flash).&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;string.h&amp;gt;
#include &amp;quot;fds.h&amp;quot;

unsigned long gulFlashStoreNeeded = 0;////flash_store();                            
unsigned long gulFlashStoreCnt = 0x80000000;

#define CONFIG_FILE     (0x8010)
#define CONFIG_REC_KEY  (0x7010)
T_Para gtPara;

extern T_LL_HardwareID gtHwID;
///* A dummy structure to save in flash. */
typedef struct
{
    unsigned long p_v;
    T_DeviceLumen temote;
    signed char scBleAddr[6];
    E_Mode eMOWL; //
    unsigned long ulB;
    unsigned long eBmo;
    unsigned long ulNeedCharge;
    signed short sxyz[3];
    signed short ssThd;
    unsigned short usAdcValue;
    unsigned char aucSN[20];
    unsigned char ucBLight;
    E_B_M_O_P_O_O b_m_f;
    unsigned long flash_erase_flag;
    #define GET_BAR_STATE(states, bar_index)    ((states&amp;gt;&amp;gt;(bar_index&amp;lt;&amp;lt;1)) &amp;amp; 0x3) // 2 bit for each bar
    unsigned long c_f_t_s[6][7];   
    unsigned char brightness[6];    
    unsigned long ulChecksum;
    unsigned char publicNet;
    unsigned char netID[4];
    #define getCustomizedSetting(xxx)  gtPara.xxx
	unsigned char Profiles[4];
	unsigned char mode;
	unsigned char profile_nn;
    unsigned char netID_e;
    unsigned char PowerOff;
    unsigned char enabledHallSensor;    
}T_Para; //284 bytes

static T_Para gtStorePara = 
{
	.p_v = 0x01010102,
	.temote.ulRoundRobin = 0,
	.temote.ulIng        = 0,
	.temote.ID[0] = 0x00,
	.temote.ID[1] = 0x00,
	.temote.bitsc = 0x00,
	.eMOWL = 0,
	.ulB = 0,
	.eBmo = 0x02,
	.ulNeedCharge = 0,
	.sxyz[0] = 100,
	.sxyz[1] = 0,
	.sxyz[2] = 0,
	.ssThd = -3500,
	.ucBLight = 70,
	.publicNet = 0,
	.netID_e = 0,
	.PowerOff = 0,
	.b_m_f = 0x06,
};


/* A record containing dummy configuration data. */
static fds_record_t const gtPara_record =
{
    .file_id           = CONFIG_FILE,
    .key               = CONFIG_REC_KEY,
    .data.p_data       = &amp;amp;gtStorePara,
    /* The length of a record is always expressed in 4-byte units (words). */
    .data.length_words = (sizeof(gtStorePara) + sizeof(uint32_t) - 1) / sizeof(uint32_t),
};


static bool volatile m_fds_initialized;
static bool volatile m_fds_writeflag;
static bool volatile m_fds_gc_done = true;

static void fds_evt_handler(fds_evt_t const * p_evt)
{

    switch (p_evt-&amp;gt;id)
    {
        case FDS_EVT_INIT:
            if (p_evt-&amp;gt;result == NRF_SUCCESS)
            {
                m_fds_initialized = true;
            }
            break;

        case FDS_EVT_WRITE:
        case FDS_EVT_UPDATE:
        {
            if (p_evt-&amp;gt;result == NRF_SUCCESS)
            {
				m_fds_writeflag = true;
            }                
        }break;
        case FDS_EVT_DEL_RECORD:
        {
            if (p_evt-&amp;gt;result == NRF_SUCCESS)
            {

            }
            //m_delete_all.pending = false;
        } break;
        case FDS_EVT_GC:
            if (p_evt-&amp;gt;result == NRF_SUCCESS)
            {
                LL_LED_OFF(E_LL_LED_PCB_G);
                m_fds_gc_done = true;
            }
        default:
            break;
    }
}


void flash_init(void)
{
   ret_code_t err_code;
    
    //write_flag init
    m_fds_writeflag = true;
    
   (void) fds_register(fds_evt_handler);//FDS register
    err_code = fds_init();//fds init
	
    APP_ERROR_CHECK(err_code);
	
	while (!m_fds_initialized)//wait init
    {
        sd_app_evt_wait();//standby
    }		
		
	fds_stat_t stat = {0};
    err_code = fds_stat(&amp;amp;stat);//Set statistics
    APP_ERROR_CHECK(err_code);
	
	//record_delete_next();//clear all records
			
	fds_record_desc_t desc = {0};//The descriptor structure used to manipulate the record is zeroed out
    fds_find_token_t  tok  = {0};//The token that saves the key is cleared
		
    err_code = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok);//Find data, file ID, record KEY
		
		
 if (err_code == NRF_SUCCESS)//if find successful
    {
        /* A config file is in flash. Let&amp;#39;s update it. */
        fds_flash_record_t config = {0};//res

        /* Open the record and read its contents. */
        err_code = fds_record_open(&amp;amp;desc, &amp;amp;config);//open record to read data

        APP_ERROR_CHECK(err_code); 
        /* Copy the configuration from flash into m_dummy_cfg*/
        memcpy(&amp;amp;gtPara, config.p_data, sizeof(gtPara));

        //data = (uint32_t *)config.p_data;
                
        /* Close the record when done reading. */
        err_code = fds_record_close(&amp;amp;desc);//close record
        APP_ERROR_CHECK(err_code);
    }else{
    	set_default; //set all data default
    	gulFlashStoreNeeded = 1; //store data
	}			
}

bool flash_gc_finish(void)
{
    return m_fds_gc_done;
}

bool flash_store_finish(void)
{
    return m_fds_writeflag;
}

void flash_store(void)
{
		ret_code_t err_code;
		fds_record_desc_t desc = {0};//The descriptor structure used to manipulate the record is zeroed out
        fds_find_token_t  tok  = {0};//The token that saves the key is cleared
   
        memcpy(&amp;amp;gtStorePara, &amp;amp;gtPara, sizeof(gtPara));
        
        //write_flag set
        m_fds_writeflag = false;
		// Check the record exists
		err_code = fds_record_find( CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok );
		
		/* Write the updated record to flash.*/
		if( err_code == NRF_SUCCESS){
            err_code = fds_record_update(&amp;amp;desc, &amp;amp;gtPara_record);
            if(err_code == FDS_ERR_NO_SPACE_IN_FLASH){
                m_fds_gc_done = false;
                LL_LED_ON(E_LL_LED_PCB_G);
                err_code = fds_gc();
                APP_ERROR_CHECK(err_code);
                
                gulFlashStoreNeeded = 1; //store data
            }               
    
            fds_stat_t stat = {0};
            err_code = fds_stat(&amp;amp;stat);
            APP_ERROR_CHECK(err_code);				
		}else{
            err_code = fds_record_write(&amp;amp;desc, &amp;amp;gtPara_record);//Write records and data. Descriptor, used later to access the record ID
            APP_ERROR_CHECK(err_code);					
		}			
}

#define LL_FLASH_WAITING_TIME_BEFORE_SLEEP  (98 + 338*1024/1000 + 1280) + 1000    // 98ms page-erase, 338us 4-byte-write, 1024 4-byte per page, 1280ms top-prior-BLE-adv. (refer to &amp;quot;chapter 9 Flash memory API&amp;quot; of &amp;quot;S132_SDS_v2.0.pdf&amp;quot;)
void LL_Power_BeforeSleep(void)
{
    //Store data and write crc checks again before sleep if charge or some data has not written
    if(gulFlashStoreNeeded){
        flash_store();
    }	
    
    //wait flash write success 
    if(!flash_store_finish()){ 
        for(uint16_t i=0; i&amp;lt;LL_FLASH_WAITING_TIME_BEFORE_SLEEP; i++){
            if(flash_store_finish()){break;}
            nrf_delay_ms(1);
        }
    }
}

int main(void)
{
    bool erase_bonds;
    ret_code_t err_code;
		
    // Initialize the async SVCI interface to bootloader before any interrupts are enabled.
    err_code = ble_dfu_buttonless_async_svci_init();
    APP_ERROR_CHECK(err_code);
   
    // Initialize.
    timers_init();

    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();

    advertising_init();
    conn_params_init();
		//
#ifdef  _LL_TIMESLOT
    err_code = esb_timeslot_init();
    APP_ERROR_CHECK(err_code);
	
    err_code = esb_timeslot_sd_start();
    APP_ERROR_CHECK(err_code);
#endif

	LL_Key_Init();
		
    LL_Battery_Charging__Init();
        
	flash_init();

    // system off if not from key trigger or charging.
	if(LL_Battery_Charge__isCharging()) {  //if charging
        gtPara.ulNeedCharge = 0; gulFlashStoreNeeded = 1;  //store gtPara.ulNeedCharge
        LL_Power_BeforeSleep(); 
        LL_Power_Sleep(); 
    }else if( key_shortPress() ) {		// if key pressed
       	// continue			       
    }else{
        LL_Power_BeforeSleep(); 
        sd_power_system_off();            
    }
    
	// Start execution.
    advertising_start();//	
	
	while(1)
	{
		if(gulFlashStoreNeeded == 1){
			gulFlashStoreNeeded = 2;
			TimeStart(0);//Start the clock here 
		}else if(gulFlashStoreNeeded == 2){
			if(TimeStart(2000)) {//If you time it for 2000ms, start storing data
				flash_store();	
				gulFlashStoreNeeded = 0; //Wait for the next data store
			}
		}
		
		if( key_longPress()) { //if key longPress
			gtPara.ulNeedCharge = 1; gulFlashStoreNeeded = 1;
        	LL_Power_BeforeSleep(); 
        	sd_power_system_off();
		}
	}
}&lt;/pre&gt;&lt;br /&gt;When I checked thoroughly today, I found that it was because I did not know when I called &lt;strong&gt;err_code = fds_record_write(&amp;amp;desc, &amp;amp;gtPara_record)&lt;/strong&gt; twice; The result is that two pieces of data are stored under the same KEY and File ID. &lt;br /&gt;In my code, the condition for calling &lt;strong&gt;fds_record_write&lt;/strong&gt; is &lt;strong&gt;err_code = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok);&lt;/strong&gt; fails to call&amp;nbsp;&lt;strong&gt;fds_record_write &lt;/strong&gt;again.&lt;br /&gt;&lt;br /&gt;I wonder if the constant charging reset will cause this situation in flash？&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/497819?ContentTypeID=1</link><pubDate>Mon, 12 Aug 2024 12:41:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d44b845-cd97-485e-a038-e901f1339684</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Kenyon,&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Have you managed to reproduce the issue ? If not please double check the code to see if there is any chance that you created two record with the same key ?&amp;nbsp;&lt;br /&gt;How did you find out the problem ? Did it has any relation to a DFU update ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/497784?ContentTypeID=1</link><pubDate>Mon, 12 Aug 2024 11:26:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2bc2487-a1d0-4a8b-b905-6719ed820720</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#define CONFIG_FILE (0x8010)
#define CONFIG_REC_KEY (0x7010)
static fds_record_t const gtPara_record =
{
.file_id = CONFIG_FILE,
.key = CONFIG_REC_KEY,
.data.p_data = &amp;amp;gtStorePara,
/* The length of a record is always expressed in 4-byte units (words). */
.data.length_words = (sizeof(gtStorePara) + sizeof(uint32_t) - 1) / sizeof(uint32_t),
};&lt;/pre&gt;&lt;br /&gt;We are constantly updating data under the same File ID and Record Key. So I think in flash we should only have one valid data. But strangely enough, we found two valid bits of data in flash using the hex file. Why is that?&lt;br /&gt;Because of the existence of two valid data, we have to read the data two times each time to read out the latest update data.How can I avoid this?&lt;br /&gt;&lt;br /&gt;Best regard&lt;br /&gt;Kenyon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/497730?ContentTypeID=1</link><pubDate>Mon, 12 Aug 2024 07:16:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:72fcc7f6-72cc-4a17-9059-86b1d103a5c4</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Kenyon,&amp;nbsp;&lt;br /&gt;Could you please explain a little bit more on &amp;quot;&lt;span&gt;&amp;nbsp;there were two valid flash data in the flash area of the hex file&amp;quot;&amp;nbsp; ?&amp;nbsp;&lt;br /&gt;Please describe what do you expect and what was wrong with that.&amp;nbsp;Did&amp;nbsp;it have anything to do with&amp;nbsp;FDS_ERR_NO_SPACE_IN_FLASH issue ?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;You mentioned that you used secure bootloader, was the problem related to the DFU update ?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/497676?ContentTypeID=1</link><pubDate>Fri, 09 Aug 2024 17:21:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e47d7deb-3f8d-4535-bc45-56bdf02ad212</guid><dc:creator>kenyon</dc:creator><description>&lt;p&gt;Hi Hung Bui&lt;br /&gt;&lt;br /&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/20240810_2D00_012002.jpg" /&gt;&lt;br /&gt;After I used the FDS module, there was a problem found that there were two valid flash data in the flash area of the hex file, why is this, is there a solution? Or how can we avoid it？&lt;br /&gt;&lt;br /&gt;Best Regard&lt;br /&gt;Kenyon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE+FDS Problem to read/write flash data err</title><link>https://devzone.nordicsemi.com/thread/497626?ContentTypeID=1</link><pubDate>Fri, 09 Aug 2024 12:39:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9df7aae5-9ce8-4173-bc49-36899e632525</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Zicongkuang,&amp;nbsp;&lt;br /&gt;&lt;br /&gt;1. It depends. The gc process basically erase all the records that&amp;#39;s marked as &amp;quot;dirty&amp;quot; . It does it by copy the &amp;quot;valid&amp;quot; records to the swap page, erase the page and copy the valid record to the current page. So the process may take longer time if you have a lot of pages to copy&amp;amp;erase.&amp;nbsp;&lt;br /&gt;As mentioned in the &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_fds_functionality.html?cp=9_1_3_16_1_2#lib_fds_functionality_gc"&gt;documentation&lt;/a&gt;, you should wait for the FDS_EVT_GC event with NRF_SUCCESS status&amp;nbsp;&lt;span&gt;before continue. But I don&amp;#39;t think it would take very long time (usually less than a second). You can find the timing specification here:&amp;nbsp;&lt;a href="https://docs.nordicsemi.com/bundle/ps_nrf52810/page/nvmc.html#unique_728973123"&gt;https://docs.nordicsemi.com/bundle/ps_nrf52810/page/nvmc.html#unique_728973123&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2. There is a checksum at each step in gc process. A page is backed up before it&amp;#39;s erased. So there shouldn&amp;#39;t be a situation when you lose your data when interrupted when doing gc. I don&amp;#39;t think the GC process automatically resume after a reset, so you may need to call it again if you run out of flash again.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;It&amp;#39;s a good practice to use&amp;nbsp; fds_stat to monitor the amount of flash available instead of waiting for&amp;nbsp;FDS_ERR_NO_SPACE_IN_FLASH error.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>