|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | + mbed |
| 4 | + Copyright (c) 2017-2017 ARM Limited |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import print_function |
| 20 | +import sys |
| 21 | +import os |
| 22 | +import argparse |
| 23 | +from os.path import join, abspath, dirname |
| 24 | +from flash_algo import PackFlashAlgo |
| 25 | + |
| 26 | +# Be sure that the tools directory is in the search path |
| 27 | +ROOT = abspath(join(dirname(__file__), "..", "..")) |
| 28 | +sys.path.insert(0, ROOT) |
| 29 | + |
| 30 | +from tools.targets import TARGETS |
| 31 | +from tools.arm_pack_manager import Cache |
| 32 | + |
| 33 | +TEMPLATE_PATH = "c_blob_mbed.tmpl" |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + """Generate flash algorithms""" |
| 38 | + parser = argparse.ArgumentParser(description='Flash generator') |
| 39 | + parser.add_argument("--rebuild_all", action="store_true", |
| 40 | + help="Rebuild entire cache") |
| 41 | + parser.add_argument("--rebuild_descriptors", action="store_true", |
| 42 | + help="Rebuild descriptors") |
| 43 | + parser.add_argument("--target", default=None, |
| 44 | + help="Name of target to generate algo for") |
| 45 | + parser.add_argument("--all", action="store_true", |
| 46 | + help="Build all flash algos for devcies") |
| 47 | + args = parser.parse_args() |
| 48 | + |
| 49 | + cache = Cache(True, True) |
| 50 | + if args.rebuild_all: |
| 51 | + cache.cache_everything() |
| 52 | + print("Cache rebuilt") |
| 53 | + return |
| 54 | + |
| 55 | + if args.rebuild_descriptors: |
| 56 | + cache.cache_descriptors() |
| 57 | + print("Descriptors rebuilt") |
| 58 | + return |
| 59 | + |
| 60 | + if args.target is None: |
| 61 | + device_and_filenames = [(target.device_name, target.name) for target |
| 62 | + in TARGETS if hasattr(target, "device_name")] |
| 63 | + else: |
| 64 | + device_and_filenames = [(args.target, args.target.replace("/", "-"))] |
| 65 | + |
| 66 | + try: |
| 67 | + os.mkdir("output") |
| 68 | + except OSError: |
| 69 | + # Directory already exists |
| 70 | + pass |
| 71 | + |
| 72 | + for device, filename in device_and_filenames: |
| 73 | + dev = cache.index[device] |
| 74 | + binaries = cache.get_flash_algorthim_binary(device, all=True) |
| 75 | + algos = [PackFlashAlgo(binary.read()) for binary in binaries] |
| 76 | + filtered_algos = algos if args.all else filter_algos(dev, algos) |
| 77 | + for idx, algo in enumerate(filtered_algos): |
| 78 | + file_name = ("%s_%i.c" % (filename, idx) |
| 79 | + if args.all or len(filtered_algos) != 1 |
| 80 | + else "%s.c" % filename) |
| 81 | + output_path = join("output", file_name) |
| 82 | + algo.process_template(TEMPLATE_PATH, output_path) |
| 83 | + print("%s: %s \r" % (device, filename)) |
| 84 | + |
| 85 | + |
| 86 | +def filter_algos(dev, algos): |
| 87 | + if "memory" not in dev: |
| 88 | + return algos |
| 89 | + if "IROM1" not in dev["memory"]: |
| 90 | + return algos |
| 91 | + if "IROM2" in dev["memory"]: |
| 92 | + return algos |
| 93 | + |
| 94 | + rom_rgn = dev["memory"]["IROM1"] |
| 95 | + try: |
| 96 | + start = int(rom_rgn["start"], 0) |
| 97 | + size = int(rom_rgn["size"], 0) |
| 98 | + except ValueError: |
| 99 | + return algos |
| 100 | + |
| 101 | + matching_algos = [algo for algo in algos if |
| 102 | + algo.flash_start == start and algo.flash_size == size] |
| 103 | + return matching_algos if len(matching_algos) == 1 else algos |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
0 commit comments