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