Good morning, I´m trying the example of this post (ble_app_template_with_pstorage_operations_nRF51DK.zip). I have SDK 8.1.0, softdevice 110 and nrf51-dk.
The source works, but i need to modify, following the example, pstorage functions. I have added uart to show messages and data, I have modified only "pstorage_test_store_and_update" and "example_cb_handler" (adding printf) functions.
The example uses three blocks but as I only want to do a bit test, I only use one block. Save, clear, load works without error but when load the data and show it, is corrupt. I have seen posts about pstorage but i don´t find why when i load, the data is corrupt.
uart_event_handle function:
void uart_event_handle(app_uart_evt_t * p_event){
uint8_t cr;
static uint8_t index = 0;
uint32_t err_code;
switch (p_event->evt_type){
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&cr));
UNUSED_VARIABLE(app_uart_put(cr));
if(cr == 's'){
pstorage_test_store_and_update(0);
}
if(cr == 'l'){
pstorage_test_store_and_update(1);
}
if(cr == 'c'){
pstorage_test_store_and_update(2);
}
if(cr == 'u'){
pstorage_test_store_and_update(3);
}
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
pstorage_test_store_and_update function:
static void pstorage_test_store_and_update(int funcion){
pstorage_handle_t handle;
pstorage_handle_t block_0_handle;
pstorage_module_param_t param;
uint8_t source_data_0[10] = {(uint8_t)0, (uint8_t)1, (uint8_t)2, (uint8_t)3, (uint8_t)4, (uint8_t)5, (uint8_t)6, (uint8_t)7, (uint8_t)8, (uint8_t)9};
uint8_t dest_data_0[10];
uint32_t error_code;
error_code = pstorage_init();
if(error_code != NRF_SUCCESS){
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
printf("error: %zu ", error_code);
}
param.block_size = 16; //Select block size of 16 bytes
param.block_count = 1; //Select 1 block, total of 16 bytes
param.cb = example_cb_handler; //Set the pstorage callback handler
error_code = pstorage_register(¶m, &handle);
if (error_code != NRF_SUCCESS){
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
printf("error: %zu ", error_code);
}
//Get block identifiers
pstorage_block_identifier_get(&handle, 0, &block_0_handle);
if(funcion == 0){
//Store data
pstorage_store(&block_0_handle, source_data_0, 16, 0); //Write to flash, only one block is allowed for each pstorage_store command
if(error_code != NRF_SUCCESS){
bsp_indication_set(BSP_INDICATE_RCV_ERROR);
}
}
if(funcion == 1){
error_code = pstorage_load(dest_data_0, &block_0_handle, 16, 0); //Read from flash, only one block is allowed for each pstorage_load command
printf("error_code: %zu\n", error_code);
printf("datos: %d / %d / %d ", (int)dest_data_0[0], (int)dest_data_0[1], (int)dest_data_0[2]);
}
if(funcion == 2){
//Clear 16 bytes, i.e. one block
error_code = pstorage_clear(&block_0_handle, 16);
if(error_code != NRF_SUCCESS){
bsp_indication_set(BSP_INDICATE_RCV_ERROR);
}
}
if(funcion == 3){
pstorage_update(&block_0_handle, source_data_0, 16, 0);
}
}
Hyperterminal with messages from uart.
Thank you.