Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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