changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers softdevice_handler.cpp Source File

softdevice_handler.cpp

00001 /* Copyright (c)  2013 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 #include "softdevice_handler.h "
00014 #include <stdlib.h>
00015 #include "nordic_common.h"
00016 #include "app_error.h "
00017 #include "app_util.h "
00018 #include "nrf_assert.h"
00019 #include "nrf_soc.h"
00020 #include "mbed.h"
00021 
00022 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
00023     #include "ant_interface.h"
00024 #elif defined(ANT_STACK_SUPPORT_REQD) 
00025     #include "ant_interface.h"
00026 #elif defined(BLE_STACK_SUPPORT_REQD)
00027     #include "ble.h"
00028 #endif
00029 
00030 
00031 static softdevice_evt_schedule_func_t m_evt_schedule_func;              /**< Pointer to function for propagating SoftDevice events to the scheduler. */
00032 
00033 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
00034 // The following two definition is needed only if ANT or BLE events are needed to be pulled from the stack.
00035 static uint8_t *                      m_evt_buffer;                     /**< Buffer for receiving events from the SoftDevice. */
00036 #endif
00037 
00038 #ifdef BLE_STACK_SUPPORT_REQD
00039 static uint16_t                       m_ble_evt_buffer_size;            /**< Size of BLE event buffer. */
00040 #endif
00041 
00042 static volatile bool                  m_softdevice_enabled = false;     /**< Variable to indicate whether the SoftDevice is enabled. */
00043 
00044 #ifdef BLE_STACK_SUPPORT_REQD
00045 static ble_evt_handler_t              m_ble_evt_handler;                /**< Application event handler for handling BLE events. */
00046 #endif
00047 
00048 #ifdef ANT_STACK_SUPPORT_REQD
00049 static ant_evt_handler_t              m_ant_evt_handler;                /**< Application event handler for handling ANT events.  */
00050 #endif
00051 
00052 static sys_evt_handler_t              m_sys_evt_handler;                /**< Application event handler for handling System (SOC) events.  */
00053 
00054 
00055 /**@brief       Callback function for asserts in the SoftDevice.
00056  *
00057  * @details     A pointer to this function will be passed to the SoftDevice. This function will be
00058  *              called if an ASSERT statement in the SoftDevice fails.
00059  *
00060  * @param[in]   pc         The value of the program counter when the ASSERT call failed.
00061  * @param[in]   line_num   Line number of the failing ASSERT call.
00062  * @param[in]   file_name  File name of the failing ASSERT call.
00063  */
00064 void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name)
00065 {
00066     UNUSED_PARAMETER(pc);
00067     assert_nrf_callback(line_num, file_name);
00068 }
00069 
00070 
00071 void intern_softdevice_events_execute(void)
00072 {
00073     if (!m_softdevice_enabled)
00074     {
00075         // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
00076         // application without using this module's API (i.e softdevice_handler_init)
00077 
00078         return;
00079     }
00080 
00081     bool no_more_soc_evts = (m_sys_evt_handler == NULL);
00082 #ifdef BLE_STACK_SUPPORT_REQD
00083     bool no_more_ble_evts = (m_ble_evt_handler == NULL);
00084 #endif
00085 #ifdef ANT_STACK_SUPPORT_REQD
00086     bool no_more_ant_evts = (m_ant_evt_handler == NULL);
00087 #endif
00088 
00089     for (;;)
00090     {
00091         uint32_t err_code;
00092 
00093         if (!no_more_soc_evts)
00094         {
00095             uint32_t evt_id;
00096 
00097             // Pull event from SOC.
00098             err_code = sd_evt_get(&evt_id);
00099             
00100             if (err_code == NRF_ERROR_NOT_FOUND)
00101             {
00102                 no_more_soc_evts = true;
00103             }
00104             else if (err_code != NRF_SUCCESS)
00105             {
00106                 APP_ERROR_HANDLER(err_code);
00107             }
00108             else
00109             {
00110                 // Call application's SOC event handler.
00111                 m_sys_evt_handler(evt_id);
00112             }
00113         }
00114 
00115 #ifdef BLE_STACK_SUPPORT_REQD
00116         // Fetch BLE Events.
00117         if (!no_more_ble_evts)
00118         {
00119             // Pull event from stack
00120             uint16_t evt_len = m_ble_evt_buffer_size;
00121 
00122             err_code = sd_ble_evt_get(m_evt_buffer, &evt_len);
00123             if (err_code == NRF_ERROR_NOT_FOUND)
00124             {
00125                 no_more_ble_evts = true;
00126             }
00127             else if (err_code != NRF_SUCCESS)
00128             {
00129                 APP_ERROR_HANDLER(err_code);
00130             }
00131             else
00132             {
00133                 // Call application's BLE stack event handler.
00134                 m_ble_evt_handler((ble_evt_t *)m_evt_buffer);
00135             }
00136         }
00137 #endif
00138 
00139 #ifdef ANT_STACK_SUPPORT_REQD
00140         // Fetch ANT Events.
00141         if (!no_more_ant_evts)
00142         {
00143             // Pull event from stack
00144             err_code = sd_ant_event_get(&((ant_evt_t *)m_evt_buffer)->channel,
00145                                         &((ant_evt_t *)m_evt_buffer)->event,
00146                                         ((ant_evt_t *)m_evt_buffer)->evt_buffer);
00147             if (err_code == NRF_ERROR_NOT_FOUND)
00148             {
00149                 no_more_ant_evts = true;
00150             }
00151             else if (err_code != NRF_SUCCESS)
00152             {
00153                 APP_ERROR_HANDLER(err_code);
00154             }
00155             else
00156             {
00157                 // Call application's ANT stack event handler.
00158                 m_ant_evt_handler((ant_evt_t *)m_evt_buffer);
00159             }
00160         }
00161 #endif
00162 
00163         if (no_more_soc_evts)
00164         {
00165             // There are no remaining System (SOC) events to be fetched from the SoftDevice.
00166 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
00167             // Check if there are any remaining BLE and ANT events.
00168             if (no_more_ble_evts && no_more_ant_evts)
00169             {
00170                 break;
00171             }
00172 #elif defined(BLE_STACK_SUPPORT_REQD)
00173             // Check if there are any remaining BLE events.
00174             if (no_more_ble_evts)
00175             {
00176                 break;
00177             }
00178 #elif defined(ANT_STACK_SUPPORT_REQD)
00179             // Check if there are any remaining ANT events.
00180             if (no_more_ant_evts)
00181             {
00182                 break;
00183             }
00184 #else
00185             // No need to check for BLE or ANT events since there is no support for BLE and ANT
00186             // required.
00187             break;
00188 #endif
00189         }
00190     }
00191 }
00192 
00193 
00194 uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t           clock_source,
00195                                  void *                         p_evt_buffer,
00196                                  uint16_t                       evt_buffer_size,
00197                                  softdevice_evt_schedule_func_t evt_schedule_func)
00198 {
00199     uint32_t err_code;
00200 
00201     // Save configuration.
00202 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
00203     // Check that buffer is not NULL.
00204     if (p_evt_buffer == NULL)
00205     {
00206         return NRF_ERROR_INVALID_PARAM;
00207     }
00208     
00209     // Check that buffer is correctly aligned.
00210     if (!is_word_aligned(p_evt_buffer))
00211     {
00212         return NRF_ERROR_INVALID_PARAM;
00213     }
00214 
00215     m_evt_buffer = (uint8_t *)p_evt_buffer;
00216 #else
00217     // The variable p_evt_buffer is not needed if neither BLE Stack nor ANT stack support is 
00218     // required.
00219     UNUSED_PARAMETER(p_evt_buffer);
00220 #endif
00221 
00222 #if defined (BLE_STACK_SUPPORT_REQD)     
00223     m_ble_evt_buffer_size = evt_buffer_size;
00224 #else
00225     // The variable evt_buffer_size is not needed if BLE Stack support is NOT required.
00226     UNUSED_PARAMETER(evt_buffer_size);
00227 #endif
00228     
00229     m_evt_schedule_func = evt_schedule_func;
00230 
00231     // Initialize SoftDevice.
00232    
00233     err_code = sd_softdevice_enable(clock_source, softdevice_assertion_handler);
00234     if (err_code != NRF_SUCCESS)
00235     {
00236         return err_code;
00237     }
00238 
00239     m_softdevice_enabled = true;
00240     // Enable BLE event interrupt (interrupt priority has already been set by the stack).
00241     return sd_nvic_EnableIRQ(SWI2_IRQn);
00242 }
00243 
00244 
00245 uint32_t softdevice_handler_sd_disable(void)
00246 {
00247     uint32_t err_code = sd_softdevice_disable();
00248  
00249     m_softdevice_enabled = !(err_code == NRF_SUCCESS);
00250 
00251     return err_code;
00252 }
00253 
00254 
00255 #ifdef BLE_STACK_SUPPORT_REQD
00256 uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler)
00257 {
00258     if (ble_evt_handler == NULL)
00259     {
00260         return NRF_ERROR_NULL;
00261     }
00262 
00263     m_ble_evt_handler = ble_evt_handler;
00264 
00265     return NRF_SUCCESS;
00266 }
00267 #endif
00268 
00269 
00270 #ifdef ANT_STACK_SUPPORT_REQD
00271 uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler)
00272 {
00273     if (ant_evt_handler == NULL)
00274     {
00275         return NRF_ERROR_NULL;
00276     }
00277 
00278     m_ant_evt_handler = ant_evt_handler;
00279 
00280     return NRF_SUCCESS;
00281 }
00282 #endif
00283 
00284 
00285 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
00286 {
00287     if (sys_evt_handler == NULL)
00288     {
00289         return NRF_ERROR_NULL;
00290     }
00291 
00292     m_sys_evt_handler = sys_evt_handler;
00293 
00294     return NRF_SUCCESS;
00295 }
00296 
00297 
00298 /**@brief   Function for handling the Application's BLE Stack events interrupt.
00299  *
00300  * @details This function is called whenever an event is ready to be pulled.
00301  */
00302 extern "C" void SWI2_IRQHandler(void)
00303 {
00304     if (m_evt_schedule_func != NULL)
00305     {
00306         uint32_t err_code = m_evt_schedule_func();
00307         APP_ERROR_CHECK(err_code);
00308     }
00309     else
00310     {
00311         intern_softdevice_events_execute();
00312     }
00313 }