Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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