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

could anybody give an example that pstorage works?

[environment: s110_nRF51822_7.0.0 + SDK nrf51_sdk_v6_0_0_43681 + PCA10000(QF AAG0)]

I failed to get pstorage working in my own code, then I picked up the sample code from DEV Zone and tried to test with two simple functions, I still failed to get it working, I even used nrf_delay_us(1000000). Could anybody help to point out the error in the code? Or give an example that works on 51822?

Code test with "ble_app_template":

//=========================test code ==========================

static void dm_pstorage_cb_handler(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) { }

static void pstorage_test(void) { pstorage_module_param_t flashparam; pstorage_handle_t flashhandle; pstorage_handle_t flash_block_handle;

uint32_t retval;

flashparam.block_size  = 20;
flashparam.block_count = 1;
flashparam.cb          = dm_pstorage_cb_handler;

// Initialize pStorage & Clear if first boot
retval = pstorage_init();
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}

// pStorage registration
retval = pstorage_register(&flashparam, &flashhandle);
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}
nrf_delay_us(1000000); 
retval = pstorage_clear(&flashhandle, 512*100);
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}

nrf_delay_us(1000000);

// Get block handle
retval = pstorage_block_identifier_get(&flashhandle, 0, &flash_block_handle);
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}
nrf_delay_us(1000000);
// Use pstorage to store data
uint8_t test_data[4] = {0x01,0x02,0x03,0x04};
retval = pstorage_store(&flash_block_handle, test_data, 4, 0);
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}
nrf_delay_us(1000000);
// Use pstorage to load data, for test check
// Get block handle
retval = pstorage_block_identifier_get(&flashhandle, 0, &flash_block_handle);
if (retval == NRF_SUCCESS){
    //Module initialization successful
}
else {
    //Initialization failed, take corrective action
}
nrf_delay_us(1000000);
uint8_t load_data[4];
retval = pstorage_load(load_data, &flash_block_handle, 4, 0);
if (retval == NRF_SUCCESS){
    //Module initialization successful
    
    if ((load_data[0] == 0x01)&(load_data[1] == 0x02)&(load_data[2] == 0x03)&(load_data[3] == 0x04))
    {
        //BlinkLED(2,500,500);
    }
}
else {
    //Initialization failed, take corrective action
}

}

//=========================test code====================

Parents
  • I took all the advices and changed my code, but still failed to get pstorage working. I added the main part of my code below, and what I can tell is the pstorage_clear signal can be received, which means the event handler is set properly; the code just stuck at the line while(ps_flag == 0); after I called pstorage_store, it means there are no interrupts for pstorage_store. Can anybody help to tell how to make pstorage_store working?

    /**===============test code========================*/
    
    static uint8_t test_data[4] = {0x01,0x02,0x03,0x04};
    static uint8_t ps_flag = 0;
    
    static void sys_evt_dispatch(uint32_t sys_evt)
    {
        pstorage_sys_event_handler(sys_evt);
    }
    
    static void ble_stack_init(void)
    {    
        SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
        uint32_t err_code;
        ble_enable_params_t ble_enable_params;
        memset(&ble_enable_params, 0, sizeof(ble_enable_params));
        ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
        err_code = sd_ble_enable(&ble_enable_params);
        APP_ERROR_CHECK(err_code);
        
        err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    }
    
    static void dm_pstorage_cb_handler(pstorage_handle_t * p_handle,
                                       uint8_t             op_code,
                                       uint32_t            result,
                                       uint8_t           * p_data,
                                       uint32_t            data_len)
    {
        switch(op_code)
        {
            case PSTORAGE_ERROR_OP_CODE:
                    ps_flag = 1;
                break;
            case PSTORAGE_STORE_OP_CODE: 
                    ps_flag = 2;           
                break;
            case PSTORAGE_LOAD_OP_CODE:
                    ps_flag = 3;           
                break;
            case PSTORAGE_CLEAR_OP_CODE:
                    ps_flag = 4;            
                break;
            case PSTORAGE_UPDATE_OP_CODE:
                    ps_flag = 5;
                break;
            default:
                    ps_flag = 6;
                break;
        }
    }
    
    
    
    static void pstorage_test(void)
    {
        pstorage_module_param_t flashparam;
        pstorage_handle_t       flashhandle;
        pstorage_handle_t       flash_block_handle;
        
        uint32_t retval;
        
        flashparam.block_size  = 20;
        flashparam.block_count = 1;
        flashparam.cb          = dm_pstorage_cb_handler;
      
        retval = pstorage_init();   
        retval = pstorage_register(&flashparam, &flashhandle);   
        
        retval = pstorage_clear(&flashhandle, 20);  
        
        while(ps_flag == 0);
        ps_flag = 0;
       
        retval = pstorage_block_identifier_get(&flashhandle, 0, &flash_block_handle);      
        retval = pstorage_store(&flash_block_handle, test_data, 4, 0);
           
        while(ps_flag == 0);
        ps_flag = 0;    
       
        retval = pstorage_block_identifier_get(&flashhandle, 0, &flash_block_handle);
         
        uint8_t load_data[4];
        retval = pstorage_load(load_data, &flash_block_handle, 4, 0);
        
        while(ps_flag == 0);
        ps_flag = 0;
        
        if (retval == NRF_SUCCESS)
        {   
            if ((load_data[0] == 0x01)&(load_data[1] == 0x02)&(load_data[2] == 0x03)&(load_data[3] == 0x04))
            {
                ps_flag = 100;
            }
        }   
    }
    
    /**=============test code===================================*/
    
  • ps: I tested it on QF AAC0 and QF AAG0, both failed.

Reply Children
No Data
Related