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 #include "rpc.h"
switches 0:5c4d7b2438d3 17
switches 0:5c4d7b2438d3 18 using namespace std;
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 namespace mbed {
switches 0:5c4d7b2438d3 21
switches 0:5c4d7b2438d3 22 RPC::RPC(const char *name) {
switches 0:5c4d7b2438d3 23 _from_construct = false;
switches 0:5c4d7b2438d3 24 if (name != NULL) {
switches 0:5c4d7b2438d3 25 _name = new char[strlen(name) + 1];
switches 0:5c4d7b2438d3 26 strcpy(_name, name);
switches 0:5c4d7b2438d3 27 } else {
switches 0:5c4d7b2438d3 28 _name = new char[12];
switches 0:5c4d7b2438d3 29 sprintf(_name, "obj%p", this);
switches 0:5c4d7b2438d3 30 }
switches 0:5c4d7b2438d3 31 // put this object at head of the list
switches 0:5c4d7b2438d3 32 _next = _head;
switches 0:5c4d7b2438d3 33 _head = this;
switches 0:5c4d7b2438d3 34 }
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 RPC::~RPC() {
switches 0:5c4d7b2438d3 37 // remove this object from the list
switches 0:5c4d7b2438d3 38 if (_head == this) { // first in the list, so just drop me
switches 0:5c4d7b2438d3 39 _head = _next;
switches 0:5c4d7b2438d3 40 } else { // find the object before me, then drop me
switches 0:5c4d7b2438d3 41 RPC* p = _head;
switches 0:5c4d7b2438d3 42 while (p->_next != this) {
switches 0:5c4d7b2438d3 43 p = p->_next;
switches 0:5c4d7b2438d3 44 }
switches 0:5c4d7b2438d3 45 p->_next = _next;
switches 0:5c4d7b2438d3 46 }
switches 0:5c4d7b2438d3 47 }
switches 0:5c4d7b2438d3 48
switches 0:5c4d7b2438d3 49 const rpc_method *RPC::get_rpc_methods() {
switches 0:5c4d7b2438d3 50 static const rpc_method methods[] = {
switches 0:5c4d7b2438d3 51 {"delete", rpc_method_caller<RPC, &RPC::delete_self> },
switches 0:5c4d7b2438d3 52 RPC_METHOD_END
switches 0:5c4d7b2438d3 53 };
switches 0:5c4d7b2438d3 54 return methods;
switches 0:5c4d7b2438d3 55 }
switches 0:5c4d7b2438d3 56
switches 0:5c4d7b2438d3 57 RPC *RPC::lookup(const char *name) {
switches 0:5c4d7b2438d3 58 size_t len = strlen(name);
switches 0:5c4d7b2438d3 59 for (RPC *p = _head; p != NULL; p = p->_next) {
switches 0:5c4d7b2438d3 60 /* Check that p->_name matches name and is the correct length */
switches 0:5c4d7b2438d3 61 if (strncmp(p->_name, name, len) == 0 && (strlen(p->_name) == len)) {
switches 0:5c4d7b2438d3 62 return p;
switches 0:5c4d7b2438d3 63 }
switches 0:5c4d7b2438d3 64 }
switches 0:5c4d7b2438d3 65 return NULL;
switches 0:5c4d7b2438d3 66 }
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 void RPC::delete_self() {
switches 0:5c4d7b2438d3 69 delete[] _name;
switches 0:5c4d7b2438d3 70 if (_from_construct) {
switches 0:5c4d7b2438d3 71 delete this;
switches 0:5c4d7b2438d3 72 }
switches 0:5c4d7b2438d3 73 }
switches 0:5c4d7b2438d3 74
switches 0:5c4d7b2438d3 75 void RPC::list_objs(Arguments *args, Reply *result) {
switches 0:5c4d7b2438d3 76 for (RPC *ptr = RPC::_head; ptr != NULL; ptr = ptr->_next) {
switches 0:5c4d7b2438d3 77 if (ptr->_from_construct) {
switches 0:5c4d7b2438d3 78 result->putData<const char*>(ptr->_name);
switches 0:5c4d7b2438d3 79 }
switches 0:5c4d7b2438d3 80 }
switches 0:5c4d7b2438d3 81 }
switches 0:5c4d7b2438d3 82
switches 0:5c4d7b2438d3 83 void RPC::clear(Arguments*, Reply*) {
switches 0:5c4d7b2438d3 84 RPC *ptr = RPC::_head;
switches 0:5c4d7b2438d3 85 while (ptr != NULL) {
switches 0:5c4d7b2438d3 86 RPC *tmp = ptr;
switches 0:5c4d7b2438d3 87 ptr = ptr->_next;
switches 0:5c4d7b2438d3 88 delete[] tmp->_name;
switches 0:5c4d7b2438d3 89 if (tmp->_from_construct) {
switches 0:5c4d7b2438d3 90 delete tmp;
switches 0:5c4d7b2438d3 91 }
switches 0:5c4d7b2438d3 92 }
switches 0:5c4d7b2438d3 93 }
switches 0:5c4d7b2438d3 94
switches 0:5c4d7b2438d3 95 const rpc_function RPC::_RPC_funcs[] = {
switches 0:5c4d7b2438d3 96 {"clear", &RPC::clear },
switches 0:5c4d7b2438d3 97 { "objects", &RPC::list_objs },
switches 0:5c4d7b2438d3 98 RPC_METHOD_END
switches 0:5c4d7b2438d3 99 };
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 rpc_class RPC::_RPC_class = { "RPC", _RPC_funcs, NULL };
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103 RPC *RPC::_head = NULL;
switches 0:5c4d7b2438d3 104
switches 0:5c4d7b2438d3 105 rpc_class *RPC::_classes = &_RPC_class;
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 bool RPC::call(const char *request, char *reply) {
switches 0:5c4d7b2438d3 108 if (request == NULL) return false;
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 Arguments args(request);
switches 0:5c4d7b2438d3 111 Reply r(reply);
switches 0:5c4d7b2438d3 112
switches 0:5c4d7b2438d3 113 /* If there's no name print object and class names to result */
switches 0:5c4d7b2438d3 114 if (args.obj_name == NULL) {
switches 0:5c4d7b2438d3 115 for (RPC *p = RPC::_head; p != NULL; p = p->_next) {
switches 0:5c4d7b2438d3 116 r.putData<const char*>(p->_name);
switches 0:5c4d7b2438d3 117 }
switches 0:5c4d7b2438d3 118 for (rpc_class *c = RPC::_classes; c != NULL; c = c->next) {
switches 0:5c4d7b2438d3 119 r.putData<const char*>(c->name);
switches 0:5c4d7b2438d3 120 }
switches 0:5c4d7b2438d3 121 return true;
switches 0:5c4d7b2438d3 122 }
switches 0:5c4d7b2438d3 123
switches 0:5c4d7b2438d3 124 /* First try matching an instance */
switches 0:5c4d7b2438d3 125 RPC *p = lookup(args.obj_name);
switches 0:5c4d7b2438d3 126 if (p != NULL) {
switches 0:5c4d7b2438d3 127 /* Get the list of methods we support */
switches 0:5c4d7b2438d3 128 const rpc_method *cur_method = p->get_rpc_methods();
switches 0:5c4d7b2438d3 129
switches 0:5c4d7b2438d3 130 /* When there's no method print method names to result */
switches 0:5c4d7b2438d3 131 if (args.method_name == NULL) {
switches 0:5c4d7b2438d3 132 while (true) {
switches 0:5c4d7b2438d3 133 for (; cur_method->name != NULL; cur_method++) {
switches 0:5c4d7b2438d3 134 r.putData<const char*>(cur_method->name);
switches 0:5c4d7b2438d3 135 }
switches 0:5c4d7b2438d3 136
switches 0:5c4d7b2438d3 137 /* write_name_arr's args are references, so result and cur_method will have changed */
switches 0:5c4d7b2438d3 138 if (cur_method->super != 0) {
switches 0:5c4d7b2438d3 139 cur_method = cur_method->super(p);
switches 0:5c4d7b2438d3 140 } else {
switches 0:5c4d7b2438d3 141 return true;
switches 0:5c4d7b2438d3 142 }
switches 0:5c4d7b2438d3 143 }
switches 0:5c4d7b2438d3 144 }
switches 0:5c4d7b2438d3 145
switches 0:5c4d7b2438d3 146 /* Look through the methods for the one whose name matches */
switches 0:5c4d7b2438d3 147 while (true) {
switches 0:5c4d7b2438d3 148 for (; cur_method->name != NULL; cur_method++) {
switches 0:5c4d7b2438d3 149 if (strcmp(cur_method->name, args.method_name) == 0) {
switches 0:5c4d7b2438d3 150 (cur_method->method_caller)(p, &args, &r);
switches 0:5c4d7b2438d3 151 return true;
switches 0:5c4d7b2438d3 152 }
switches 0:5c4d7b2438d3 153 }
switches 0:5c4d7b2438d3 154
switches 0:5c4d7b2438d3 155 if (cur_method->super != 0) {
switches 0:5c4d7b2438d3 156 cur_method = cur_method->super(p);
switches 0:5c4d7b2438d3 157 } else {
switches 0:5c4d7b2438d3 158 /* end of methods and no match */
switches 0:5c4d7b2438d3 159 return false;
switches 0:5c4d7b2438d3 160 }
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 }
switches 0:5c4d7b2438d3 163 }
switches 0:5c4d7b2438d3 164
switches 0:5c4d7b2438d3 165 /* Then try a class */
switches 0:5c4d7b2438d3 166 for (const rpc_class *q = _classes; q != NULL; q = q->next) {
switches 0:5c4d7b2438d3 167 if (strcmp(q->name, args.obj_name) == 0) {
switches 0:5c4d7b2438d3 168 /* Matched the class name, so get its functions */
switches 0:5c4d7b2438d3 169 const rpc_function *cur_func = q->static_functions;
switches 0:5c4d7b2438d3 170 if (args.method_name == NULL) {
switches 0:5c4d7b2438d3 171 for (; cur_func->name != NULL; cur_func++) {
switches 0:5c4d7b2438d3 172 r.putData<const char*>(cur_func->name);
switches 0:5c4d7b2438d3 173 }
switches 0:5c4d7b2438d3 174 return true;
switches 0:5c4d7b2438d3 175 } else {
switches 0:5c4d7b2438d3 176 /* Otherwise call the appropriate function */
switches 0:5c4d7b2438d3 177 for (; cur_func->name != NULL; cur_func++) {
switches 0:5c4d7b2438d3 178 if (strcmp(cur_func->name, args.method_name) == 0) {
switches 0:5c4d7b2438d3 179 (cur_func->function_caller)(&args, &r);
switches 0:5c4d7b2438d3 180 return true;
switches 0:5c4d7b2438d3 181 }
switches 0:5c4d7b2438d3 182 }
switches 0:5c4d7b2438d3 183 return false;
switches 0:5c4d7b2438d3 184 }
switches 0:5c4d7b2438d3 185 }
switches 0:5c4d7b2438d3 186 }
switches 0:5c4d7b2438d3 187
switches 0:5c4d7b2438d3 188 return false;
switches 0:5c4d7b2438d3 189 }
switches 0:5c4d7b2438d3 190
switches 0:5c4d7b2438d3 191 } // namespace mbed