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

pstorage didn't load correct data

Hi everybody,

I test the pstorage library. But it didn't work. I print the result with uart, as the image shows. But as you see, It has some problem. And I have no idea why.

image description

I show the code In the following. I hope somebody can help me find which step went wrong.

By the way, I used the softdevice s110 v7.1.

Many thanks!

main function:

    uint8_t data[4]={1,2,3,4};
	uint8_t getdata[8];

	storage_init();
	clear_data();
	
	store_data(0,data,4,0);
	printf("store\r\n ");
	printf("%d ",data[0]);
	printf("%d ",data[1]);
	printf("%d ",data[2]);
	printf("%d \r\n",data[3]);
	
	load_data(0,getdata,4,0);
	printf("load \r\n ");
	printf("%d ",getdata[0]);
	printf("%d ",getdata[1]);
	printf("%d ",getdata[2]);
	printf("%d \r\n",getdata[3]);

function I used:

static void example_cb_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_LOAD_OP_CODE:
       if (result == NRF_SUCCESS)
       {
           printf("OP successful\r\n");
       }
       else
       {
           printf("OP failed.\r\n");
       }
       // Source memory can now be reused or freed.
       break;

}
}

======

static pstorage_handle_t base_handle;
static void storage_init(void)
{
     uint32_t retval;
	retval = pstorage_init();
	if(retval == NRF_SUCCESS)
	{
			printf("Module initialization successful\r\n");
	}
	else
	{
		 printf("Initialization failed, take corrective action.\r\n");
	}
	pstorage_module_param_t param;
				
	param.block_size  = 16;
	param.block_count = 32;
	param.cb          = example_cb_handler;
			
	retval = pstorage_register(&param, &base_handle);
	if (retval == NRF_SUCCESS)
	{
			printf("Registration successful%d    %d.\r\n",base_handle.module_id,base_handle.block_id);
	}
	else
	{
			printf("Registration failed.\r\n");
	}

  }

=======

    static void load_data(pstorage_size_t block_num,
										uint8_t * dest_data,
										pstorage_size_t size,
										pstorage_size_t offset)
  {

pstorage_handle_t block_handle;
uint32_t          retval;

retval = pstorage_block_identifier_get(&base_handle, block_num, &block_handle);
if (retval == NRF_SUCCESS)
{
		printf("get block ID success.ID: %d\r\n",block_handle.block_id);
}
else
{
		printf("get block failed.\r\n");
}  
// Request to read 4 bytes from block at an offset of 12 bytes.
retval = pstorage_load(dest_data, &block_handle, size, offset);
if (retval == NRF_SUCCESS)
{
		printf("load success.\r\n");
}
else
{
		printf("load failed.\r\n");
}

    }

========

    static void store_data(pstorage_size_t block_num,
										 uint8_t * source_data,
										 pstorage_size_t size,
										 pstorage_size_t offset)
   {
pstorage_handle_t block_handle;
uint32_t          retval;

retval = pstorage_block_identifier_get(&base_handle, block_num, &block_handle);
if (retval == NRF_SUCCESS)
{
	printf("get block ID success.ID: %d\r\n",block_handle.block_id);
}
else
{
		printf("get block failed.\r\n");
}

// Request to write 8 bytes to block at an offset of 20 bytes.
retval = pstorage_store(&block_handle, source_data, size, offset);
if (retval == NRF_SUCCESS)
{
		printf("store success.\r\n");
}
else
{
		printf("store failed.\r\n");
}

    }

=======

    static void clear_data(void){
uint32_t          retval;    
// Request clearing of all blocks in the module. 32 blocks each with 16 bytes in size.
retval = pstorage_clear(&base_handle, 16 * 32);

if (retval == NRF_SUCCESS)
{
		printf("clear success.\r\n");
}
else
{
		printf("clear failed.\r\n");
}

    }
Parents
  • Hi,

    From the provided code, it looks fine but i guess you are missing one thing which is there in documentation , that is you should always call CLEAR api before storing any data to flash, and just trust on event based callback for pstorage, it ill process the queue for sure.

    Please call clear first, then store api, then you can able to read it back properly, and there is no need to introduce any delay anywhere, it just consume 100% cpu, instead, trust on callback approach, where u can set flag and then process or do make calls in main context every time. Hope this will help you. It worked for me!

Reply
  • Hi,

    From the provided code, it looks fine but i guess you are missing one thing which is there in documentation , that is you should always call CLEAR api before storing any data to flash, and just trust on event based callback for pstorage, it ill process the queue for sure.

    Please call clear first, then store api, then you can able to read it back properly, and there is no need to introduce any delay anywhere, it just consume 100% cpu, instead, trust on callback approach, where u can set flag and then process or do make calls in main context every time. Hope this will help you. It worked for me!

Children
Related