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

SDK12.3 use fs_erase and fs_store

I am using SDK12.3 and the Flash Storage module to store some data in Flash. I frequently need to update some of these data basing on received commands.

When I use "fs_erase", it has been cycling at "power_manage ();"

fs_callback_flag = 1;	        
ret = fs_erase(&fs_config, fs_config.p_start_addr, NUM_PAGES, NULL);
    	if (ret != FS_SUCCESS)
    	{
    		bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    	}
    	while(fs_callback_flag == 1)
    	{
    		power_manage();
    	}
    
    
        static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
    {
    	if (result != FS_SUCCESS)
    	{
    		bsp_indication_set(BSP_INDICATE_USER_STATE_1);
    	}
    	else
    	{
    		NRF_LOG_INFO("fstorage command successfully completed\n\r");
    		fs_callback_flag = 0;
    	}
    }
  • if i commented out

    while(fs_callback_flag == 1)
            {
                power_manage();
            }
    

    this is Complete code

    	NRF_LOG_INFO("Erasing a flash page at address 0x%X\r\n", (uint32_t)fs_config.p_start_addr);
    //	fs_callback_flag = 1;																																				
    	ret = fs_erase(&fs_config, fs_config.p_start_addr, NUM_PAGES, NULL);												
    	if (ret != FS_SUCCESS)
    	{
    		bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    	}
    //	while(fs_callback_flag == 1)
    //	{
    //		power_manage();
    //	}
    	for(uint8_t i=0; i<4; i++)
    	{
    		data[i] |= (p_data[i*4+0] << 24);
    		data[i] |= (p_data[i*4+1] << 16);
    		data[i] |= (p_data[i*4+2] << 8);
    		data[i] |= (p_data[i*4+3] << 0);
    		NRF_LOG_INFO("Writing data (0x%X) to address 0x%X\r\n\r\n", data[i], (uint32_t)fs_config.p_start_addr + (i*4));
    //		fs_callback_flag = 1;
    		ret = fs_store(&fs_config, fs_config.p_start_addr + i, &data[i], 1, fs_evt_handler);      					
    		if (ret != FS_SUCCESS)
    		{
    			bsp_indication_set(BSP_INDICATE_USER_STATE_1);
    		}
    //		while(fs_callback_flag == 1)
    //		{
    //			power_manage();
    //		}
    	}
    	
    	for(int i=0; i<NUM_PAGES_SN; i++)
    	{
    		flash_data[i] = *(fs_config.p_start_addr + i);
    		NRF_LOG_INFO("Reading from flash address 0x%X (hex):0x%X\r\n\r\n", (uint32_t)fs_config.p_start_addr + (i*4), flash_data[i]);
    	}
    

    log print show:The write data and the read data are not the same

    *MyFlash:INFO:Writing data (0x484D0001) to address 0x79000

    MyFlash:INFO:Writing data (0x4744434E) to address 0x79004

    MyFlash:INFO:Writing data (0x16090217) to address 0x79008

    MyFlash:INFO:Writing data (0x940458DB) to address 0x7900C

    MyFlash:INFO:Reading from flash address 0x79000 (hex):0xD0A

    MyFlash:INFO:Reading from flash address 0x79004 (hex):0x98DF

    MyFlash:INFO:Reading from flash address 0x79008 (hex):0x0

    MyFlash:INFO:Reading from flash address 0x7900C (hex):0xFFFFFFFF*

    MyFlash:INFO:fstorage command successfully completed

    MyFlash:INFO:fstorage command successfully completed

    MyFlash:INFO:fstorage command successfully completed

    MyFlash:INFO:fstorage command successfully completed

  • Have you made sure you put fs_sys_event_handler(sys_evt); inside sys_evt_dispatch() ?

    You can have a look at an example here: github.com/.../fstorage_example_simple

  • I am sure,

        err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
    APP_ERROR_CHECK(err_code);
    
    static void sys_evt_dispatch(uint32_t sys_evt)
    {
    	fs_sys_event_handler(sys_evt);
    	ble_advertising_on_sys_evt(sys_evt);
    }
    
  • If you simply test the example in the link , would it work ?

Related