static void example_cb_handler(pstorage_handle_t * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) { if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; //If we are waiting for this callback, clear the wait flag. } switch(op_code) { case PSTORAGE_LOAD_OP_CODE: if (result == NRF_SUCCESS) {} else {} break; case PSTORAGE_STORE_OP_CODE: if (result == NRF_SUCCESS) {} else {} break; case PSTORAGE_UPDATE_OP_CODE: if (result == NRF_SUCCESS) {} else {} break; case PSTORAGE_CLEAR_OP_CODE: if (result == NRF_SUCCESS) {} else {} break; } } static void pstorage_test_store_and_update(void) { pstorage_handle_t handle; pstorage_handle_t block_0_handle; pstorage_module_param_t param; uint8_t source_data_0[16] = {'N', 'O', 'R', 'D', 'I', 'C', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; uint8_t dest_data_0[16]; uint32_t retval; retval = pstorage_init(); if(retval == NRF_SUCCESS) { param.block_size = 16; //Select block size of 16 bytes param.block_count = 1; //Select 10 blocks, total of 160 bytes param.cb = example_cb_handler; //Set the pstorage callback handler retval = pstorage_register(¶m, &handle); if (retval == NRF_SUCCESS) { //Get block identifiers pstorage_block_identifier_get(&handle, 0, &block_0_handle); pstorage_clear(&block_0_handle, 16); //Clear 16 bytes pstorage_store(&block_0_handle, source_data_0, 16, 0); //Write to flash, only one block is allowed for each pstorage_store command pstorage_wait_flag = 1; //Set the wait flag. Cleared in the example_cb_handler while(pstorage_wait_flag) { power_manage(); } //Sleep until store operation is finished. pstorage_load(dest_data_0, &block_0_handle, 16, 0); //Read from flash, only one block is allowed for each pstorage_load command //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 //pstorage_update(&block_0_handle, source_data_0, 16, 0); //update flash block 0 while(pstorage_wait_flag) { power_manage(); } //Sleep until update operation is finished. } } } #endif //PSTORAGE /**@brief Application main function. */ int main(void) { bool erase_bonds; // Initialize. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false); buttons_leds_init(&erase_bonds); ble_stack_init(); gap_params_init((const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); services_init(); advertising_init(); conn_params_init(); bsp_event_handler(BSP_EVENT_ADVERTISING_START); #ifdef PSTORAGE pstorage_test_store_and_update(); #endif // Enter main loop. for (;;) { power_manage(); } }