Persistent Storage Interface
#include <stdbool.h>
#include "nrf_delay.h"
#include "app_trace.h"
#include "app_error.h"
#include "storage.h"
#include "ble_conn_params.h"
#include "nrf_sdm.h"
#include "softdevice_handler.h"
#include "pstorage.h"
static uint8_t pstorage_wait_flag = 0;
static pstorage_block_t pstorage_wait_handle = 0;
void app_error_handler ( uint32_t error_code,
uint32_t line_num,
const uint8_t * p_file_name
) {
app_trace_log("error_code: %d\r\n", error_code);
app_trace_log("line_num: %d\r\n", line_num);
app_trace_log("p_file_name: %s\r\n", p_file_name);
}
static void sys_evt_dispatch(uint32_t sys_evt)
{
pstorage_sys_event_handler(sys_evt);
}
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
}
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; }
switch(op_code)
{
case PSTORAGE_LOAD_OP_CODE:
if (result == NRF_SUCCESS)
{
app_trace_log("PSTORAGE_LOAD_OP_CODE: SUCCESS\r\n");
}
else
{
app_trace_log("PSTORAGE_LOAD_OP_CODE: FAULT\r\n");
}
break;
case PSTORAGE_STORE_OP_CODE:
if (result == NRF_SUCCESS)
{
app_trace_log("PSTORAGE_STORE_OP_CODE: SUCCESS\r\n");
}
else
{
app_trace_log("PSTORAGE_STORE_OP_CODE: FAULT\r\n");
}
break;
case PSTORAGE_UPDATE_OP_CODE:
if (result == NRF_SUCCESS)
{
app_trace_log("PSTORAGE_UPDATE_OP_CODE: SUCCESS\r\n");
}
else
{
app_trace_log("PSTORAGE_ERROR_LED_PIN_NO: FAULT\r\n");
}
break;
case PSTORAGE_CLEAR_OP_CODE:
if (result == NRF_SUCCESS)
{
app_trace_log("PSTORAGE_ERROR_LED_PIN_NO: SUCCESS\r\n");
}
else
{
app_trace_log("PSTORAGE_ERROR_LED_PIN_NO: FAULT\r\n");
}
break;
}
}
static void power_manage(void)
{
uint32_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
}
int main(void)
{
app_trace_init();
uint32_t err_code;
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, false);
ble_enable_params_t ble_enable_params;
memset(&ble_enable_params, 0, sizeof(ble_enable_params));
err_code = sd_ble_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
APP_ERROR_CHECK(err_code);
pstorage_handle_t handle;
pstorage_handle_t block_0_handle;
pstorage_handle_t block_1_handle;
pstorage_module_param_t param;
uint8_t write_data_0[16] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50};
uint8_t write_data_1[16] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x4d, 0x3e, 0x3f, 0x40};
uint8_t read_data_0[16];
uint8_t read_data_1[16];
err_code = pstorage_init();
if(err_code != NRF_SUCCESS)
{
app_trace_log("fail of intialize pstorage.");
}
param.block_size = 16;
param.block_count = 10;
param.cb = example_cb_handler;
err_code = pstorage_register(¶m, &handle);
if (err_code != NRF_SUCCESS)
{
app_trace_log(" fail of register pstorage.");
}
pstorage_block_identifier_get(&handle, 0, &block_0_handle);
pstorage_block_identifier_get(&handle, 1, &block_1_handle);
pstorage_clear(&block_0_handle, 32);
pstorage_wait_handle = block_1_handle.block_id;
pstorage_wait_flag = 1;
pstorage_store(&block_0_handle, write_data_0, 16, 0);
pstorage_store(&block_1_handle, write_data_1, 16, 0);
while(pstorage_wait_flag) { power_manage(); }
pstorage_wait_handle = block_0_handle.block_id;
pstorage_wait_flag = 1;
pstorage_load(read_data_0, &block_0_handle, 16, 0);
pstorage_load(read_data_1, &block_1_handle, 16, 0);
app_trace_log("First value of read_data_0: %c\r\n", read_data_0[0]);
app_trace_log("First value of read_data_1: %c\r\n", read_data_1[0]);
while (true)
{
power_manage();
}
}