changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Initial import of the nRF51822 code

Who changed what in which revision?

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