cvb
Fork of nrf51-sdk by
source/nordic_sdk/components/softdevice/common/softdevice_handler/softdevice_handler.c@0:bc2961fa1ef0, 2016-04-06 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Wed Apr 06 23:55:04 2016 +0100
- Revision:
- 0:bc2961fa1ef0
Synchronized with git rev 90647e3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:bc2961fa1ef0 | 1 | /* |
Jonathan Austin |
0:bc2961fa1ef0 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Jonathan Austin |
0:bc2961fa1ef0 | 3 | * All rights reserved. |
Jonathan Austin |
0:bc2961fa1ef0 | 4 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Jonathan Austin |
0:bc2961fa1ef0 | 6 | * are permitted provided that the following conditions are met: |
Jonathan Austin |
0:bc2961fa1ef0 | 7 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Jonathan Austin |
0:bc2961fa1ef0 | 9 | * list of conditions and the following disclaimer. |
Jonathan Austin |
0:bc2961fa1ef0 | 10 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Jonathan Austin |
0:bc2961fa1ef0 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Jonathan Austin |
0:bc2961fa1ef0 | 13 | * other materials provided with the distribution. |
Jonathan Austin |
0:bc2961fa1ef0 | 14 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Jonathan Austin |
0:bc2961fa1ef0 | 16 | * contributors to this software may be used to endorse or promote products |
Jonathan Austin |
0:bc2961fa1ef0 | 17 | * derived from this software without specific prior written permission. |
Jonathan Austin |
0:bc2961fa1ef0 | 18 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 19 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Jonathan Austin |
0:bc2961fa1ef0 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Jonathan Austin |
0:bc2961fa1ef0 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Jonathan Austin |
0:bc2961fa1ef0 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Jonathan Austin |
0:bc2961fa1ef0 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Jonathan Austin |
0:bc2961fa1ef0 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Jonathan Austin |
0:bc2961fa1ef0 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Jonathan Austin |
0:bc2961fa1ef0 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Jonathan Austin |
0:bc2961fa1ef0 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Jonathan Austin |
0:bc2961fa1ef0 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jonathan Austin |
0:bc2961fa1ef0 | 30 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 31 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 32 | |
Jonathan Austin |
0:bc2961fa1ef0 | 33 | #include "softdevice_handler.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 34 | #include <stdlib.h> |
Jonathan Austin |
0:bc2961fa1ef0 | 35 | #include "nordic_common.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 36 | #include "app_error.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 37 | #include "app_util.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 38 | #include "nrf_assert.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 39 | #include "nrf_soc.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 40 | #include "nrf.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 41 | |
Jonathan Austin |
0:bc2961fa1ef0 | 42 | #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 43 | #include "ant_interface.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 44 | #elif defined(ANT_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 45 | #include "ant_interface.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 46 | #elif defined(BLE_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 47 | #include "ble.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 48 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 49 | |
Jonathan Austin |
0:bc2961fa1ef0 | 50 | #ifdef NRF51 |
Jonathan Austin |
0:bc2961fa1ef0 | 51 | #define SOFTDEVICE_EVT_IRQ SD_EVT_IRQn /**< SoftDevice Event IRQ number. Used for both protocol events and SoC events. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 52 | #define SOFTDEVICE_EVT_IRQHandler SD_EVT_IRQHandler |
Jonathan Austin |
0:bc2961fa1ef0 | 53 | #elif defined (NRF52) |
Jonathan Austin |
0:bc2961fa1ef0 | 54 | #define SOFTDEVICE_EVT_IRQ SWI2_EGU2_IRQn |
Jonathan Austin |
0:bc2961fa1ef0 | 55 | #define SOFTDEVICE_EVT_IRQHandler SWI2_EGU2_IRQHandler |
Jonathan Austin |
0:bc2961fa1ef0 | 56 | #endif /* NRF51 */ |
Jonathan Austin |
0:bc2961fa1ef0 | 57 | |
Jonathan Austin |
0:bc2961fa1ef0 | 58 | static softdevice_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating SoftDevice events to the scheduler. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 59 | |
Jonathan Austin |
0:bc2961fa1ef0 | 60 | static volatile bool m_softdevice_enabled = false; /**< Variable to indicate whether the SoftDevice is enabled. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 61 | |
Jonathan Austin |
0:bc2961fa1ef0 | 62 | #ifdef BLE_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 63 | // The following three definitions is needed only if BLE events are needed to be pulled from the stack. |
Jonathan Austin |
0:bc2961fa1ef0 | 64 | static uint8_t * mp_ble_evt_buffer; /**< Buffer for receiving BLE events from the SoftDevice. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 65 | static uint16_t m_ble_evt_buffer_size; /**< Size of BLE event buffer. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 66 | static ble_evt_handler_t m_ble_evt_handler; /**< Application event handler for handling BLE events. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 67 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 68 | |
Jonathan Austin |
0:bc2961fa1ef0 | 69 | #ifdef ANT_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 70 | // The following two definition is needed only if ANT events are needed to be pulled from the stack. |
Jonathan Austin |
0:bc2961fa1ef0 | 71 | static ant_evt_t m_ant_evt_buffer; /**< Buffer for receiving ANT events from the SoftDevice. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 72 | static ant_evt_handler_t m_ant_evt_handler; /**< Application event handler for handling ANT events. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 73 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 74 | |
Jonathan Austin |
0:bc2961fa1ef0 | 75 | static sys_evt_handler_t m_sys_evt_handler; /**< Application event handler for handling System (SOC) events. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 76 | |
Jonathan Austin |
0:bc2961fa1ef0 | 77 | |
Jonathan Austin |
0:bc2961fa1ef0 | 78 | /**@brief Callback function for asserts in the SoftDevice. |
Jonathan Austin |
0:bc2961fa1ef0 | 79 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 80 | * @details A pointer to this function will be passed to the SoftDevice. This function will be |
Jonathan Austin |
0:bc2961fa1ef0 | 81 | * called if an ASSERT statement in the SoftDevice fails. |
Jonathan Austin |
0:bc2961fa1ef0 | 82 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 83 | * @param[in] pc The value of the program counter when the ASSERT call failed. |
Jonathan Austin |
0:bc2961fa1ef0 | 84 | * @param[in] line_num Line number of the failing ASSERT call. |
Jonathan Austin |
0:bc2961fa1ef0 | 85 | * @param[in] file_name File name of the failing ASSERT call. |
Jonathan Austin |
0:bc2961fa1ef0 | 86 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 87 | void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name) |
Jonathan Austin |
0:bc2961fa1ef0 | 88 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 89 | UNUSED_PARAMETER(pc); |
Jonathan Austin |
0:bc2961fa1ef0 | 90 | assert_nrf_callback(line_num, file_name); |
Jonathan Austin |
0:bc2961fa1ef0 | 91 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 92 | |
Jonathan Austin |
0:bc2961fa1ef0 | 93 | |
Jonathan Austin |
0:bc2961fa1ef0 | 94 | void intern_softdevice_events_execute(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 95 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 96 | if (!m_softdevice_enabled) |
Jonathan Austin |
0:bc2961fa1ef0 | 97 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 98 | // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the |
Jonathan Austin |
0:bc2961fa1ef0 | 99 | // application without using this module's API (i.e softdevice_handler_init) |
Jonathan Austin |
0:bc2961fa1ef0 | 100 | |
Jonathan Austin |
0:bc2961fa1ef0 | 101 | return; |
Jonathan Austin |
0:bc2961fa1ef0 | 102 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 103 | |
Jonathan Austin |
0:bc2961fa1ef0 | 104 | bool no_more_soc_evts = (m_sys_evt_handler == NULL); |
Jonathan Austin |
0:bc2961fa1ef0 | 105 | #ifdef BLE_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 106 | bool no_more_ble_evts = (m_ble_evt_handler == NULL); |
Jonathan Austin |
0:bc2961fa1ef0 | 107 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 108 | #ifdef ANT_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 109 | bool no_more_ant_evts = (m_ant_evt_handler == NULL); |
Jonathan Austin |
0:bc2961fa1ef0 | 110 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 111 | |
Jonathan Austin |
0:bc2961fa1ef0 | 112 | for (;;) |
Jonathan Austin |
0:bc2961fa1ef0 | 113 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 114 | uint32_t err_code; |
Jonathan Austin |
0:bc2961fa1ef0 | 115 | |
Jonathan Austin |
0:bc2961fa1ef0 | 116 | if (!no_more_soc_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 117 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 118 | uint32_t evt_id; |
Jonathan Austin |
0:bc2961fa1ef0 | 119 | |
Jonathan Austin |
0:bc2961fa1ef0 | 120 | // Pull event from SOC. |
Jonathan Austin |
0:bc2961fa1ef0 | 121 | err_code = sd_evt_get(&evt_id); |
Jonathan Austin |
0:bc2961fa1ef0 | 122 | |
Jonathan Austin |
0:bc2961fa1ef0 | 123 | if (err_code == NRF_ERROR_NOT_FOUND) |
Jonathan Austin |
0:bc2961fa1ef0 | 124 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 125 | no_more_soc_evts = true; |
Jonathan Austin |
0:bc2961fa1ef0 | 126 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 127 | else if (err_code != NRF_SUCCESS) |
Jonathan Austin |
0:bc2961fa1ef0 | 128 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 129 | APP_ERROR_HANDLER(err_code); |
Jonathan Austin |
0:bc2961fa1ef0 | 130 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 131 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 132 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 133 | // Call application's SOC event handler. |
Jonathan Austin |
0:bc2961fa1ef0 | 134 | m_sys_evt_handler(evt_id); |
Jonathan Austin |
0:bc2961fa1ef0 | 135 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 136 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 137 | |
Jonathan Austin |
0:bc2961fa1ef0 | 138 | #ifdef BLE_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 139 | // Fetch BLE Events. |
Jonathan Austin |
0:bc2961fa1ef0 | 140 | if (!no_more_ble_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 141 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 142 | // Pull event from stack |
Jonathan Austin |
0:bc2961fa1ef0 | 143 | uint16_t evt_len = m_ble_evt_buffer_size; |
Jonathan Austin |
0:bc2961fa1ef0 | 144 | |
Jonathan Austin |
0:bc2961fa1ef0 | 145 | err_code = sd_ble_evt_get(mp_ble_evt_buffer, &evt_len); |
Jonathan Austin |
0:bc2961fa1ef0 | 146 | if (err_code == NRF_ERROR_NOT_FOUND) |
Jonathan Austin |
0:bc2961fa1ef0 | 147 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 148 | no_more_ble_evts = true; |
Jonathan Austin |
0:bc2961fa1ef0 | 149 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 150 | else if (err_code != NRF_SUCCESS) |
Jonathan Austin |
0:bc2961fa1ef0 | 151 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 152 | APP_ERROR_HANDLER(err_code); |
Jonathan Austin |
0:bc2961fa1ef0 | 153 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 154 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 155 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 156 | // Call application's BLE stack event handler. |
Jonathan Austin |
0:bc2961fa1ef0 | 157 | m_ble_evt_handler((ble_evt_t *)mp_ble_evt_buffer); |
Jonathan Austin |
0:bc2961fa1ef0 | 158 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 159 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 160 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 161 | |
Jonathan Austin |
0:bc2961fa1ef0 | 162 | #ifdef ANT_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 163 | // Fetch ANT Events. |
Jonathan Austin |
0:bc2961fa1ef0 | 164 | if (!no_more_ant_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 165 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 166 | // Pull event from stack |
Jonathan Austin |
0:bc2961fa1ef0 | 167 | err_code = sd_ant_event_get(&m_ant_evt_buffer.channel, |
Jonathan Austin |
0:bc2961fa1ef0 | 168 | &m_ant_evt_buffer.event, |
Jonathan Austin |
0:bc2961fa1ef0 | 169 | m_ant_evt_buffer.evt_buffer); |
Jonathan Austin |
0:bc2961fa1ef0 | 170 | if (err_code == NRF_ERROR_NOT_FOUND) |
Jonathan Austin |
0:bc2961fa1ef0 | 171 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 172 | no_more_ant_evts = true; |
Jonathan Austin |
0:bc2961fa1ef0 | 173 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 174 | else if (err_code != NRF_SUCCESS) |
Jonathan Austin |
0:bc2961fa1ef0 | 175 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 176 | APP_ERROR_HANDLER(err_code); |
Jonathan Austin |
0:bc2961fa1ef0 | 177 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 178 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 179 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 180 | // Call application's ANT stack event handler. |
Jonathan Austin |
0:bc2961fa1ef0 | 181 | m_ant_evt_handler(&m_ant_evt_buffer); |
Jonathan Austin |
0:bc2961fa1ef0 | 182 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 183 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 184 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 185 | |
Jonathan Austin |
0:bc2961fa1ef0 | 186 | if (no_more_soc_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 187 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 188 | // There are no remaining System (SOC) events to be fetched from the SoftDevice. |
Jonathan Austin |
0:bc2961fa1ef0 | 189 | #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 190 | // Check if there are any remaining BLE and ANT events. |
Jonathan Austin |
0:bc2961fa1ef0 | 191 | if (no_more_ble_evts && no_more_ant_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 192 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 193 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 194 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 195 | #elif defined(BLE_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 196 | // Check if there are any remaining BLE events. |
Jonathan Austin |
0:bc2961fa1ef0 | 197 | if (no_more_ble_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 198 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 199 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 200 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 201 | #elif defined(ANT_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 202 | // Check if there are any remaining ANT events. |
Jonathan Austin |
0:bc2961fa1ef0 | 203 | if (no_more_ant_evts) |
Jonathan Austin |
0:bc2961fa1ef0 | 204 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 205 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 206 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 207 | #else |
Jonathan Austin |
0:bc2961fa1ef0 | 208 | // No need to check for BLE or ANT events since there is no support for BLE and ANT |
Jonathan Austin |
0:bc2961fa1ef0 | 209 | // required. |
Jonathan Austin |
0:bc2961fa1ef0 | 210 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 211 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 212 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 213 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 214 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 215 | |
Jonathan Austin |
0:bc2961fa1ef0 | 216 | bool softdevice_handler_isEnabled(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 217 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 218 | return m_softdevice_enabled; |
Jonathan Austin |
0:bc2961fa1ef0 | 219 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 220 | |
Jonathan Austin |
0:bc2961fa1ef0 | 221 | uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t clock_source, |
Jonathan Austin |
0:bc2961fa1ef0 | 222 | void * p_ble_evt_buffer, |
Jonathan Austin |
0:bc2961fa1ef0 | 223 | uint16_t ble_evt_buffer_size, |
Jonathan Austin |
0:bc2961fa1ef0 | 224 | softdevice_evt_schedule_func_t evt_schedule_func) |
Jonathan Austin |
0:bc2961fa1ef0 | 225 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 226 | uint32_t err_code; |
Jonathan Austin |
0:bc2961fa1ef0 | 227 | |
Jonathan Austin |
0:bc2961fa1ef0 | 228 | // Save configuration. |
Jonathan Austin |
0:bc2961fa1ef0 | 229 | #if defined (BLE_STACK_SUPPORT_REQD) |
Jonathan Austin |
0:bc2961fa1ef0 | 230 | // Check that buffer is not NULL. |
Jonathan Austin |
0:bc2961fa1ef0 | 231 | if (p_ble_evt_buffer == NULL) |
Jonathan Austin |
0:bc2961fa1ef0 | 232 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 233 | return NRF_ERROR_INVALID_PARAM; |
Jonathan Austin |
0:bc2961fa1ef0 | 234 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 235 | |
Jonathan Austin |
0:bc2961fa1ef0 | 236 | // Check that buffer is correctly aligned. |
Jonathan Austin |
0:bc2961fa1ef0 | 237 | if (!is_word_aligned(p_ble_evt_buffer)) |
Jonathan Austin |
0:bc2961fa1ef0 | 238 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 239 | return NRF_ERROR_INVALID_PARAM; |
Jonathan Austin |
0:bc2961fa1ef0 | 240 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 241 | |
Jonathan Austin |
0:bc2961fa1ef0 | 242 | mp_ble_evt_buffer = (uint8_t *)p_ble_evt_buffer; |
Jonathan Austin |
0:bc2961fa1ef0 | 243 | m_ble_evt_buffer_size = ble_evt_buffer_size; |
Jonathan Austin |
0:bc2961fa1ef0 | 244 | #else |
Jonathan Austin |
0:bc2961fa1ef0 | 245 | // The variables p_ble_evt_buffer and ble_evt_buffer_size is not needed if BLE Stack support |
Jonathan Austin |
0:bc2961fa1ef0 | 246 | // is not required. |
Jonathan Austin |
0:bc2961fa1ef0 | 247 | UNUSED_PARAMETER(p_ble_evt_buffer); |
Jonathan Austin |
0:bc2961fa1ef0 | 248 | UNUSED_PARAMETER(ble_evt_buffer_size); |
Jonathan Austin |
0:bc2961fa1ef0 | 249 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 250 | |
Jonathan Austin |
0:bc2961fa1ef0 | 251 | m_evt_schedule_func = evt_schedule_func; |
Jonathan Austin |
0:bc2961fa1ef0 | 252 | |
Jonathan Austin |
0:bc2961fa1ef0 | 253 | //Enabling FPU for SoftDevice |
Jonathan Austin |
0:bc2961fa1ef0 | 254 | #ifdef S132 |
Jonathan Austin |
0:bc2961fa1ef0 | 255 | SCB->CPACR |= (3UL << 20) | (3UL << 22); |
Jonathan Austin |
0:bc2961fa1ef0 | 256 | __DSB(); |
Jonathan Austin |
0:bc2961fa1ef0 | 257 | __ISB(); |
Jonathan Austin |
0:bc2961fa1ef0 | 258 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 259 | // Initialize SoftDevice. |
Jonathan Austin |
0:bc2961fa1ef0 | 260 | err_code = sd_softdevice_enable(clock_source, softdevice_assertion_handler); |
Jonathan Austin |
0:bc2961fa1ef0 | 261 | if (err_code != NRF_SUCCESS) |
Jonathan Austin |
0:bc2961fa1ef0 | 262 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 263 | return err_code; |
Jonathan Austin |
0:bc2961fa1ef0 | 264 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 265 | #ifdef S132 |
Jonathan Austin |
0:bc2961fa1ef0 | 266 | SCB->CPACR = 0; |
Jonathan Austin |
0:bc2961fa1ef0 | 267 | __DSB(); |
Jonathan Austin |
0:bc2961fa1ef0 | 268 | __ISB(); |
Jonathan Austin |
0:bc2961fa1ef0 | 269 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 270 | |
Jonathan Austin |
0:bc2961fa1ef0 | 271 | m_softdevice_enabled = true; |
Jonathan Austin |
0:bc2961fa1ef0 | 272 | |
Jonathan Austin |
0:bc2961fa1ef0 | 273 | // Enable BLE event interrupt (interrupt priority has already been set by the stack). |
Jonathan Austin |
0:bc2961fa1ef0 | 274 | return sd_nvic_EnableIRQ(SOFTDEVICE_EVT_IRQ); |
Jonathan Austin |
0:bc2961fa1ef0 | 275 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 276 | |
Jonathan Austin |
0:bc2961fa1ef0 | 277 | |
Jonathan Austin |
0:bc2961fa1ef0 | 278 | uint32_t softdevice_handler_sd_disable(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 279 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 280 | uint32_t err_code = sd_softdevice_disable(); |
Jonathan Austin |
0:bc2961fa1ef0 | 281 | |
Jonathan Austin |
0:bc2961fa1ef0 | 282 | m_softdevice_enabled = !(err_code == NRF_SUCCESS); |
Jonathan Austin |
0:bc2961fa1ef0 | 283 | |
Jonathan Austin |
0:bc2961fa1ef0 | 284 | return err_code; |
Jonathan Austin |
0:bc2961fa1ef0 | 285 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 286 | |
Jonathan Austin |
0:bc2961fa1ef0 | 287 | |
Jonathan Austin |
0:bc2961fa1ef0 | 288 | #ifdef BLE_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 289 | uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler) |
Jonathan Austin |
0:bc2961fa1ef0 | 290 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 291 | if (ble_evt_handler == NULL) |
Jonathan Austin |
0:bc2961fa1ef0 | 292 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 293 | return NRF_ERROR_NULL; |
Jonathan Austin |
0:bc2961fa1ef0 | 294 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 295 | |
Jonathan Austin |
0:bc2961fa1ef0 | 296 | m_ble_evt_handler = ble_evt_handler; |
Jonathan Austin |
0:bc2961fa1ef0 | 297 | |
Jonathan Austin |
0:bc2961fa1ef0 | 298 | return NRF_SUCCESS; |
Jonathan Austin |
0:bc2961fa1ef0 | 299 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 300 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 301 | |
Jonathan Austin |
0:bc2961fa1ef0 | 302 | |
Jonathan Austin |
0:bc2961fa1ef0 | 303 | #ifdef ANT_STACK_SUPPORT_REQD |
Jonathan Austin |
0:bc2961fa1ef0 | 304 | uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler) |
Jonathan Austin |
0:bc2961fa1ef0 | 305 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 306 | if (ant_evt_handler == NULL) |
Jonathan Austin |
0:bc2961fa1ef0 | 307 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 308 | return NRF_ERROR_NULL; |
Jonathan Austin |
0:bc2961fa1ef0 | 309 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 310 | |
Jonathan Austin |
0:bc2961fa1ef0 | 311 | m_ant_evt_handler = ant_evt_handler; |
Jonathan Austin |
0:bc2961fa1ef0 | 312 | |
Jonathan Austin |
0:bc2961fa1ef0 | 313 | return NRF_SUCCESS; |
Jonathan Austin |
0:bc2961fa1ef0 | 314 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 315 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 316 | |
Jonathan Austin |
0:bc2961fa1ef0 | 317 | |
Jonathan Austin |
0:bc2961fa1ef0 | 318 | uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler) |
Jonathan Austin |
0:bc2961fa1ef0 | 319 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 320 | if (sys_evt_handler == NULL) |
Jonathan Austin |
0:bc2961fa1ef0 | 321 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 322 | return NRF_ERROR_NULL; |
Jonathan Austin |
0:bc2961fa1ef0 | 323 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 324 | |
Jonathan Austin |
0:bc2961fa1ef0 | 325 | m_sys_evt_handler = sys_evt_handler; |
Jonathan Austin |
0:bc2961fa1ef0 | 326 | |
Jonathan Austin |
0:bc2961fa1ef0 | 327 | return NRF_SUCCESS; |
Jonathan Austin |
0:bc2961fa1ef0 | 328 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 329 | |
Jonathan Austin |
0:bc2961fa1ef0 | 330 | |
Jonathan Austin |
0:bc2961fa1ef0 | 331 | /**@brief Function for handling the Application's BLE Stack events interrupt. |
Jonathan Austin |
0:bc2961fa1ef0 | 332 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 333 | * @details This function is called whenever an event is ready to be pulled. |
Jonathan Austin |
0:bc2961fa1ef0 | 334 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 335 | void SOFTDEVICE_EVT_IRQHandler(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 336 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 337 | if (m_evt_schedule_func != NULL) |
Jonathan Austin |
0:bc2961fa1ef0 | 338 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 339 | uint32_t err_code = m_evt_schedule_func(); |
Jonathan Austin |
0:bc2961fa1ef0 | 340 | APP_ERROR_CHECK(err_code); |
Jonathan Austin |
0:bc2961fa1ef0 | 341 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 342 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 343 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 344 | intern_softdevice_events_execute(); |
Jonathan Austin |
0:bc2961fa1ef0 | 345 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 346 | } |