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

Pstorage causes weird behaviour

Hi all, I've been trying to develope a project by using an nRF51822 chip. The project is about to finish. However, I couldn't implement the pstorage module in a correct way, I think. This thead gives a lot of hint about pstorage but I need something more complex.

I followed instructions in the example given and module like this.

param.block_size = 0x10; // 16 bayt each
param.block_count = 10;
param.cb = example_cb_handler;

On restart, it reads 160 bytes data like this ( Here BN[0] hols the block number ) : // This function is called 10 times sequentially on restart

static void ps_read_data( pstorage_handle_t *hand, pstorage_handle_t *block ){
    int retval;
static uint8_t dest[16] = {0};
		
retval = pstorage_block_identifier_get(&handle, BN[0], &block_handle);        
retval = pstorage_load( dest, block, 16, 0);
	
APP_ERROR_CHECK( retval);
static int i = 0;
for( ; i < 16 ; i++){
	dataArray[ i + BN[0]*16 ] = dest[i];
}
i = 0;
 }

After reading whole data, It write a new 16 bytes data to flash every 15 minutes. Every time this function called it writes only 1 block of data. Never called sequentially.

static void ps_write_data( pstorage_handle_t *hand, pstorage_handle_t *block ){
	int retval;	
	static uint8_t src[16] __attribute__((aligned(16))) =   {0};
	
		
	static int i = 0;// veri src icine kopyalanir
	for( ; i < 16 ; i++){
			src[i] = dataArray[ i + BN[ 0 ] * 16 ]; 
	}
	i = 0;
		
	retval = pstorage_block_identifier_get(&handle, BN[0], block); //get block to write
	
           APP_ERROR_CHECK(retval);
		
	pstorage_wait_handle = block->block_id;
	pstorage_wait_flag = 1;
	retval = pstorage_clear( block, 0x10);
	while(pstorage_wait_flag) { power_manage(); } 
					
	pstorage_wait_handle = block->block_id;
	pstorage_wait_flag = 1;
	retval = pstorage_store( block, src, 16, 0);
	while(pstorage_wait_flag) { power_manage(); } 		
	    APP_ERROR_CHECK( retval);
}

The last part of code uses pstorage is about clearing the field we are writing. The aim of clearing is not to read same data after restart if the data is transmited. ( This function is called after BLE_GAP_EVT_DISCONNECTED event):

 static void ps_clear_data( pstorage_handle_t *hand, pstorage_handle_t *block ){
	// bir kere yap1lan fonksiyon çagrisi yeterli olur. parametreler daha sonra düzenlenecek.
	
	int retval;	
	static uint8_t src[16] __attribute__((aligned(16))) =   {0};
	static pstorage_handle_t block_handlers[10];
	static int i = 0;
	/*
	pstorage_wait_flag = 1;			
	pstorage_wait_handle = hand->block_id;
	pstorage_clear( hand, (0x10)*10);
	while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
	*/		
		
	for( i = 0; i < days / 16 ; i++){
			pstorage_block_identifier_get( &handle, i , &block_handlers[i]);
			pstorage_wait_flag = 1;
			pstorage_wait_handle = block_handlers[i].block_id;
			//pstorage_store( &block_handlers[i], src, 16, 0);
			retval = pstorage_clear( &block_handlers[i], 0x10);		
			while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
	}
		

Those are codes i am using to implement pstorage. When i omit those lines by using preprocessor

#define  PSTORAGE 0

The code works well.

However, I don't want to lose data on restart when there is no connection. When activate pstorage, the nRF51 chip doesn't work well. Sometimes the device is not available on device list. It also stops sending chunk data.

I know it is hard to debug someone else code but what am I missing here?

  • Hi, I don't see anything wrong in the code you've listed above. Are you able to share the code so I can try to debug it here? You can send it in a PM if you don't want it to be public.

    "When activate pstorage, the nRF51 chip doesn't work well" This could indicate that it's either asserting or hanging in a loop. I'd suggest to attach a debugger to confirm this to locate the problem.

Related