I want to use inner flash to work as event log storage.
So in pstorage_platform.h
I set PSTORAGE_NUM_OF_PAGES
as 60. So I can use 60 pages.
#define USER_BLOCK_SIZE 64
#define USER_BLOCK_COUNT 6
then I register pstorage.
the log function is as follow:
struct user_flash_event_t
{
uint8_t logs[16][4];
};
void user_flash_event_log(log_node log)
{
uint32_t err_code;
pstorage_handle_t handle_block;
BLE_LOG("save in (%d,%d)", event_block_index, event_log_index);
struct user_flash_event_t event;
err_code = pstorage_block_identifier_get(&m_base_block, event_block_index, &handle_block);
APP_ERROR_CHECK(err_code);
err_code = pstorage_load((uint8_t *)(&event), &handle_block, sizeof(struct user_flash_event_t), 0);
APP_ERROR_CHECK(err_code);
event.logs[event_log_index][0] = 0;//log.time_stamp >> 8;
event.logs[event_log_index][1] = 0;//log.time_stamp & 0x00ff;
event.logs[event_log_index][2] = 0;
event.logs[event_log_index][3] = 0;//log.event_id;
err_code = pstorage_update(&handle_block, (uint8_t *)&event, sizeof(struct user_flash_event_t), 0);
APP_ERROR_CHECK(err_code);
log_next_position();
}
the log_next_position()
function will update event_block_index
& event_log_index
to next place.
void user_flash_event_read_last(void){
if(read_log == NULL){
return;
}
int err_code;
int block_index = event_block_index;
int log_index = event_log_index;
pstorage_handle_t handle_block;
log_last_position(&block_index, &log_index);
BLE_LOG("read (%d,%d)", block_index, log_index);
/* read flash data to event */
struct user_flash_event_t event;
err_code = pstorage_block_identifier_get(&m_base_block, block_index, &handle_block);
APP_ERROR_CHECK(err_code);
err_code = pstorage_load((uint8_t *)(&event), &handle_block, sizeof(struct user_flash_event_t), 0);
APP_ERROR_CHECK(err_code);
read_log(to_log_node(event.logs[log_index]));
}
the log_last_position()
function will get the last log position stored in flash.
when I call following function in a 1.5s timer handler
static bool change = false;
if(change){
log_node ln;
ln.time_stamp = time_stamp_min;
ln.event_id = 0;
user_flash_event_log(ln);
}
else{
user_flash_event_read_log_register(readlog);
user_flash_event_read_last();
}
change = !change;
results are like this:
the numbers should all be zero. But this picture shows that the reading data is wrong. So what should I do?
softDevice is s110_nrf51_8.0.0_softdevice