Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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