<?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>I have written a custom characteristic and now I want to preserve its value in flash.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/14225/i-have-written-a-custom-characteristic-and-now-i-want-to-preserve-its-value-in-flash</link><description>I have written a custom characteristic and now I want to preserve its value in flash. 
 Is there any example of how to do this?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 01 Jun 2016 14:47:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/14225/i-have-written-a-custom-characteristic-and-now-i-want-to-preserve-its-value-in-flash" /><item><title>RE: I have written a custom characteristic and now I want to preserve its value in flash.</title><link>https://devzone.nordicsemi.com/thread/54341?ContentTypeID=1</link><pubDate>Wed, 01 Jun 2016 14:47:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:72554fec-d55f-44e1-9592-e02b9d0f9e6a</guid><dc:creator>mosi</dc:creator><description>&lt;p&gt;I finally got it to work. &lt;strong&gt;But could someone at Nordic comment on this code if I do something wrong (even while it works).&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is my test code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static pstorage_handle_t pstorage_handle;
static uint8_t pstorage_wait_flag = 0;
static pstorage_block_t pstorage_wait_handle = 0;

// Event Notification Handler.
static void example_cb_handler( pstorage_handle_t * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len )
{
	SEGGER_RTT_printf( 0, &amp;quot;op_code = %&amp;quot; PRIu8 &amp;quot;, result = %&amp;quot; PRIu32 &amp;quot;, data_len = %&amp;quot; PRIu32 &amp;quot;, data = &amp;quot;, op_code, result, data_len );

	for ( uint32_t i = 0; i &amp;lt; data_len; i++ )
	{
		SEGGER_RTT_printf( 0, &amp;quot;%&amp;quot; PRIu8 &amp;quot; &amp;quot;, p_data[ i ] );

	}
	SEGGER_RTT_printf( 0, &amp;quot;\n&amp;quot; );

	if ( handle-&amp;gt;block_id == pstorage_wait_handle )
	{
		pstorage_wait_flag = 0;
	}

	switch ( op_code )
	{
	case PSTORAGE_STORE_OP_CODE:
		break;
	case PSTORAGE_LOAD_OP_CODE:
		break;
	case PSTORAGE_CLEAR_OP_CODE:
		break;
	case PSTORAGE_UPDATE_OP_CODE:
		break;
	}
}

void test( void )
{
	uint32_t err_code;
	pstorage_handle_t block_0_handle;
	uint8_t block_0_write_data[ 32 ] =
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
	uint8_t block_0_read_data[ 32 ];

	// Initialize persistent storage module.
	err_code = pstorage_init();
	APP_ERROR_CHECK( err_code );

	pstorage_module_param_t param =
	{ .block_size = 32, .block_count = 1, .cb = example_cb_handler };
	SEGGER_RTT_printf( 0, &amp;quot;\r\n\r\npstorage initializing ... \r\n&amp;quot; );
	err_code = pstorage_register( &amp;amp;param, &amp;amp;pstorage_handle );
	APP_ERROR_CHECK( err_code );

	pstorage_block_identifier_get( &amp;amp;pstorage_handle, 0, &amp;amp;block_0_handle );

	pstorage_wait_handle = block_0_handle.block_id;
	pstorage_wait_flag = 1;
	err_code = pstorage_clear( &amp;amp;block_0_handle, 32 );
	APP_ERROR_CHECK( err_code );
	while ( pstorage_wait_flag )
	{
		power_manage();     //Sleep until store operation is finished.
	}

	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
	SEGGER_RTT_printf( 0, &amp;quot;pstorage store into block 0\r\n&amp;quot; );
	err_code = pstorage_store( &amp;amp;block_0_handle, block_0_write_data, 32, 0 );     //Write to flash
	APP_ERROR_CHECK( err_code );
	SEGGER_RTT_printf( 0, &amp;quot;pstorage wait for block 2 store to complete ... \r\n&amp;quot; );
	while ( pstorage_wait_flag )
	{
		power_manage();     //Sleep until store operation is finished.
	}

	SEGGER_RTT_printf( 0, &amp;quot;pstorage load blocks 0 \r\n&amp;quot; );
	err_code = pstorage_load( block_0_read_data, &amp;amp;block_0_handle, 32, 0 );
	APP_ERROR_CHECK( err_code );
	SEGGER_RTT_printf( 0, &amp;quot;dest_data_0 = &amp;quot; );

	for ( uint32_t i = 0; i &amp;lt; 32; i++ )
	{
		SEGGER_RTT_printf( 0, &amp;quot;%&amp;quot; PRIu8 &amp;quot; &amp;quot;, block_0_read_data[ i ] );

	}
	SEGGER_RTT_printf( 0, &amp;quot;\n&amp;quot; );
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the RTT viewer output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;0&amp;gt;  pstorage initializing ... 
 0&amp;gt;  sys_evt_dispatch()
 0&amp;gt;  op_code = 3, result = 0 data_len = 32 data = 192 7 0 0 209 6 0 0 209 0 0 0 177 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
 0&amp;gt;  pstorage store into block 0
 0&amp;gt;  pstorage wait for block 2 store to complete ... 
 0&amp;gt;  sys_evt_dispatch()
 0&amp;gt;  op_code = 1, result = 0 data_len = 32 data = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
 0&amp;gt;  pstorage load blocks 0 
 0&amp;gt;  op_code = 2, result = 0 data_len = 32 data = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
 0&amp;gt;  dest_data_0 = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I have written a custom characteristic and now I want to preserve its value in flash.</title><link>https://devzone.nordicsemi.com/thread/54340?ContentTypeID=1</link><pubDate>Wed, 01 Jun 2016 13:48:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e34401f-bcf7-4f56-b7b2-dacee3e4edea</guid><dc:creator>mosi</dc:creator><description>&lt;p&gt;It would be so much easier and faster if there was an example. I can&amp;#39;t get it to work.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I have written a custom characteristic and now I want to preserve its value in flash.</title><link>https://devzone.nordicsemi.com/thread/54339?ContentTypeID=1</link><pubDate>Wed, 01 Jun 2016 11:38:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:910659d0-7713-4e35-935e-df99c6763290</guid><dc:creator>Wojtek</dc:creator><description>&lt;p&gt;You can use pstorage library, it is well described &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v11.0.0/lib_pstorage.html?cp=5_0_0_3_19"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>