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

How to do pstorage store after advertising

Hello,

I try to perform an pstorage store action after I'm done advertising just beform the devices goes in system OFF mode. Pstorage works fine before I start advertising but when I have started advertising the pstorage store doesn't get it's callback anymore.

Now I read in a couple of questions you can't perform a store action on a advertising event because the callback will not be received.

My question is how or where can I do a store action when the devices goes into IDLE mode/stops advertising. I tried triggering it on a timer, putting it in the scheduler I'm kind of out of options.

  • Thank you for your answer! I checked and my device isn't advertising when I try to do the pstorage update just before system goes in system_off mode. Because this is triggered when device is in IDLE and not advertising. On start-up before advertising is started the pstorage update works fine but after it doesn't.

    And I don't get why it is not getting the callback.

    This is my code:

    Init Function:

    void livit_pstorage_init(void)
    {
    uint32_t               err_code;
    
    pstorage_module_param_t param;
    param.block_size  = BLOCKS;
    param.block_count = 1;
    param.cb          = livit_pstorage_cb_handler;
    
    err_code = pstorage_init();
    APP_ERROR_CHECK(err_code);
    err_code = pstorage_register(&param, &base_handle);
    APP_ERROR_CHECK(err_code);	
    }
    

    Handler:

    static void livit_pstorage_cb_handler(pstorage_handle_t * p_handle,
                                   uint8_t             op_code,
                                   uint32_t            result,
                                   uint8_t           * p_data,
                                   uint32_t            data_len)
    {
    pstorage_flag = 0;
    }
    

    Update Function:

    void livit_pstorage_update(void)
    {
    pstorage_handle_t       block_handle;
    uint8_t 								index;
    uint32_t               	err_code;
    uint16_t								i = 0;
    uint8_t									k = 0;
    
    __align(4) uint8_t 		  pstorage_data[BLOCKS];
    
    pstorage_data[i++] = m_first_boot;
    
    pstorage_data[i++] = m_config.toggle_settings;
    pstorage_data[i++] = m_config.step_ths;
    pstorage_data[i++] = m_config.step_debounce;
    pstorage_data[i++] = m_config.wear_time_ths;
    pstorage_data[i++] = m_config.amount_sync_days;
    pstorage_data[i++] = m_config.amount_of_filled_entries;
        ....... (deleded some code for better overview)
    
    index = 0; //we will write to the second block
    err_code = pstorage_block_identifier_get(&base_handle, index,&block_handle);
    APP_ERROR_CHECK(err_code);
    
    err_code = pstorage_store(&block_handle, pstorage_data, BLOCKS,0);
    APP_ERROR_CHECK(err_code);
    
    }
    

    Waiting for callback:

    void pstorage_write_alldata(void)
    {	
    pstorage_flag = 1;	
    livit_pstorage_update(); //Note if you use livit_pstorage_update, you don't have to erase before storing
    
    while(pstorage_flag) 
    { 
    	power_manage();
    }   
    }
    

    SOLUTION: Call the write function in the main by checking the advertising status.

  • Check if this post helps. You can do pstorage while advertising but the outcome depends on your advertising interval. pstorage needs long time to do writes and your advertising event will win in priority with your store operation.

  • have you added this below code

    static void sys_evt_dispatch(uint32_t sys_evt)
    {
        pstorage_sys_event_handler(sys_evt);
    }
    

    this is needed for your callback to be triggered

  • Yes I have. I was just testing this code:

        // Enter main loop.
    for (;;)
    {
    		if (m_adv_mode_current == BLE_ADV_MODE_IDLE)
    		{
    			pstorage_write_alldata();
    		}
    		
    		app_sched_execute();
    		power_manage();
    }
    

    And it seems to work. Probably has something todo with calling the function in an BLE event.

Related