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

pstorage - can only clear/write from main loop ?

Hi,

Maybe someone can point me into the right direction, I'm on S110 and SDK10 for hardware/legacy reason.

I have a pstorage function using init, load, clear and write functions to write default data to flash at device startup in case a reference byte is not detected (clean start), this all works nicely and occurs after initialising all the BLE functions (incl. starting & stopping advertising), I confirmed all OK by reading out the flash values in debug or doing specific flash writes and reads after that - so far so good.

My problem now is that when I'm in a connection and getting new config data via BLE_GATTS_EVT_WRITE and its handler, the databytes come in nicely (validated) but I can't seem to find a good way (or timing) to write them to flash. I tried stopping all timers, stopping the connection and advertising but each time I do the clean (same function to write as validate after startup) it seems to hang - I'm having trouble to debug properly with BLE active where exactly it goes wrong but the functions themself seem to work fine as long not in connection.

Anyone recognizing this problem or some suggestions what to try next ?

Didn't want to dump code just yet but if needed let me know.

thanks

Parents
  • Eventually found a workaround by getting the advertising status in a on_adv_evt() and then trigger a clear/write in the main loop when advertising is idle and data was changed:

    // Main loop.
    for (;;)
    {
        if (!advertising && flash_changed) write_flash();
        power_manage();
    }
    

    Inspired by this post

    However, is this the only way to handle this, from the main loop or are there other options ?

  • Hi,

    Flash operations might timeout when you are setting up a connection. The reason is that there is not enough time in-between SoftDevice operation to provide a timeslot long enough for performing the flash operation. A timeslot is needed because the CPU is halted during certain flash operations, and the SoftDevice relies on CPU availability for correct time-critical operation.

    This means you can not update pstorage when trying to connect. Similarly flash operation may fail in other scenarios when radio usage is frequent. As you will block execution of code for the duration of a flash access, you want to schedule flash operation to when no other tasks needs to be performed. In this devzone post here there are some pstorage examples with ble_app_template project. Note that the pstorage_wait_flag is updated in interrupt priority 3. So you will need to run the code that write to flash using pstorage in the main context, and not from an interrupt context.

Reply
  • Hi,

    Flash operations might timeout when you are setting up a connection. The reason is that there is not enough time in-between SoftDevice operation to provide a timeslot long enough for performing the flash operation. A timeslot is needed because the CPU is halted during certain flash operations, and the SoftDevice relies on CPU availability for correct time-critical operation.

    This means you can not update pstorage when trying to connect. Similarly flash operation may fail in other scenarios when radio usage is frequent. As you will block execution of code for the duration of a flash access, you want to schedule flash operation to when no other tasks needs to be performed. In this devzone post here there are some pstorage examples with ble_app_template project. Note that the pstorage_wait_flag is updated in interrupt priority 3. So you will need to run the code that write to flash using pstorage in the main context, and not from an interrupt context.

Children
No Data
Related