joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers make.py Source File

make.py

00001 #! /usr/bin/env python2
00002 """
00003 mbed SDK
00004 Copyright (c) 2011-2013 ARM Limited
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may 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,
00014 WITHOUT 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 TEST BUILD & RUN
00020 """
00021 import sys
00022 from time import sleep
00023 from shutil import copy
00024 from os.path import join, abspath, dirname, isfile, isdir
00025 
00026 # Be sure that the tools directory is in the search path
00027 ROOT = abspath(join(dirname(__file__), ".."))
00028 sys.path.insert(0, ROOT)
00029 
00030 from tools.utils import args_error
00031 from tools.paths import BUILD_DIR
00032 from tools.paths import RTOS_LIBRARIES
00033 from tools.paths import RPC_LIBRARY
00034 from tools.paths import ETH_LIBRARY
00035 from tools.paths import USB_HOST_LIBRARIES, USB_LIBRARIES
00036 from tools.paths import DSP_LIBRARIES
00037 from tools.paths import FS_LIBRARY
00038 from tools.paths import UBLOX_LIBRARY
00039 from tools.tests import TESTS, Test, TEST_MAP
00040 from tools.tests import TEST_MBED_LIB
00041 from tools.tests import test_known, test_name_known
00042 from tools.targets import TARGET_MAP
00043 from tools.options import get_default_options_parser
00044 from tools.build_api import build_project
00045 from tools.build_api import mcu_toolchain_matrix
00046 from utils import argparse_filestring_type
00047 from utils import argparse_many
00048 from utils import argparse_dir_not_parent
00049 from argparse import ArgumentTypeError
00050 from tools.toolchains import mbedToolchain
00051 from tools.settings import CLI_COLOR_MAP
00052 
00053 if __name__ == '__main__':
00054     # Parse Options
00055     parser = get_default_options_parser(add_app_config=True)
00056     group = parser.add_mutually_exclusive_group(required=False)
00057     group.add_argument("-p",
00058                       type=argparse_many(test_known),
00059                       dest="program",
00060                       help="The index of the desired test program: [0-%d]" % (len(TESTS)-1))
00061 
00062     group.add_argument("-n",
00063                        type=argparse_many(test_name_known),
00064                       dest="program",
00065                       help="The name of the desired test program")
00066 
00067     parser.add_argument("-j", "--jobs",
00068                       type=int,
00069                       dest="jobs",
00070                       default=0,
00071                       help="Number of concurrent jobs. Default: 0/auto (based on host machine's number of CPUs)")
00072 
00073     parser.add_argument("-v", "--verbose",
00074                       action="store_true",
00075                       dest="verbose",
00076                       default=False,
00077                       help="Verbose diagnostic output")
00078 
00079     parser.add_argument("--silent",
00080                       action="store_true",
00081                       dest="silent",
00082                       default=False,
00083                       help="Silent diagnostic output (no copy, compile notification)")
00084 
00085     parser.add_argument("-D",
00086                       action="append",
00087                       dest="macros",
00088                       help="Add a macro definition")
00089 
00090     group.add_argument("-S", "--supported-toolchains",
00091                       action="store_true",
00092                       dest="supported_toolchains",
00093                       default=False,
00094                       help="Displays supported matrix of MCUs and toolchains")
00095 
00096     parser.add_argument('-f', '--filter',
00097                       dest='general_filter_regex',
00098                       default=None,
00099                       help='For some commands you can use filter to filter out results')
00100 
00101     # Local run
00102     parser.add_argument("--automated", action="store_true", dest="automated",
00103                       default=False, help="Automated test")
00104     parser.add_argument("--host", dest="host_test",
00105                       default=None, help="Host test")
00106     parser.add_argument("--extra", dest="extra",
00107                       default=None, help="Extra files")
00108     parser.add_argument("--peripherals", dest="peripherals",
00109                       default=None, help="Required peripherals")
00110     parser.add_argument("--dep", dest="dependencies",
00111                       default=None, help="Dependencies")
00112     parser.add_argument("--source", dest="source_dir", type=argparse_filestring_type,
00113                        default=None, help="The source (input) directory", action="append")
00114     parser.add_argument("--duration", type=int, dest="duration",
00115                       default=None, help="Duration of the test")
00116     parser.add_argument("--build", dest="build_dir", type=argparse_dir_not_parent(ROOT),
00117                       default=None, help="The build (output) directory")
00118     parser.add_argument("-N", "--artifact-name", dest="artifact_name",
00119                       default=None, help="The built project's name")
00120     parser.add_argument("-d", "--disk", dest="disk",
00121                       default=None, help="The mbed disk")
00122     parser.add_argument("-s", "--serial", dest="serial",
00123                       default=None, help="The mbed serial port")
00124     parser.add_argument("-b", "--baud", type=int, dest="baud",
00125                       default=None, help="The mbed serial baud rate")
00126     group.add_argument("-L", "--list-tests", action="store_true", dest="list_tests",
00127                       default=False, help="List available tests in order and exit")
00128 
00129     # Ideally, all the tests with a single "main" thread can be run with, or
00130     # without the rtos, eth, usb_host, usb, dsp, fat, ublox
00131     parser.add_argument("--rtos",
00132                       action="store_true", dest="rtos",
00133                       default=False, help="Link with RTOS library")
00134 
00135     parser.add_argument("--rpc",
00136                       action="store_true", dest="rpc",
00137                       default=False, help="Link with RPC library")
00138 
00139     parser.add_argument("--eth",
00140                       action="store_true", dest="eth",
00141                       default=False,
00142                       help="Link with Ethernet library")
00143 
00144     parser.add_argument("--usb_host",
00145                       action="store_true",
00146                       dest="usb_host",
00147                       default=False,
00148                       help="Link with USB Host library")
00149 
00150     parser.add_argument("--usb",
00151                       action="store_true",
00152                       dest="usb",
00153                       default=False,
00154                       help="Link with USB Device library")
00155 
00156     parser.add_argument("--dsp",
00157                       action="store_true",
00158                       dest="dsp",
00159                       default=False,
00160                       help="Link with DSP library")
00161 
00162     parser.add_argument("--fat",
00163                       action="store_true",
00164                       dest="fat",
00165                       default=False,
00166                       help="Link with FS ad SD card file system library")
00167 
00168     parser.add_argument("--ublox",
00169                       action="store_true",
00170                       dest="ublox",
00171                       default=False,
00172                       help="Link with U-Blox library")
00173 
00174     parser.add_argument("--testlib",
00175                       action="store_true",
00176                       dest="testlib",
00177                       default=False,
00178                       help="Link with mbed test library")
00179 
00180     # Specify a different linker script
00181     parser.add_argument("-l", "--linker", dest="linker_script",
00182                       type=argparse_filestring_type,
00183                       default=None, help="use the specified linker script")
00184 
00185     options = parser.parse_args()
00186 
00187     # Only prints matrix of supported toolchains
00188     if options.supported_toolchains:
00189         print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
00190         exit(0)
00191 
00192     # Print available tests in order and exit
00193     if options.list_tests is True:
00194         print '\n'.join(map(str, sorted(TEST_MAP.values())))
00195         sys.exit()
00196 
00197     # force program to "0" if a source dir is specified
00198     if options.source_dir is not None:
00199         p = 0
00200     else:
00201     # Program Number or name
00202         p = options.program
00203 
00204     # If 'p' was set via -n to list of numbers make this a single element integer list
00205     if type(p) != type([]):
00206         p = [p]
00207 
00208     # Target
00209     if options.mcu is None :
00210         args_error(parser, "argument -m/--mcu is required")
00211     mcu = options.mcu[0]
00212 
00213     # Toolchain
00214     if options.tool is None:
00215         args_error(parser, "argument -t/--tool is required")
00216     toolchain = options.tool[0]
00217 
00218     if (options.program is None) and (not options.source_dir):
00219         args_error(parser, "one of -p, -n, or --source is required")
00220 
00221     if options.source_dir and not options.build_dir:
00222         args_error(parser, "argument --build is required when argument --source is provided")
00223 
00224     if options.color:
00225         # This import happens late to prevent initializing colorization when we don't need it
00226         import colorize
00227         if options.verbose:
00228             notify = mbedToolchain.print_notify_verbose
00229         else:
00230             notify = mbedToolchain.print_notify
00231         notify = colorize.print_in_color_notifier(CLI_COLOR_MAP, notify)
00232     else:
00233         notify = None
00234 
00235     # Test
00236     for test_no in p:
00237         test = Test(test_no)
00238         if options.automated is not None:    test.automated = options.automated
00239         if options.dependencies is not None: test.dependencies = options.dependencies
00240         if options.host_test is not None:    test.host_test = options.host_test;
00241         if options.peripherals is not None:  test.peripherals = options.peripherals;
00242         if options.duration is not None:     test.duration = options.duration;
00243         if options.extra is not None:        test.extra_files = options.extra
00244 
00245         if not test.is_supported(mcu, toolchain):
00246             print 'The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain)
00247             sys.exit()
00248 
00249         # Linking with extra libraries
00250         if options.rtos:     test.dependencies.append(RTOS_LIBRARIES)
00251         if options.rpc:      test.dependencies.append(RPC_LIBRARY)
00252         if options.eth:      test.dependencies.append(ETH_LIBRARY)
00253         if options.usb_host: test.dependencies.append(USB_HOST_LIBRARIES)
00254         if options.usb:      test.dependencies.append(USB_LIBRARIES)
00255         if options.dsp:      test.dependencies.append(DSP_LIBRARIES)
00256         if options.fat:      test.dependencies.append(FS_LIBRARY)
00257         if options.ublox:    test.dependencies.append(UBLOX_LIBRARY)
00258         if options.testlib:  test.dependencies.append(TEST_MBED_LIB)
00259 
00260         build_dir = join(BUILD_DIR, "test", mcu, toolchain, test.id)
00261         if options.source_dir is not None:
00262             test.source_dir = options.source_dir
00263             build_dir = options.source_dir
00264 
00265         if options.build_dir is not None:
00266             build_dir = options.build_dir
00267 
00268         try:
00269             bin_file = build_project(test.source_dir, build_dir, mcu, toolchain, test.dependencies, options.options,
00270                                      linker_script=options.linker_script,
00271                                      clean=options.clean,
00272                                      verbose=options.verbose,
00273                                      notify=notify,
00274                                      silent=options.silent,
00275                                      macros=options.macros,
00276                                      jobs=options.jobs,
00277                                      name=options.artifact_name,
00278                                      app_config=options.app_config)
00279             print 'Image: %s'% bin_file
00280 
00281             if options.disk:
00282                 # Simple copy to the mbed disk
00283                 copy(bin_file, options.disk)
00284 
00285             if options.serial:
00286                 # Import pyserial: https://pypi.python.org/pypi/pyserial
00287                 from serial import Serial
00288 
00289                 sleep(TARGET_MAP[mcu].program_cycle_s)
00290 
00291                 serial = Serial(options.serial, timeout = 1)
00292                 if options.baud:
00293                     serial.setBaudrate(options.baud)
00294 
00295                 serial.flushInput()
00296                 serial.flushOutput()
00297 
00298                 try:
00299                     serial.sendBreak()
00300                 except:
00301                     # In linux a termios.error is raised in sendBreak and in setBreak.
00302                     # The following setBreak() is needed to release the reset signal on the target mcu.
00303                     try:
00304                         serial.setBreak(False)
00305                     except:
00306                         pass
00307 
00308                 while True:
00309                     c = serial.read(512)
00310                     sys.stdout.write(c)
00311                     sys.stdout.flush()
00312 
00313         except KeyboardInterrupt, e:
00314             print "\n[CTRL+c] exit"
00315         except Exception,e:
00316             if options.verbose:
00317                 import traceback
00318                 traceback.print_exc(file=sys.stdout)
00319             else:
00320                 print "[ERROR] %s" % str(e)
00321             
00322             sys.exit(1)