This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NRF51822 Flash write ,lead to undesirable value

I have Inited the pstorage

err_code = pstorage_init();
 APP_ERROR_CHECK(err_code);

Here PSTORAGE_NUM_OF_PAGES=2(1page for bonding ,1page for my application use ).and register my page

param.block_size  = sizeof (m_sys_data_store) ;//36bytes
	param.block_count = 1;
	param.cb          = sys_updata_storage_cb_handler;
	
	err_code = pstorage_register(&param, &mp_sys_info_flash);

here param.block_size=36 bytes.Mine flash operation like this

err_code=sys_info_clear_from_flash();//==pstorage_clear(&mp_sys_info_flash, sizeof(m_sys_data_store));
	if (err_code != NRF_SUCCESS)
	{
	  return ;
	}
	for(err_code=0;err_code<4800000;err_code++){;}//wait  for flash operation finish
	
	m_sys_data_store._flag=0xaabbccdd;

	err_code=sys_info_updata_to_flash(&m_sys_data_store);
	if (err_code != NRF_SUCCESS)
	{
		return ;
	}
	for(err_code=0;err_code<4800000;err_code++){;}

sometime the write result is 0xaabbccdd ,but somtime is0xffffffff, 0x00000000or others. the function sys_info_updata_to_flash show as follow:

uint32_t sys_info_updata_to_flash(sys_info_store_block_t * sys_data) 
{
  uint32_t                  err_code;
  pstorage_handle_t         dest_block;
	
		
  // Get block pointer from base
	err_code = pstorage_block_identifier_get(&mp_sys_info_flash,0,&dest_block);
	if (err_code != NRF_SUCCESS)
	{
			return err_code;
	}
	err_code=sizeof(m_sys_data_store);//test
	err_code = pstorage_store(&dest_block,(uint8_t *)sys_data,sizeof(m_sys_data_store),0);
	if (err_code != NRF_SUCCESS)
	{
			return err_code;
	}
	return NRF_SUCCESS;
}

The porject base on SDK9.0,S130.Why does the flash operation be so complex?I have no ideal to solute it,help me please.

  • If you want to update the contents of flash memory, you can't use pstorage_store(); for every time you want to write something to flash. That is valid only for the first time. If you want to update flash contents to which you have already written, you should use pstorage_update(); instead. Using pstorage_store(); on a flash region which was already written might cause unpredictable results. I have recently also tried to run pstorage module and i got everything working correct, but i did it on S110 V8. You can check it here pstorage_update callback

  • Thank you for your anser.Before wirting to the flash,I have erased ,There should not be dirty on the flash,So I think there isn't wrong.Besides,I don't know what the mechanism of pstorage_update() is.Would it work steadily? I will try it ,TKS.

  • Oh, i did not notice that you are doing flash erase. You are correct, if you are erasing flash and then trying to write it with pstorage_store(); it should work fine. The pstorage_update(); actually does the same thing for you. It first erases the flash area and then writes to it the new data. It is just for the convenience. If you are doing erase and then writing, there should be no difference.

  • You shouldn't wait a fixed time before the flash operation is finished, to be sure that it is finished you should wait until you get the event in the callback handler. See this. If you do a clear operation you should wait for the PSTORAGE_CLEAR_OP_CODE event before you write.

  • Thank you.You are right.I have fixed the bug.

Related