Clone of the nRF51822 repository from github The correct home is https://github.com/lancaster-university/nRF51822

Dependencies:   nrf51-sdk

Dependents:   microbit-dal microbit-ble-open microbit-dal-eddystone microbit-dal-ble-accelerometer-example ... more

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 = NULL;
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     if (deviceInstance == NULL)
00051         deviceInstance = new nRF5xn();
00052 
00053     return *deviceInstance;
00054 }
00055 
00056 nRF5xn::nRF5xn(void) :
00057     initialized(false),
00058     instanceID(BLE::DEFAULT_INSTANCE),
00059     gapInstance(),
00060     gattServerInstance(NULL),
00061     gattClientInstance(NULL),
00062     securityManagerInstance(NULL)
00063 {
00064 }
00065 
00066 nRF5xn::~nRF5xn(void)
00067 {
00068 }
00069 
00070 const char *nRF5xn::getVersion(void)
00071 {
00072     if (!initialized) {
00073         return "INITIALIZATION_INCOMPLETE";
00074     }
00075 
00076     static char versionString[32];
00077     static bool versionFetched = false;
00078 
00079     if (!versionFetched) {
00080         ble_version_t version;
00081         if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) {
00082             switch (version.version_number) {
00083                 case 0x07:
00084                 case 0x08:
00085                     snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number);
00086                     break;
00087                 default:
00088                     snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number);
00089                     break;
00090             }
00091             versionFetched = true;
00092         } else {
00093             strncpy(versionString, "unknown", sizeof(versionString));
00094         }
00095     }
00096 
00097     return versionString;
00098 }
00099 
00100 /**************************************************************************/
00101 /*!
00102     @brief  Initialize the BLE stack.
00103 
00104     @returns    ble_error_t
00105 
00106     @retval     BLE_ERROR_NONE if everything executed properly and
00107                 BLE_ERROR_ALREADY_INITIALIZED if the stack has already
00108                 been initialized (possibly through a call to nRF5xn::init()).
00109                 BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization
00110                 of the internal stack (SoftDevice) failed.
00111 
00112 */
00113 /**************************************************************************/
00114 ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback)
00115 {
00116     if (initialized) {
00117         BLE::InitializationCompleteCallbackContext context = {
00118             BLE::Instance(instanceID),
00119             BLE_ERROR_ALREADY_INITIALIZED
00120         };
00121         callback.call(&context);
00122         return BLE_ERROR_ALREADY_INITIALIZED;
00123     }
00124 
00125     instanceID   = instanceID;
00126 
00127     /* ToDo: Clear memory contents, reset the SD, etc. */
00128     if (btle_init() != ERROR_NONE) {
00129         return BLE_ERROR_INTERNAL_STACK_FAILURE;
00130     }
00131 
00132     initialized = true;
00133     BLE::InitializationCompleteCallbackContext context = {
00134         BLE::Instance(instanceID),
00135         BLE_ERROR_NONE
00136     };
00137     callback.call(&context);
00138     return BLE_ERROR_NONE;
00139 }
00140 
00141 /**************************************************************************/
00142 /*!
00143     @brief  Purge the BLE stack of GATT and GAP state.
00144 
00145     @returns    ble_error_t
00146 
00147     @retval     BLE_ERROR_NONE
00148                 Everything executed properly
00149 
00150     @note  When using S110, GattClient::shutdown() will not be called
00151            since Gatt client features are not supported.
00152 */
00153 /**************************************************************************/
00154 ble_error_t nRF5xn::shutdown(void)
00155 {
00156     if (!initialized) {
00157         return BLE_ERROR_INITIALIZATION_INCOMPLETE;
00158     }
00159 
00160     /*
00161      * Shutdown the SoftDevice first. This is because we need to disable all
00162      * interrupts. Otherwise if we clear the BLE API and glue code first there
00163      * will be many NULL references and no config information which could lead
00164      * to errors if the shutdown process is interrupted.
00165      */
00166     if (softdevice_handler_sd_disable() != NRF_SUCCESS) {
00167         return BLE_STACK_BUSY;
00168     }
00169 
00170 
00171     /* Shutdown the BLE API and nRF51 glue code */
00172     ble_error_t error;
00173 
00174     if (gattServerInstance != NULL) {
00175         error = gattServerInstance->reset();
00176         if (error != BLE_ERROR_NONE) {
00177             return error;
00178         }
00179     }
00180 
00181     if (securityManagerInstance != NULL) {
00182         error = securityManagerInstance->reset();
00183         if (error != BLE_ERROR_NONE) {
00184             return error;
00185         }
00186     }
00187 
00188     /* S110 does not support BLE client features, nothing to reset. */
00189 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
00190     if (gattClientInstance != NULL) {
00191         error = gattClientInstance->reset();
00192         if (error != BLE_ERROR_NONE) {
00193             return error;
00194         }
00195     }
00196 #endif
00197 
00198     /* Gap instance is always present */
00199     error = gapInstance.reset();
00200     if (error != BLE_ERROR_NONE) {
00201         return error;
00202     }
00203 
00204     initialized = false;
00205     return BLE_ERROR_NONE;
00206 }
00207 
00208 void
00209 nRF5xn::waitForEvent(void)
00210 {
00211     sd_app_evt_wait();
00212 }