テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8468a4403fea 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
jksoft 0:8468a4403fea 2 *
jksoft 0:8468a4403fea 3 * The information contained herein is property of Nordic Semiconductor ASA.
jksoft 0:8468a4403fea 4 * Terms and conditions of usage are described in detail in NORDIC
jksoft 0:8468a4403fea 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
jksoft 0:8468a4403fea 6 *
jksoft 0:8468a4403fea 7 * Licensees are granted free, non-transferable use of the information. NO
jksoft 0:8468a4403fea 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
jksoft 0:8468a4403fea 9 * the file.
jksoft 0:8468a4403fea 10 *
jksoft 0:8468a4403fea 11 */
jksoft 0:8468a4403fea 12
jksoft 0:8468a4403fea 13 #include "softdevice_handler.h"
jksoft 0:8468a4403fea 14 #include <stdlib.h>
jksoft 0:8468a4403fea 15 #include "nordic_common.h"
jksoft 0:8468a4403fea 16 #include "app_error.h"
jksoft 0:8468a4403fea 17 #include "app_util.h"
jksoft 0:8468a4403fea 18 #include "nrf_assert.h"
jksoft 0:8468a4403fea 19 #include "nrf_soc.h"
jksoft 0:8468a4403fea 20
jksoft 0:8468a4403fea 21 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 22 #include "ant_interface.h"
jksoft 0:8468a4403fea 23 #elif defined(ANT_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 24 #include "ant_interface.h"
jksoft 0:8468a4403fea 25 #elif defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 26 #include "ble.h"
jksoft 0:8468a4403fea 27 #endif
jksoft 0:8468a4403fea 28
jksoft 0:8468a4403fea 29
jksoft 0:8468a4403fea 30 static softdevice_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating SoftDevice events to the scheduler. */
jksoft 0:8468a4403fea 31
jksoft 0:8468a4403fea 32 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 33 // The following two definition is needed only if ANT or BLE events are needed to be pulled from the stack.
jksoft 0:8468a4403fea 34 static uint8_t * m_evt_buffer; /**< Buffer for receiving events from the SoftDevice. */
jksoft 0:8468a4403fea 35 #endif
jksoft 0:8468a4403fea 36
jksoft 0:8468a4403fea 37 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 38 static uint16_t m_ble_evt_buffer_size; /**< Size of BLE event buffer. */
jksoft 0:8468a4403fea 39 #endif
jksoft 0:8468a4403fea 40
jksoft 0:8468a4403fea 41 static volatile bool m_softdevice_enabled = false; /**< Variable to indicate whether the SoftDevice is enabled. */
jksoft 0:8468a4403fea 42
jksoft 0:8468a4403fea 43 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 44 static ble_evt_handler_t m_ble_evt_handler; /**< Application event handler for handling BLE events. */
jksoft 0:8468a4403fea 45 #endif
jksoft 0:8468a4403fea 46
jksoft 0:8468a4403fea 47 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 48 static ant_evt_handler_t m_ant_evt_handler; /**< Application event handler for handling ANT events. */
jksoft 0:8468a4403fea 49 #endif
jksoft 0:8468a4403fea 50
jksoft 0:8468a4403fea 51 static sys_evt_handler_t m_sys_evt_handler; /**< Application event handler for handling System (SOC) events. */
jksoft 0:8468a4403fea 52
jksoft 0:8468a4403fea 53
jksoft 0:8468a4403fea 54 /**@brief Callback function for asserts in the SoftDevice.
jksoft 0:8468a4403fea 55 *
jksoft 0:8468a4403fea 56 * @details A pointer to this function will be passed to the SoftDevice. This function will be
jksoft 0:8468a4403fea 57 * called if an ASSERT statement in the SoftDevice fails.
jksoft 0:8468a4403fea 58 *
jksoft 0:8468a4403fea 59 * @param[in] pc The value of the program counter when the ASSERT call failed.
jksoft 0:8468a4403fea 60 * @param[in] line_num Line number of the failing ASSERT call.
jksoft 0:8468a4403fea 61 * @param[in] file_name File name of the failing ASSERT call.
jksoft 0:8468a4403fea 62 */
jksoft 0:8468a4403fea 63 void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name)
jksoft 0:8468a4403fea 64 {
jksoft 0:8468a4403fea 65 UNUSED_PARAMETER(pc);
jksoft 0:8468a4403fea 66 assert_nrf_callback(line_num, file_name);
jksoft 0:8468a4403fea 67 }
jksoft 0:8468a4403fea 68
jksoft 0:8468a4403fea 69
jksoft 0:8468a4403fea 70 void intern_softdevice_events_execute(void)
jksoft 0:8468a4403fea 71 {
jksoft 0:8468a4403fea 72 if (!m_softdevice_enabled)
jksoft 0:8468a4403fea 73 {
jksoft 0:8468a4403fea 74 // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
jksoft 0:8468a4403fea 75 // application without using this module's API (i.e softdevice_handler_init)
jksoft 0:8468a4403fea 76
jksoft 0:8468a4403fea 77 return;
jksoft 0:8468a4403fea 78 }
jksoft 0:8468a4403fea 79
jksoft 0:8468a4403fea 80 bool no_more_soc_evts = (m_sys_evt_handler == NULL);
jksoft 0:8468a4403fea 81 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 82 bool no_more_ble_evts = (m_ble_evt_handler == NULL);
jksoft 0:8468a4403fea 83 #endif
jksoft 0:8468a4403fea 84 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 85 bool no_more_ant_evts = (m_ant_evt_handler == NULL);
jksoft 0:8468a4403fea 86 #endif
jksoft 0:8468a4403fea 87
jksoft 0:8468a4403fea 88 for (;;)
jksoft 0:8468a4403fea 89 {
jksoft 0:8468a4403fea 90 uint32_t err_code;
jksoft 0:8468a4403fea 91
jksoft 0:8468a4403fea 92 if (!no_more_soc_evts)
jksoft 0:8468a4403fea 93 {
jksoft 0:8468a4403fea 94 uint32_t evt_id;
jksoft 0:8468a4403fea 95
jksoft 0:8468a4403fea 96 // Pull event from SOC.
jksoft 0:8468a4403fea 97 err_code = sd_evt_get(&evt_id);
jksoft 0:8468a4403fea 98
jksoft 0:8468a4403fea 99 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:8468a4403fea 100 {
jksoft 0:8468a4403fea 101 no_more_soc_evts = true;
jksoft 0:8468a4403fea 102 }
jksoft 0:8468a4403fea 103 else if (err_code != NRF_SUCCESS)
jksoft 0:8468a4403fea 104 {
jksoft 0:8468a4403fea 105 APP_ERROR_HANDLER(err_code);
jksoft 0:8468a4403fea 106 }
jksoft 0:8468a4403fea 107 else
jksoft 0:8468a4403fea 108 {
jksoft 0:8468a4403fea 109 // Call application's SOC event handler.
jksoft 0:8468a4403fea 110 m_sys_evt_handler(evt_id);
jksoft 0:8468a4403fea 111 }
jksoft 0:8468a4403fea 112 }
jksoft 0:8468a4403fea 113
jksoft 0:8468a4403fea 114 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 115 // Fetch BLE Events.
jksoft 0:8468a4403fea 116 if (!no_more_ble_evts)
jksoft 0:8468a4403fea 117 {
jksoft 0:8468a4403fea 118 // Pull event from stack
jksoft 0:8468a4403fea 119 uint16_t evt_len = m_ble_evt_buffer_size;
jksoft 0:8468a4403fea 120
jksoft 0:8468a4403fea 121 err_code = sd_ble_evt_get(m_evt_buffer, &evt_len);
jksoft 0:8468a4403fea 122 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:8468a4403fea 123 {
jksoft 0:8468a4403fea 124 no_more_ble_evts = true;
jksoft 0:8468a4403fea 125 }
jksoft 0:8468a4403fea 126 else if (err_code != NRF_SUCCESS)
jksoft 0:8468a4403fea 127 {
jksoft 0:8468a4403fea 128 APP_ERROR_HANDLER(err_code);
jksoft 0:8468a4403fea 129 }
jksoft 0:8468a4403fea 130 else
jksoft 0:8468a4403fea 131 {
jksoft 0:8468a4403fea 132 // Call application's BLE stack event handler.
jksoft 0:8468a4403fea 133 m_ble_evt_handler((ble_evt_t *)m_evt_buffer);
jksoft 0:8468a4403fea 134 }
jksoft 0:8468a4403fea 135 }
jksoft 0:8468a4403fea 136 #endif
jksoft 0:8468a4403fea 137
jksoft 0:8468a4403fea 138 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 139 // Fetch ANT Events.
jksoft 0:8468a4403fea 140 if (!no_more_ant_evts)
jksoft 0:8468a4403fea 141 {
jksoft 0:8468a4403fea 142 // Pull event from stack
jksoft 0:8468a4403fea 143 err_code = sd_ant_event_get(&((ant_evt_t *)m_evt_buffer)->channel,
jksoft 0:8468a4403fea 144 &((ant_evt_t *)m_evt_buffer)->event,
jksoft 0:8468a4403fea 145 ((ant_evt_t *)m_evt_buffer)->evt_buffer);
jksoft 0:8468a4403fea 146 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:8468a4403fea 147 {
jksoft 0:8468a4403fea 148 no_more_ant_evts = true;
jksoft 0:8468a4403fea 149 }
jksoft 0:8468a4403fea 150 else if (err_code != NRF_SUCCESS)
jksoft 0:8468a4403fea 151 {
jksoft 0:8468a4403fea 152 APP_ERROR_HANDLER(err_code);
jksoft 0:8468a4403fea 153 }
jksoft 0:8468a4403fea 154 else
jksoft 0:8468a4403fea 155 {
jksoft 0:8468a4403fea 156 // Call application's ANT stack event handler.
jksoft 0:8468a4403fea 157 m_ant_evt_handler((ant_evt_t *)m_evt_buffer);
jksoft 0:8468a4403fea 158 }
jksoft 0:8468a4403fea 159 }
jksoft 0:8468a4403fea 160 #endif
jksoft 0:8468a4403fea 161
jksoft 0:8468a4403fea 162 if (no_more_soc_evts)
jksoft 0:8468a4403fea 163 {
jksoft 0:8468a4403fea 164 // There are no remaining System (SOC) events to be fetched from the SoftDevice.
jksoft 0:8468a4403fea 165 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 166 // Check if there are any remaining BLE and ANT events.
jksoft 0:8468a4403fea 167 if (no_more_ble_evts && no_more_ant_evts)
jksoft 0:8468a4403fea 168 {
jksoft 0:8468a4403fea 169 break;
jksoft 0:8468a4403fea 170 }
jksoft 0:8468a4403fea 171 #elif defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 172 // Check if there are any remaining BLE events.
jksoft 0:8468a4403fea 173 if (no_more_ble_evts)
jksoft 0:8468a4403fea 174 {
jksoft 0:8468a4403fea 175 break;
jksoft 0:8468a4403fea 176 }
jksoft 0:8468a4403fea 177 #elif defined(ANT_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 178 // Check if there are any remaining ANT events.
jksoft 0:8468a4403fea 179 if (no_more_ant_evts)
jksoft 0:8468a4403fea 180 {
jksoft 0:8468a4403fea 181 break;
jksoft 0:8468a4403fea 182 }
jksoft 0:8468a4403fea 183 #else
jksoft 0:8468a4403fea 184 // No need to check for BLE or ANT events since there is no support for BLE and ANT
jksoft 0:8468a4403fea 185 // required.
jksoft 0:8468a4403fea 186 break;
jksoft 0:8468a4403fea 187 #endif
jksoft 0:8468a4403fea 188 }
jksoft 0:8468a4403fea 189 }
jksoft 0:8468a4403fea 190 }
jksoft 0:8468a4403fea 191
jksoft 0:8468a4403fea 192
jksoft 0:8468a4403fea 193 uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t clock_source,
jksoft 0:8468a4403fea 194 void * p_evt_buffer,
jksoft 0:8468a4403fea 195 uint16_t evt_buffer_size,
jksoft 0:8468a4403fea 196 softdevice_evt_schedule_func_t evt_schedule_func)
jksoft 0:8468a4403fea 197 {
jksoft 0:8468a4403fea 198 uint32_t err_code;
jksoft 0:8468a4403fea 199
jksoft 0:8468a4403fea 200 // Save configuration.
jksoft 0:8468a4403fea 201 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 202 // Check that buffer is not NULL.
jksoft 0:8468a4403fea 203 if (p_evt_buffer == NULL)
jksoft 0:8468a4403fea 204 {
jksoft 0:8468a4403fea 205 return NRF_ERROR_INVALID_PARAM;
jksoft 0:8468a4403fea 206 }
jksoft 0:8468a4403fea 207
jksoft 0:8468a4403fea 208 // Check that buffer is correctly aligned.
jksoft 0:8468a4403fea 209 if (!is_word_aligned(p_evt_buffer))
jksoft 0:8468a4403fea 210 {
jksoft 0:8468a4403fea 211 return NRF_ERROR_INVALID_PARAM;
jksoft 0:8468a4403fea 212 }
jksoft 0:8468a4403fea 213
jksoft 0:8468a4403fea 214 m_evt_buffer = (uint8_t *)p_evt_buffer;
jksoft 0:8468a4403fea 215 #else
jksoft 0:8468a4403fea 216 // The variable p_evt_buffer is not needed if neither BLE Stack nor ANT stack support is
jksoft 0:8468a4403fea 217 // required.
jksoft 0:8468a4403fea 218 UNUSED_PARAMETER(p_evt_buffer);
jksoft 0:8468a4403fea 219 #endif
jksoft 0:8468a4403fea 220
jksoft 0:8468a4403fea 221 #if defined (BLE_STACK_SUPPORT_REQD)
jksoft 0:8468a4403fea 222 m_ble_evt_buffer_size = evt_buffer_size;
jksoft 0:8468a4403fea 223 #else
jksoft 0:8468a4403fea 224 // The variable evt_buffer_size is not needed if BLE Stack support is NOT required.
jksoft 0:8468a4403fea 225 UNUSED_PARAMETER(evt_buffer_size);
jksoft 0:8468a4403fea 226 #endif
jksoft 0:8468a4403fea 227
jksoft 0:8468a4403fea 228 m_evt_schedule_func = evt_schedule_func;
jksoft 0:8468a4403fea 229
jksoft 0:8468a4403fea 230 // Initialize SoftDevice.
jksoft 0:8468a4403fea 231
jksoft 0:8468a4403fea 232 err_code = sd_softdevice_enable(clock_source, softdevice_assertion_handler);
jksoft 0:8468a4403fea 233 if (err_code != NRF_SUCCESS)
jksoft 0:8468a4403fea 234 {
jksoft 0:8468a4403fea 235 return err_code;
jksoft 0:8468a4403fea 236 }
jksoft 0:8468a4403fea 237
jksoft 0:8468a4403fea 238 m_softdevice_enabled = true;
jksoft 0:8468a4403fea 239
jksoft 0:8468a4403fea 240 // Enable BLE event interrupt (interrupt priority has already been set by the stack).
jksoft 0:8468a4403fea 241 return sd_nvic_EnableIRQ(SWI2_IRQn);
jksoft 0:8468a4403fea 242 }
jksoft 0:8468a4403fea 243
jksoft 0:8468a4403fea 244
jksoft 0:8468a4403fea 245 uint32_t softdevice_handler_sd_disable(void)
jksoft 0:8468a4403fea 246 {
jksoft 0:8468a4403fea 247 uint32_t err_code = sd_softdevice_disable();
jksoft 0:8468a4403fea 248
jksoft 0:8468a4403fea 249 m_softdevice_enabled = !(err_code == NRF_SUCCESS);
jksoft 0:8468a4403fea 250
jksoft 0:8468a4403fea 251 return err_code;
jksoft 0:8468a4403fea 252 }
jksoft 0:8468a4403fea 253
jksoft 0:8468a4403fea 254
jksoft 0:8468a4403fea 255 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 256 uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler)
jksoft 0:8468a4403fea 257 {
jksoft 0:8468a4403fea 258 if (ble_evt_handler == NULL)
jksoft 0:8468a4403fea 259 {
jksoft 0:8468a4403fea 260 return NRF_ERROR_NULL;
jksoft 0:8468a4403fea 261 }
jksoft 0:8468a4403fea 262
jksoft 0:8468a4403fea 263 m_ble_evt_handler = ble_evt_handler;
jksoft 0:8468a4403fea 264
jksoft 0:8468a4403fea 265 return NRF_SUCCESS;
jksoft 0:8468a4403fea 266 }
jksoft 0:8468a4403fea 267 #endif
jksoft 0:8468a4403fea 268
jksoft 0:8468a4403fea 269
jksoft 0:8468a4403fea 270 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:8468a4403fea 271 uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler)
jksoft 0:8468a4403fea 272 {
jksoft 0:8468a4403fea 273 if (ant_evt_handler == NULL)
jksoft 0:8468a4403fea 274 {
jksoft 0:8468a4403fea 275 return NRF_ERROR_NULL;
jksoft 0:8468a4403fea 276 }
jksoft 0:8468a4403fea 277
jksoft 0:8468a4403fea 278 m_ant_evt_handler = ant_evt_handler;
jksoft 0:8468a4403fea 279
jksoft 0:8468a4403fea 280 return NRF_SUCCESS;
jksoft 0:8468a4403fea 281 }
jksoft 0:8468a4403fea 282 #endif
jksoft 0:8468a4403fea 283
jksoft 0:8468a4403fea 284
jksoft 0:8468a4403fea 285 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
jksoft 0:8468a4403fea 286 {
jksoft 0:8468a4403fea 287 if (sys_evt_handler == NULL)
jksoft 0:8468a4403fea 288 {
jksoft 0:8468a4403fea 289 return NRF_ERROR_NULL;
jksoft 0:8468a4403fea 290 }
jksoft 0:8468a4403fea 291
jksoft 0:8468a4403fea 292 m_sys_evt_handler = sys_evt_handler;
jksoft 0:8468a4403fea 293
jksoft 0:8468a4403fea 294 return NRF_SUCCESS;
jksoft 0:8468a4403fea 295 }
jksoft 0:8468a4403fea 296
jksoft 0:8468a4403fea 297
jksoft 0:8468a4403fea 298 /**@brief Function for handling the Application's BLE Stack events interrupt.
jksoft 0:8468a4403fea 299 *
jksoft 0:8468a4403fea 300 * @details This function is called whenever an event is ready to be pulled.
jksoft 0:8468a4403fea 301 */
jksoft 0:8468a4403fea 302 extern "C" void SWI2_IRQHandler(void)
jksoft 0:8468a4403fea 303 {
jksoft 0:8468a4403fea 304 if (m_evt_schedule_func != NULL)
jksoft 0:8468a4403fea 305 {
jksoft 0:8468a4403fea 306 uint32_t err_code = m_evt_schedule_func();
jksoft 0:8468a4403fea 307 APP_ERROR_CHECK(err_code);
jksoft 0:8468a4403fea 308 }
jksoft 0:8468a4403fea 309 else
jksoft 0:8468a4403fea 310 {
jksoft 0:8468a4403fea 311 intern_softdevice_events_execute();
jksoft 0:8468a4403fea 312 }
jksoft 0:8468a4403fea 313 }