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 import json
switches 0:5c4d7b2438d3 2 import os
switches 0:5c4d7b2438d3 3 import stat
switches 0:5c4d7b2438d3 4 import re
switches 0:5c4d7b2438d3 5 from collections import OrderedDict
switches 0:5c4d7b2438d3 6 from subprocess import Popen
switches 0:5c4d7b2438d3 7
switches 0:5c4d7b2438d3 8 git_processes = []
switches 0:5c4d7b2438d3 9
switches 0:5c4d7b2438d3 10 class MyJSONEncoder(json.JSONEncoder):
switches 0:5c4d7b2438d3 11 def __init__(self, *args, **kwargs):
switches 0:5c4d7b2438d3 12 super(MyJSONEncoder, self).__init__(*args, **kwargs)
switches 0:5c4d7b2438d3 13 self.current_indent = 0
switches 0:5c4d7b2438d3 14 self.current_indent_str = ""
switches 0:5c4d7b2438d3 15
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 def encode(self, o):
switches 0:5c4d7b2438d3 18 #Special Processing for lists
switches 0:5c4d7b2438d3 19 if isinstance(o, (list, tuple)):
switches 0:5c4d7b2438d3 20 primitives_only = True
switches 0:5c4d7b2438d3 21 for item in o:
switches 0:5c4d7b2438d3 22 if isinstance(item, (list, tuple, dict)):
switches 0:5c4d7b2438d3 23 primitives_only = False
switches 0:5c4d7b2438d3 24 break
switches 0:5c4d7b2438d3 25 output = []
switches 0:5c4d7b2438d3 26 if primitives_only:
switches 0:5c4d7b2438d3 27 for item in o:
switches 0:5c4d7b2438d3 28 output.append(json.dumps(item))
switches 0:5c4d7b2438d3 29 return "[" + ", ".join(output) + "]"
switches 0:5c4d7b2438d3 30 else:
switches 0:5c4d7b2438d3 31 self.current_indent += self.indent
switches 0:5c4d7b2438d3 32 self.current_indent_str = " " * self.current_indent
switches 0:5c4d7b2438d3 33 for item in o:
switches 0:5c4d7b2438d3 34 output.append(self.current_indent_str + self.encode(item))
switches 0:5c4d7b2438d3 35 self.current_indent -= self.indent
switches 0:5c4d7b2438d3 36 self.current_indent_str = " " * self.current_indent
switches 0:5c4d7b2438d3 37 return "[\n" + ",\n".join(output) + "\n" + self.current_indent_str + "]"
switches 0:5c4d7b2438d3 38 elif isinstance(o, dict):
switches 0:5c4d7b2438d3 39 primitives_only = True
switches 0:5c4d7b2438d3 40 for item in o.values():
switches 0:5c4d7b2438d3 41 if isinstance(item, (list, tuple, dict)):
switches 0:5c4d7b2438d3 42 primitives_only = False
switches 0:5c4d7b2438d3 43 break
switches 0:5c4d7b2438d3 44 output = []
switches 0:5c4d7b2438d3 45 if primitives_only and len(o) < 3:
switches 0:5c4d7b2438d3 46 for key, value in o.iteritems():
switches 0:5c4d7b2438d3 47 output.append(json.dumps(key) + ": " + self.encode(value))
switches 0:5c4d7b2438d3 48 return "{" + ", ".join(output) + "}"
switches 0:5c4d7b2438d3 49 else:
switches 0:5c4d7b2438d3 50 self.current_indent += self.indent
switches 0:5c4d7b2438d3 51 self.current_indent_str = " " * self.current_indent
switches 0:5c4d7b2438d3 52 for key, value in o.iteritems():
switches 0:5c4d7b2438d3 53 output.append(self.current_indent_str + json.dumps(key) + ": " + self.encode(value))
switches 0:5c4d7b2438d3 54 self.current_indent -= self.indent
switches 0:5c4d7b2438d3 55 self.current_indent_str = " " * self.current_indent
switches 0:5c4d7b2438d3 56 return "{\n" + ",\n".join(output) + "\n" + self.current_indent_str + "}"
switches 0:5c4d7b2438d3 57 else:
switches 0:5c4d7b2438d3 58 return json.dumps(o)
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 def load(path):
switches 0:5c4d7b2438d3 61 with open(path, 'r') as f :
switches 0:5c4d7b2438d3 62 return json.load(f, object_pairs_hook=OrderedDict)
switches 0:5c4d7b2438d3 63
switches 0:5c4d7b2438d3 64 def dump(path, obj):
switches 0:5c4d7b2438d3 65 with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, stat.S_IRUSR | stat.S_IWUSR), 'w') as f :
switches 0:5c4d7b2438d3 66 os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)
switches 0:5c4d7b2438d3 67 f.write(MyJSONEncoder(indent=4).encode(obj))
switches 0:5c4d7b2438d3 68 f.write(u'\n')
switches 0:5c4d7b2438d3 69 f.truncate()
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 def find(stem, path) :
switches 0:5c4d7b2438d3 72 for root, directories, files in os.walk(path, followlinks=True) :
switches 0:5c4d7b2438d3 73 [dir for dir in directories if dir[0] != '.']
switches 0:5c4d7b2438d3 74 if (stem_match(stem,os.path.basename(os.path.normpath(root))) and
switches 0:5c4d7b2438d3 75 "device.h" in files) :
switches 0:5c4d7b2438d3 76 return os.path.join(root, "device.h")
switches 0:5c4d7b2438d3 77
switches 0:5c4d7b2438d3 78 def find_all_devices(path, verbose=False) :
switches 0:5c4d7b2438d3 79 for root, directories, files in os.walk(path, followlinks=True) :
switches 0:5c4d7b2438d3 80 [dir for dir in directories if dir[0] != '.']
switches 0:5c4d7b2438d3 81 if "device.h" in files :
switches 0:5c4d7b2438d3 82 if verbose : print("[VERBOSE] found a device.h file in {}".format(root))
switches 0:5c4d7b2438d3 83 yield os.path.join(root, "device.h")
switches 0:5c4d7b2438d3 84
switches 0:5c4d7b2438d3 85 mbed_matcher = re.compile('mbed', re.IGNORECASE)
switches 0:5c4d7b2438d3 86 def stem_match(stem, thing) :
switches 0:5c4d7b2438d3 87 return (stem in thing or
switches 0:5c4d7b2438d3 88 re.sub(mbed_matcher, '', stem) in thing)
switches 0:5c4d7b2438d3 89
switches 0:5c4d7b2438d3 90 attr_matcher = re.compile('^#define\W+DEVICE_(\w+)\W+1.*$')
switches 0:5c4d7b2438d3 91 def parse_attributes(path) :
switches 0:5c4d7b2438d3 92 with open(path) as input :
switches 0:5c4d7b2438d3 93 for line in input :
switches 0:5c4d7b2438d3 94 m = re.match(attr_matcher, line)
switches 0:5c4d7b2438d3 95 if m: yield m.group(1)
switches 0:5c4d7b2438d3 96
switches 0:5c4d7b2438d3 97 remove_matcher = re.compile('^#define\W+DEVICE_(\w+)\W+[10].*$')
switches 0:5c4d7b2438d3 98 def remove_attributes(path) :
switches 0:5c4d7b2438d3 99 with open(path) as input :
switches 0:5c4d7b2438d3 100 remainder = filter(lambda l: not re.match(remove_matcher, l), input)
switches 0:5c4d7b2438d3 101 with open(path,"wb") as output :
switches 0:5c4d7b2438d3 102 output.truncate(0)
switches 0:5c4d7b2438d3 103 output.write("// The 'provides' section in 'target.json' is now used"+
switches 0:5c4d7b2438d3 104 " to create the device's hardware preprocessor switches.\n")
switches 0:5c4d7b2438d3 105 output.write("// Check the 'provides' section of the target description"+
switches 0:5c4d7b2438d3 106 " in 'targets.json' for more details.\n")
switches 0:5c4d7b2438d3 107 output.writelines(remainder)
switches 0:5c4d7b2438d3 108
switches 0:5c4d7b2438d3 109 def user_select(things, message) :
switches 0:5c4d7b2438d3 110 print(message)
switches 0:5c4d7b2438d3 111 for thing, number in zip(things, range(len(things))):
switches 0:5c4d7b2438d3 112 print("{} : {}".format(number, thing))
switches 0:5c4d7b2438d3 113 selection = None
switches 0:5c4d7b2438d3 114 while selection is None :
switches 0:5c4d7b2438d3 115 print("please select an integer [0..{}] or specify all".format(len(things) - 1))
switches 0:5c4d7b2438d3 116 try :
switches 0:5c4d7b2438d3 117 i = raw_input()
switches 0:5c4d7b2438d3 118 if i == "all" :
switches 0:5c4d7b2438d3 119 selection = "all"
switches 0:5c4d7b2438d3 120 else :
switches 0:5c4d7b2438d3 121 selection = int(i)
switches 0:5c4d7b2438d3 122 if (selection > len(things) or
switches 0:5c4d7b2438d3 123 selection < 0) :
switches 0:5c4d7b2438d3 124 print("selection {} out of range".format(selection))
switches 0:5c4d7b2438d3 125 selection = None
switches 0:5c4d7b2438d3 126 except (ValueError, SyntaxError) :
switches 0:5c4d7b2438d3 127 print("selection not understood")
switches 0:5c4d7b2438d3 128 if selection == "all" :
switches 0:5c4d7b2438d3 129 return things
switches 0:5c4d7b2438d3 130 else :
switches 0:5c4d7b2438d3 131 return [things[selection]]
switches 0:5c4d7b2438d3 132
switches 0:5c4d7b2438d3 133 target_matcher = re.compile("TARGET_")
switches 0:5c4d7b2438d3 134 def strip_target(str) :
switches 0:5c4d7b2438d3 135 return re.sub(target_matcher, "", str)
switches 0:5c4d7b2438d3 136
switches 0:5c4d7b2438d3 137 def add_to_targets(targets, device_file, verbose=False, remove=False) :
switches 0:5c4d7b2438d3 138 if verbose : print("[VERBOSE] trying target {}".format(device_file))
switches 0:5c4d7b2438d3 139 device = strip_target(os.path.basename(os.path.normpath(os.path.dirname(device_file))))
switches 0:5c4d7b2438d3 140 if not device :
switches 0:5c4d7b2438d3 141 print("[WARNING] device {} did not have an associated device.h".format(device))
switches 0:5c4d7b2438d3 142 else :
switches 0:5c4d7b2438d3 143 possible_matches = set([key for key in targets.keys() if stem_match(device, key)])
switches 0:5c4d7b2438d3 144 for key, value in targets.iteritems() :
switches 0:5c4d7b2438d3 145 for alt in value['extra_labels'] if 'extra_labels' in value else [] :
switches 0:5c4d7b2438d3 146 if stem_match(device, alt) : possible_matches.add(key)
switches 0:5c4d7b2438d3 147 for alt in value['extra_labels_add'] if 'extra_labels_add' in value else [] :
switches 0:5c4d7b2438d3 148 if stem_match(device, alt) : possible_matches.add(key)
switches 0:5c4d7b2438d3 149 possible_matches = list(possible_matches)
switches 0:5c4d7b2438d3 150 for match in possible_matches :
switches 0:5c4d7b2438d3 151 if device == match : possible_matches = [match]
switches 0:5c4d7b2438d3 152 if not possible_matches :
switches 0:5c4d7b2438d3 153 print("[WARNING] device {} did not have an associated entry in targets.json".format(device))
switches 0:5c4d7b2438d3 154 return None
switches 0:5c4d7b2438d3 155 elif len(possible_matches) > 1 :
switches 0:5c4d7b2438d3 156 message = ("possible matches for file {}".format(device_file))
switches 0:5c4d7b2438d3 157 target = user_select(possible_matches, message)
switches 0:5c4d7b2438d3 158 else :
switches 0:5c4d7b2438d3 159 target = possible_matches
switches 0:5c4d7b2438d3 160 attrs = list(parse_attributes(device_file))
switches 0:5c4d7b2438d3 161 if attrs :
switches 0:5c4d7b2438d3 162 for t in target :
switches 0:5c4d7b2438d3 163 targets[t]["device_has"] = sorted(list(set(targets[t].setdefault("device_has",[]) + attrs)))
switches 0:5c4d7b2438d3 164 if verbose : print("[VERBOSE] target {} now device_has {}".format(t, attrs))
switches 0:5c4d7b2438d3 165 if remove is True:
switches 0:5c4d7b2438d3 166 remove_attributes(device_file)
switches 0:5c4d7b2438d3 167
switches 0:5c4d7b2438d3 168 if __name__ == '__main__' :
switches 0:5c4d7b2438d3 169 import argparse
switches 0:5c4d7b2438d3 170 parser = argparse.ArgumentParser(description='A helpful little script for converting' +
switches 0:5c4d7b2438d3 171 ' device.h files to parts of the targets.json file')
switches 0:5c4d7b2438d3 172 parser.add_argument('-a', '--all', action='store_true',
switches 0:5c4d7b2438d3 173 help='find and convert all available device.h files in the'+
switches 0:5c4d7b2438d3 174 ' directory tree starting at the current directory')
switches 0:5c4d7b2438d3 175 parser.add_argument('-f', '--file', nargs='+', help='specify an individual file to '+
switches 0:5c4d7b2438d3 176 'convert from device.h format to a piece of targets.json')
switches 0:5c4d7b2438d3 177 parser.add_argument('-t', '--target', nargs='+', help='specify an individual target'+
switches 0:5c4d7b2438d3 178 ' to convert from device.h format to a piece of targets.json')
switches 0:5c4d7b2438d3 179 parser.add_argument('-v', '--verbose', action='store_true',
switches 0:5c4d7b2438d3 180 help="print out every target that is updated in the targets.json")
switches 0:5c4d7b2438d3 181 parser.add_argument('-r', '--rm', action='store_true',
switches 0:5c4d7b2438d3 182 help="remove the used attributes from a device.h file")
switches 0:5c4d7b2438d3 183 args = parser.parse_args()
switches 0:5c4d7b2438d3 184 if not args.target and not args.file and not args.all :
switches 0:5c4d7b2438d3 185 print("[WARNING] no action specified; auto-formatting targets.json")
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 targets_file_name = os.path.join(os.curdir, "hal", "targets.json")
switches 0:5c4d7b2438d3 188 try :
switches 0:5c4d7b2438d3 189 targets = load(targets_file_name)
switches 0:5c4d7b2438d3 190 except OSError :
switches 0:5c4d7b2438d3 191 print("[ERROR] did not find targets.json where I expected it {}".format(targets_file_name))
switches 0:5c4d7b2438d3 192 exit(1)
switches 0:5c4d7b2438d3 193 except ValueError :
switches 0:5c4d7b2438d3 194 print("[ERROR] invalid json found in {}".format(targets_file_name))
switches 0:5c4d7b2438d3 195 exit(2)
switches 0:5c4d7b2438d3 196
switches 0:5c4d7b2438d3 197 if args.target :
switches 0:5c4d7b2438d3 198 for target in args.target :
switches 0:5c4d7b2438d3 199 device_file = find(target, os.curdir)
switches 0:5c4d7b2438d3 200 if device_file :
switches 0:5c4d7b2438d3 201 add_to_targets(targets, device_file, verbose=args.verbose, remove=args.rm)
switches 0:5c4d7b2438d3 202 else :
switches 0:5c4d7b2438d3 203 print("[WARNING] could not locate a device file for target {}".format(target))
switches 0:5c4d7b2438d3 204
switches 0:5c4d7b2438d3 205 if args.file :
switches 0:5c4d7b2438d3 206 for file in args.file :
switches 0:5c4d7b2438d3 207 add_to_targets(targets, file, verbose=args.verbose, remove=args.rm)
switches 0:5c4d7b2438d3 208
switches 0:5c4d7b2438d3 209 if args.all :
switches 0:5c4d7b2438d3 210 for file in find_all_devices(os.curdir, verbose=args.verbose) :
switches 0:5c4d7b2438d3 211 add_to_targets(targets, file, verbose=args.verbose, remove=args.rm)
switches 0:5c4d7b2438d3 212
switches 0:5c4d7b2438d3 213 dump(targets_file_name, targets)
switches 0:5c4d7b2438d3 214
switches 0:5c4d7b2438d3 215 for process in git_processes :
switches 0:5c4d7b2438d3 216 process.wait()