|
3 | 3 | SoftDevice handler |
4 | 4 | ################## |
5 | 5 |
|
6 | | -The SoftDevice handler is a library that handles SoftDevice initialization tasks, and pulls and dispatches the SoftDevice events to registered components. |
| 6 | +Overview |
| 7 | +******** |
| 8 | + |
| 9 | +The SoftDevice handler is a library that integrates the SoftDevice into the application. |
| 10 | + |
| 11 | +It provides core functionality such as: |
| 12 | + |
| 13 | +* Defining ISRs for interrupts belonging to peripherals used by the SoftDevice. |
| 14 | +* Automatically forwarding IRQs for SoftDevice-owned peripherals to the SoftDevice as necessary. |
| 15 | +* Managing SoftDevice state changes (enabling/disabling). |
| 16 | +* Retrieving and forwarding SoftDevice events to different parts of the application. |
| 17 | +* Simplifying SoftDevice and Bluetooth LE configuration and initialization. |
| 18 | +* Provide a default behavior on SoftDevice fault |
| 19 | + |
| 20 | +Interrupt configuration |
| 21 | +======================= |
| 22 | + |
| 23 | +The SoftDevice handler defines ISRs for interrupts belonging to peripherals used by the SoftDevice. |
| 24 | +It automatically configures and enables those interrupts, and determines whether, upon receiving them, they should be forwarded to the SoftDevice or not, based on whether the SoftDevice is enabled. |
| 25 | + |
| 26 | +The interrupts forwarded to the SoftDevice are declared as Zero Latency interrupts, meaning they cannot be masked using :c:func:`irq_lock`. |
| 27 | + |
| 28 | +Events and observers |
| 29 | +==================== |
| 30 | + |
| 31 | +The SoftDevice handler lets the application register functions to receive various events. |
| 32 | +A function that receives SoftDevice handler events is called an observer. |
| 33 | + |
| 34 | +There are four type of observers, each receiving different types of events: |
| 35 | + |
| 36 | +* State observers - Events pertaining to the SoftDevice state (enabled/disabled) |
| 37 | +* Bluetooth observers - Bluetooth events coming from the SoftDevice |
| 38 | +* SoC observers - SoC events coming from the SoftDevice |
| 39 | +* Stack observers - All events coming from the SoftDevice |
| 40 | + |
| 41 | +An observer has a priority relative to other observers of the same type. |
| 42 | +The priority of an observer determines the order with which the observers receive the same event. |
| 43 | + |
| 44 | +The following five priority levels are defined: |
| 45 | + |
| 46 | +* ``HIGHEST`` |
| 47 | +* ``HIGH`` |
| 48 | +* ``USER`` |
| 49 | +* ``USER_LOW`` |
| 50 | +* ``LOWEST`` |
| 51 | + |
| 52 | +An observer can optionally have a context, defined as a pointer that is passed as a parameter to the observer function. |
| 53 | + |
| 54 | +Fault handling |
| 55 | +============== |
| 56 | + |
| 57 | +The SoftDevice handler provides a weak implementation of the SoftDevice fault handler function. |
| 58 | +This implementation prints the fault on the terminal and enters an endless loop. |
| 59 | + |
| 60 | + |
| 61 | +Configuration |
| 62 | +************* |
| 63 | + |
| 64 | +The library is enabled by default in all projects using the SoftDevice. |
| 65 | + |
| 66 | +The SoftDevice handler main configuration options are related to how it dispatches events, the clock and the Bluetooth stack. |
| 67 | + |
| 68 | +Dispatch model |
| 69 | +============== |
| 70 | + |
| 71 | +The SoftDevice handler implements the SoftDevice event ISR, receiving events from the SoftDevice and dispatching them to the registered observers. |
| 72 | + |
| 73 | +You can configure the SoftDevice handler to dispatch those events in three different ways, using the following Kconfig options: |
| 74 | + |
| 75 | +* :kconfig:option:`CONFIG_NRF_SDH_DISPATCH_MODEL_IRQ` - Events are dispatched from the SoftDevice event ISR (default). |
| 76 | +* :kconfig:option:`CONFIG_NRF_SDH_DISPATCH_MODEL_SCHED` - Events are dispatched using the event scheduler library, from the main application loop. |
| 77 | +* :kconfig:option:`CONFIG_NRF_SDH_DISPATCH_MODEL_POLL` - Events are dispatched from the same context as the function calling :c:func:`nrf_sdh_evts_poll`. |
| 78 | + |
| 79 | + |
| 80 | +Clock configuration |
| 81 | +=================== |
| 82 | + |
| 83 | +The SoftDevice handler has several options to control the clock configuration passed to the :c:func:`sd_softdevice_enable` function. |
| 84 | + |
| 85 | +These options are used automatically when you enable the SoftDevice by calling the :c:func:`nrf_sdh_enable_request` function. |
| 86 | + |
| 87 | +Bluetooth configuration |
| 88 | +======================= |
| 89 | + |
| 90 | +The SoftDevice Bluetooth stack can store a set of configuration options and associate them with a numerical tag called a connection configuration tag. |
| 91 | + |
| 92 | +The SoftDevice handler can be used to automatically create a Bluetooth configuration and associate it with the connection configuration tag :kconfig:option:`CONFIG_NRF_SDH_BLE_CONN_TAG`. |
| 93 | + |
| 94 | + |
| 95 | +Usage |
| 96 | +***** |
| 97 | + |
| 98 | +The SoftDevice handler API can be used to declare observers, changing the SoftDevice state, and enabling the SoftDevice Bluetooth stack. |
| 99 | + |
| 100 | +Declaring observers |
| 101 | +=================== |
| 102 | + |
| 103 | +You can declare an observer in any part of your application by using a macro and specifying an event handler function, an optional parameter and a priority. |
| 104 | + |
| 105 | +State observers |
| 106 | +--------------- |
| 107 | + |
| 108 | +A state observer receives the following events related to the SoftDevice state: |
| 109 | + |
| 110 | +These are: |
| 111 | + |
| 112 | +* :c:enumerator:`NRF_SDH_STATE_EVT_ENABLE_PREPARE` - SoftDevice is going to be enabled. |
| 113 | +* :c:enumerator:`NRF_SDH_STATE_EVT_ENABLED` - SoftDevice is enabled. |
| 114 | +* :c:enumerator:`NRF_SDH_STATE_EVT_BLE_ENABLED` - Bluetooth is enabled. |
| 115 | +* :c:enumerator:`NRF_SDH_STATE_EVT_DISABLE_PREPARE` - SoftDevice is going to be disabled. |
| 116 | +* :c:enumerator:`NRF_SDH_STATE_EVT_DISABLED` - SoftDevice is disabled |
| 117 | + |
| 118 | +A state observer is declared using the :c:macro:`NRF_SDH_STATE_EVT_OBSERVER` macro. |
| 119 | + |
| 120 | +The following snippet shows how to declare a state observer. |
| 121 | + |
| 122 | +.. code:: c |
| 123 | +
|
| 124 | + #include <bm/softdevice_handler/nrf_sdh.h> |
| 125 | +
|
| 126 | + static int on_state_change(enum nrf_sdh_state_evt state, void *ctx) |
| 127 | + { |
| 128 | + LOG_INF("SoftDevice state %d", state); |
| 129 | + return 0; |
| 130 | + } |
| 131 | + NRF_SDH_STATE_EVT_OBSERVER(sdh_state, on_state_change, NULL, USER_LOW); |
| 132 | +
|
| 133 | +
|
| 134 | +Bluetooth observers |
| 135 | +------------------- |
| 136 | + |
| 137 | +A Bluetooth observer receives Bluetooth events coming from the SoftDevice. |
| 138 | + |
| 139 | +A Bluetooth observer is declared using the :c:macro:`NRF_SDH_BLE_OBSERVER` macro. |
| 140 | + |
| 141 | +The following snippet shows how to declare a Bluetooth observer. |
| 142 | + |
| 143 | +.. code:: c |
| 144 | +
|
| 145 | + #include <bm/softdevice_handler/nrf_sdh_ble.h> |
| 146 | +
|
| 147 | + static void on_ble_evt(const ble_evt_t *evt, void *ctx) |
| 148 | + { |
| 149 | + LOG_INF("BLE event %d", evt->header.evt_id); |
| 150 | + } |
| 151 | + NRF_SDH_BLE_OBSERVER(sdh_ble, on_ble_evt, NULL, USER_LOW); |
| 152 | +
|
| 153 | +
|
| 154 | +SoC observers |
| 155 | +------------- |
| 156 | + |
| 157 | +An SoC observer receives SoC events coming from the SoftDevice. |
| 158 | + |
| 159 | +An SoC observer is declared using the :c:macro:`NRF_SDH_BLE_OBSERVER` macro. |
| 160 | + |
| 161 | +The following snippet shows how to declare an SoC observer. |
| 162 | + |
| 163 | +.. code:: c |
| 164 | +
|
| 165 | + #include <bm/softdevice_handler/nrf_sdh_soc.h> |
| 166 | +
|
| 167 | + static void on_soc_evt(uint32_t evt, void *ctx) |
| 168 | + { |
| 169 | + LOG_INF("SoC event %d", evt); |
| 170 | + } |
| 171 | + NRF_SDH_SOC_OBSERVER(sdh_soc, on_soc_evt, NULL, USER_LOW); |
| 172 | +
|
| 173 | +
|
| 174 | +Stack observers |
| 175 | +--------------- |
| 176 | + |
| 177 | +Stack observers are a special kind of observer that receive all SoftDevice events. |
| 178 | +Normally the application does not register stack observers, but Bluetooth and SoC observers. |
| 179 | + |
| 180 | + |
| 181 | +Changing the SoftDevice state |
| 182 | +============================= |
| 183 | + |
| 184 | +The SoftDevice handler is used by the application to change the SoftDevice state. |
| 185 | +When changing the state, it notifies the state observers. |
| 186 | + |
| 187 | +You can enable the SoftDevice by calling the :c:func:`nrf_sdh_enable_request` function, sending the :c:enumerator:`NRF_SDH_STATE_EVT_ENABLE_PREPARE` event to the observers. |
| 188 | +The observers can return ``0`` from the event handler to let the SoftDevice change the state, or return a non-zero value to halt the SoftDevice state change. |
| 189 | +When an observer returns non-zero to the :c:enumerator:`NRF_SDH_STATE_EVT_ENABLE_PREPARE` event, it must call the :c:func:`nrf_sdh_observer_ready` function when it becomes ready for the SoftDevice state change. |
| 190 | + |
| 191 | +When the last state observer becomes ready for the SoftDevice state change, the SoftDevice changes state and the :c:enumerator:`NRF_SDH_STATE_EVT_ENABLED` event is sent. |
| 192 | + |
| 193 | +Similarly, you can disable the SoftDevice by calling the :c:func:`nrf_sdh_disable_request` function, sending the :c:enumerator:`NRF_SDH_STATE_EVT_DISABLE_PREPARE` event to the observers. |
| 194 | +The observers can return ``0`` from the event handler to let the SoftDevice change the state, or return a non-zero value to halt the SoftDevice state change. |
| 195 | +When an observer returns non-zero to the :c:enumerator:`NRF_SDH_STATE_EVT_DISABLE_PREPARE` event, it must call the :c:func:`nrf_sdh_observer_ready` function when it becomes ready for the SoftDevice state change. |
| 196 | + |
| 197 | +When the last state observer becomes ready for the SoftDevice state change, the SoftDevice changes the state and the :c:enumerator:`NRF_SDH_STATE_EVT_DISABLED` event is sent. |
| 198 | + |
| 199 | +Fault handling |
| 200 | +============== |
| 201 | + |
| 202 | +You can override the SoftDevice fault handler function by defining the following function, as shown in the snippet below: |
| 203 | + |
| 204 | +.. code:: c |
| 205 | +
|
| 206 | + #include <nrf_sdm.h> |
| 207 | +
|
| 208 | + void softdevice_fault_handler(uint32_t id, uint32_t pc, uint32_t info) |
| 209 | + { |
| 210 | + /* your code */ |
| 211 | + } |
| 212 | +
|
| 213 | +
|
| 214 | +Dependencies |
| 215 | +************ |
| 216 | + |
| 217 | +This library depends on the :kconfig:option:`CONFIG_SOFTDEVICE` and :kconfig:option:`CONFIG_ZERO_LATENCY_IRQS` Kconfig options. |
| 218 | + |
| 219 | +Additionally, the dispatch model :kconfig:option:`CONFIG_NRF_SDH_DISPATCH_MODEL_SCHED` option depends on :kconfig:option:`CONFIG_BM_SCHEDULER`. |
| 220 | + |
| 221 | +API documentation |
| 222 | +***************** |
| 223 | + |
| 224 | +| Header file: :file:`include/bm/softdevice_handler/nrf_sdh.h` |
| 225 | +| Header file: :file:`include/bm/softdevice_handler/nrf_sdh_ble.h` |
| 226 | +| Header file: :file:`include/bm/softdevice_handler/nrf_sdh_soc.h` |
| 227 | +| Source files: :file:`subsys/softdevice_handler/` |
| 228 | +
|
| 229 | +:ref:`SoftDevice handler API reference <api_nrf_sdh>` |
0 commit comments