Qi Yao / LinkNode---test123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

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 #include "mbed.h"
00018 #include "nRF5xn.h"
00019 #include "ble/blecommon.h"
00020 #include "nrf_soc.h"
00021 
00022 #include "btle/btle.h"
00023 #include "nrf_delay.h"
00024 
00025 extern "C" {
00026 #include "softdevice_handler.h "
00027 }
00028 
00029 /**
00030  * The singleton which represents the nRF51822 transport for the BLE.
00031  */
00032 static nRF5xn deviceInstance;
00033 
00034 /**
00035  * BLE-API requires an implementation of the following function in order to
00036  * obtain its transport handle.
00037  */
00038 BLEInstanceBase *
00039 createBLEInstance(void)
00040 {
00041     return (&deviceInstance);
00042 }
00043 
00044 nRF5xn::nRF5xn(void) : initialized(false), instanceID(BLE::DEFAULT_INSTANCE)
00045 {
00046 }
00047 
00048 nRF5xn::~nRF5xn(void)
00049 {
00050 }
00051 
00052 const char *nRF5xn::getVersion(void)
00053 {
00054     if (!initialized) {
00055         return "INITIALIZATION_INCOMPLETE";
00056     }
00057 
00058     static char versionString[32];
00059     static bool versionFetched = false;
00060 
00061     if (!versionFetched) {
00062         ble_version_t version;
00063         if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) {
00064             switch (version.version_number) {
00065                 case 0x07:
00066                 case 0x08:
00067                     snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number);
00068                     break;
00069                 default:
00070                     snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number);
00071                     break;
00072             }
00073             versionFetched = true;
00074         } else {
00075             strncpy(versionString, "unknown", sizeof(versionString));
00076         }
00077     }
00078 
00079     return versionString;
00080 }
00081 
00082 ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback)
00083 {
00084     if (initialized) {
00085         BLE::InitializationCompleteCallbackContext context = {
00086             BLE::Instance(instanceID),
00087             BLE_ERROR_ALREADY_INITIALIZED
00088         };
00089         callback.call(&context);
00090         return BLE_ERROR_ALREADY_INITIALIZED;
00091     }
00092 
00093     instanceID   = instanceID;
00094 
00095     /* ToDo: Clear memory contents, reset the SD, etc. */
00096     btle_init();
00097 
00098     initialized = true;
00099     BLE::InitializationCompleteCallbackContext context = {
00100         BLE::Instance(instanceID),
00101         BLE_ERROR_NONE
00102     };
00103     callback.call(&context);
00104     return BLE_ERROR_NONE;
00105 }
00106 
00107 ble_error_t nRF5xn::shutdown(void)
00108 {
00109     if (!initialized) {
00110         return BLE_ERROR_INITIALIZATION_INCOMPLETE;
00111     }
00112 
00113     if(softdevice_handler_sd_disable() != NRF_SUCCESS) {
00114         return BLE_STACK_BUSY;
00115     }
00116 
00117     initialized = false;
00118     return BLE_ERROR_NONE;
00119 }
00120 
00121 void
00122 nRF5xn::waitForEvent(void)
00123 {
00124     sd_app_evt_wait();
00125 }