To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

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