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

PStorage Event Handler Problems

Hello!

I have been experiencing some trouble with the PStorage API. I have browsed every example and similar question I can find and none seem to match the continuing issues that I am having.

I have been very sure to register the pstorage event handler:

    static void sys_evt_dispatch(uint32_t sys_evt)
{
    if ((sys_evt == NRF_EVT_FLASH_OPERATION_SUCCESS) ||
            (sys_evt == NRF_EVT_FLASH_OPERATION_ERROR)) {
        pstorage_sys_event_handler(sys_evt);
    }
}

When I call on my functions to store or clear in pstorage, the pstorage events handler is never called, and as such no callback is sent and the queue element is not eliminated. As a temporary workaround, for the purposes of testing further, I made a small function to be called after my first store operation had actually written to memory.

 void cheap_trick(void){

	pstorage_sys_event_handler(NRF_EVT_FLASH_OPERATION_SUCCESS);
}

This allowed me, after having checked the write actually completed with a quick load operation, to simulate the event that the handler should be getting passed and remove it from the queue as it should be on completion, this was confirmed by running pstorage_access_status_get() showing 0 operations left in the queue.

However, the subsequent store operation is queued but fails to even write to the memory, even though in debugging I can see that it calls sd_flash_write() and returns NRF_SUCCESS there.

In addition I have called sd_flash_write()directly twice in a row to the first 4 then second 4 bytes of the pstorage block area, the first write succeeds and the second does not and using sd_evt_get() neither of these operation appears to be giving the NRF_EVT_FLASH_OPERATION_SUCCESS event on completion.

Is there somthing I could have missed in the setup of PStorage or could there be a problem in the Softdevice itself? I welcome all suggestions regarding solving this problem!

Thanks,

Chris

  • Are you using radio notifications? It would be helpful to give information about the SDK version you are using.

  • Thank you for your quick response. BLE Stack is disabled and I am not using any radio notifications. I am using nRF51 SDK v9.0.0. I should also mention, I am using a bootloader if that can have any bearing here as well.

  • Pstorage Library is designed to work with Softdevice enabled because it uses sd_flash_xxx functions inside it.

    sd_flash_xxx functions can work even if the softdevice is disabled, but they do not give the callbacks.

    You wont get any events from softdevice (when it is disabled) because flash requests are completed synchronously as there do not have any competition for CPU time and AHB bus That means that when pstorage function call is returned your operation is already complete.

    This is clearly given in the API documentation inside nrf_soc.h

    /**@brief Flash Write
    *
    * Commands to write a buffer to flash
    *
    * If the SoftDevice is enabled:
    *  This call initiates the flash access command, and its completion will be communicated to the
    *  application with exactly one of the following events:
    *      - @ref NRF_EVT_FLASH_OPERATION_SUCCESS - The command was successfully completed.
    *      - @ref NRF_EVT_FLASH_OPERATION_ERROR   - The command could not be started.
    *
    * If the SoftDevice is not enabled no event will be generated, and this call will return @ref NRF_SUCCESS when the write has been completed
    *
    

    In short if softdevice is disabled, then your flash operation is completed immediately and it will never fail. If you still want to use pstorage with softdevice disabled, then you need use your so called "cheap_trick" function. I think the subsequent store operation should also pass if you call the cheap_trick() everytime you do a pstorage request. Could you debug more and see what blocked that subsequent store operation?

  • I have the Softdevice enabled, and have confirmed that it is definitely enabled with sd_softdevice_is_enabled(&sd_enabled) As far as the cheap trick method is concerned, it appears to work for a single element in queue but no subsequent queue elements that I add later function correctly.

    I have debugged right through the subsequent store operation and ensured it reached the call to sd_flash_write, which did return NRF_SUCCESS however checking the memory location showed that it had not done anything.

  • but you said "BLE Stack is disabled " in your comment one hour before this.

Related