Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF5xn.cpp Source File

nRF5xn.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifdef YOTTA_CFG_MBED_OS
00018     #include "mbed-drivers/mbed.h"
00019 #else
00020     #include "mbed.h"
00021 #endif
00022 #include "nRF5xn.h"
00023 #include "ble/blecommon.h"
00024 #include "nrf_soc.h"
00025 
00026 #include "btle/btle.h"
00027 #include "nrf_delay.h"
00028 
00029 extern "C" {
00030 #include "softdevice_handler.h "
00031 }
00032 
00033 /**
00034  * The singleton which represents the nRF51822 transport for the BLE.
00035  */
00036 static nRF5xn deviceInstance;
00037 
00038 /**
00039  * BLE-API requires an implementation of the following function in order to
00040  * obtain its transport handle.
00041  */
00042 BLEInstanceBase *
00043 createBLEInstance(void)
00044 {
00045     return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
00046 }
00047 
00048 nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId)
00049 {
00050     return deviceInstance;
00051 }
00052 
00053 nRF5xn::nRF5xn(void) :
00054     initialized(false),
00055     instanceID(BLE::DEFAULT_INSTANCE),
00056     gapInstance(),
00057     gattServerInstance(NULL),
00058     gattClientInstance(NULL),
00059     securityManagerInstance(NULL)
00060 {
00061 }
00062 
00063 nRF5xn::~nRF5xn(void)
00064 {
00065 }
00066 
00067 const char *nRF5xn::getVersion(void)
00068 {
00069     if (!initialized) {
00070         return "INITIALIZATION_INCOMPLETE";
00071     }
00072 
00073     static char versionString[32];
00074     static bool versionFetched = false;
00075 
00076     if (!versionFetched) {
00077         ble_version_t version;
00078         if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) {
00079             switch (version.version_number) {
00080                 case 0x07:
00081                 case 0x08:
00082                     snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number);
00083                     break;
00084                 default:
00085                     snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number);
00086                     break;
00087             }
00088             versionFetched = true;
00089         } else {
00090             strncpy(versionString, "unknown", sizeof(versionString));
00091         }
00092     }
00093 
00094     return versionString;
00095 }
00096 
00097 /**************************************************************************/
00098 /*!
00099     @brief  Initialize the BLE stack.
00100 
00101     @returns    ble_error_t
00102 
00103     @retval     BLE_ERROR_NONE if everything executed properly and
00104                 BLE_ERROR_ALREADY_INITIALIZED if the stack has already
00105                 been initialized (possibly through a call to nRF5xn::init()).
00106                 BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization
00107                 of the internal stack (SoftDevice) failed.
00108 
00109 */
00110 /**************************************************************************/
00111 ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback)
00112 {
00113     if (initialized) {
00114         BLE::InitializationCompleteCallbackContext context = {
00115             BLE::Instance(instanceID),
00116             BLE_ERROR_ALREADY_INITIALIZED
00117         };
00118         callback.call(&context);
00119         return BLE_ERROR_ALREADY_INITIALIZED;
00120     }
00121 
00122     instanceID   = instanceID;
00123 
00124     /* ToDo: Clear memory contents, reset the SD, etc. */
00125     if (btle_init() != ERROR_NONE) {
00126         return BLE_ERROR_INTERNAL_STACK_FAILURE;
00127     }
00128 
00129     initialized = true;
00130     BLE::InitializationCompleteCallbackContext context = {
00131         BLE::Instance(instanceID),
00132         BLE_ERROR_NONE
00133     };
00134     callback.call(&context);
00135     return BLE_ERROR_NONE;
00136 }
00137 
00138 /**************************************************************************/
00139 /*!
00140     @brief  Purge the BLE stack of GATT and GAP state.
00141 
00142     @returns    ble_error_t
00143 
00144     @retval     BLE_ERROR_NONE
00145                 Everything executed properly
00146 
00147     @note  When using S110, GattClient::shutdown() will not be called
00148            since Gatt client features are not supported.
00149 */
00150 /**************************************************************************/
00151 ble_error_t nRF5xn::shutdown(void)
00152 {
00153     if (!initialized) {
00154         return BLE_ERROR_INITIALIZATION_INCOMPLETE;
00155     }
00156 
00157     /*
00158      * Shutdown the SoftDevice first. This is because we need to disable all
00159      * interrupts. Otherwise if we clear the BLE API and glue code first there
00160      * will be many NULL references and no config information which could lead
00161      * to errors if the shutdown process is interrupted.
00162      */
00163     if (softdevice_handler_sd_disable() != NRF_SUCCESS) {
00164         return BLE_STACK_BUSY;
00165     }
00166 
00167 
00168     /* Shutdown the BLE API and nRF51 glue code */
00169     ble_error_t error;
00170 
00171     if (gattServerInstance != NULL) {
00172         error = gattServerInstance->reset();
00173         if (error != BLE_ERROR_NONE) {
00174             return error;
00175         }
00176     }
00177 
00178     if (securityManagerInstance != NULL) {
00179         error = securityManagerInstance->reset();
00180         if (error != BLE_ERROR_NONE) {
00181             return error;
00182         }
00183     }
00184 
00185     /* S110 does not support BLE client features, nothing to reset. */
00186 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
00187     if (gattClientInstance != NULL) {
00188         error = gattClientInstance->reset();
00189         if (error != BLE_ERROR_NONE) {
00190             return error;
00191         }
00192     }
00193 #endif
00194 
00195     /* Gap instance is always present */
00196     error = gapInstance.reset();
00197     if (error != BLE_ERROR_NONE) {
00198         return error;
00199     }
00200 
00201     initialized = false;
00202     return BLE_ERROR_NONE;
00203 }
00204 
00205 void
00206 nRF5xn::waitForEvent(void)
00207 {
00208     sd_app_evt_wait();
00209 }