Microbit as a BLE gamepad

Dependents:   nRF51822

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers softdevice_handler.c Source File

softdevice_handler.c

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 #include "softdevice_handler.h "
00034 #include <stdlib.h>
00035 #include "nordic_common.h"
00036 #include "app_error.h "
00037 #include "app_util.h "
00038 #include "nrf_assert.h"
00039 #include "nrf_soc.h"
00040 #include "nrf.h"
00041 
00042 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
00043     #include "ant_interface.h"
00044 #elif defined(ANT_STACK_SUPPORT_REQD) 
00045     #include "ant_interface.h"
00046 #elif defined(BLE_STACK_SUPPORT_REQD)
00047     #include "ble.h"
00048 #endif
00049 
00050 #ifdef NRF51
00051 #define SOFTDEVICE_EVT_IRQ        SD_EVT_IRQn       /**< SoftDevice Event IRQ number. Used for both protocol events and SoC events. */
00052 #define SOFTDEVICE_EVT_IRQHandler SD_EVT_IRQHandler
00053 #elif defined (NRF52)
00054 #define SOFTDEVICE_EVT_IRQ        SWI2_EGU2_IRQn
00055 #define SOFTDEVICE_EVT_IRQHandler SWI2_EGU2_IRQHandler
00056 #endif /* NRF51 */
00057 
00058 static softdevice_evt_schedule_func_t m_evt_schedule_func;              /**< Pointer to function for propagating SoftDevice events to the scheduler. */
00059 
00060 static volatile bool                  m_softdevice_enabled = false;     /**< Variable to indicate whether the SoftDevice is enabled. */
00061 
00062 #ifdef BLE_STACK_SUPPORT_REQD
00063 // The following three definitions is needed only if BLE events are needed to be pulled from the stack.
00064 static uint8_t                      * mp_ble_evt_buffer;                /**< Buffer for receiving BLE events from the SoftDevice. */
00065 static uint16_t                       m_ble_evt_buffer_size;            /**< Size of BLE event buffer. */
00066 static ble_evt_handler_t              m_ble_evt_handler;                /**< Application event handler for handling BLE events. */
00067 #endif
00068 
00069 #ifdef ANT_STACK_SUPPORT_REQD
00070 // The following two definition is needed only if ANT events are needed to be pulled from the stack.
00071 static ant_evt_t                      m_ant_evt_buffer;                 /**< Buffer for receiving ANT events from the SoftDevice. */
00072 static ant_evt_handler_t              m_ant_evt_handler;                /**< Application event handler for handling ANT events.  */
00073 #endif
00074 
00075 static sys_evt_handler_t              m_sys_evt_handler;                /**< Application event handler for handling System (SOC) events.  */
00076 
00077 
00078 /**@brief       Callback function for asserts in the SoftDevice.
00079  *
00080  * @details     A pointer to this function will be passed to the SoftDevice. This function will be
00081  *              called if an ASSERT statement in the SoftDevice fails.
00082  *
00083  * @param[in]   pc         The value of the program counter when the ASSERT call failed.
00084  * @param[in]   line_num   Line number of the failing ASSERT call.
00085  * @param[in]   file_name  File name of the failing ASSERT call.
00086  */
00087 void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name)
00088 {
00089     UNUSED_PARAMETER(pc);
00090     assert_nrf_callback(line_num, file_name);
00091 }
00092 
00093 
00094 void intern_softdevice_events_execute(void)
00095 {
00096     if (!m_softdevice_enabled)
00097     {
00098         // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
00099         // application without using this module's API (i.e softdevice_handler_init)
00100 
00101         return;
00102     }
00103 
00104     bool no_more_soc_evts = (m_sys_evt_handler == NULL);
00105 #ifdef BLE_STACK_SUPPORT_REQD
00106     bool no_more_ble_evts = (m_ble_evt_handler == NULL);
00107 #endif
00108 #ifdef ANT_STACK_SUPPORT_REQD
00109     bool no_more_ant_evts = (m_ant_evt_handler == NULL);
00110 #endif
00111 
00112     for (;;)
00113     {
00114         uint32_t err_code;
00115 
00116         if (!no_more_soc_evts)
00117         {
00118             uint32_t evt_id;
00119 
00120             // Pull event from SOC.
00121             err_code = sd_evt_get(&evt_id);
00122             
00123             if (err_code == NRF_ERROR_NOT_FOUND)
00124             {
00125                 no_more_soc_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 SOC event handler.
00134                 m_sys_evt_handler(evt_id);
00135             }
00136         }
00137 
00138 #ifdef BLE_STACK_SUPPORT_REQD
00139         // Fetch BLE Events.
00140         if (!no_more_ble_evts)
00141         {
00142             // Pull event from stack
00143             uint16_t evt_len = m_ble_evt_buffer_size;
00144 
00145             err_code = sd_ble_evt_get(mp_ble_evt_buffer, &evt_len);
00146             if (err_code == NRF_ERROR_NOT_FOUND)
00147             {
00148                 no_more_ble_evts = true;
00149             }
00150             else if (err_code != NRF_SUCCESS)
00151             {
00152                 APP_ERROR_HANDLER(err_code);
00153             }
00154             else
00155             {
00156                 // Call application's BLE stack event handler.
00157                 m_ble_evt_handler((ble_evt_t *)mp_ble_evt_buffer);
00158             }
00159         }
00160 #endif
00161 
00162 #ifdef ANT_STACK_SUPPORT_REQD
00163         // Fetch ANT Events.
00164         if (!no_more_ant_evts)
00165         {
00166             // Pull event from stack
00167             err_code = sd_ant_event_get(&m_ant_evt_buffer.channel,
00168                                         &m_ant_evt_buffer.event,
00169                                         m_ant_evt_buffer.evt_buffer);
00170             if (err_code == NRF_ERROR_NOT_FOUND)
00171             {
00172                 no_more_ant_evts = true;
00173             }
00174             else if (err_code != NRF_SUCCESS)
00175             {
00176                 APP_ERROR_HANDLER(err_code);
00177             }
00178             else
00179             {
00180                 // Call application's ANT stack event handler.
00181                 m_ant_evt_handler(&m_ant_evt_buffer);
00182             }
00183         }
00184 #endif
00185 
00186         if (no_more_soc_evts)
00187         {
00188             // There are no remaining System (SOC) events to be fetched from the SoftDevice.
00189 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
00190             // Check if there are any remaining BLE and ANT events.
00191             if (no_more_ble_evts && no_more_ant_evts)
00192             {
00193                 break;
00194             }
00195 #elif defined(BLE_STACK_SUPPORT_REQD)
00196             // Check if there are any remaining BLE events.
00197             if (no_more_ble_evts)
00198             {
00199                 break;
00200             }
00201 #elif defined(ANT_STACK_SUPPORT_REQD)
00202             // Check if there are any remaining ANT events.
00203             if (no_more_ant_evts)
00204             {
00205                 break;
00206             }
00207 #else
00208             // No need to check for BLE or ANT events since there is no support for BLE and ANT
00209             // required.
00210             break;
00211 #endif
00212         }
00213     }
00214 }
00215 
00216 bool softdevice_handler_isEnabled(void)
00217 {
00218     return m_softdevice_enabled;
00219 }
00220 
00221 uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t           clock_source,
00222                                  void *                         p_ble_evt_buffer,
00223                                  uint16_t                       ble_evt_buffer_size,
00224                                  softdevice_evt_schedule_func_t evt_schedule_func)
00225 {
00226     uint32_t err_code;
00227 
00228     // Save configuration.
00229 #if defined (BLE_STACK_SUPPORT_REQD)
00230     // Check that buffer is not NULL.
00231     if (p_ble_evt_buffer == NULL)
00232     {
00233         return NRF_ERROR_INVALID_PARAM;
00234     }
00235     
00236     // Check that buffer is correctly aligned.
00237     if (!is_word_aligned(p_ble_evt_buffer))
00238     {
00239         return NRF_ERROR_INVALID_PARAM;
00240     }
00241 
00242     mp_ble_evt_buffer     = (uint8_t *)p_ble_evt_buffer;
00243     m_ble_evt_buffer_size = ble_evt_buffer_size;
00244 #else
00245     // The variables p_ble_evt_buffer and ble_evt_buffer_size is not needed if BLE Stack support
00246     // is not required.
00247     UNUSED_PARAMETER(p_ble_evt_buffer);
00248     UNUSED_PARAMETER(ble_evt_buffer_size);
00249 #endif
00250 
00251     m_evt_schedule_func = evt_schedule_func;
00252 
00253 //Enabling FPU for SoftDevice
00254 #ifdef S132
00255     SCB->CPACR |= (3UL << 20) | (3UL << 22);
00256     __DSB();
00257     __ISB();
00258 #endif
00259     // Initialize SoftDevice.
00260     err_code = sd_softdevice_enable(clock_source, softdevice_assertion_handler);
00261     if (err_code != NRF_SUCCESS)
00262     {
00263         return err_code;
00264     }
00265 #ifdef S132
00266     SCB->CPACR = 0;
00267     __DSB();
00268     __ISB();
00269 #endif
00270 
00271     m_softdevice_enabled = true;
00272 
00273     // Enable BLE event interrupt (interrupt priority has already been set by the stack).
00274     return sd_nvic_EnableIRQ(SOFTDEVICE_EVT_IRQ);
00275 }
00276 
00277 
00278 uint32_t softdevice_handler_sd_disable(void)
00279 {
00280     uint32_t err_code = sd_softdevice_disable();
00281  
00282     m_softdevice_enabled = !(err_code == NRF_SUCCESS);
00283 
00284     return err_code;
00285 }
00286 
00287 
00288 #ifdef BLE_STACK_SUPPORT_REQD
00289 uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler)
00290 {
00291     if (ble_evt_handler == NULL)
00292     {
00293         return NRF_ERROR_NULL;
00294     }
00295 
00296     m_ble_evt_handler = ble_evt_handler;
00297 
00298     return NRF_SUCCESS;
00299 }
00300 #endif
00301 
00302 
00303 #ifdef ANT_STACK_SUPPORT_REQD
00304 uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler)
00305 {
00306     if (ant_evt_handler == NULL)
00307     {
00308         return NRF_ERROR_NULL;
00309     }
00310 
00311     m_ant_evt_handler = ant_evt_handler;
00312 
00313     return NRF_SUCCESS;
00314 }
00315 #endif
00316 
00317 
00318 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
00319 {
00320     if (sys_evt_handler == NULL)
00321     {
00322         return NRF_ERROR_NULL;
00323     }
00324 
00325     m_sys_evt_handler = sys_evt_handler;
00326 
00327     return NRF_SUCCESS;
00328 }
00329 
00330 
00331 /**@brief   Function for handling the Application's BLE Stack events interrupt.
00332  *
00333  * @details This function is called whenever an event is ready to be pulled.
00334  */
00335 void SOFTDEVICE_EVT_IRQHandler(void)
00336 {
00337     if (m_evt_schedule_func != NULL)
00338     {
00339         uint32_t err_code = m_evt_schedule_func();
00340         APP_ERROR_CHECK(err_code);
00341     }
00342     else
00343     {
00344         intern_softdevice_events_execute();
00345     }
00346 }