1 #! /usr/bin/env python2
4 Copyright (c) 2011-2013 ARM Limited
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
22 from time import sleep
23 from shutil import copy
24 from os.path import join, abspath, dirname
26 # Be sure that the tools directory is in the search path
27 ROOT = abspath(join(dirname(__file__), ".."))
28 sys.path.insert(0, ROOT)
30 from workspace_tools.utils import args_error
31 from workspace_tools.paths import BUILD_DIR
32 from workspace_tools.paths import RTOS_LIBRARIES
33 from workspace_tools.paths import ETH_LIBRARY
34 from workspace_tools.paths import USB_HOST_LIBRARIES, USB_LIBRARIES
35 from workspace_tools.paths import DSP_LIBRARIES
36 from workspace_tools.paths import FS_LIBRARY
37 from workspace_tools.paths import UBLOX_LIBRARY
38 from workspace_tools.tests import TESTS, Test, TEST_MAP
39 from workspace_tools.tests import TEST_MBED_LIB
40 from workspace_tools.targets import TARGET_MAP
41 from workspace_tools.options import get_default_options_parser
42 from workspace_tools.build_api import build_project
44 import workspace_tools.private_settings as ps
49 if __name__ == '__main__':
51 parser = get_default_options_parser()
52 parser.add_option("-p",
55 help="The index of the desired test program: [0-%d]" % (len(TESTS)-1))
57 parser.add_option("-n",
59 help="The name of the desired test program")
61 parser.add_option("-j", "--jobs",
65 help="Number of concurrent jobs (default 1). Use 0 for auto based on host machine's number of CPUs")
67 parser.add_option("-v", "--verbose",
71 help="Verbose diagnostic output")
73 parser.add_option("--silent",
77 help="Silent diagnostic output (no copy, compile notification)")
79 parser.add_option("-D", "",
82 help="Add a macro definition")
85 parser.add_option("--automated", action="store_true", dest="automated",
86 default=False, help="Automated test")
87 parser.add_option("--host", dest="host_test",
88 default=None, help="Host test")
89 parser.add_option("--extra", dest="extra",
90 default=None, help="Extra files")
91 parser.add_option("--peripherals", dest="peripherals",
92 default=None, help="Required peripherals")
93 parser.add_option("--dep", dest="dependencies",
94 default=None, help="Dependencies")
95 parser.add_option("--source", dest="source_dir",
96 default=None, help="The source (input) directory")
97 parser.add_option("--duration", type="int", dest="duration",
98 default=None, help="Duration of the test")
99 parser.add_option("--build", dest="build_dir",
100 default=None, help="The build (output) directory")
101 parser.add_option("-d", "--disk", dest="disk",
102 default=None, help="The mbed disk")
103 parser.add_option("-s", "--serial", dest="serial",
104 default=None, help="The mbed serial port")
105 parser.add_option("-b", "--baud", type="int", dest="baud",
106 default=None, help="The mbed serial baud rate")
107 parser.add_option("-L", "--list-tests", action="store_true", dest="list_tests",
108 default=False, help="List available tests in order and exit")
110 # Ideally, all the tests with a single "main" thread can be run with, or
111 # without the rtos, eth, usb_host, usb, dsp, fat, ublox
112 parser.add_option("--rtos",
113 action="store_true", dest="rtos",
114 default=False, help="Link with RTOS library")
116 parser.add_option("--eth",
117 action="store_true", dest="eth",
119 help="Link with Ethernet library")
121 parser.add_option("--usb_host",
125 help="Link with USB Host library")
127 parser.add_option("--usb",
131 help="Link with USB Device library")
133 parser.add_option("--dsp",
137 help="Link with DSP library")
139 parser.add_option("--fat",
143 help="Link with FS ad SD card file system library")
145 parser.add_option("--ublox",
149 help="Link with U-Blox library")
151 parser.add_option("--testlib",
155 help="Link with mbed test library")
157 # Specify a different linker script
158 parser.add_option("-l", "--linker", dest="linker_script",
159 default=None, help="use the specified linker script")
161 (options, args) = parser.parse_args()
163 # Print available tests in order and exit
164 if options.list_tests is True:
165 print '\n'.join(map(str, sorted(TEST_MAP.values())))
168 # force program to "0" if a source dir is specified
169 if options.source_dir is not None:
173 # Program Number or name
174 p, n = options.program, options.program_name
176 if n is not None and p is not None:
177 args_error(parser, "[ERROR] specify either '-n' or '-p', not both")
179 # We will transform 'n' to list of 'p' (integers which are test numbers)
181 for test_id in nlist:
182 if test_id not in TEST_MAP.keys():
183 args_error(parser, "[ERROR] Program with name '%s' not found"% test_id)
185 p = [TEST_MAP[n].n for n in nlist]
186 elif p is None or (p < 0) or (p > (len(TESTS)-1)):
187 message = "[ERROR] You have to specify one of the following tests:\n"
188 message += '\n'.join(map(str, sorted(TEST_MAP.values())))
189 args_error(parser, message)
191 # If 'p' was set via -n to list of numbers make this a single element integer list
192 if type(p) != type([]):
196 if options.mcu is None :
197 args_error(parser, "[ERROR] You should specify an MCU")
201 if options.tool is None:
202 args_error(parser, "[ERROR] You should specify a TOOLCHAIN")
203 toolchain = options.tool
208 if options.automated is not None: test.automated = options.automated
209 if options.dependencies is not None: test.dependencies = options.dependencies
210 if options.host_test is not None: test.host_test = options.host_test;
211 if options.peripherals is not None: test.peripherals = options.peripherals;
212 if options.duration is not None: test.duration = options.duration;
213 if options.extra is not None: test.extra_files = options.extra
215 if not test.is_supported(mcu, toolchain):
216 print 'The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain)
219 # Linking with extra libraries
220 if options.rtos: test.dependencies.append(RTOS_LIBRARIES)
221 if options.eth: test.dependencies.append(ETH_LIBRARY)
222 if options.usb_host: test.dependencies.append(USB_HOST_LIBRARIES)
223 if options.usb: test.dependencies.append(USB_LIBRARIES)
224 if options.dsp: test.dependencies.append(DSP_LIBRARIES)
225 if options.fat: test.dependencies.append(FS_LIBRARY)
226 if options.ublox: test.dependencies.append(UBLOX_LIBRARY)
227 if options.testlib: test.dependencies.append(TEST_MBED_LIB)
229 build_dir = join(BUILD_DIR, "test", mcu, toolchain, test.id)
230 if options.source_dir is not None:
231 test.source_dir = options.source_dir
232 build_dir = options.source_dir
234 if options.build_dir is not None:
235 build_dir = options.build_dir
237 target = TARGET_MAP[mcu]
239 bin_file = build_project(test.source_dir, build_dir, target, toolchain, test.dependencies, options.options,
240 linker_script=options.linker_script,
242 verbose=options.verbose,
243 silent=options.silent,
244 macros=options.macros,
246 print 'Image: %s'% bin_file
249 # Simple copy to the mbed disk
250 copy(bin_file, options.disk)
253 # Import pyserial: https://pypi.python.org/pypi/pyserial
254 from serial import Serial
256 sleep(target.program_cycle_s())
258 serial = Serial(options.serial, timeout = 1)
260 serial.setBaudrate(options.baud)
268 # In linux a termios.error is raised in sendBreak and in setBreak.
269 # The following setBreak() is needed to release the reset signal on the target mcu.
271 serial.setBreak(False)
280 except KeyboardInterrupt, e:
281 print "\n[CTRL+c] exit"
285 traceback.print_exc(file=sys.stdout)
287 print "[ERROR] %s" % str(e)