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