hr with 30102
Dependencies: BLE_API mbed X_NUCLEO_IDB0XA1
Fork of BLE_HeartRate by
nRF51822/TARGET_NRF5/source/nRF5xn.cpp@80:808f6f367f45, 2017-07-30 (annotated)
- Committer:
- mssarwar
- Date:
- Sun Jul 30 05:52:57 2017 +0000
- Revision:
- 80:808f6f367f45
with 30102;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mssarwar | 80:808f6f367f45 | 1 | /* mbed Microcontroller Library |
mssarwar | 80:808f6f367f45 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mssarwar | 80:808f6f367f45 | 3 | * |
mssarwar | 80:808f6f367f45 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mssarwar | 80:808f6f367f45 | 5 | * you may not use this file except in compliance with the License. |
mssarwar | 80:808f6f367f45 | 6 | * You may obtain a copy of the License at |
mssarwar | 80:808f6f367f45 | 7 | * |
mssarwar | 80:808f6f367f45 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mssarwar | 80:808f6f367f45 | 9 | * |
mssarwar | 80:808f6f367f45 | 10 | * Unless required by applicable law or agreed to in writing, software |
mssarwar | 80:808f6f367f45 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mssarwar | 80:808f6f367f45 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mssarwar | 80:808f6f367f45 | 13 | * See the License for the specific language governing permissions and |
mssarwar | 80:808f6f367f45 | 14 | * limitations under the License. |
mssarwar | 80:808f6f367f45 | 15 | */ |
mssarwar | 80:808f6f367f45 | 16 | |
mssarwar | 80:808f6f367f45 | 17 | #ifdef YOTTA_CFG_MBED_OS |
mssarwar | 80:808f6f367f45 | 18 | #include "mbed-drivers/mbed.h" |
mssarwar | 80:808f6f367f45 | 19 | #else |
mssarwar | 80:808f6f367f45 | 20 | #include "mbed.h" |
mssarwar | 80:808f6f367f45 | 21 | #endif |
mssarwar | 80:808f6f367f45 | 22 | #include "nRF5xn.h" |
mssarwar | 80:808f6f367f45 | 23 | #include "ble/blecommon.h" |
mssarwar | 80:808f6f367f45 | 24 | #include "nrf_soc.h" |
mssarwar | 80:808f6f367f45 | 25 | |
mssarwar | 80:808f6f367f45 | 26 | #include "btle/btle.h" |
mssarwar | 80:808f6f367f45 | 27 | #include "btle/custom/custom_helper.h" |
mssarwar | 80:808f6f367f45 | 28 | #include "nrf_delay.h" |
mssarwar | 80:808f6f367f45 | 29 | |
mssarwar | 80:808f6f367f45 | 30 | extern "C" { |
mssarwar | 80:808f6f367f45 | 31 | #include "softdevice_handler.h" |
mssarwar | 80:808f6f367f45 | 32 | } |
mssarwar | 80:808f6f367f45 | 33 | |
mssarwar | 80:808f6f367f45 | 34 | /** |
mssarwar | 80:808f6f367f45 | 35 | * The singleton which represents the nRF51822 transport for the BLE. |
mssarwar | 80:808f6f367f45 | 36 | */ |
mssarwar | 80:808f6f367f45 | 37 | static nRF5xn deviceInstance; |
mssarwar | 80:808f6f367f45 | 38 | |
mssarwar | 80:808f6f367f45 | 39 | /** |
mssarwar | 80:808f6f367f45 | 40 | * BLE-API requires an implementation of the following function in order to |
mssarwar | 80:808f6f367f45 | 41 | * obtain its transport handle. |
mssarwar | 80:808f6f367f45 | 42 | */ |
mssarwar | 80:808f6f367f45 | 43 | BLEInstanceBase * |
mssarwar | 80:808f6f367f45 | 44 | createBLEInstance(void) |
mssarwar | 80:808f6f367f45 | 45 | { |
mssarwar | 80:808f6f367f45 | 46 | return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE); |
mssarwar | 80:808f6f367f45 | 47 | } |
mssarwar | 80:808f6f367f45 | 48 | |
mssarwar | 80:808f6f367f45 | 49 | nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId) |
mssarwar | 80:808f6f367f45 | 50 | { |
mssarwar | 80:808f6f367f45 | 51 | return deviceInstance; |
mssarwar | 80:808f6f367f45 | 52 | } |
mssarwar | 80:808f6f367f45 | 53 | |
mssarwar | 80:808f6f367f45 | 54 | nRF5xn::nRF5xn(void) : |
mssarwar | 80:808f6f367f45 | 55 | initialized(false), |
mssarwar | 80:808f6f367f45 | 56 | instanceID(BLE::DEFAULT_INSTANCE), |
mssarwar | 80:808f6f367f45 | 57 | gapInstance(), |
mssarwar | 80:808f6f367f45 | 58 | gattServerInstance(NULL), |
mssarwar | 80:808f6f367f45 | 59 | gattClientInstance(NULL), |
mssarwar | 80:808f6f367f45 | 60 | securityManagerInstance(NULL) |
mssarwar | 80:808f6f367f45 | 61 | { |
mssarwar | 80:808f6f367f45 | 62 | } |
mssarwar | 80:808f6f367f45 | 63 | |
mssarwar | 80:808f6f367f45 | 64 | nRF5xn::~nRF5xn(void) |
mssarwar | 80:808f6f367f45 | 65 | { |
mssarwar | 80:808f6f367f45 | 66 | } |
mssarwar | 80:808f6f367f45 | 67 | |
mssarwar | 80:808f6f367f45 | 68 | const char *nRF5xn::getVersion(void) |
mssarwar | 80:808f6f367f45 | 69 | { |
mssarwar | 80:808f6f367f45 | 70 | if (!initialized) { |
mssarwar | 80:808f6f367f45 | 71 | return "INITIALIZATION_INCOMPLETE"; |
mssarwar | 80:808f6f367f45 | 72 | } |
mssarwar | 80:808f6f367f45 | 73 | |
mssarwar | 80:808f6f367f45 | 74 | static char versionString[32]; |
mssarwar | 80:808f6f367f45 | 75 | static bool versionFetched = false; |
mssarwar | 80:808f6f367f45 | 76 | |
mssarwar | 80:808f6f367f45 | 77 | if (!versionFetched) { |
mssarwar | 80:808f6f367f45 | 78 | ble_version_t version; |
mssarwar | 80:808f6f367f45 | 79 | if ((sd_ble_version_get(&version) == NRF_SUCCESS) && (version.company_id == 0x0059)) { |
mssarwar | 80:808f6f367f45 | 80 | switch (version.version_number) { |
mssarwar | 80:808f6f367f45 | 81 | case 0x07: |
mssarwar | 80:808f6f367f45 | 82 | case 0x08: |
mssarwar | 80:808f6f367f45 | 83 | snprintf(versionString, sizeof(versionString), "Nordic BLE4.1 ver:%u fw:%04x", version.version_number, version.subversion_number); |
mssarwar | 80:808f6f367f45 | 84 | break; |
mssarwar | 80:808f6f367f45 | 85 | default: |
mssarwar | 80:808f6f367f45 | 86 | snprintf(versionString, sizeof(versionString), "Nordic (spec unknown) ver:%u fw:%04x", version.version_number, version.subversion_number); |
mssarwar | 80:808f6f367f45 | 87 | break; |
mssarwar | 80:808f6f367f45 | 88 | } |
mssarwar | 80:808f6f367f45 | 89 | versionFetched = true; |
mssarwar | 80:808f6f367f45 | 90 | } else { |
mssarwar | 80:808f6f367f45 | 91 | strncpy(versionString, "unknown", sizeof(versionString)); |
mssarwar | 80:808f6f367f45 | 92 | } |
mssarwar | 80:808f6f367f45 | 93 | } |
mssarwar | 80:808f6f367f45 | 94 | |
mssarwar | 80:808f6f367f45 | 95 | return versionString; |
mssarwar | 80:808f6f367f45 | 96 | } |
mssarwar | 80:808f6f367f45 | 97 | |
mssarwar | 80:808f6f367f45 | 98 | /**************************************************************************/ |
mssarwar | 80:808f6f367f45 | 99 | /*! |
mssarwar | 80:808f6f367f45 | 100 | @brief Initialize the BLE stack. |
mssarwar | 80:808f6f367f45 | 101 | |
mssarwar | 80:808f6f367f45 | 102 | @returns ble_error_t |
mssarwar | 80:808f6f367f45 | 103 | |
mssarwar | 80:808f6f367f45 | 104 | @retval BLE_ERROR_NONE if everything executed properly and |
mssarwar | 80:808f6f367f45 | 105 | BLE_ERROR_ALREADY_INITIALIZED if the stack has already |
mssarwar | 80:808f6f367f45 | 106 | been initialized (possibly through a call to nRF5xn::init()). |
mssarwar | 80:808f6f367f45 | 107 | BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization |
mssarwar | 80:808f6f367f45 | 108 | of the internal stack (SoftDevice) failed. |
mssarwar | 80:808f6f367f45 | 109 | |
mssarwar | 80:808f6f367f45 | 110 | */ |
mssarwar | 80:808f6f367f45 | 111 | /**************************************************************************/ |
mssarwar | 80:808f6f367f45 | 112 | ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) |
mssarwar | 80:808f6f367f45 | 113 | { |
mssarwar | 80:808f6f367f45 | 114 | if (initialized) { |
mssarwar | 80:808f6f367f45 | 115 | BLE::InitializationCompleteCallbackContext context = { |
mssarwar | 80:808f6f367f45 | 116 | BLE::Instance(instanceID), |
mssarwar | 80:808f6f367f45 | 117 | BLE_ERROR_ALREADY_INITIALIZED |
mssarwar | 80:808f6f367f45 | 118 | }; |
mssarwar | 80:808f6f367f45 | 119 | callback.call(&context); |
mssarwar | 80:808f6f367f45 | 120 | return BLE_ERROR_ALREADY_INITIALIZED; |
mssarwar | 80:808f6f367f45 | 121 | } |
mssarwar | 80:808f6f367f45 | 122 | |
mssarwar | 80:808f6f367f45 | 123 | instanceID = instanceID; |
mssarwar | 80:808f6f367f45 | 124 | |
mssarwar | 80:808f6f367f45 | 125 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
mssarwar | 80:808f6f367f45 | 126 | if (btle_init() != ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 127 | return BLE_ERROR_INTERNAL_STACK_FAILURE; |
mssarwar | 80:808f6f367f45 | 128 | } |
mssarwar | 80:808f6f367f45 | 129 | |
mssarwar | 80:808f6f367f45 | 130 | initialized = true; |
mssarwar | 80:808f6f367f45 | 131 | BLE::InitializationCompleteCallbackContext context = { |
mssarwar | 80:808f6f367f45 | 132 | BLE::Instance(instanceID), |
mssarwar | 80:808f6f367f45 | 133 | BLE_ERROR_NONE |
mssarwar | 80:808f6f367f45 | 134 | }; |
mssarwar | 80:808f6f367f45 | 135 | callback.call(&context); |
mssarwar | 80:808f6f367f45 | 136 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 137 | } |
mssarwar | 80:808f6f367f45 | 138 | |
mssarwar | 80:808f6f367f45 | 139 | /**************************************************************************/ |
mssarwar | 80:808f6f367f45 | 140 | /*! |
mssarwar | 80:808f6f367f45 | 141 | @brief Purge the BLE stack of GATT and GAP state. |
mssarwar | 80:808f6f367f45 | 142 | |
mssarwar | 80:808f6f367f45 | 143 | @returns ble_error_t |
mssarwar | 80:808f6f367f45 | 144 | |
mssarwar | 80:808f6f367f45 | 145 | @retval BLE_ERROR_NONE |
mssarwar | 80:808f6f367f45 | 146 | Everything executed properly |
mssarwar | 80:808f6f367f45 | 147 | |
mssarwar | 80:808f6f367f45 | 148 | @note When using S110, GattClient::shutdown() will not be called |
mssarwar | 80:808f6f367f45 | 149 | since Gatt client features are not supported. |
mssarwar | 80:808f6f367f45 | 150 | */ |
mssarwar | 80:808f6f367f45 | 151 | /**************************************************************************/ |
mssarwar | 80:808f6f367f45 | 152 | ble_error_t nRF5xn::shutdown(void) |
mssarwar | 80:808f6f367f45 | 153 | { |
mssarwar | 80:808f6f367f45 | 154 | if (!initialized) { |
mssarwar | 80:808f6f367f45 | 155 | return BLE_ERROR_INITIALIZATION_INCOMPLETE; |
mssarwar | 80:808f6f367f45 | 156 | } |
mssarwar | 80:808f6f367f45 | 157 | |
mssarwar | 80:808f6f367f45 | 158 | /* |
mssarwar | 80:808f6f367f45 | 159 | * Shutdown the SoftDevice first. This is because we need to disable all |
mssarwar | 80:808f6f367f45 | 160 | * interrupts. Otherwise if we clear the BLE API and glue code first there |
mssarwar | 80:808f6f367f45 | 161 | * will be many NULL references and no config information which could lead |
mssarwar | 80:808f6f367f45 | 162 | * to errors if the shutdown process is interrupted. |
mssarwar | 80:808f6f367f45 | 163 | */ |
mssarwar | 80:808f6f367f45 | 164 | if (softdevice_handler_sd_disable() != NRF_SUCCESS) { |
mssarwar | 80:808f6f367f45 | 165 | return BLE_STACK_BUSY; |
mssarwar | 80:808f6f367f45 | 166 | } |
mssarwar | 80:808f6f367f45 | 167 | |
mssarwar | 80:808f6f367f45 | 168 | |
mssarwar | 80:808f6f367f45 | 169 | /* Shutdown the BLE API and nRF51 glue code */ |
mssarwar | 80:808f6f367f45 | 170 | ble_error_t error; |
mssarwar | 80:808f6f367f45 | 171 | |
mssarwar | 80:808f6f367f45 | 172 | if (gattServerInstance != NULL) { |
mssarwar | 80:808f6f367f45 | 173 | error = gattServerInstance->reset(); |
mssarwar | 80:808f6f367f45 | 174 | if (error != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 175 | return error; |
mssarwar | 80:808f6f367f45 | 176 | } |
mssarwar | 80:808f6f367f45 | 177 | } |
mssarwar | 80:808f6f367f45 | 178 | |
mssarwar | 80:808f6f367f45 | 179 | if (securityManagerInstance != NULL) { |
mssarwar | 80:808f6f367f45 | 180 | error = securityManagerInstance->reset(); |
mssarwar | 80:808f6f367f45 | 181 | if (error != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 182 | return error; |
mssarwar | 80:808f6f367f45 | 183 | } |
mssarwar | 80:808f6f367f45 | 184 | } |
mssarwar | 80:808f6f367f45 | 185 | |
mssarwar | 80:808f6f367f45 | 186 | /* S110 does not support BLE client features, nothing to reset. */ |
mssarwar | 80:808f6f367f45 | 187 | #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
mssarwar | 80:808f6f367f45 | 188 | if (gattClientInstance != NULL) { |
mssarwar | 80:808f6f367f45 | 189 | error = gattClientInstance->reset(); |
mssarwar | 80:808f6f367f45 | 190 | if (error != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 191 | return error; |
mssarwar | 80:808f6f367f45 | 192 | } |
mssarwar | 80:808f6f367f45 | 193 | } |
mssarwar | 80:808f6f367f45 | 194 | #endif |
mssarwar | 80:808f6f367f45 | 195 | |
mssarwar | 80:808f6f367f45 | 196 | /* Gap instance is always present */ |
mssarwar | 80:808f6f367f45 | 197 | error = gapInstance.reset(); |
mssarwar | 80:808f6f367f45 | 198 | if (error != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 199 | return error; |
mssarwar | 80:808f6f367f45 | 200 | } |
mssarwar | 80:808f6f367f45 | 201 | |
mssarwar | 80:808f6f367f45 | 202 | custom_reset_128bits_uuid_table(); |
mssarwar | 80:808f6f367f45 | 203 | |
mssarwar | 80:808f6f367f45 | 204 | initialized = false; |
mssarwar | 80:808f6f367f45 | 205 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 206 | } |
mssarwar | 80:808f6f367f45 | 207 | |
mssarwar | 80:808f6f367f45 | 208 | void |
mssarwar | 80:808f6f367f45 | 209 | nRF5xn::waitForEvent(void) |
mssarwar | 80:808f6f367f45 | 210 | { |
mssarwar | 80:808f6f367f45 | 211 | processEvents(); |
mssarwar | 80:808f6f367f45 | 212 | sd_app_evt_wait(); |
mssarwar | 80:808f6f367f45 | 213 | } |
mssarwar | 80:808f6f367f45 | 214 | |
mssarwar | 80:808f6f367f45 | 215 | void nRF5xn::processEvents() { |
mssarwar | 80:808f6f367f45 | 216 | if (isEventsSignaled) { |
mssarwar | 80:808f6f367f45 | 217 | isEventsSignaled = false; |
mssarwar | 80:808f6f367f45 | 218 | intern_softdevice_events_execute(); |
mssarwar | 80:808f6f367f45 | 219 | } |
mssarwar | 80:808f6f367f45 | 220 | } |