This should be simple, but I just can't get it working.
Many examples show setting a flag before calling the pstorage clear/store/load functions and then looping, waiting for the pstorage handler to change this flag to signify that it finished.
pstorage_sys_event_handler(sys_evt) is in sys_evt_dispatch(uint32_t sys_evt){} and my pstorage handler gets called (I have RTT printf statements that show successful).
volatile uint8_t pstorageState;
static void device_cb_handler(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len) {
switch(op_code) {
case PSTORAGE_STORE_OP_CODE:
if(result == NRF_SUCCESS) {
SEGGER_RTT_WriteString(0, "pstorage_store success\n");
}
else {
SEGGER_RTT_WriteString(0, "pstorage_store fail\n");
}
pstorageState = 0;
break;
case PSTORAGE_CLEAR_OP_CODE:
if(result == NRF_SUCCESS) {
SEGGER_RTT_WriteString(0, "pstorage_clear success\n");
}
else {
SEGGER_RTT_WriteString(0, "pstorage_clear fail\n");
}
pstorageState = 0;
break;
case PSTORAGE_LOAD_OP_CODE:
...
}
}
I do the storing like this:
void store() {
uint32_t err_code;
pstorage_handle_t p_block_id;
//clear before store
pstorageState = 1;
err_code = pstorage_clear(&m_device_handle, sizeof(device_state_t));
if(err_code != NRF_SUCCESS) {
APP_ERROR_CHECK(err_code);
SEGGER_RTT_WriteString(0, "store() pstorage_clear fail\n");
return;
}
//wait for clear to return
while(pstorageState) {
power_manage(); //stuck in this loop forever
}
...pstorage_store()...
}
If I remove the while() loops everything works just fine and I have not seen the pstorage handler print out a fail message yet, but I assume I should actually be waiting for the clear to finish before starting the store...
Even though pstorageState is volatile, I tried disabling optimizations (-O0), but that made no difference.
Am I getting stuck in the while loop because I am not using a scheduler?