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?
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?
I finally got it to work. But could someone at Nordic comment on this code if I do something wrong (even while it works).
This is my test code:
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, "op_code = %" PRIu8 ", result = %" PRIu32 ", data_len = %" PRIu32 ", data = ", op_code, result, data_len );
for ( uint32_t i = 0; i < data_len; i++ )
{
SEGGER_RTT_printf( 0, "%" PRIu8 " ", p_data[ i ] );
}
SEGGER_RTT_printf( 0, "\n" );
if ( handle->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, "\r\n\r\npstorage initializing ... \r\n" );
err_code = pstorage_register( ¶m, &pstorage_handle );
APP_ERROR_CHECK( err_code );
pstorage_block_identifier_get( &pstorage_handle, 0, &block_0_handle );
pstorage_wait_handle = block_0_handle.block_id;
pstorage_wait_flag = 1;
err_code = pstorage_clear( &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, "pstorage store into block 0\r\n" );
err_code = pstorage_store( &block_0_handle, block_0_write_data, 32, 0 ); //Write to flash
APP_ERROR_CHECK( err_code );
SEGGER_RTT_printf( 0, "pstorage wait for block 2 store to complete ... \r\n" );
while ( pstorage_wait_flag )
{
power_manage(); //Sleep until store operation is finished.
}
SEGGER_RTT_printf( 0, "pstorage load blocks 0 \r\n" );
err_code = pstorage_load( block_0_read_data, &block_0_handle, 32, 0 );
APP_ERROR_CHECK( err_code );
SEGGER_RTT_printf( 0, "dest_data_0 = " );
for ( uint32_t i = 0; i < 32; i++ )
{
SEGGER_RTT_printf( 0, "%" PRIu8 " ", block_0_read_data[ i ] );
}
SEGGER_RTT_printf( 0, "\n" );
}
And the RTT viewer output:
0> pstorage initializing ...
0> sys_evt_dispatch()
0> 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> pstorage store into block 0
0> pstorage wait for block 2 store to complete ...
0> sys_evt_dispatch()
0> 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> pstorage load blocks 0
0> 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> 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
I finally got it to work. But could someone at Nordic comment on this code if I do something wrong (even while it works).
This is my test code:
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, "op_code = %" PRIu8 ", result = %" PRIu32 ", data_len = %" PRIu32 ", data = ", op_code, result, data_len );
for ( uint32_t i = 0; i < data_len; i++ )
{
SEGGER_RTT_printf( 0, "%" PRIu8 " ", p_data[ i ] );
}
SEGGER_RTT_printf( 0, "\n" );
if ( handle->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, "\r\n\r\npstorage initializing ... \r\n" );
err_code = pstorage_register( ¶m, &pstorage_handle );
APP_ERROR_CHECK( err_code );
pstorage_block_identifier_get( &pstorage_handle, 0, &block_0_handle );
pstorage_wait_handle = block_0_handle.block_id;
pstorage_wait_flag = 1;
err_code = pstorage_clear( &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, "pstorage store into block 0\r\n" );
err_code = pstorage_store( &block_0_handle, block_0_write_data, 32, 0 ); //Write to flash
APP_ERROR_CHECK( err_code );
SEGGER_RTT_printf( 0, "pstorage wait for block 2 store to complete ... \r\n" );
while ( pstorage_wait_flag )
{
power_manage(); //Sleep until store operation is finished.
}
SEGGER_RTT_printf( 0, "pstorage load blocks 0 \r\n" );
err_code = pstorage_load( block_0_read_data, &block_0_handle, 32, 0 );
APP_ERROR_CHECK( err_code );
SEGGER_RTT_printf( 0, "dest_data_0 = " );
for ( uint32_t i = 0; i < 32; i++ )
{
SEGGER_RTT_printf( 0, "%" PRIu8 " ", block_0_read_data[ i ] );
}
SEGGER_RTT_printf( 0, "\n" );
}
And the RTT viewer output:
0> pstorage initializing ...
0> sys_evt_dispatch()
0> 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> pstorage store into block 0
0> pstorage wait for block 2 store to complete ...
0> sys_evt_dispatch()
0> 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> pstorage load blocks 0
0> 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> 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