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

Pstorage and NUS not working together

Hi,

I have a project that uses PStorage and the Nordic UART Service. PStorage is used to store a variable that is updated by the device, irrespective of the Bluetooth, and needs to be sent over the NUS when requested. Without using PStorage, the application works perfectly. However, the variables are lost on reset which is not ideal.

The steps taken to reproduce the issue are as follows:

  1. Initialise PStorage
  2. Initialise BLE (Device Manager is initialised here as well. PStorage is NOT initialised again)
  3. Initialise Peripherals + Register PStorage
  4. Connect to Device via BLE

After connecting to the device, the device will respond to one or 2 commands that do not utilise the PStorage before freezing. On freezing, the only way to start the code again is to restart the device.

In the debugger, app_error_handler is never called and the wdt never times out. However, the device is unresponsive.

Any help would be much appreciated. I am using SDK10, SD8.0.0 on the nRF51822.

Thanks,

EDIT 1:

Attached are my header and C file for the pstorage functions.

pstorage_custom.c

pstorage_custom.h

EDIT 2:

Please find attached an example project that proves the incorrect functionality. The while loop at the end of the pstorage_custom_init() function never ends.

Pstorage-Issues.zip

  • I end up if nrf_drv_wdt_feed();. I cannot be sure if it is from these while loops or somewhere else. However, I know if is pstorage that is causing the issues as if I remove this the program works fine. I am using device manager if that changes things?

  • I'm not sure. It would be helpful if you could share you complete project, or some code that reproduces the issue. So I can debug it here.

  • Hi Petter, in the process of getting code that reproduces the error now. I have narrowed the error down to the callback not always being called. I assume this means that the pstorage operation has not finished and the code is waiting for the callback to be called to set the wait flag to 0. I am unsure why it is not completing though. I thought it might be my advertising interval being too low, so I upped that. Do you know of any other common practices that cause this error?

  • Petter, I have attached a project that proves incorrect functionality. The while loop at the end of the pstorage_custom_init() function is never left. I assume this is because the callback handler is not called for whatever reason. thanks for all your help so far!

  • The events from the SoftDevice, both BLE and system events (for pstorage), are run in NRF_APP_PRIORITY_LOW(3) context. You are calling your pstorage functions in this context and you have a while where you are waiting for the flag to be cleared. The flag is cleared by a system event that is run in the same context, so it will not interrupt your while, and thus the flag will not be cleared. Try running your pstorage functions from main context instead. For example set a flag in nus_data_handler() and in your main loop:

    // Enter main loop.
    for (;;)
    {
        if(write_flag)
        {
            	pstorage_custom_read(BLOCK_1, &custom_data);
                custom_data += 1;
                pstorage_custom_update(BLOCK_1, &custom_data);
                write_flag = 0;
        }       
        power_manage();
    }
    
Related