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

Storing characteristic data after on_disconnect

I am trying to persist characteristic data written by the central side after on_disconnect. I can see the data written inside the evt_write->data ptr and I copy it to store once the central disconnects.

This is on nrf51-dk sd 7.0.0 and sdk 6.1.0.

Pstorage works when I try to do debug it, without disconnecting from device. What would be the preferred way for persisting characteristic values written by the central side?

EDIT: My first problem was not being able to receive on_disconnect callbacks. The reason on_disconnect not being called was the missing 4 byte alignment. I was getting NRF_ERROR_INVALID_ADDR from store operation. Now on_disconnect gets called and no errors returned from pstorage operations, but when the app is trying to restart advertising, the command queue still shows unfinished pstorage memory access. The count is 2 in the below code, I'm issuing 2 store operations at on_disconnect. I tried stopping the advertisement and reducing the interval but none of it seems to change anything.

// Verify if there is any flash access pending, if yes delay starting advertising until 
// it's complete.
err_code = pstorage_access_status_get(&count);
APP_ERROR_CHECK(err_code);

if (count != 0)
{
    m_memory_access_in_progress = true;
    return;
}

I see this post devzone.nordicsemi.com/.../ with similar problem but still not sure how to fix this for my case.

The example hrs code checks for the command queue for pstorage access before restarting advertising, since the queue is not empty after writing a value to a characteristic and disconnecting from peripheral, the app stops at that point. Should i just force it to wait?

EDIT 2: Now i do receive pstorage callbacks once i call them from main, outside ble event callbacks. What i do is to setup a boolean flag(could not find ble idle macro in sdk 6.1.0) when on_disconnect event is received, then check this flag inside main loop and start by cleaning first. Once i receive the clean op_code i setup another flag for storing and store the data. Then on next init, i try to load data, again waiting for the callback. I believe the last wait could be unnecessary if pstorage works async but nothing is loaded either way, apart from lots of 0s. What could i be doing wrong? Are you sure pstorage works on nrf51-dk with my sd and sdk? Should i share any code if that would help?

EDIT 3: Attaching example project:ble_app_hrs_pstorage_test.rar where I added one example characteristic in hrs example and trying to run the example code Stefan has linked. Increased PSTORAGE_MAX_APPLICATIONS as 2 since the device manager also using it and commented out pstorage_init() not to do it for a second time.

I get assert inside the pstorage clean method when its checking for alignment.

Parents
  • Here is example project based on ble_app_hrs example for pca10001 from SDK v6.1.0, tested with SD v7.3.0. I added to this project custom service with two characteristics. Second characteristic with read and write permissions allows you to read and write 1 byte value that will be stored in pstorage. The characteristic value located in pstorage will be assigned to this characteristic after reset.

    ble_app_hrs_pstorage.zip

  • I see see some uart related stuff in your example yet it works over reboot. After using a 64 byte block my code started to work too, it just does not load the block right away but after a reconnect, i can see the correct value is loaded. Maybe you just need to wait a few milliseconds for the operation. Im still not 100% confident with this but ill do a bit more experimenting when i got the time. Thank you.

Reply
  • I see see some uart related stuff in your example yet it works over reboot. After using a 64 byte block my code started to work too, it just does not load the block right away but after a reconnect, i can see the correct value is loaded. Maybe you just need to wait a few milliseconds for the operation. Im still not 100% confident with this but ill do a bit more experimenting when i got the time. Thank you.

Children
No Data
Related