Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 """
marcozecchini 0:9fca2b23d0ba 2 mbed SDK
marcozecchini 0:9fca2b23d0ba 3 Copyright (c) 2011-2013 ARM Limited
marcozecchini 0:9fca2b23d0ba 4
marcozecchini 0:9fca2b23d0ba 5 Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 6 you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 7 You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 8
marcozecchini 0:9fca2b23d0ba 9 http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 10
marcozecchini 0:9fca2b23d0ba 11 Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 12 distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 14 See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 15 limitations under the License.
marcozecchini 0:9fca2b23d0ba 16 """
marcozecchini 0:9fca2b23d0ba 17 from os.path import join
marcozecchini 0:9fca2b23d0ba 18 from jinja2 import Template
marcozecchini 0:9fca2b23d0ba 19
marcozecchini 0:9fca2b23d0ba 20 from tools.paths import TOOLS_DATA, MBED_RPC
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 RPC_TEMPLATES_PATH = join(TOOLS_DATA, "rpc")
marcozecchini 0:9fca2b23d0ba 23
marcozecchini 0:9fca2b23d0ba 24 RPC_TEMPLATE = "RPCClasses.h"
marcozecchini 0:9fca2b23d0ba 25 CLASS_TEMPLATE = "class.cpp"
marcozecchini 0:9fca2b23d0ba 26 RPC_CLASSES_PATH = join(MBED_RPC, RPC_TEMPLATE)
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 def get_template(name):
marcozecchini 0:9fca2b23d0ba 30 return Template(open(join(RPC_TEMPLATES_PATH, name)).read())
marcozecchini 0:9fca2b23d0ba 31
marcozecchini 0:9fca2b23d0ba 32
marcozecchini 0:9fca2b23d0ba 33 def write_rpc_classes(classes):
marcozecchini 0:9fca2b23d0ba 34 template = get_template(RPC_TEMPLATE)
marcozecchini 0:9fca2b23d0ba 35 open(RPC_CLASSES_PATH, "w").write(template.render({"classes":classes}))
marcozecchini 0:9fca2b23d0ba 36
marcozecchini 0:9fca2b23d0ba 37
marcozecchini 0:9fca2b23d0ba 38 RPC_CLASSES = (
marcozecchini 0:9fca2b23d0ba 39 {
marcozecchini 0:9fca2b23d0ba 40 "name": "DigitalOut",
marcozecchini 0:9fca2b23d0ba 41 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 42 "methods": [
marcozecchini 0:9fca2b23d0ba 43 (None , "write", ["int"]),
marcozecchini 0:9fca2b23d0ba 44 ("int", "read" , []),
marcozecchini 0:9fca2b23d0ba 45 ]
marcozecchini 0:9fca2b23d0ba 46 },
marcozecchini 0:9fca2b23d0ba 47 {
marcozecchini 0:9fca2b23d0ba 48 "name": "DigitalIn",
marcozecchini 0:9fca2b23d0ba 49 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 50 "methods": [
marcozecchini 0:9fca2b23d0ba 51 ("int", "read" , []),
marcozecchini 0:9fca2b23d0ba 52 ]
marcozecchini 0:9fca2b23d0ba 53 },
marcozecchini 0:9fca2b23d0ba 54 {
marcozecchini 0:9fca2b23d0ba 55 "name": "DigitalInOut",
marcozecchini 0:9fca2b23d0ba 56 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 57 "methods": [
marcozecchini 0:9fca2b23d0ba 58 ("int", "read" , []),
marcozecchini 0:9fca2b23d0ba 59 (None , "write" , ["int"]),
marcozecchini 0:9fca2b23d0ba 60 (None , "input" , []),
marcozecchini 0:9fca2b23d0ba 61 (None , "output", []),
marcozecchini 0:9fca2b23d0ba 62 ]
marcozecchini 0:9fca2b23d0ba 63 },
marcozecchini 0:9fca2b23d0ba 64 {
marcozecchini 0:9fca2b23d0ba 65 "name": "AnalogIn",
marcozecchini 0:9fca2b23d0ba 66 "required": "ANALOGIN",
marcozecchini 0:9fca2b23d0ba 67 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 68 "methods": [
marcozecchini 0:9fca2b23d0ba 69 ("float" , "read" , []),
marcozecchini 0:9fca2b23d0ba 70 ("unsigned short", "read_u16", []),
marcozecchini 0:9fca2b23d0ba 71 ]
marcozecchini 0:9fca2b23d0ba 72 },
marcozecchini 0:9fca2b23d0ba 73 {
marcozecchini 0:9fca2b23d0ba 74 "name": "AnalogOut",
marcozecchini 0:9fca2b23d0ba 75 "required": "ANALOGOUT",
marcozecchini 0:9fca2b23d0ba 76 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 77 "methods": [
marcozecchini 0:9fca2b23d0ba 78 ("float", "read" , []),
marcozecchini 0:9fca2b23d0ba 79 (None , "write" , ["float"]),
marcozecchini 0:9fca2b23d0ba 80 (None , "write_u16", ["unsigned short"]),
marcozecchini 0:9fca2b23d0ba 81 ]
marcozecchini 0:9fca2b23d0ba 82 },
marcozecchini 0:9fca2b23d0ba 83 {
marcozecchini 0:9fca2b23d0ba 84 "name": "PwmOut",
marcozecchini 0:9fca2b23d0ba 85 "required": "PWMOUT",
marcozecchini 0:9fca2b23d0ba 86 "cons_args": ["PinName"],
marcozecchini 0:9fca2b23d0ba 87 "methods": [
marcozecchini 0:9fca2b23d0ba 88 ("float", "read" , []),
marcozecchini 0:9fca2b23d0ba 89 (None , "write" , ["float"]),
marcozecchini 0:9fca2b23d0ba 90 (None , "period" , ["float"]),
marcozecchini 0:9fca2b23d0ba 91 (None , "period_ms" , ["int"]),
marcozecchini 0:9fca2b23d0ba 92 (None , "pulsewidth" , ["float"]),
marcozecchini 0:9fca2b23d0ba 93 (None , "pulsewidth_ms", ["int"]),
marcozecchini 0:9fca2b23d0ba 94 ]
marcozecchini 0:9fca2b23d0ba 95 },
marcozecchini 0:9fca2b23d0ba 96 {
marcozecchini 0:9fca2b23d0ba 97 "name": "SPI",
marcozecchini 0:9fca2b23d0ba 98 "required": "SPI",
marcozecchini 0:9fca2b23d0ba 99 "cons_args": ["PinName", "PinName", "PinName"],
marcozecchini 0:9fca2b23d0ba 100 "methods": [
marcozecchini 0:9fca2b23d0ba 101 (None , "format" , ["int", "int"]),
marcozecchini 0:9fca2b23d0ba 102 (None , "frequency", ["int"]),
marcozecchini 0:9fca2b23d0ba 103 ("int", "write" , ["int"]),
marcozecchini 0:9fca2b23d0ba 104 ]
marcozecchini 0:9fca2b23d0ba 105 },
marcozecchini 0:9fca2b23d0ba 106 {
marcozecchini 0:9fca2b23d0ba 107 "name": "Serial",
marcozecchini 0:9fca2b23d0ba 108 "required": "SERIAL",
marcozecchini 0:9fca2b23d0ba 109 "cons_args": ["PinName", "PinName"],
marcozecchini 0:9fca2b23d0ba 110 "methods": [
marcozecchini 0:9fca2b23d0ba 111 (None , "baud" , ["int"]),
marcozecchini 0:9fca2b23d0ba 112 ("int", "readable" , []),
marcozecchini 0:9fca2b23d0ba 113 ("int", "writeable", []),
marcozecchini 0:9fca2b23d0ba 114 ("int", "putc" , ["int"]),
marcozecchini 0:9fca2b23d0ba 115 ("int", "getc" , []),
marcozecchini 0:9fca2b23d0ba 116 ("int", "puts" , ["const char *"]),
marcozecchini 0:9fca2b23d0ba 117 ]
marcozecchini 0:9fca2b23d0ba 118 },
marcozecchini 0:9fca2b23d0ba 119 {
marcozecchini 0:9fca2b23d0ba 120 "name": "Timer",
marcozecchini 0:9fca2b23d0ba 121 "cons_args": [],
marcozecchini 0:9fca2b23d0ba 122 "methods": [
marcozecchini 0:9fca2b23d0ba 123 (None , "start" , []),
marcozecchini 0:9fca2b23d0ba 124 (None , "stop" , []),
marcozecchini 0:9fca2b23d0ba 125 (None , "reset" , []),
marcozecchini 0:9fca2b23d0ba 126 ("float", "read" , []),
marcozecchini 0:9fca2b23d0ba 127 ("int" , "read_ms", []),
marcozecchini 0:9fca2b23d0ba 128 ("int" , "read_us", []),
marcozecchini 0:9fca2b23d0ba 129 ]
marcozecchini 0:9fca2b23d0ba 130 }
marcozecchini 0:9fca2b23d0ba 131 )
marcozecchini 0:9fca2b23d0ba 132
marcozecchini 0:9fca2b23d0ba 133
marcozecchini 0:9fca2b23d0ba 134 def get_args_proto(args_types, extra=None):
marcozecchini 0:9fca2b23d0ba 135 args = ["%s a%d" % (s, n) for n, s in enumerate(args_types)]
marcozecchini 0:9fca2b23d0ba 136 if extra:
marcozecchini 0:9fca2b23d0ba 137 args.extend(extra)
marcozecchini 0:9fca2b23d0ba 138 return ', '.join(args)
marcozecchini 0:9fca2b23d0ba 139
marcozecchini 0:9fca2b23d0ba 140
marcozecchini 0:9fca2b23d0ba 141 def get_args_call(args):
marcozecchini 0:9fca2b23d0ba 142 return ', '.join(["a%d" % (n) for n in range(len(args))])
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144
marcozecchini 0:9fca2b23d0ba 145 classes = []
marcozecchini 0:9fca2b23d0ba 146 class_template = get_template(CLASS_TEMPLATE)
marcozecchini 0:9fca2b23d0ba 147
marcozecchini 0:9fca2b23d0ba 148 for c in RPC_CLASSES:
marcozecchini 0:9fca2b23d0ba 149 c_args = c['cons_args']
marcozecchini 0:9fca2b23d0ba 150 data = {
marcozecchini 0:9fca2b23d0ba 151 'name': c['name'],
marcozecchini 0:9fca2b23d0ba 152 'cons_type': ', '.join(c_args + ['const char*']),
marcozecchini 0:9fca2b23d0ba 153 "cons_proto": get_args_proto(c_args, ["const char *name=NULL"]),
marcozecchini 0:9fca2b23d0ba 154 "cons_call": get_args_call(c_args)
marcozecchini 0:9fca2b23d0ba 155 }
marcozecchini 0:9fca2b23d0ba 156
marcozecchini 0:9fca2b23d0ba 157 c_name = "Rpc" + c['name']
marcozecchini 0:9fca2b23d0ba 158
marcozecchini 0:9fca2b23d0ba 159 methods = []
marcozecchini 0:9fca2b23d0ba 160 rpc_methods = []
marcozecchini 0:9fca2b23d0ba 161 for r, m, a in c['methods']:
marcozecchini 0:9fca2b23d0ba 162 ret_proto = r if r else "void"
marcozecchini 0:9fca2b23d0ba 163 args_proto = "void"
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 ret_defin = "return " if r else ""
marcozecchini 0:9fca2b23d0ba 166 args_defin = ""
marcozecchini 0:9fca2b23d0ba 167
marcozecchini 0:9fca2b23d0ba 168 if a:
marcozecchini 0:9fca2b23d0ba 169 args_proto = get_args_proto(a)
marcozecchini 0:9fca2b23d0ba 170 args_defin = get_args_call(a)
marcozecchini 0:9fca2b23d0ba 171
marcozecchini 0:9fca2b23d0ba 172 proto = "%s %s(%s)" % (ret_proto, m, args_proto)
marcozecchini 0:9fca2b23d0ba 173 defin = "{%so.%s(%s);}" % (ret_defin, m, args_defin)
marcozecchini 0:9fca2b23d0ba 174 methods.append("%s %s" % (proto, defin))
marcozecchini 0:9fca2b23d0ba 175
marcozecchini 0:9fca2b23d0ba 176 rpc_method_type = [r] if r else []
marcozecchini 0:9fca2b23d0ba 177 rpc_method_type.append(c_name)
marcozecchini 0:9fca2b23d0ba 178 rpc_method_type.extend(a)
marcozecchini 0:9fca2b23d0ba 179 rpc_methods.append('{"%s", rpc_method_caller<%s, &%s::%s>}' % (m, ', '.join(rpc_method_type), c_name, m))
marcozecchini 0:9fca2b23d0ba 180
marcozecchini 0:9fca2b23d0ba 181 data['methods'] = "\n ".join(methods)
marcozecchini 0:9fca2b23d0ba 182 data['rpc_methods'] = ",\n ".join(rpc_methods)
marcozecchini 0:9fca2b23d0ba 183
marcozecchini 0:9fca2b23d0ba 184 class_decl = class_template.render(data)
marcozecchini 0:9fca2b23d0ba 185 if 'required' in c:
marcozecchini 0:9fca2b23d0ba 186 class_decl = "#if DEVICE_%s\n%s\n#endif" % (c['required'], class_decl)
marcozecchini 0:9fca2b23d0ba 187
marcozecchini 0:9fca2b23d0ba 188 classes.append(class_decl)
marcozecchini 0:9fca2b23d0ba 189
marcozecchini 0:9fca2b23d0ba 190 write_rpc_classes('\n\n'.join(classes))