Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* mbed Microcontroller Library
switches 0:5c4d7b2438d3 2 * Copyright (c) 2006-2013 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 5 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 6 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 7 *
switches 0:5c4d7b2438d3 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 13 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 14 * limitations under the License.
switches 0:5c4d7b2438d3 15 */
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 #include "ble/BLE.h"
switches 0:5c4d7b2438d3 18 #include "ble/BLEInstanceBase.h"
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 #if defined(TARGET_OTA_ENABLED)
switches 0:5c4d7b2438d3 21 #include "ble/services/DFUService.h"
switches 0:5c4d7b2438d3 22 #endif
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 #ifdef YOTTA_CFG_MBED_OS
switches 0:5c4d7b2438d3 25 #include <minar/minar.h>
switches 0:5c4d7b2438d3 26 #endif
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 #if !defined(YOTTA_CFG_MBED_OS)
switches 0:5c4d7b2438d3 29 #include <mbed_error.h>
switches 0:5c4d7b2438d3 30 #include <toolchain.h>
switches 0:5c4d7b2438d3 31 #endif
switches 0:5c4d7b2438d3 32
switches 0:5c4d7b2438d3 33 ble_error_t
switches 0:5c4d7b2438d3 34 BLE::initImplementation(FunctionPointerWithContext<InitializationCompleteCallbackContext*> callback)
switches 0:5c4d7b2438d3 35 {
switches 0:5c4d7b2438d3 36 ble_error_t err = transport->init(instanceID, callback);
switches 0:5c4d7b2438d3 37 if (err != BLE_ERROR_NONE) {
switches 0:5c4d7b2438d3 38 return err;
switches 0:5c4d7b2438d3 39 }
switches 0:5c4d7b2438d3 40
switches 0:5c4d7b2438d3 41 /* Platforms enabled for DFU should introduce the DFU Service into
switches 0:5c4d7b2438d3 42 * applications automatically. */
switches 0:5c4d7b2438d3 43 #if defined(TARGET_OTA_ENABLED)
switches 0:5c4d7b2438d3 44 static DFUService dfu(*this); // defined static so that the object remains alive
switches 0:5c4d7b2438d3 45 #endif // TARGET_OTA_ENABLED
switches 0:5c4d7b2438d3 46
switches 0:5c4d7b2438d3 47 return BLE_ERROR_NONE;
switches 0:5c4d7b2438d3 48 }
switches 0:5c4d7b2438d3 49
switches 0:5c4d7b2438d3 50 /**
switches 0:5c4d7b2438d3 51 * BLE::Instance() and BLE constructor rely upon a static array of initializers
switches 0:5c4d7b2438d3 52 * to create actual BLE transport instances. A description of these instances
switches 0:5c4d7b2438d3 53 * and initializers is supposed to be put in some .json file contributing to
switches 0:5c4d7b2438d3 54 * yotta's configuration (typically in the target definition described by
switches 0:5c4d7b2438d3 55 * target.json). Here's a sample:
switches 0:5c4d7b2438d3 56 *
switches 0:5c4d7b2438d3 57 * "config": {
switches 0:5c4d7b2438d3 58 * ...
switches 0:5c4d7b2438d3 59 * "ble_instances": {
switches 0:5c4d7b2438d3 60 * "count": 1,
switches 0:5c4d7b2438d3 61 * "0" : {
switches 0:5c4d7b2438d3 62 * "initializer" : "createBLEInstance"
switches 0:5c4d7b2438d3 63 * }
switches 0:5c4d7b2438d3 64 * }
switches 0:5c4d7b2438d3 65 * ...
switches 0:5c4d7b2438d3 66 * }
switches 0:5c4d7b2438d3 67 *
switches 0:5c4d7b2438d3 68 * The following macros result in translating the above config into a static
switches 0:5c4d7b2438d3 69 * array: instanceConstructors.
switches 0:5c4d7b2438d3 70 */
switches 0:5c4d7b2438d3 71 #ifdef YOTTA_CFG_BLE_INSTANCES_COUNT
switches 0:5c4d7b2438d3 72 #define CONCATENATE(A, B) A ## B
switches 0:5c4d7b2438d3 73 #define EXPAND(X) X /* this adds a level of indirection needed to allow macro-expansion following a token-paste operation (see use of CONCATENATE() below). */
switches 0:5c4d7b2438d3 74
switches 0:5c4d7b2438d3 75 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1 YOTTA_CFG_BLE_INSTANCES_0_INITIALIZER
switches 0:5c4d7b2438d3 76 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_1, YOTTA_CFG_BLE_INSTANCES_1_INITIALIZER
switches 0:5c4d7b2438d3 77 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_2, YOTTA_CFG_BLE_INSTANCES_2_INITIALIZER
switches 0:5c4d7b2438d3 78 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_3, YOTTA_CFG_BLE_INSTANCES_3_INITIALIZER
switches 0:5c4d7b2438d3 79 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_5 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_4, YOTTA_CFG_BLE_INSTANCES_4_INITIALIZER
switches 0:5c4d7b2438d3 80 /* ... add more of the above if ever needed */
switches 0:5c4d7b2438d3 81
switches 0:5c4d7b2438d3 82 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(N) EXPAND(CONCATENATE(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS_, N))
switches 0:5c4d7b2438d3 83 #elif !defined(INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS)
switches 0:5c4d7b2438d3 84 /*
switches 0:5c4d7b2438d3 85 * The following applies when building without yotta. By default BLE_API provides
switches 0:5c4d7b2438d3 86 * a trivial initializer list containing a single constructor: createBLEInstance.
switches 0:5c4d7b2438d3 87 * This may be overridden.
switches 0:5c4d7b2438d3 88 */
switches 0:5c4d7b2438d3 89 #define INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS createBLEInstance
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 // yotta unlike mbed-cli has proper dependency mechanisms
switches 0:5c4d7b2438d3 92 // It is not required to defined a stub for createBLEInstance
switches 0:5c4d7b2438d3 93 #if !defined(YOTTA_CFG_MBED_OS)
switches 0:5c4d7b2438d3 94
switches 0:5c4d7b2438d3 95 // this stub is required by ARMCC otherwise link will systematically fail
switches 0:5c4d7b2438d3 96 MBED_WEAK BLEInstanceBase* createBLEInstance() {
switches 0:5c4d7b2438d3 97 error("Please provide an implementation for mbed BLE");
switches 0:5c4d7b2438d3 98 return NULL;
switches 0:5c4d7b2438d3 99 }
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 #endif
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103
switches 0:5c4d7b2438d3 104 #endif /* YOTTA_CFG_BLE_INSTANCES_COUNT */
switches 0:5c4d7b2438d3 105
switches 0:5c4d7b2438d3 106 typedef BLEInstanceBase *(*InstanceConstructor_t)(void);
switches 0:5c4d7b2438d3 107 static const InstanceConstructor_t instanceConstructors[BLE::NUM_INSTANCES] = {
switches 0:5c4d7b2438d3 108 #ifndef YOTTA_CFG_BLE_INSTANCES_COUNT
switches 0:5c4d7b2438d3 109 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS
switches 0:5c4d7b2438d3 110 #else
switches 0:5c4d7b2438d3 111 INITIALIZER_LIST_FOR_INSTANCE_CONSTRUCTORS(YOTTA_CFG_BLE_INSTANCES_COUNT)
switches 0:5c4d7b2438d3 112 #endif
switches 0:5c4d7b2438d3 113 };
switches 0:5c4d7b2438d3 114
switches 0:5c4d7b2438d3 115 BLE &
switches 0:5c4d7b2438d3 116 BLE::Instance(InstanceID_t id)
switches 0:5c4d7b2438d3 117 {
switches 0:5c4d7b2438d3 118 static BLE *singletons[NUM_INSTANCES];
switches 0:5c4d7b2438d3 119 if (id < NUM_INSTANCES) {
switches 0:5c4d7b2438d3 120 if (singletons[id] == NULL) {
switches 0:5c4d7b2438d3 121 singletons[id] = new BLE(id); /* This object will never be freed. */
switches 0:5c4d7b2438d3 122 }
switches 0:5c4d7b2438d3 123
switches 0:5c4d7b2438d3 124 return *singletons[id];
switches 0:5c4d7b2438d3 125 }
switches 0:5c4d7b2438d3 126
switches 0:5c4d7b2438d3 127 /* we come here only in the case of a bad interfaceID. */
switches 0:5c4d7b2438d3 128 static BLE badSingleton(NUM_INSTANCES /* this is a bad index; and will result in a NULL transport. */);
switches 0:5c4d7b2438d3 129 return badSingleton;
switches 0:5c4d7b2438d3 130 }
switches 0:5c4d7b2438d3 131
switches 0:5c4d7b2438d3 132 #ifdef YOTTA_CFG_MBED_OS
switches 0:5c4d7b2438d3 133 void defaultSchedulingCallback(BLE::OnEventsToProcessCallbackContext* params) {
switches 0:5c4d7b2438d3 134 minar::Scheduler::postCallback(&params->ble, &BLE::processEvents);
switches 0:5c4d7b2438d3 135 }
switches 0:5c4d7b2438d3 136 #else
switches 0:5c4d7b2438d3 137 #define defaultSchedulingCallback NULL
switches 0:5c4d7b2438d3 138 #endif
switches 0:5c4d7b2438d3 139
switches 0:5c4d7b2438d3 140
switches 0:5c4d7b2438d3 141 BLE::BLE(InstanceID_t instanceIDIn) : instanceID(instanceIDIn), transport(),
switches 0:5c4d7b2438d3 142 whenEventsToProcess(defaultSchedulingCallback)
switches 0:5c4d7b2438d3 143 {
switches 0:5c4d7b2438d3 144 static BLEInstanceBase *transportInstances[NUM_INSTANCES];
switches 0:5c4d7b2438d3 145
switches 0:5c4d7b2438d3 146 if (instanceID < NUM_INSTANCES) {
switches 0:5c4d7b2438d3 147 if (!transportInstances[instanceID]) {
switches 0:5c4d7b2438d3 148 transportInstances[instanceID] = instanceConstructors[instanceID](); /* Call the stack's initializer for the transport object. */
switches 0:5c4d7b2438d3 149 }
switches 0:5c4d7b2438d3 150 transport = transportInstances[instanceID];
switches 0:5c4d7b2438d3 151 } else {
switches 0:5c4d7b2438d3 152 transport = NULL;
switches 0:5c4d7b2438d3 153 }
switches 0:5c4d7b2438d3 154 }
switches 0:5c4d7b2438d3 155
switches 0:5c4d7b2438d3 156 bool BLE::hasInitialized(void) const
switches 0:5c4d7b2438d3 157 {
switches 0:5c4d7b2438d3 158 if (!transport) {
switches 0:5c4d7b2438d3 159 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 160 }
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 return transport->hasInitialized();
switches 0:5c4d7b2438d3 163 }
switches 0:5c4d7b2438d3 164
switches 0:5c4d7b2438d3 165 ble_error_t BLE::shutdown(void)
switches 0:5c4d7b2438d3 166 {
switches 0:5c4d7b2438d3 167 if (!transport) {
switches 0:5c4d7b2438d3 168 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 169 }
switches 0:5c4d7b2438d3 170
switches 0:5c4d7b2438d3 171 return transport->shutdown();
switches 0:5c4d7b2438d3 172 }
switches 0:5c4d7b2438d3 173
switches 0:5c4d7b2438d3 174 const char *BLE::getVersion(void)
switches 0:5c4d7b2438d3 175 {
switches 0:5c4d7b2438d3 176 if (!transport) {
switches 0:5c4d7b2438d3 177 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 178 }
switches 0:5c4d7b2438d3 179
switches 0:5c4d7b2438d3 180 return transport->getVersion();
switches 0:5c4d7b2438d3 181 }
switches 0:5c4d7b2438d3 182
switches 0:5c4d7b2438d3 183 const Gap &BLE::gap() const
switches 0:5c4d7b2438d3 184 {
switches 0:5c4d7b2438d3 185 if (!transport) {
switches 0:5c4d7b2438d3 186 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 187 }
switches 0:5c4d7b2438d3 188
switches 0:5c4d7b2438d3 189 return transport->getGap();
switches 0:5c4d7b2438d3 190 }
switches 0:5c4d7b2438d3 191
switches 0:5c4d7b2438d3 192 Gap &BLE::gap()
switches 0:5c4d7b2438d3 193 {
switches 0:5c4d7b2438d3 194 if (!transport) {
switches 0:5c4d7b2438d3 195 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 196 }
switches 0:5c4d7b2438d3 197
switches 0:5c4d7b2438d3 198 return transport->getGap();
switches 0:5c4d7b2438d3 199 }
switches 0:5c4d7b2438d3 200
switches 0:5c4d7b2438d3 201 const GattServer& BLE::gattServer() const
switches 0:5c4d7b2438d3 202 {
switches 0:5c4d7b2438d3 203 if (!transport) {
switches 0:5c4d7b2438d3 204 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 205 }
switches 0:5c4d7b2438d3 206
switches 0:5c4d7b2438d3 207 return transport->getGattServer();
switches 0:5c4d7b2438d3 208 }
switches 0:5c4d7b2438d3 209
switches 0:5c4d7b2438d3 210 GattServer& BLE::gattServer()
switches 0:5c4d7b2438d3 211 {
switches 0:5c4d7b2438d3 212 if (!transport) {
switches 0:5c4d7b2438d3 213 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 214 }
switches 0:5c4d7b2438d3 215
switches 0:5c4d7b2438d3 216 return transport->getGattServer();
switches 0:5c4d7b2438d3 217 }
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 const GattClient& BLE::gattClient() const
switches 0:5c4d7b2438d3 220 {
switches 0:5c4d7b2438d3 221 if (!transport) {
switches 0:5c4d7b2438d3 222 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 223 }
switches 0:5c4d7b2438d3 224
switches 0:5c4d7b2438d3 225 return transport->getGattClient();
switches 0:5c4d7b2438d3 226 }
switches 0:5c4d7b2438d3 227
switches 0:5c4d7b2438d3 228 GattClient& BLE::gattClient()
switches 0:5c4d7b2438d3 229 {
switches 0:5c4d7b2438d3 230 if (!transport) {
switches 0:5c4d7b2438d3 231 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 232 }
switches 0:5c4d7b2438d3 233
switches 0:5c4d7b2438d3 234 return transport->getGattClient();
switches 0:5c4d7b2438d3 235 }
switches 0:5c4d7b2438d3 236
switches 0:5c4d7b2438d3 237 const SecurityManager& BLE::securityManager() const
switches 0:5c4d7b2438d3 238 {
switches 0:5c4d7b2438d3 239 if (!transport) {
switches 0:5c4d7b2438d3 240 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 241 }
switches 0:5c4d7b2438d3 242
switches 0:5c4d7b2438d3 243 return transport->getSecurityManager();
switches 0:5c4d7b2438d3 244 }
switches 0:5c4d7b2438d3 245
switches 0:5c4d7b2438d3 246 SecurityManager& BLE::securityManager()
switches 0:5c4d7b2438d3 247 {
switches 0:5c4d7b2438d3 248 if (!transport) {
switches 0:5c4d7b2438d3 249 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 250 }
switches 0:5c4d7b2438d3 251
switches 0:5c4d7b2438d3 252 return transport->getSecurityManager();
switches 0:5c4d7b2438d3 253 }
switches 0:5c4d7b2438d3 254
switches 0:5c4d7b2438d3 255 void BLE::waitForEvent(void)
switches 0:5c4d7b2438d3 256 {
switches 0:5c4d7b2438d3 257 if (!transport) {
switches 0:5c4d7b2438d3 258 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 259 }
switches 0:5c4d7b2438d3 260
switches 0:5c4d7b2438d3 261 transport->waitForEvent();
switches 0:5c4d7b2438d3 262 }
switches 0:5c4d7b2438d3 263
switches 0:5c4d7b2438d3 264 void BLE::processEvents()
switches 0:5c4d7b2438d3 265 {
switches 0:5c4d7b2438d3 266 if (!transport) {
switches 0:5c4d7b2438d3 267 error("bad handle to underlying transport");
switches 0:5c4d7b2438d3 268 }
switches 0:5c4d7b2438d3 269
switches 0:5c4d7b2438d3 270 transport->processEvents();
switches 0:5c4d7b2438d3 271 }
switches 0:5c4d7b2438d3 272
switches 0:5c4d7b2438d3 273 void BLE::onEventsToProcess(const BLE::OnEventsToProcessCallback_t& callback)
switches 0:5c4d7b2438d3 274 {
switches 0:5c4d7b2438d3 275 whenEventsToProcess = callback;
switches 0:5c4d7b2438d3 276 }
switches 0:5c4d7b2438d3 277
switches 0:5c4d7b2438d3 278 void BLE::signalEventsToProcess()
switches 0:5c4d7b2438d3 279 {
switches 0:5c4d7b2438d3 280 if (whenEventsToProcess) {
switches 0:5c4d7b2438d3 281 OnEventsToProcessCallbackContext params = {
switches 0:5c4d7b2438d3 282 *this
switches 0:5c4d7b2438d3 283 };
switches 0:5c4d7b2438d3 284 whenEventsToProcess(&params);
switches 0:5c4d7b2438d3 285 }
switches 0:5c4d7b2438d3 286 }