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

memory corruption problems with pstorage

Hi everyone,

I'm using NRF51822 controller for an application, to store 2 float variables in the memory using persistent storage manager feature.

However, when I try to update and then retrive these variables, it seems there is some memory corruption & I'm getting garbage values returned back after an update & read back.

Here is my sample code:

#include <pstorage.h>


uint8_t registore_for_mem(void)
{
	param.block_size  = 32;
	param.block_count = 2;
	param.cb          = example_cb_handler;

		retval = pstorage_register(&param, &handle);
		if (retval == NRF_SUCCESS)
		{   
			// Registration successful.
			return 1;
		}
		else if(retval==NRF_ERROR_INVALID_STATE)
		{    
			// Failed to register, take corrective action.
            return 0;
		}
		else if(retval==NRF_ERROR_NULL)
		{    
			// Failed to register, take corrective action.
            return 0;
		}
		else if(retval==NRF_ERROR_INVALID_PARAM)
		{    
			// Failed to register, take corrective action.
            return 0;
		}
		else if(retval==NRF_ERROR_NO_MEM)
		{    
			// Failed to register, take corrective action.
            return 0;
		}
		nrf_delay_ms(100);
}


uint8_t get_block_id(int block_no)
{
	// Registration successfully completed, base_handle is identifier for allocated blocks....
	
	if(block_no==0)
	retval = pstorage_block_identifier_get(&handle, block_no, &block_handle1);
	else if(block_no==1)
	retval = pstorage_block_identifier_get(&handle, block_no, &block_handle2);
	
	if (retval == NRF_SUCCESS)
	{    // Get Block Identifier successful.
			return 1;
	}
	else
	{    // Failed to get block id, take corrective action.
            return 0;
	}
	return 0;
}


uint8_t store_mem_data(char* source_data,int no)
{

  // Request to write 8 bytes to block at an offset of 20 bytes.
	if(no==1)
	retval = pstorage_store(&block_handle1, (uint8_t*)source_data, 4, 0);
	else if(no==2)
	retval = pstorage_store(&block_handle2, (uint8_t*)source_data, 4, 0);
	//retval = pstorage_update(&block_handle, (uint8_t*)source_data, 4, 0);
	if (retval == NRF_SUCCESS)
	{    
		// Store successfully requested. Wait for operation result.
		return 1;
	}
	else
	{    
		// Failed to request store, take corrective action.
		return 0;
	}
}


uint8_t read_mem_data(int no)
{
	// Request to read 4 bytes from block at an offset of 12 bytes.
    retval=0;

	if(no==1)
		retval = pstorage_load(dest_data, &block_handle1, 4, 0);
	else if(no==2)
		retval = pstorage_load(dest_data, &block_handle2, 4, 0);
		
	if (retval == NRF_SUCCESS)
	{   
		// Load successful. Consume data.
		return 1;
    }
	else
	{    
		// Failed to load, take corrective action.}
		return 0;
	}
}

uint8_t clear_data(void)
{
	// Request update of one blocks. Block size is 8bytes.
	retval = pstorage_clear(&handle, 32*2);
	
	        if (retval == NRF_SUCCESS)
			{   
				// Registration successful.
				return 1;
			}
			else if(retval==NRF_ERROR_INVALID_STATE)
			{    
				// Failed to register, take corrective action.
                return 0;
			}
			else if(retval==NRF_ERROR_NULL)
			{   
				// Failed to register, take corrective action.
                return 0;
			}
			else if(retval==NRF_ERROR_INVALID_PARAM)
			{   
				// Failed to register, take corrective action.
                return 0;
			}
			else if(retval==NRF_ERROR_NO_MEM)
			{    
				// Failed to register, take corrective action.
				return 0;
			}

}

void main()
{

	char dest_data[8] ;
	
	pstorage_init();
	
	do
		{
			status=registore_for_mem();  // Requesting for memory 
		} 	while(!status);
		
		
	do
		{
			status=get_block_id(0);		// Getting the Memory block ID which was allocated fo data1
		}	while(!status);
		
		
	do
		{
			status=get_block_id(1);		// Getting the Memory block ID which was allocated for data2
		}	while(!status);	
		
	
	do
		{
			status=store_mem_data("xxxx",1); // Stores the data into a particular location
		}	while(!status);
		
		
	do
		{
			status=store_mem_data("yyyy",2); // Stores the data into a particular location
		}	while(!status);

	do
		{
			status=read_mem_data(1);	// Getting Data2 from requested Memory block
		}	while(!status);	
		
		
	do
		{
			status=read_mem_data(2); 	// Getting the Data1 from requested Memory block
		}	while(!status);	
		
	
	do
		{
			status=clear_data();  // Clears the memory location where data1 and data2 is stored
		}	while(!status);
		
	do
		{
			status=store_mem_data("bbbb",1); // updating data into a particular location
		}	while(!status);	
	
	do
		{
			status=store_mem_data("aaaa",2); // updating the data into a particular location
		}	while(!status);

	
}
Related