mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 """
elessair 0:f269e3021894 2 mbed SDK
elessair 0:f269e3021894 3 Copyright (c) 2011-2016 ARM Limited
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 6 you may not use this file except in compliance with the License.
elessair 0:f269e3021894 7 You may obtain a copy of the License at
elessair 0:f269e3021894 8
elessair 0:f269e3021894 9 http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10
elessair 0:f269e3021894 11 Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 limitations under the License.
elessair 0:f269e3021894 16 """
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18 import os
elessair 0:f269e3021894 19 import binascii
elessair 0:f269e3021894 20 import struct
elessair 0:f269e3021894 21 import shutil
elessair 0:f269e3021894 22 import inspect
elessair 0:f269e3021894 23 import sys
elessair 0:f269e3021894 24 from collections import namedtuple
elessair 0:f269e3021894 25 from tools.patch import patch
elessair 0:f269e3021894 26 from tools.paths import TOOLS_BOOTLOADERS
elessair 0:f269e3021894 27 from tools.utils import json_file_to_dict
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 __all__ = ["target", "TARGETS", "TARGET_MAP", "TARGET_NAMES", "CORE_LABELS",
elessair 0:f269e3021894 30 "HookError", "generate_py_target", "Target",
elessair 0:f269e3021894 31 "CUMULATIVE_ATTRIBUTES", "get_resolution_order"]
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 CORE_LABELS = {
elessair 0:f269e3021894 34 "ARM7TDMI-S": ["ARM7", "LIKE_CORTEX_ARM7"],
elessair 0:f269e3021894 35 "Cortex-M0" : ["M0", "CORTEX_M", "LIKE_CORTEX_M0"],
elessair 0:f269e3021894 36 "Cortex-M0+": ["M0P", "CORTEX_M", "LIKE_CORTEX_M0"],
elessair 0:f269e3021894 37 "Cortex-M1" : ["M1", "CORTEX_M", "LIKE_CORTEX_M1"],
elessair 0:f269e3021894 38 "Cortex-M3" : ["M3", "CORTEX_M", "LIKE_CORTEX_M3"],
elessair 0:f269e3021894 39 "Cortex-M4" : ["M4", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M4"],
elessair 0:f269e3021894 40 "Cortex-M4F" : ["M4", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M4"],
elessair 0:f269e3021894 41 "Cortex-M7" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"],
elessair 0:f269e3021894 42 "Cortex-M7F" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"],
elessair 0:f269e3021894 43 "Cortex-M7FD" : ["M7", "CORTEX_M", "RTOS_M4_M7", "LIKE_CORTEX_M7"],
elessair 0:f269e3021894 44 "Cortex-A9" : ["A9", "CORTEX_A", "LIKE_CORTEX_A9"]
elessair 0:f269e3021894 45 }
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 ################################################################################
elessair 0:f269e3021894 48 # Generic Target class that reads and interprets the data in targets.json
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 class HookError(Exception):
elessair 0:f269e3021894 51 """ A simple class that represents all the exceptions associated with
elessair 0:f269e3021894 52 hooking
elessair 0:f269e3021894 53 """
elessair 0:f269e3021894 54 pass
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 CACHES = {}
elessair 0:f269e3021894 57 def cached(func):
elessair 0:f269e3021894 58 """A simple decorator used for automatically caching data returned by a
elessair 0:f269e3021894 59 function
elessair 0:f269e3021894 60 """
elessair 0:f269e3021894 61 def wrapper(*args, **kwargs):
elessair 0:f269e3021894 62 """The wrapped function itself"""
elessair 0:f269e3021894 63 if not CACHES.has_key((func.__name__, args)):
elessair 0:f269e3021894 64 CACHES[(func.__name__, args)] = func(*args, **kwargs)
elessair 0:f269e3021894 65 return CACHES[(func.__name__, args)]
elessair 0:f269e3021894 66 return wrapper
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 # Cumulative attributes can have values appended to them, so they
elessair 0:f269e3021894 70 # need to be computed differently than regular attributes
elessair 0:f269e3021894 71 CUMULATIVE_ATTRIBUTES = ['extra_labels', 'macros', 'device_has', 'features']
elessair 0:f269e3021894 72
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 def get_resolution_order(json_data, target_name, order, level=0):
elessair 0:f269e3021894 75 """ Return the order in which target descriptions are searched for
elessair 0:f269e3021894 76 attributes. This mimics the Python 2.2 method resolution order, which
elessair 0:f269e3021894 77 is what the old targets.py module used. For more details, check
elessair 0:f269e3021894 78 http://makina-corpus.com/blog/metier/2014/python-tutorial-understanding-python-mro-class-search-path
elessair 0:f269e3021894 79 The resolution order contains (name, level) tuples, where "name" is the
elessair 0:f269e3021894 80 name of the class and "level" is the level in the inheritance hierarchy
elessair 0:f269e3021894 81 (the target itself is at level 0, its first parent at level 1, its
elessair 0:f269e3021894 82 parent's parent at level 2 and so on)
elessair 0:f269e3021894 83 """
elessair 0:f269e3021894 84 # the resolution order can't contain duplicate target names
elessair 0:f269e3021894 85 if target_name not in [l[0] for l in order]:
elessair 0:f269e3021894 86 order.append((target_name, level))
elessair 0:f269e3021894 87 parents = json_data[target_name].get("inherits", [])
elessair 0:f269e3021894 88 for par in parents:
elessair 0:f269e3021894 89 order = get_resolution_order(json_data, par, order, level + 1)
elessair 0:f269e3021894 90 return order
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 def target(name, json_data):
elessair 0:f269e3021894 94 """Construct a target object"""
elessair 0:f269e3021894 95 resolution_order = get_resolution_order(json_data, name, [])
elessair 0:f269e3021894 96 resolution_order_names = [tgt for tgt, _ in resolution_order]
elessair 0:f269e3021894 97 return Target(name=name,
elessair 0:f269e3021894 98 json_data={key: value for key, value in json_data.items()
elessair 0:f269e3021894 99 if key in resolution_order_names},
elessair 0:f269e3021894 100 resolution_order=resolution_order,
elessair 0:f269e3021894 101 resolution_order_names=resolution_order_names)
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 def generate_py_target(new_targets, name):
elessair 0:f269e3021894 104 """Add one or more new target(s) represented as a Python dictionary
elessair 0:f269e3021894 105 in 'new_targets'. It is an error to add a target with a name that
elessair 0:f269e3021894 106 already exists.
elessair 0:f269e3021894 107 """
elessair 0:f269e3021894 108 base_targets = Target.get_json_target_data()
elessair 0:f269e3021894 109 for new_target in new_targets.keys():
elessair 0:f269e3021894 110 if new_target in base_targets:
elessair 0:f269e3021894 111 raise Exception("Attempt to add target '%s' that already exists"
elessair 0:f269e3021894 112 % new_target)
elessair 0:f269e3021894 113 total_data = {}
elessair 0:f269e3021894 114 total_data.update(new_targets)
elessair 0:f269e3021894 115 total_data.update(base_targets)
elessair 0:f269e3021894 116 return target(name, total_data)
elessair 0:f269e3021894 117
elessair 0:f269e3021894 118 class Target(namedtuple("Target", "name json_data resolution_order resolution_order_names")):
elessair 0:f269e3021894 119 """An object to represent a Target (MCU/Board)"""
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 # Default location of the 'targets.json' file
elessair 0:f269e3021894 122 __targets_json_location_default = os.path.join(
elessair 0:f269e3021894 123 os.path.dirname(os.path.abspath(__file__)), '..', 'targets', 'targets.json')
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 # Current/new location of the 'targets.json' file
elessair 0:f269e3021894 126 __targets_json_location = None
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 @staticmethod
elessair 0:f269e3021894 129 @cached
elessair 0:f269e3021894 130 def get_json_target_data():
elessair 0:f269e3021894 131 """Load the description of JSON target data"""
elessair 0:f269e3021894 132 return json_file_to_dict(Target.__targets_json_location or
elessair 0:f269e3021894 133 Target.__targets_json_location_default)
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 @staticmethod
elessair 0:f269e3021894 136 def set_targets_json_location(location=None):
elessair 0:f269e3021894 137 """Set the location of the targets.json file"""
elessair 0:f269e3021894 138 Target.__targets_json_location = (location or
elessair 0:f269e3021894 139 Target.__targets_json_location_default)
elessair 0:f269e3021894 140 # Invalidate caches, since the location of the JSON file changed
elessair 0:f269e3021894 141 CACHES.clear()
elessair 0:f269e3021894 142
elessair 0:f269e3021894 143 @staticmethod
elessair 0:f269e3021894 144 @cached
elessair 0:f269e3021894 145 def get_module_data():
elessair 0:f269e3021894 146 """Get the members of this module using Python's "inspect" module"""
elessair 0:f269e3021894 147 return dict([(m[0], m[1]) for m in
elessair 0:f269e3021894 148 inspect.getmembers(sys.modules[__name__])])
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 @staticmethod
elessair 0:f269e3021894 151 def __add_paths_to_progen(data):
elessair 0:f269e3021894 152 """Modify the exporter specification ("progen") by changing all
elessair 0:f269e3021894 153 "template" keys to full paths
elessair 0:f269e3021894 154 """
elessair 0:f269e3021894 155 out = {}
elessair 0:f269e3021894 156 for key, val in data.items():
elessair 0:f269e3021894 157 if isinstance(val, dict):
elessair 0:f269e3021894 158 out[key] = Target.__add_paths_to_progen(val)
elessair 0:f269e3021894 159 elif key == "template":
elessair 0:f269e3021894 160 out[key] = [os.path.join(os.path.dirname(__file__), 'export', v)
elessair 0:f269e3021894 161 for v in val]
elessair 0:f269e3021894 162 else:
elessair 0:f269e3021894 163 out[key] = val
elessair 0:f269e3021894 164 return out
elessair 0:f269e3021894 165
elessair 0:f269e3021894 166 def __getattr_cumulative(self, attrname):
elessair 0:f269e3021894 167 """Look for the attribute in the class and its parents, as defined by
elessair 0:f269e3021894 168 the resolution order
elessair 0:f269e3021894 169 """
elessair 0:f269e3021894 170 tdata = self.json_data
elessair 0:f269e3021894 171 # For a cumulative attribute, figure out when it was defined the
elessair 0:f269e3021894 172 # last time (in attribute resolution order) then follow the "_add"
elessair 0:f269e3021894 173 # and "_remove" data fields
elessair 0:f269e3021894 174 for idx, tgt in enumerate(self.resolution_order):
elessair 0:f269e3021894 175 # the attribute was defined at this level in the resolution
elessair 0:f269e3021894 176 # order
elessair 0:f269e3021894 177 if attrname in tdata[tgt[0]]:
elessair 0:f269e3021894 178 def_idx = idx
elessair 0:f269e3021894 179 break
elessair 0:f269e3021894 180 else:
elessair 0:f269e3021894 181 raise AttributeError("Attribute '%s' not found in target '%s'"
elessair 0:f269e3021894 182 % (attrname, self.name))
elessair 0:f269e3021894 183 # Get the starting value of the attribute
elessair 0:f269e3021894 184 starting_value = (tdata[self.resolution_order[def_idx][0]][attrname]
elessair 0:f269e3021894 185 or [])[:]
elessair 0:f269e3021894 186 # Traverse the resolution list in high inheritance to low
elessair 0:f269e3021894 187 # inheritance level, left to right order to figure out all the
elessair 0:f269e3021894 188 # other classes that change the definition by adding or removing
elessair 0:f269e3021894 189 # elements
elessair 0:f269e3021894 190 for idx in xrange(self.resolution_order[def_idx][1] - 1, -1, -1):
elessair 0:f269e3021894 191 same_level_targets = [tar[0] for tar in self.resolution_order
elessair 0:f269e3021894 192 if tar[1] == idx]
elessair 0:f269e3021894 193 for tar in same_level_targets:
elessair 0:f269e3021894 194 data = tdata[tar]
elessair 0:f269e3021894 195 # Do we have anything to add ?
elessair 0:f269e3021894 196 if data.has_key(attrname + "_add"):
elessair 0:f269e3021894 197 starting_value.extend(data[attrname + "_add"])
elessair 0:f269e3021894 198 # Do we have anything to remove ?
elessair 0:f269e3021894 199 if data.has_key(attrname + "_remove"):
elessair 0:f269e3021894 200 # Macros can be defined either without a value (MACRO)
elessair 0:f269e3021894 201 # or with a value (MACRO=10). When removing, we specify
elessair 0:f269e3021894 202 # only the name of the macro, without the value. So we
elessair 0:f269e3021894 203 # need to create a mapping between the macro name and
elessair 0:f269e3021894 204 # its value. This will work for extra_labels and other
elessair 0:f269e3021894 205 # type of arrays as well, since they fall into the
elessair 0:f269e3021894 206 # "macros without a value" category (simple definitions
elessair 0:f269e3021894 207 # without a value).
elessair 0:f269e3021894 208 name_def_map = {}
elessair 0:f269e3021894 209 for crtv in starting_value:
elessair 0:f269e3021894 210 if crtv.find('=') != -1:
elessair 0:f269e3021894 211 temp = crtv.split('=')
elessair 0:f269e3021894 212 if len(temp) != 2:
elessair 0:f269e3021894 213 raise ValueError(
elessair 0:f269e3021894 214 "Invalid macro definition '%s'" % crtv)
elessair 0:f269e3021894 215 name_def_map[temp[0]] = crtv
elessair 0:f269e3021894 216 else:
elessair 0:f269e3021894 217 name_def_map[crtv] = crtv
elessair 0:f269e3021894 218 for element in data[attrname + "_remove"]:
elessair 0:f269e3021894 219 if element not in name_def_map:
elessair 0:f269e3021894 220 raise ValueError(
elessair 0:f269e3021894 221 ("Unable to remove '%s' in '%s.%s' since "
elessair 0:f269e3021894 222 % (element, self.name, attrname)) +
elessair 0:f269e3021894 223 "it doesn't exist")
elessair 0:f269e3021894 224 starting_value.remove(name_def_map[element])
elessair 0:f269e3021894 225 return starting_value
elessair 0:f269e3021894 226
elessair 0:f269e3021894 227 def __getattr_helper(self, attrname):
elessair 0:f269e3021894 228 """Compute the value of a given target attribute"""
elessair 0:f269e3021894 229 if attrname in CUMULATIVE_ATTRIBUTES:
elessair 0:f269e3021894 230 return self.__getattr_cumulative(attrname)
elessair 0:f269e3021894 231 else:
elessair 0:f269e3021894 232 tdata = self.json_data
elessair 0:f269e3021894 233 starting_value = None
elessair 0:f269e3021894 234 for tgt in self.resolution_order:
elessair 0:f269e3021894 235 data = tdata[tgt[0]]
elessair 0:f269e3021894 236 if data.has_key(attrname):
elessair 0:f269e3021894 237 starting_value = data[attrname]
elessair 0:f269e3021894 238 break
elessair 0:f269e3021894 239 else: # Attribute not found
elessair 0:f269e3021894 240 raise AttributeError(
elessair 0:f269e3021894 241 "Attribute '%s' not found in target '%s'"
elessair 0:f269e3021894 242 % (attrname, self.name))
elessair 0:f269e3021894 243 # 'progen' needs the full path to the template (the path in JSON is
elessair 0:f269e3021894 244 # relative to tools/export)
elessair 0:f269e3021894 245 if attrname == "progen":
elessair 0:f269e3021894 246 return self.__add_paths_to_progen(starting_value)
elessair 0:f269e3021894 247 else:
elessair 0:f269e3021894 248 return starting_value
elessair 0:f269e3021894 249
elessair 0:f269e3021894 250 def __getattr__(self, attrname):
elessair 0:f269e3021894 251 """ Return the value of an attribute. This function only computes the
elessair 0:f269e3021894 252 attribute's value once, then adds it to the instance attributes (in
elessair 0:f269e3021894 253 __dict__), so the next time it is returned directly
elessair 0:f269e3021894 254 """
elessair 0:f269e3021894 255 result = self.__getattr_helper(attrname)
elessair 0:f269e3021894 256 self.__dict__[attrname] = result
elessair 0:f269e3021894 257 return result
elessair 0:f269e3021894 258
elessair 0:f269e3021894 259 @staticmethod
elessair 0:f269e3021894 260 @cached
elessair 0:f269e3021894 261 def get_target(target_name):
elessair 0:f269e3021894 262 """ Return the target instance starting from the target name """
elessair 0:f269e3021894 263 return target(target_name, Target.get_json_target_data())
elessair 0:f269e3021894 264
elessair 0:f269e3021894 265
elessair 0:f269e3021894 266 @property
elessair 0:f269e3021894 267 def program_cycle_s(self):
elessair 0:f269e3021894 268 """Special override for program_cycle_s as it's default value depends
elessair 0:f269e3021894 269 upon is_disk_virtual
elessair 0:f269e3021894 270 """
elessair 0:f269e3021894 271 try:
elessair 0:f269e3021894 272 return self.__getattr__("program_cycle_s")
elessair 0:f269e3021894 273 except AttributeError:
elessair 0:f269e3021894 274 return 4 if self.is_disk_virtual else 1.5
elessair 0:f269e3021894 275
elessair 0:f269e3021894 276 @property
elessair 0:f269e3021894 277 def labels(self):
elessair 0:f269e3021894 278 """Get all possible labels for this target"""
elessair 0:f269e3021894 279 labels = [self.name] + CORE_LABELS[self.core] + self.extra_labels
elessair 0:f269e3021894 280 # Automatically define UVISOR_UNSUPPORTED if the target doesn't
elessair 0:f269e3021894 281 # specifically define UVISOR_SUPPORTED
elessair 0:f269e3021894 282 if "UVISOR_SUPPORTED" not in labels:
elessair 0:f269e3021894 283 labels.append("UVISOR_UNSUPPORTED")
elessair 0:f269e3021894 284 return labels
elessair 0:f269e3021894 285
elessair 0:f269e3021894 286 def init_hooks(self, hook, toolchain_name):
elessair 0:f269e3021894 287 """Initialize the post-build hooks for a toolchain. For now, this
elessair 0:f269e3021894 288 function only allows "post binary" hooks (hooks that are executed
elessair 0:f269e3021894 289 after the binary image is extracted from the executable file)
elessair 0:f269e3021894 290 """
elessair 0:f269e3021894 291
elessair 0:f269e3021894 292 # If there's no hook, simply return
elessair 0:f269e3021894 293 try:
elessair 0:f269e3021894 294 hook_data = self.post_binary_hook
elessair 0:f269e3021894 295 except AttributeError:
elessair 0:f269e3021894 296 return
elessair 0:f269e3021894 297 # A hook was found. The hook's name is in the format
elessair 0:f269e3021894 298 # "classname.functionname"
elessair 0:f269e3021894 299 temp = hook_data["function"].split(".")
elessair 0:f269e3021894 300 if len(temp) != 2:
elessair 0:f269e3021894 301 raise HookError(
elessair 0:f269e3021894 302 ("Invalid format for hook '%s' in target '%s'"
elessair 0:f269e3021894 303 % (hook_data["function"], self.name)) +
elessair 0:f269e3021894 304 " (must be 'class_name.function_name')")
elessair 0:f269e3021894 305 class_name, function_name = temp[0], temp[1]
elessair 0:f269e3021894 306 # "class_name" must refer to a class in this file, so check if the
elessair 0:f269e3021894 307 # class exists
elessair 0:f269e3021894 308 mdata = self.get_module_data()
elessair 0:f269e3021894 309 if not mdata.has_key(class_name) or \
elessair 0:f269e3021894 310 not inspect.isclass(mdata[class_name]):
elessair 0:f269e3021894 311 raise HookError(
elessair 0:f269e3021894 312 ("Class '%s' required by '%s' in target '%s'"
elessair 0:f269e3021894 313 % (class_name, hook_data["function"], self.name)) +
elessair 0:f269e3021894 314 " not found in targets.py")
elessair 0:f269e3021894 315 # "function_name" must refer to a static function inside class
elessair 0:f269e3021894 316 # "class_name"
elessair 0:f269e3021894 317 cls = mdata[class_name]
elessair 0:f269e3021894 318 if (not hasattr(cls, function_name)) or \
elessair 0:f269e3021894 319 (not inspect.isfunction(getattr(cls, function_name))):
elessair 0:f269e3021894 320 raise HookError(
elessair 0:f269e3021894 321 ("Static function '%s' " % function_name) +
elessair 0:f269e3021894 322 ("required by '%s' " % hook_data["function"]) +
elessair 0:f269e3021894 323 ("in target '%s' " % self.name) +
elessair 0:f269e3021894 324 ("not found in class '%s'" % class_name))
elessair 0:f269e3021894 325 # Check if the hook specification also has target restrictions
elessair 0:f269e3021894 326 toolchain_restrictions = hook_data.get("toolchains", [])
elessair 0:f269e3021894 327 if toolchain_restrictions and \
elessair 0:f269e3021894 328 (toolchain_name not in toolchain_restrictions):
elessair 0:f269e3021894 329 return
elessair 0:f269e3021894 330 # Finally, hook the requested function
elessair 0:f269e3021894 331 hook.hook_add_binary("post", getattr(cls, function_name))
elessair 0:f269e3021894 332
elessair 0:f269e3021894 333 ################################################################################
elessair 0:f269e3021894 334 # Target specific code goes in this section
elessair 0:f269e3021894 335 # This code can be invoked from the target description using the
elessair 0:f269e3021894 336 # "post_binary_hook" key
elessair 0:f269e3021894 337
elessair 0:f269e3021894 338 class LPCTargetCode(object):
elessair 0:f269e3021894 339 """General LPC Target patching code"""
elessair 0:f269e3021894 340 @staticmethod
elessair 0:f269e3021894 341 def lpc_patch(t_self, resources, elf, binf):
elessair 0:f269e3021894 342 """Patch an elf file"""
elessair 0:f269e3021894 343 t_self.debug("LPC Patch: %s" % os.path.split(binf)[1])
elessair 0:f269e3021894 344 patch(binf)
elessair 0:f269e3021894 345
elessair 0:f269e3021894 346 class LPC4088Code(object):
elessair 0:f269e3021894 347 """Code specific to the LPC4088"""
elessair 0:f269e3021894 348 @staticmethod
elessair 0:f269e3021894 349 def binary_hook(t_self, resources, elf, binf):
elessair 0:f269e3021894 350 """Hook to be run after an elf file is built"""
elessair 0:f269e3021894 351 if not os.path.isdir(binf):
elessair 0:f269e3021894 352 # Regular binary file, nothing to do
elessair 0:f269e3021894 353 LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
elessair 0:f269e3021894 354 return
elessair 0:f269e3021894 355 outbin = open(binf + ".temp", "wb")
elessair 0:f269e3021894 356 partf = open(os.path.join(binf, "ER_IROM1"), "rb")
elessair 0:f269e3021894 357 # Pad the fist part (internal flash) with 0xFF to 512k
elessair 0:f269e3021894 358 data = partf.read()
elessair 0:f269e3021894 359 outbin.write(data)
elessair 0:f269e3021894 360 outbin.write('\xFF' * (512*1024 - len(data)))
elessair 0:f269e3021894 361 partf.close()
elessair 0:f269e3021894 362 # Read and append the second part (external flash) in chunks of fixed
elessair 0:f269e3021894 363 # size
elessair 0:f269e3021894 364 chunksize = 128 * 1024
elessair 0:f269e3021894 365 partf = open(os.path.join(binf, "ER_IROM2"), "rb")
elessair 0:f269e3021894 366 while True:
elessair 0:f269e3021894 367 data = partf.read(chunksize)
elessair 0:f269e3021894 368 outbin.write(data)
elessair 0:f269e3021894 369 if len(data) < chunksize:
elessair 0:f269e3021894 370 break
elessair 0:f269e3021894 371 partf.close()
elessair 0:f269e3021894 372 outbin.close()
elessair 0:f269e3021894 373 # Remove the directory with the binary parts and rename the temporary
elessair 0:f269e3021894 374 # file to 'binf'
elessair 0:f269e3021894 375 shutil.rmtree(binf, True)
elessair 0:f269e3021894 376 os.rename(binf + '.temp', binf)
elessair 0:f269e3021894 377 t_self.debug("Generated custom binary file (internal flash + SPIFI)")
elessair 0:f269e3021894 378 LPCTargetCode.lpc_patch(t_self, resources, elf, binf)
elessair 0:f269e3021894 379
elessair 0:f269e3021894 380 class TEENSY3_1Code(object):
elessair 0:f269e3021894 381 """Hooks for the TEENSY3.1"""
elessair 0:f269e3021894 382 @staticmethod
elessair 0:f269e3021894 383 def binary_hook(t_self, resources, elf, binf):
elessair 0:f269e3021894 384 """Hook that is run after elf is generated"""
elessair 0:f269e3021894 385 from intelhex import IntelHex
elessair 0:f269e3021894 386 binh = IntelHex()
elessair 0:f269e3021894 387 binh.loadbin(binf, offset=0)
elessair 0:f269e3021894 388
elessair 0:f269e3021894 389 with open(binf.replace(".bin", ".hex"), "w") as file_desc:
elessair 0:f269e3021894 390 binh.tofile(file_desc, format='hex')
elessair 0:f269e3021894 391
elessair 0:f269e3021894 392 class MTSCode(object):
elessair 0:f269e3021894 393 """Generic MTS code"""
elessair 0:f269e3021894 394 @staticmethod
elessair 0:f269e3021894 395 def _combine_bins_helper(target_name, binf):
elessair 0:f269e3021894 396 """combine bins with the bootloader for a particular target"""
elessair 0:f269e3021894 397 loader = os.path.join(TOOLS_BOOTLOADERS, target_name, "bootloader.bin")
elessair 0:f269e3021894 398 target = binf + ".tmp"
elessair 0:f269e3021894 399 if not os.path.exists(loader):
elessair 0:f269e3021894 400 print "Can't find bootloader binary: " + loader
elessair 0:f269e3021894 401 return
elessair 0:f269e3021894 402 outbin = open(target, 'w+b')
elessair 0:f269e3021894 403 part = open(loader, 'rb')
elessair 0:f269e3021894 404 data = part.read()
elessair 0:f269e3021894 405 outbin.write(data)
elessair 0:f269e3021894 406 outbin.write('\xFF' * (64*1024 - len(data)))
elessair 0:f269e3021894 407 part.close()
elessair 0:f269e3021894 408 part = open(binf, 'rb')
elessair 0:f269e3021894 409 data = part.read()
elessair 0:f269e3021894 410 outbin.write(data)
elessair 0:f269e3021894 411 part.close()
elessair 0:f269e3021894 412 outbin.seek(0, 0)
elessair 0:f269e3021894 413 data = outbin.read()
elessair 0:f269e3021894 414 outbin.seek(0, 1)
elessair 0:f269e3021894 415 crc = struct.pack('<I', binascii.crc32(data) & 0xFFFFFFFF)
elessair 0:f269e3021894 416 outbin.write(crc)
elessair 0:f269e3021894 417 outbin.close()
elessair 0:f269e3021894 418 os.remove(binf)
elessair 0:f269e3021894 419 os.rename(target, binf)
elessair 0:f269e3021894 420
elessair 0:f269e3021894 421 @staticmethod
elessair 0:f269e3021894 422 def combine_bins_mts_dot(t_self, resources, elf, binf):
elessair 0:f269e3021894 423 """A hook for the MTS MDOT"""
elessair 0:f269e3021894 424 MTSCode._combine_bins_helper("MTS_MDOT_F411RE", binf)
elessair 0:f269e3021894 425
elessair 0:f269e3021894 426 @staticmethod
elessair 0:f269e3021894 427 def combine_bins_mts_dragonfly(t_self, resources, elf, binf):
elessair 0:f269e3021894 428 """A hoof for the MTS Dragonfly"""
elessair 0:f269e3021894 429 MTSCode._combine_bins_helper("MTS_DRAGONFLY_F411RE", binf)
elessair 0:f269e3021894 430
elessair 0:f269e3021894 431 class MCU_NRF51Code(object):
elessair 0:f269e3021894 432 """NRF51 Hooks"""
elessair 0:f269e3021894 433 @staticmethod
elessair 0:f269e3021894 434 def binary_hook(t_self, resources, _, binf):
elessair 0:f269e3021894 435 """Hook that merges the soft device with the bin file"""
elessair 0:f269e3021894 436 # Scan to find the actual paths of soft device
elessair 0:f269e3021894 437 sdf = None
elessair 0:f269e3021894 438 for softdevice_and_offset_entry\
elessair 0:f269e3021894 439 in t_self.target.EXPECTED_SOFTDEVICES_WITH_OFFSETS:
elessair 0:f269e3021894 440 for hexf in resources.hex_files:
elessair 0:f269e3021894 441 if hexf.find(softdevice_and_offset_entry['name']) != -1:
elessair 0:f269e3021894 442 t_self.debug("SoftDevice file found %s."
elessair 0:f269e3021894 443 % softdevice_and_offset_entry['name'])
elessair 0:f269e3021894 444 sdf = hexf
elessair 0:f269e3021894 445
elessair 0:f269e3021894 446 if sdf is not None:
elessair 0:f269e3021894 447 break
elessair 0:f269e3021894 448 if sdf is not None:
elessair 0:f269e3021894 449 break
elessair 0:f269e3021894 450
elessair 0:f269e3021894 451 if sdf is None:
elessair 0:f269e3021894 452 t_self.debug("Hex file not found. Aborting.")
elessair 0:f269e3021894 453 return
elessair 0:f269e3021894 454
elessair 0:f269e3021894 455 # Look for bootloader file that matches this soft device or bootloader
elessair 0:f269e3021894 456 # override image
elessair 0:f269e3021894 457 blf = None
elessair 0:f269e3021894 458 if t_self.target.MERGE_BOOTLOADER is True:
elessair 0:f269e3021894 459 for hexf in resources.hex_files:
elessair 0:f269e3021894 460 if hexf.find(t_self.target.OVERRIDE_BOOTLOADER_FILENAME) != -1:
elessair 0:f269e3021894 461 t_self.debug("Bootloader file found %s."
elessair 0:f269e3021894 462 % t_self.target.OVERRIDE_BOOTLOADER_FILENAME)
elessair 0:f269e3021894 463 blf = hexf
elessair 0:f269e3021894 464 break
elessair 0:f269e3021894 465 elif hexf.find(softdevice_and_offset_entry['boot']) != -1:
elessair 0:f269e3021894 466 t_self.debug("Bootloader file found %s."
elessair 0:f269e3021894 467 % softdevice_and_offset_entry['boot'])
elessair 0:f269e3021894 468 blf = hexf
elessair 0:f269e3021894 469 break
elessair 0:f269e3021894 470
elessair 0:f269e3021894 471 # Merge user code with softdevice
elessair 0:f269e3021894 472 from intelhex import IntelHex
elessair 0:f269e3021894 473 binh = IntelHex()
elessair 0:f269e3021894 474 binh.loadbin(binf, offset=softdevice_and_offset_entry['offset'])
elessair 0:f269e3021894 475
elessair 0:f269e3021894 476 if t_self.target.MERGE_SOFT_DEVICE is True:
elessair 0:f269e3021894 477 t_self.debug("Merge SoftDevice file %s"
elessair 0:f269e3021894 478 % softdevice_and_offset_entry['name'])
elessair 0:f269e3021894 479 sdh = IntelHex(sdf)
elessair 0:f269e3021894 480 binh.merge(sdh)
elessair 0:f269e3021894 481
elessair 0:f269e3021894 482 if t_self.target.MERGE_BOOTLOADER is True and blf is not None:
elessair 0:f269e3021894 483 t_self.debug("Merge BootLoader file %s" % blf)
elessair 0:f269e3021894 484 blh = IntelHex(blf)
elessair 0:f269e3021894 485 binh.merge(blh)
elessair 0:f269e3021894 486
elessair 0:f269e3021894 487 with open(binf.replace(".bin", ".hex"), "w") as fileout:
elessair 0:f269e3021894 488 binh.tofile(fileout, format='hex')
elessair 0:f269e3021894 489
elessair 0:f269e3021894 490 class NCS36510TargetCode:
elessair 0:f269e3021894 491 @staticmethod
elessair 0:f269e3021894 492 def ncs36510_addfib(t_self, resources, elf, binf):
elessair 0:f269e3021894 493 from tools.add_fib import add_fib_at_start
elessair 0:f269e3021894 494 print("binf ", binf)
elessair 0:f269e3021894 495 add_fib_at_start(binf[:-4])
elessair 0:f269e3021894 496 ################################################################################
elessair 0:f269e3021894 497
elessair 0:f269e3021894 498 # Instantiate all public targets
elessair 0:f269e3021894 499 TARGETS = [Target.get_target(name) for name, value
elessair 0:f269e3021894 500 in Target.get_json_target_data().items()
elessair 0:f269e3021894 501 if value.get("public", True)]
elessair 0:f269e3021894 502
elessair 0:f269e3021894 503 # Map each target name to its unique instance
elessair 0:f269e3021894 504 TARGET_MAP = dict([(t.name, t) for t in TARGETS])
elessair 0:f269e3021894 505
elessair 0:f269e3021894 506 TARGET_NAMES = TARGET_MAP.keys()
elessair 0:f269e3021894 507
elessair 0:f269e3021894 508 # Some targets with different name have the same exporters
elessair 0:f269e3021894 509 EXPORT_MAP = {}
elessair 0:f269e3021894 510
elessair 0:f269e3021894 511 # Detection APIs
elessair 0:f269e3021894 512 def get_target_detect_codes():
elessair 0:f269e3021894 513 """ Returns dictionary mapping detect_code -> platform_name
elessair 0:f269e3021894 514 """
elessair 0:f269e3021894 515 result = {}
elessair 0:f269e3021894 516 for tgt in TARGETS:
elessair 0:f269e3021894 517 for detect_code in tgt.detect_code:
elessair 0:f269e3021894 518 result[detect_code] = tgt.name
elessair 0:f269e3021894 519 return result
elessair 0:f269e3021894 520
elessair 0:f269e3021894 521 def set_targets_json_location(location=None):
elessair 0:f269e3021894 522 """Sets the location of the JSON file that contains the targets"""
elessair 0:f269e3021894 523 # First instruct Target about the new location
elessair 0:f269e3021894 524 Target.set_targets_json_location(location)
elessair 0:f269e3021894 525 # Then re-initialize TARGETS, TARGET_MAP and TARGET_NAMES. The
elessair 0:f269e3021894 526 # re-initialization does not create new variables, it keeps the old ones
elessair 0:f269e3021894 527 # instead. This ensures compatibility with code that does
elessair 0:f269e3021894 528 # "from tools.targets import TARGET_NAMES"
elessair 0:f269e3021894 529 TARGETS[:] = [Target.get_target(tgt) for tgt, obj
elessair 0:f269e3021894 530 in Target.get_json_target_data().items()
elessair 0:f269e3021894 531 if obj.get("public", True)]
elessair 0:f269e3021894 532 TARGET_MAP.clear()
elessair 0:f269e3021894 533 TARGET_MAP.update(dict([(tgt.name, tgt) for tgt in TARGETS]))
elessair 0:f269e3021894 534 TARGET_NAMES[:] = TARGET_MAP.keys()