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

SDK12 Pstorage (Solution)

Hi, SDK12 does not have Pstorage. I want to show you how to add it.

Steps:

  1. Copy the pstorage folder in SDK11 into the SDK12 \components folder.

  2. Open any project in SDK12

  3. Add pstorage.c in nRF_Libraries on the project side.

    image description

  4. Keil, Options for Target -> C/C++ Tab inside

image description

Okay.

its now code time:

  1. Added library in to main.c

    #include "pstorage.h"

  2. Added Initalize Event functions

     static void sys_evt_dispatch(uint32_t sys_evt)
    

    { pstorage_sys_event_handler(sys_evt);

    }

  3. Added Flash Read/Write Codes (handle,update and only read functions)

     static uint8_t pstorage_wait_flag = 0;
    

    static pstorage_block_t pstorage_wait_handle = 0; static void flash_cb_handler(pstorage_handle_t * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) { if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; } //If we are waiting for this callback, clear the wait flag.

     	switch(op_code)
     	{
     		case PSTORAGE_LOAD_OP_CODE:
     			 if (result == NRF_SUCCESS)
     			 {
     			
     					 bsp_indication_set(BSP_INDICATE_ALERT_0);				 
     			 }
     			 else
     			 {
     				
     					 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     			 }
     			 break;
     		case PSTORAGE_STORE_OP_CODE:
     			 if (result == NRF_SUCCESS)
     			 {
     			
     					 bsp_indication_set(BSP_INDICATE_ALERT_1);
     			 }
     			 else
     			 {
     				
     					 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     			 }
     			 break;				 
     		case PSTORAGE_UPDATE_OP_CODE:
     			 if (result == NRF_SUCCESS)
     			 {
     					
     					 bsp_indication_set(BSP_INDICATE_ALERT_2);
     			 }
     			 else
     			 {
     					 
     					 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     			 }
     			 break;
     		case PSTORAGE_CLEAR_OP_CODE:
     			 if (result == NRF_SUCCESS)
     			 {
     					
     					 bsp_indication_set(BSP_INDICATE_ALERT_3);
     			 }
     			 else
     			 {
     				 
     					 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     			 }
     			 break;	 
     	}			
    

    }

     	uint8_t                 dest_data_0[16];
     	uint8_t                 dest_data_1[16];
     	uint8_t                 dest_data_2[16];
    

    void read_flash() { pstorage_handle_t handle; pstorage_handle_t block_0_handle; pstorage_handle_t block_1_handle; pstorage_handle_t block_2_handle; pstorage_module_param_t param;

     	uint32_t                retval;
     			retval = pstorage_init();
     	if(retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
     	}
          
     	param.block_size  = 16;                   //Select block size of 16 bytes
     	param.block_count = 4;                   //Select 10 blocks, total of 160 bytes
     	param.cb          = flash_cb_handler;   //Set the pstorage callback handler
     		
     	
     	retval = pstorage_register(&param, &handle);
     	if (retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
     	}
     	
    

    // //Get block identifiers

     	pstorage_block_identifier_get(&handle, 2, &block_2_handle);
     	
     	//Clear 100 bytes, i.e. one block
    
     	retval = pstorage_clear(&block_0_handle, 48);                       
     	if(retval != NRF_SUCCESS)  { bsp_indication_set(BSP_INDICATE_RCV_ERROR); }
     	
     	
     	//Store data to three blocks. Wait for the last store operation to finish before reading out the data.
    
    
     	if(retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     	}
     	
    
    
     	pstorage_load(dest_data_2, &block_2_handle, 16, 0);		
    
     	
    

    } static void pstorage_test_store_and_update(void) { pstorage_handle_t handle; pstorage_handle_t block_0_handle; pstorage_handle_t block_1_handle; pstorage_handle_t block_2_handle; pstorage_module_param_t param; uint8_t source_data_0[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; uint8_t source_data_1[16] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; uint8_t source_data_2[16] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F}; uint8_t source_data_9[16] = {'7', '2', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', '9', '\0'}; uint8_t dest_data_0[16]; uint8_t dest_data_1[16]; uint8_t dest_data_2[16]; uint32_t retval;

     	retval = pstorage_init();
     	if(retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
     	}
          
     	param.block_size  = 16;                   //Select block size of 16 bytes
     	param.block_count = 4;                   //Select 10 blocks, total of 160 bytes
     	param.cb          = flash_cb_handler;   //Set the pstorage callback handler
     		
     	
     	retval = pstorage_register(&param, &handle);
     	if (retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
     	}
     	
    

    // //Get block identifiers pstorage_block_identifier_get(&handle, 0, &block_0_handle); pstorage_block_identifier_get(&handle, 1, &block_1_handle); pstorage_block_identifier_get(&handle, 2, &block_2_handle);

     	//Clear 100 bytes, i.e. one block
     
     	retval = pstorage_clear(&block_0_handle, 48);                       
     	if(retval != NRF_SUCCESS)  { bsp_indication_set(BSP_INDICATE_RCV_ERROR); }
     	
     	
     	//Store data to three blocks. Wait for the last store operation to finish before reading out the data.
    
     	pstorage_store(&block_0_handle, source_data_0, 16, 0);     //Write to flash, only one block is allowed for each pstorage_store command
     	if(retval != NRF_SUCCESS)
     	{
     			bsp_indication_set(BSP_INDICATE_RCV_ERROR);
     	}
     	
     	
     	pstorage_store(&block_1_handle, source_data_1, 16, 0);     //Write to flash, only one block is allowed for each pstorage_store command		
     	pstorage_wait_handle = block_2_handle.block_id;            //Specify which pstorage handle to wait for
     	pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler
     	
     	pstorage_store(&block_2_handle, source_data_9, 16, 0);     //Write to flash
     
     	while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
     	
    
     	pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
     
     	pstorage_load(dest_data_1, &block_1_handle, 16, 0);				 //Read from flash
    
     	pstorage_load(dest_data_2, &block_2_handle, 16, 0);			   //Read from flash
     	
     	
     	
     	pstorage_wait_handle = block_0_handle.block_id;            //Specify which pstorage handle to wait for
     	pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler
     	pstorage_clear(&block_0_handle, 32);                       //Clear 32 bytes
    
       while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
     	
    
     	pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    
     	pstorage_load(dest_data_1, &block_1_handle, 16, 0);				 //Read from flash
    
     	pstorage_load(dest_data_2, &block_2_handle, 16, 0);			   //Read from flash
     	
    
     	pstorage_wait_handle = block_0_handle.block_id;            //Specify which pstorage handle to wait for 
     	pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler
     	pstorage_update(&block_0_handle, source_data_9, 16, 0);    //update flash block 0
    
       while(pstorage_wait_flag) { power_manage(); }              //Sleep until update operation is finished.
     	
     
     	pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    
     	pstorage_load(dest_data_1, &block_1_handle, 16, 0);				 //Read from flash
    
     	pstorage_load(dest_data_2, &block_2_handle, 16, 0);			   //Read from flash
    

    }

  • good to know that it worked without any conflicts .. maybe you can do the same with device manaer? as it internally uses pstorage. Many are not switching to latest SDK because they do not want to change too much code to switch from device manager to peer manager

  • To Aryan's comment above: Nordic haphazardly removed pstorage/device manager. It should have kept some hooks for backward compatibility. But, nope! For many usecases pstorage is much much better than FDS.. things like really small log data chunks, these can easily fit into a circular buffer managed through pstorage. Also, the entire buttonless DFU stuff is broken. Too much of deconstruction and rewriting of code when we move from sdk7.2/nRF51 to sdk12/nRF52!!!! Nordic - please learn something!

Related