David Kester / nRF51822

Dependents:   GonioTrainer

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers softdevice_handler.c Source File

softdevice_handler.c

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