Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to synchronize a BLE observer with my application

Examples in SDK15 use the Observer pattern to handle BLE events (using the macro NRF_SDH_BLE_OBSERVER). AFAIK, the registered handler runs in some IRQ context, typically higher then my main context.

Looking at the experimental ble_app_interactive example, I see that some data structures (e.g. m_device in ble_m.c) are accessed from both the observer context, and the main context, without synchronization:

  • in ble_m.c device_to_list_add() is called from the observer context, and modifies m_device
  • in main.c an app_timer call to scan_device_info_clear() clears m_device

Questions:

  • Is the code in the example valid, or should access to m_device be synchronized?
  • If synchronization is needed, what is the recommended way to synchronize between the observer and the main thread?

Example was taken from nRF5_SDK_15.0.0_a53641a

  • Hi,

    Is the code in the example valid, or should access to m_device be synchronized?

     It's valid. You don't need any synchronization.

    AFAIK, the registered handler runs in some IRQ context, typically higher then my main context.

    Correct, Softdevice event handler(SD_EVT_IRQn/SD_EVT_IRQHandler) runs at interrupt priority level 6. This is also the same level as the app_timer. So they will not preempt each other. (Note that in SDK 15.0 the interrupt priority level for both are set to 7, but set to 6 for both in SDK 15.2)

Related