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