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/DiscoveredCharacteristic.h"
switches 0:5c4d7b2438d3 18 #include "ble/GattClient.h"
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 ble_error_t
switches 0:5c4d7b2438d3 21 DiscoveredCharacteristic::read(uint16_t offset) const
switches 0:5c4d7b2438d3 22 {
switches 0:5c4d7b2438d3 23 if (!props.read()) {
switches 0:5c4d7b2438d3 24 return BLE_ERROR_OPERATION_NOT_PERMITTED;
switches 0:5c4d7b2438d3 25 }
switches 0:5c4d7b2438d3 26
switches 0:5c4d7b2438d3 27 if (!gattc) {
switches 0:5c4d7b2438d3 28 return BLE_ERROR_INVALID_STATE;
switches 0:5c4d7b2438d3 29 }
switches 0:5c4d7b2438d3 30
switches 0:5c4d7b2438d3 31 return gattc->read(connHandle, valueHandle, offset);
switches 0:5c4d7b2438d3 32 }
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 struct OneShotReadCallback {
switches 0:5c4d7b2438d3 35 static void launch(GattClient* client, Gap::Handle_t connHandle,
switches 0:5c4d7b2438d3 36 GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) {
switches 0:5c4d7b2438d3 37 OneShotReadCallback* oneShot = new OneShotReadCallback(client, connHandle, handle, cb);
switches 0:5c4d7b2438d3 38 oneShot->attach();
switches 0:5c4d7b2438d3 39 // delete will be made when this callback is called
switches 0:5c4d7b2438d3 40 }
switches 0:5c4d7b2438d3 41
switches 0:5c4d7b2438d3 42 private:
switches 0:5c4d7b2438d3 43 OneShotReadCallback(GattClient* client, Gap::Handle_t connHandle,
switches 0:5c4d7b2438d3 44 GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) :
switches 0:5c4d7b2438d3 45 _client(client),
switches 0:5c4d7b2438d3 46 _connHandle(connHandle),
switches 0:5c4d7b2438d3 47 _handle(handle),
switches 0:5c4d7b2438d3 48 _callback(cb) { }
switches 0:5c4d7b2438d3 49
switches 0:5c4d7b2438d3 50 void attach() {
switches 0:5c4d7b2438d3 51 _client->onDataRead(makeFunctionPointer(this, &OneShotReadCallback::call));
switches 0:5c4d7b2438d3 52 }
switches 0:5c4d7b2438d3 53
switches 0:5c4d7b2438d3 54 void call(const GattReadCallbackParams* params) {
switches 0:5c4d7b2438d3 55 // verifiy that it is the right characteristic on the right connection
switches 0:5c4d7b2438d3 56 if (params->connHandle == _connHandle && params->handle == _handle) {
switches 0:5c4d7b2438d3 57 _callback(params);
switches 0:5c4d7b2438d3 58 _client->onDataRead().detach(makeFunctionPointer(this, &OneShotReadCallback::call));
switches 0:5c4d7b2438d3 59 delete this;
switches 0:5c4d7b2438d3 60 }
switches 0:5c4d7b2438d3 61 }
switches 0:5c4d7b2438d3 62
switches 0:5c4d7b2438d3 63 GattClient* _client;
switches 0:5c4d7b2438d3 64 Gap::Handle_t _connHandle;
switches 0:5c4d7b2438d3 65 GattAttribute::Handle_t _handle;
switches 0:5c4d7b2438d3 66 GattClient::ReadCallback_t _callback;
switches 0:5c4d7b2438d3 67 };
switches 0:5c4d7b2438d3 68
switches 0:5c4d7b2438d3 69 ble_error_t DiscoveredCharacteristic::read(uint16_t offset, const GattClient::ReadCallback_t& onRead) const {
switches 0:5c4d7b2438d3 70 ble_error_t error = read(offset);
switches 0:5c4d7b2438d3 71 if (error) {
switches 0:5c4d7b2438d3 72 return error;
switches 0:5c4d7b2438d3 73 }
switches 0:5c4d7b2438d3 74
switches 0:5c4d7b2438d3 75 OneShotReadCallback::launch(gattc, connHandle, valueHandle, onRead);
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 return error;
switches 0:5c4d7b2438d3 78 }
switches 0:5c4d7b2438d3 79
switches 0:5c4d7b2438d3 80 ble_error_t
switches 0:5c4d7b2438d3 81 DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value) const
switches 0:5c4d7b2438d3 82 {
switches 0:5c4d7b2438d3 83 if (!props.write()) {
switches 0:5c4d7b2438d3 84 return BLE_ERROR_OPERATION_NOT_PERMITTED;
switches 0:5c4d7b2438d3 85 }
switches 0:5c4d7b2438d3 86
switches 0:5c4d7b2438d3 87 if (!gattc) {
switches 0:5c4d7b2438d3 88 return BLE_ERROR_INVALID_STATE;
switches 0:5c4d7b2438d3 89 }
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 return gattc->write(GattClient::GATT_OP_WRITE_REQ, connHandle, valueHandle, length, value);
switches 0:5c4d7b2438d3 92 }
switches 0:5c4d7b2438d3 93
switches 0:5c4d7b2438d3 94 ble_error_t
switches 0:5c4d7b2438d3 95 DiscoveredCharacteristic::writeWoResponse(uint16_t length, const uint8_t *value) const
switches 0:5c4d7b2438d3 96 {
switches 0:5c4d7b2438d3 97 if (!props.writeWoResp()) {
switches 0:5c4d7b2438d3 98 return BLE_ERROR_OPERATION_NOT_PERMITTED;
switches 0:5c4d7b2438d3 99 }
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 if (!gattc) {
switches 0:5c4d7b2438d3 102 return BLE_ERROR_INVALID_STATE;
switches 0:5c4d7b2438d3 103 }
switches 0:5c4d7b2438d3 104
switches 0:5c4d7b2438d3 105 return gattc->write(GattClient::GATT_OP_WRITE_CMD, connHandle, valueHandle, length, value);
switches 0:5c4d7b2438d3 106 }
switches 0:5c4d7b2438d3 107
switches 0:5c4d7b2438d3 108 struct OneShotWriteCallback {
switches 0:5c4d7b2438d3 109 static void launch(GattClient* client, Gap::Handle_t connHandle,
switches 0:5c4d7b2438d3 110 GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) {
switches 0:5c4d7b2438d3 111 OneShotWriteCallback* oneShot = new OneShotWriteCallback(client, connHandle, handle, cb);
switches 0:5c4d7b2438d3 112 oneShot->attach();
switches 0:5c4d7b2438d3 113 // delete will be made when this callback is called
switches 0:5c4d7b2438d3 114 }
switches 0:5c4d7b2438d3 115
switches 0:5c4d7b2438d3 116 private:
switches 0:5c4d7b2438d3 117 OneShotWriteCallback(GattClient* client, Gap::Handle_t connHandle,
switches 0:5c4d7b2438d3 118 GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) :
switches 0:5c4d7b2438d3 119 _client(client),
switches 0:5c4d7b2438d3 120 _connHandle(connHandle),
switches 0:5c4d7b2438d3 121 _handle(handle),
switches 0:5c4d7b2438d3 122 _callback(cb) { }
switches 0:5c4d7b2438d3 123
switches 0:5c4d7b2438d3 124 void attach() {
switches 0:5c4d7b2438d3 125 _client->onDataWritten(makeFunctionPointer(this, &OneShotWriteCallback::call));
switches 0:5c4d7b2438d3 126 }
switches 0:5c4d7b2438d3 127
switches 0:5c4d7b2438d3 128 void call(const GattWriteCallbackParams* params) {
switches 0:5c4d7b2438d3 129 // verifiy that it is the right characteristic on the right connection
switches 0:5c4d7b2438d3 130 if (params->connHandle == _connHandle && params->handle == _handle) {
switches 0:5c4d7b2438d3 131 _callback(params);
switches 0:5c4d7b2438d3 132 _client->onDataWritten().detach(makeFunctionPointer(this, &OneShotWriteCallback::call));
switches 0:5c4d7b2438d3 133 delete this;
switches 0:5c4d7b2438d3 134 }
switches 0:5c4d7b2438d3 135 }
switches 0:5c4d7b2438d3 136
switches 0:5c4d7b2438d3 137 GattClient* _client;
switches 0:5c4d7b2438d3 138 Gap::Handle_t _connHandle;
switches 0:5c4d7b2438d3 139 GattAttribute::Handle_t _handle;
switches 0:5c4d7b2438d3 140 GattClient::WriteCallback_t _callback;
switches 0:5c4d7b2438d3 141 };
switches 0:5c4d7b2438d3 142
switches 0:5c4d7b2438d3 143 ble_error_t DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value, const GattClient::WriteCallback_t& onRead) const {
switches 0:5c4d7b2438d3 144 ble_error_t error = write(length, value);
switches 0:5c4d7b2438d3 145 if (error) {
switches 0:5c4d7b2438d3 146 return error;
switches 0:5c4d7b2438d3 147 }
switches 0:5c4d7b2438d3 148
switches 0:5c4d7b2438d3 149 OneShotWriteCallback::launch(gattc, connHandle, valueHandle, onRead);
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 return error;
switches 0:5c4d7b2438d3 152 }
switches 0:5c4d7b2438d3 153
switches 0:5c4d7b2438d3 154 ble_error_t DiscoveredCharacteristic::discoverDescriptors(
switches 0:5c4d7b2438d3 155 const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& onCharacteristicDiscovered,
switches 0:5c4d7b2438d3 156 const CharacteristicDescriptorDiscovery::TerminationCallback_t& onTermination) const {
switches 0:5c4d7b2438d3 157
switches 0:5c4d7b2438d3 158 if(!gattc) {
switches 0:5c4d7b2438d3 159 return BLE_ERROR_INVALID_STATE;
switches 0:5c4d7b2438d3 160 }
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 ble_error_t err = gattc->discoverCharacteristicDescriptors(
switches 0:5c4d7b2438d3 163 *this, onCharacteristicDiscovered, onTermination
switches 0:5c4d7b2438d3 164 );
switches 0:5c4d7b2438d3 165
switches 0:5c4d7b2438d3 166 return err;
switches 0:5c4d7b2438d3 167 }