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