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

Checking if data is correct in pstorage callback

Hi,

As i wrote in the topic, after calling pstorage_update I would like to check if the data was stored correctly. Can I call another pstorage_update in pstorage callback, if the data was corrupted?

For now the callback looks like this, but I can't make it work properly if I simulate an error:

void PStorage_SerialNumberRetVal_Handler(pstorage_handle_t  * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len)																				 																																		 
{
	  switch(op_code)
    {
		 case PSTORAGE_UPDATE_OP_CODE:
           if (result == NRF_SUCCESS)		//	Check if the data were correctly flashed
           {             
		//	The max stored data lenght is for eventLog == sizeof(deviceState.eventlog_data)
		 uint8_t temp[8]={0};	//	The table where I store loaded data from flash to validate them
		 uint8_t p_dataCheck[8] = {0}; //The table where I copy the data which were just stored in flash (for debug)
		for(uint8_t i=0; i<8; i++)
		 {
		      p_dataCheck[i] = p_data[i];		//	Copy the data which were just stored, for debug
		 }
		pstorage_load(temp, handle, data_len, 0);		//	Read the data from flash
						 
		//	Check if the data is correct
	        for(uint8_t i=0; i<data_len; i++)
		 {
			 if(p_dataCheck[i] != temp[i])	//	If the data was corrupted
				{
				 //	return an error in order to try flashing the data again
					pstorage_update(handle, p_dataCheck, data_len, 0);
					for(uint16_t i=0; i<30000;i++)		//	Wait until the operation was completed
					{}		
				        break;
				}
		 }
           }
           else
           {
               // Store operation failed.
           }
           // Source memory can now be reused or freed.
           break;
					 
			 default:
				 break;
    }
}
Parents
  • for(uint8_t i=0; i<8; i++)
    {
         p_dataCheck[i] = p_data[i];       //  Copy the data which were just stored, for debug
    }
    

    you are copying only 8 bytes to p_data_check but

    for(uint8_t i=0; i<data_len; i++)
    {
       if(p_dataCheck[i] != temp[i]) 
    

    you are comparing data_len bytes, this is fine if you are sure that data_len will always be 8.

    Why do you have a waiting 'for loop' for the second update to complete? I think it is best to break out of this handler as fast as possible without waiting.

    You have written very dangerous code. If something goes wrong, you could end up in endless loop of flash writes, damaging flash memory within hour if not minutes.

  • Thanks for reply. You're right, I didn't set the guardian counter which guards against infinit flash update. The data_len is never bigger than 8, so it is ok. I just realised, that when I register one block with sufficient size, where I store different data, the callback doesn't provide any information about offset where I've just stored the data in flash. Am I correct that for that reason I am not able to check if the data wasn't corrupted?

Reply
  • Thanks for reply. You're right, I didn't set the guardian counter which guards against infinit flash update. The data_len is never bigger than 8, so it is ok. I just realised, that when I register one block with sufficient size, where I store different data, the callback doesn't provide any information about offset where I've just stored the data in flash. Am I correct that for that reason I am not able to check if the data wasn't corrupted?

Children
No Data
Related