Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers target.py Source File

target.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018  
00019 from __future__ import absolute_import
00020 import os
00021 import time
00022 import info
00023 import mbedapi
00024 from test_info import TestInfoStub
00025 from intelhex import IntelHex
00026 
00027 TEST_REPO = 'https://developer.mbed.org/users/c1728p9/code/daplink-validation/'
00028 
00029 
00030 def load_target_bundle(directory):
00031     return TargetBundle(directory)
00032 
00033 
00034 class TargetBundle(object):
00035 
00036     def __init__(self, directory):
00037         dir_contents = os.listdir(directory)
00038         name_to_target = {}
00039         for name in dir_contents:
00040             base_name, extension = os.path.splitext(name)
00041             path = directory + os.sep + name
00042             if os.path.isdir(path):
00043                 # Directories are unused
00044                 pass
00045             elif os.path.isfile(path):
00046                 if extension not in ('.bin', '.hex'):
00047                     continue
00048                 if base_name not in name_to_target:
00049                     name_to_target[base_name] = Target(base_name)
00050                 if extension == '.bin':
00051                     name_to_target[base_name].set_bin_path(path)
00052                 elif extension == '.hex':
00053                     name_to_target[base_name].set_hex_path(path)
00054                 else:
00055                     # Unsupported file type
00056                     pass
00057             else:
00058                 assert False
00059         all_targets = list(name_to_target.values())
00060         self._target_list = [target for target in all_targets if target.valid]
00061 
00062     def get_target_list(self):
00063         """Return the target objects associated with this bundle"""
00064         return self._target_list
00065 
00066 
00067 def build_target_bundle(directory, username, password, parent_test=None):
00068     """Build target firmware package"""
00069     if parent_test is None:
00070         parent_test = TestInfoStub()
00071     target_names = info.TARGET_WITH_COMPILE_API_LIST
00072     for build_name in target_names:
00073         name_base = os.path.normpath(directory + os.sep + build_name)
00074         target_hex_path = name_base + '.hex'
00075         target_bin_path = name_base + '.bin'
00076 
00077         # Build target test image
00078         test_info = parent_test.create_subtest('Building target %s' %
00079                                                build_name)
00080         if not os.path.isdir(directory):
00081             os.mkdir(directory)
00082         # Remove previous build files
00083         if os.path.isfile(target_hex_path):
00084             os.remove(target_hex_path)
00085         if os.path.isfile(target_bin_path):
00086             os.remove(target_bin_path)
00087         test_info.info('Starting remote build')
00088         start = time.time()
00089         built_file = mbedapi.build_repo(username, password,
00090                                         TEST_REPO, build_name,
00091                                         directory)
00092         stop = time.time()
00093         test_info.info("Build took %s seconds" % (stop - start))
00094         extension = os.path.splitext(built_file)[1].lower()
00095         assert extension == '.hex' or extension == '.bin'
00096         if extension == '.hex':
00097             intel_hex = IntelHex(built_file)
00098             # Only supporting devices with the starting
00099             # address at 0 currently
00100             assert intel_hex.minaddr() == 0
00101             intel_hex.tobinfile(target_bin_path)
00102             os.rename(built_file, target_hex_path)
00103         if extension == '.bin':
00104             intel_hex = IntelHex()
00105             intel_hex.loadbin(built_file, offset=0)
00106             intel_hex.tofile(target_hex_path, 'hex')
00107             os.rename(built_file, target_bin_path)
00108 
00109         # Assert that required files are present
00110         assert os.path.isfile(target_hex_path)
00111         assert os.path.isfile(target_bin_path)
00112 
00113 
00114 class Target(object):
00115 
00116     def __init__(self, name, hex_path=None, bin_path=None):
00117         self._name = name
00118         self._valid = False
00119         self._hex_path = None
00120         self._bin_path = None
00121         if hex_path is not None:
00122             self.set_hex_path(hex_path)
00123         if bin_path is not None:
00124             self.set_bin_path(bin_path)
00125         self._valid = True
00126 
00127     def __str__(self):
00128         return "Name=%s" % self.name
00129 
00130     def set_hex_path(self, path):
00131         base_name = os.path.basename(path)
00132         assert self._hex_path is None
00133         assert base_name == self._name + '.hex'
00134         path = os.path.abspath(path)
00135         assert os.path.isfile(path)
00136         self._hex_path = path
00137 
00138     def set_bin_path(self, path):
00139         base_name = os.path.basename(path)
00140         assert self._bin_path is None
00141         assert base_name == self._name + '.bin'
00142         path = os.path.abspath(path)
00143         assert os.path.isfile(path)
00144         self._bin_path = path
00145 
00146     @property
00147     def valid(self):
00148         hex_valid = self._hex_path is not None
00149         bin_valid = self._bin_path is not None
00150         return hex_valid and bin_valid and self._valid
00151 
00152     @property
00153     def name(self):
00154         return self._name
00155 
00156     @property
00157     def hex_path(self):
00158         return self._hex_path
00159 
00160     @property
00161     def bin_path(self):
00162         return self._bin_path