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