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.

  • hi @Nick, thank you for your quick answer. I don't know what could go on, maybe some code is slower or something. In keil, I didn't call app_sched_execute in the loop but everything was working. On gcc, I saw that the firmware was hanging somewhere and I tracked this to my wait function and discovered that app_sched_execute did the trick. I have edited the question, it is clearer. If my edit is good, I would think that nordic should give this snippet to other people

Reply
  • hi @Nick, thank you for your quick answer. I don't know what could go on, maybe some code is slower or something. In keil, I didn't call app_sched_execute in the loop but everything was working. On gcc, I saw that the firmware was hanging somewhere and I tracked this to my wait function and discovered that app_sched_execute did the trick. I have edited the question, it is clearer. If my edit is good, I would think that nordic should give this snippet to other people

Children
No Data
Related