<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/7421/how-to-do-pstorage-store-after-advertising</link><description>Hello, 
 I try to perform an pstorage store action after I&amp;#39;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&amp;#39;t get it&amp;#39;s callback</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 05 Jun 2015 12:37:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/7421/how-to-do-pstorage-store-after-advertising" /><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26406?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:37:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e0a70c3-988b-444d-903f-162c508f22f4</guid><dc:creator>michaeld</dc:creator><description>&lt;p&gt;Ok thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26405?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:36:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37e63d15-ba1d-425a-858c-7e3cb6d675ea</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;This should not have happened, getting BLE_ADV_EVT_IDLE means that advertising event has ended. But as many threads here mentioned that calling this way from other interrupt priority might mess up with pstorage.It is best the way you are doing now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26404?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:17:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2796c890-4e6f-4940-a285-f4fb6af8dc4e</guid><dc:creator>michaeld</dc:creator><description>&lt;p&gt;I called it in the BLE_ADV_EVT_IDLE before and then the callback isn&amp;#39;t received but when I trigger it in main it is apparently.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
	#if INDICATE_LEDS
uint32_t err_code;
	#endif
switch (ble_adv_evt)
{
		case BLE_ADV_EVT_FAST:
				#if INDICATE_LEDS
			  err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
      APP_ERROR_CHECK(err_code);
				#endif
				m_is_timeout = false;
		
				application_timers_start();
				
				break;
		case BLE_ADV_EVT_IDLE:
				#if INDICATE_LEDS
				err_code = bsp_indication_set(BSP_INDICATE_IDLE);
      APP_ERROR_CHECK(err_code);
				#endif
				m_is_timeout = true;
				pstorage_write_alldata();
				break;
		default:
				break;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26403?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:14:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3a5467f-3996-41ba-b56d-8c71dae5948e</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Aha, so before it was called when the advertisement is still happening.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26402?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:09:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d5f990d-f480-49c1-a196-63ada03d42bf</guid><dc:creator>michaeld</dc:creator><description>&lt;p&gt;Yes I have. I was just testing this code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    // Enter main loop.
for (;;)
{
		if (m_adv_mode_current == BLE_ADV_MODE_IDLE)
		{
			pstorage_write_alldata();
		}
		
		app_sched_execute();
		power_manage();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And it seems to work. Probably has something todo with calling the function in an BLE event.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26401?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 12:07:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:322a2b27-f088-4128-8f94-8ff4d5423c55</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;have you added this below code&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void sys_evt_dispatch(uint32_t sys_evt)
{
    pstorage_sys_event_handler(sys_evt);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;this is needed for your callback to be triggered&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26399?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 09:07:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69af217e-6d89-4ad2-ad9c-7e7964e12afa</guid><dc:creator>michaeld</dc:creator><description>&lt;p&gt;I have edited my answer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26398?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 08:36:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdb4e6af-21c7-4376-bd89-1d89bba6108e</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Check if &lt;a href="https://devzone.nordicsemi.com/question/37368/pstorage-best-practices/"&gt;this&lt;/a&gt; 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.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to do pstorage store after advertising</title><link>https://devzone.nordicsemi.com/thread/26400?ContentTypeID=1</link><pubDate>Fri, 05 Jun 2015 06:40:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f707719d-7b25-497a-a7eb-bcb10edaea21</guid><dc:creator>michaeld</dc:creator><description>&lt;p&gt;Thank you for your answer! I checked and my device isn&amp;#39;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&amp;#39;t.&lt;/p&gt;
&lt;p&gt;And I don&amp;#39;t get why it is not getting the callback.&lt;/p&gt;
&lt;p&gt;This is my code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Init Function:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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(&amp;amp;param, &amp;amp;base_handle);
APP_ERROR_CHECK(err_code);	
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Handler:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Update Function:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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(&amp;amp;base_handle, index,&amp;amp;block_handle);
APP_ERROR_CHECK(err_code);

err_code = pstorage_store(&amp;amp;block_handle, pstorage_data, BLOCKS,0);
APP_ERROR_CHECK(err_code);

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Waiting for callback:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void pstorage_write_alldata(void)
{	
pstorage_flag = 1;	
livit_pstorage_update(); //Note if you use livit_pstorage_update, you don&amp;#39;t have to erase before storing

while(pstorage_flag) 
{ 
	power_manage();
}   
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;
Call the write function in the main by checking the advertising status.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>