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

How to check pstorage operation is complete ?

Hi, When I write data synchronously to memory (nRF51822), I do :

APP_ERROR_CHECK(pstorage_block_identifier_get(flash_handler, 2, &block_handler));
APP_ERROR_CHECK(pstorage_clear(&block_handler, 16));
// check here ?
APP_ERROR_CHECK(pstorage_store(&block_handler, data_to_store, 16, 0)); 
// check here ?

Where I say 'check here' : I wonder if I should wait to move to next line by checking pstorage_access_status_get returns 0 or by having a variable that waits for the flash callback to be called.

Is it equivalent (apart from the details you get in the callback) ? Thanks

EDIT :

I call this synchronous function after every flash operation (clear or write), and this seems like the proper way to go. In the flash callback handler, I reset if there is anything else than a success

void wait_pstorage_access() {
    uint32_t count;
    do {
        app_sched_execute();
        pstorage_access_status_get(&count);
    } while (count > 0);
}
Parents
  • You need to exit out to the main scheduler and give the SoftDevice control of the system until the flash callback is executed, at which point you can continue to the next step of your original code. A blocking wait in the "check here" lines won't work as the SoftDevice is what invokes the flash callback in the first place.

  • app_sched_execute() is the proper way to go, as this gives the SoftDevice opportunities to complete its asynchronous flash operation, which is asynchronous due to the length of time it takes to program and erase flash. I can't possibly think of any reason why your Keil code would work, and I suspect it's not. Is count <= 0 just before executing the while(), or did you invoke pstorage_load() immediately beforehand, or you did you do anything else that gave the SoftDevice a chance to run?

Reply
  • app_sched_execute() is the proper way to go, as this gives the SoftDevice opportunities to complete its asynchronous flash operation, which is asynchronous due to the length of time it takes to program and erase flash. I can't possibly think of any reason why your Keil code would work, and I suspect it's not. Is count <= 0 just before executing the while(), or did you invoke pstorage_load() immediately beforehand, or you did you do anything else that gave the SoftDevice a chance to run?

Children
No Data
Related