This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

pstorage handler callback without scheduler?

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?

Parents
  • Yes, sorry for the confusion. Initially I had none of the while() loop stuff at all and everything seemed to be working dandy. Then I figured I should wait for the clear to finish before calling the store, so I looked at examples and they had the while() power_manage() loop, so I added that, which obviously does not work in my case. Where it prints out '1' forever above, I commented it out.

    So it looks like the best way to go is to convert this to use the scheduler, which I sort of tried yesterday, but then everything broke. I am not the original author of this code and am basically taking over a 'half-working' project - going through and fixing things.

    Thanks for the help and look out for me asking questions about using the scheduler :-)

Reply
  • Yes, sorry for the confusion. Initially I had none of the while() loop stuff at all and everything seemed to be working dandy. Then I figured I should wait for the clear to finish before calling the store, so I looked at examples and they had the while() power_manage() loop, so I added that, which obviously does not work in my case. Where it prints out '1' forever above, I commented it out.

    So it looks like the best way to go is to convert this to use the scheduler, which I sort of tried yesterday, but then everything broke. I am not the original author of this code and am basically taking over a 'half-working' project - going through and fixing things.

    Thanks for the help and look out for me asking questions about using the scheduler :-)

Children
No Data
Related