Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/tools/host_tests/host_test.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 | """ |
marcozecchini | 0:9fca2b23d0ba | 2 | mbed SDK |
marcozecchini | 0:9fca2b23d0ba | 3 | Copyright (c) 2011-2013 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 4 | |
marcozecchini | 0:9fca2b23d0ba | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 6 | you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 7 | You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 8 | |
marcozecchini | 0:9fca2b23d0ba | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 10 | |
marcozecchini | 0:9fca2b23d0ba | 11 | Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 14 | See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 15 | limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 16 | """ |
marcozecchini | 0:9fca2b23d0ba | 17 | |
marcozecchini | 0:9fca2b23d0ba | 18 | # Check if 'serial' module is installed |
marcozecchini | 0:9fca2b23d0ba | 19 | try: |
marcozecchini | 0:9fca2b23d0ba | 20 | from serial import Serial |
marcozecchini | 0:9fca2b23d0ba | 21 | except ImportError, e: |
marcozecchini | 0:9fca2b23d0ba | 22 | print "Error: Can't import 'serial' module: %s"% e |
marcozecchini | 0:9fca2b23d0ba | 23 | exit(-1) |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | import os |
marcozecchini | 0:9fca2b23d0ba | 26 | import re |
marcozecchini | 0:9fca2b23d0ba | 27 | import types |
marcozecchini | 0:9fca2b23d0ba | 28 | from sys import stdout |
marcozecchini | 0:9fca2b23d0ba | 29 | from time import sleep, time |
marcozecchini | 0:9fca2b23d0ba | 30 | from optparse import OptionParser |
marcozecchini | 0:9fca2b23d0ba | 31 | |
marcozecchini | 0:9fca2b23d0ba | 32 | import host_tests_plugins |
marcozecchini | 0:9fca2b23d0ba | 33 | |
marcozecchini | 0:9fca2b23d0ba | 34 | # This is a little tricky. We need to add upper directory to path so |
marcozecchini | 0:9fca2b23d0ba | 35 | # we can find packages we want from the same level as other files do |
marcozecchini | 0:9fca2b23d0ba | 36 | import sys |
marcozecchini | 0:9fca2b23d0ba | 37 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) |
marcozecchini | 0:9fca2b23d0ba | 38 | from tools.test_api import get_autodetected_MUTS_list |
marcozecchini | 0:9fca2b23d0ba | 39 | from tools.test_api import get_module_avail |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | |
marcozecchini | 0:9fca2b23d0ba | 42 | class Mbed: |
marcozecchini | 0:9fca2b23d0ba | 43 | """ Base class for a host driven test |
marcozecchini | 0:9fca2b23d0ba | 44 | """ |
marcozecchini | 0:9fca2b23d0ba | 45 | def __init__(self): |
marcozecchini | 0:9fca2b23d0ba | 46 | parser = OptionParser() |
marcozecchini | 0:9fca2b23d0ba | 47 | |
marcozecchini | 0:9fca2b23d0ba | 48 | parser.add_option("-m", "--micro", |
marcozecchini | 0:9fca2b23d0ba | 49 | dest="micro", |
marcozecchini | 0:9fca2b23d0ba | 50 | help="The target microcontroller", |
marcozecchini | 0:9fca2b23d0ba | 51 | metavar="MICRO") |
marcozecchini | 0:9fca2b23d0ba | 52 | |
marcozecchini | 0:9fca2b23d0ba | 53 | parser.add_option("-p", "--port", |
marcozecchini | 0:9fca2b23d0ba | 54 | dest="port", |
marcozecchini | 0:9fca2b23d0ba | 55 | help="The serial port of the target mbed", |
marcozecchini | 0:9fca2b23d0ba | 56 | metavar="PORT") |
marcozecchini | 0:9fca2b23d0ba | 57 | |
marcozecchini | 0:9fca2b23d0ba | 58 | parser.add_option("-d", "--disk", |
marcozecchini | 0:9fca2b23d0ba | 59 | dest="disk", |
marcozecchini | 0:9fca2b23d0ba | 60 | help="The target disk path", |
marcozecchini | 0:9fca2b23d0ba | 61 | metavar="DISK_PATH") |
marcozecchini | 0:9fca2b23d0ba | 62 | |
marcozecchini | 0:9fca2b23d0ba | 63 | parser.add_option("-f", "--image-path", |
marcozecchini | 0:9fca2b23d0ba | 64 | dest="image_path", |
marcozecchini | 0:9fca2b23d0ba | 65 | help="Path with target's image", |
marcozecchini | 0:9fca2b23d0ba | 66 | metavar="IMAGE_PATH") |
marcozecchini | 0:9fca2b23d0ba | 67 | |
marcozecchini | 0:9fca2b23d0ba | 68 | parser.add_option("-c", "--copy", |
marcozecchini | 0:9fca2b23d0ba | 69 | dest="copy_method", |
marcozecchini | 0:9fca2b23d0ba | 70 | help="Copy method selector", |
marcozecchini | 0:9fca2b23d0ba | 71 | metavar="COPY_METHOD") |
marcozecchini | 0:9fca2b23d0ba | 72 | |
marcozecchini | 0:9fca2b23d0ba | 73 | parser.add_option("-C", "--program_cycle_s", |
marcozecchini | 0:9fca2b23d0ba | 74 | dest="program_cycle_s", |
marcozecchini | 0:9fca2b23d0ba | 75 | help="Program cycle sleep. Define how many seconds you want wait after copying bianry onto target", |
marcozecchini | 0:9fca2b23d0ba | 76 | type="float", |
marcozecchini | 0:9fca2b23d0ba | 77 | metavar="COPY_METHOD") |
marcozecchini | 0:9fca2b23d0ba | 78 | |
marcozecchini | 0:9fca2b23d0ba | 79 | parser.add_option("-t", "--timeout", |
marcozecchini | 0:9fca2b23d0ba | 80 | dest="timeout", |
marcozecchini | 0:9fca2b23d0ba | 81 | help="Timeout", |
marcozecchini | 0:9fca2b23d0ba | 82 | metavar="TIMEOUT") |
marcozecchini | 0:9fca2b23d0ba | 83 | |
marcozecchini | 0:9fca2b23d0ba | 84 | parser.add_option("-r", "--reset", |
marcozecchini | 0:9fca2b23d0ba | 85 | dest="forced_reset_type", |
marcozecchini | 0:9fca2b23d0ba | 86 | help="Forces different type of reset") |
marcozecchini | 0:9fca2b23d0ba | 87 | |
marcozecchini | 0:9fca2b23d0ba | 88 | parser.add_option("-R", "--reset-timeout", |
marcozecchini | 0:9fca2b23d0ba | 89 | dest="forced_reset_timeout", |
marcozecchini | 0:9fca2b23d0ba | 90 | metavar="NUMBER", |
marcozecchini | 0:9fca2b23d0ba | 91 | type="int", |
marcozecchini | 0:9fca2b23d0ba | 92 | help="When forcing a reset using option -r you can set up after reset timeout in seconds") |
marcozecchini | 0:9fca2b23d0ba | 93 | |
marcozecchini | 0:9fca2b23d0ba | 94 | parser.add_option('', '--auto', |
marcozecchini | 0:9fca2b23d0ba | 95 | dest='auto_detect', |
marcozecchini | 0:9fca2b23d0ba | 96 | metavar=False, |
marcozecchini | 0:9fca2b23d0ba | 97 | action="store_true", |
marcozecchini | 0:9fca2b23d0ba | 98 | help='Use mbed-ls module to detect all connected mbed devices') |
marcozecchini | 0:9fca2b23d0ba | 99 | |
marcozecchini | 0:9fca2b23d0ba | 100 | (self.options, _) = parser.parse_args() |
marcozecchini | 0:9fca2b23d0ba | 101 | |
marcozecchini | 0:9fca2b23d0ba | 102 | self.DEFAULT_RESET_TOUT = 0 |
marcozecchini | 0:9fca2b23d0ba | 103 | self.DEFAULT_TOUT = 10 |
marcozecchini | 0:9fca2b23d0ba | 104 | |
marcozecchini | 0:9fca2b23d0ba | 105 | if self.options.port is None: |
marcozecchini | 0:9fca2b23d0ba | 106 | raise Exception("The serial port of the target mbed have to be provided as command line arguments") |
marcozecchini | 0:9fca2b23d0ba | 107 | |
marcozecchini | 0:9fca2b23d0ba | 108 | # Options related to copy / reset mbed device |
marcozecchini | 0:9fca2b23d0ba | 109 | self.port = self.options.port |
marcozecchini | 0:9fca2b23d0ba | 110 | self.disk = self.options.disk |
marcozecchini | 0:9fca2b23d0ba | 111 | self.image_path = self.options.image_path.strip('"') |
marcozecchini | 0:9fca2b23d0ba | 112 | self.copy_method = self.options.copy_method |
marcozecchini | 0:9fca2b23d0ba | 113 | self.program_cycle_s = float(self.options.program_cycle_s) |
marcozecchini | 0:9fca2b23d0ba | 114 | |
marcozecchini | 0:9fca2b23d0ba | 115 | self.serial = None |
marcozecchini | 0:9fca2b23d0ba | 116 | self.serial_baud = 9600 |
marcozecchini | 0:9fca2b23d0ba | 117 | self.serial_timeout = 1 |
marcozecchini | 0:9fca2b23d0ba | 118 | |
marcozecchini | 0:9fca2b23d0ba | 119 | self.timeout = self.DEFAULT_TOUT if self.options.timeout is None else self.options.timeout |
marcozecchini | 0:9fca2b23d0ba | 120 | print 'MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk) |
marcozecchini | 0:9fca2b23d0ba | 121 | |
marcozecchini | 0:9fca2b23d0ba | 122 | def init_serial_params(self, serial_baud=9600, serial_timeout=1): |
marcozecchini | 0:9fca2b23d0ba | 123 | """ Initialize port parameters. |
marcozecchini | 0:9fca2b23d0ba | 124 | This parameters will be used by self.init_serial() function to open serial port |
marcozecchini | 0:9fca2b23d0ba | 125 | """ |
marcozecchini | 0:9fca2b23d0ba | 126 | self.serial_baud = serial_baud |
marcozecchini | 0:9fca2b23d0ba | 127 | self.serial_timeout = serial_timeout |
marcozecchini | 0:9fca2b23d0ba | 128 | |
marcozecchini | 0:9fca2b23d0ba | 129 | def init_serial(self, serial_baud=None, serial_timeout=None): |
marcozecchini | 0:9fca2b23d0ba | 130 | """ Initialize serial port. |
marcozecchini | 0:9fca2b23d0ba | 131 | Function will return error is port can't be opened or initialized |
marcozecchini | 0:9fca2b23d0ba | 132 | """ |
marcozecchini | 0:9fca2b23d0ba | 133 | # Overload serial port configuration from default to parameters' values if they are specified |
marcozecchini | 0:9fca2b23d0ba | 134 | serial_baud = serial_baud if serial_baud is not None else self.serial_baud |
marcozecchini | 0:9fca2b23d0ba | 135 | serial_timeout = serial_timeout if serial_timeout is not None else self.serial_timeout |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | if get_module_avail('mbed_lstools') and self.options.auto_detect: |
marcozecchini | 0:9fca2b23d0ba | 138 | # Ensure serial port is up-to-date (try to find it 60 times) |
marcozecchini | 0:9fca2b23d0ba | 139 | found = False |
marcozecchini | 0:9fca2b23d0ba | 140 | |
marcozecchini | 0:9fca2b23d0ba | 141 | for i in range(0, 60): |
marcozecchini | 0:9fca2b23d0ba | 142 | print('Looking for %s with MBEDLS' % self.options.micro) |
marcozecchini | 0:9fca2b23d0ba | 143 | muts_list = get_autodetected_MUTS_list(platform_name_filter=[self.options.micro]) |
marcozecchini | 0:9fca2b23d0ba | 144 | |
marcozecchini | 0:9fca2b23d0ba | 145 | if 1 in muts_list: |
marcozecchini | 0:9fca2b23d0ba | 146 | mut = muts_list[1] |
marcozecchini | 0:9fca2b23d0ba | 147 | self.port = mut['port'] |
marcozecchini | 0:9fca2b23d0ba | 148 | found = True |
marcozecchini | 0:9fca2b23d0ba | 149 | break |
marcozecchini | 0:9fca2b23d0ba | 150 | else: |
marcozecchini | 0:9fca2b23d0ba | 151 | sleep(3) |
marcozecchini | 0:9fca2b23d0ba | 152 | |
marcozecchini | 0:9fca2b23d0ba | 153 | if not found: |
marcozecchini | 0:9fca2b23d0ba | 154 | return False |
marcozecchini | 0:9fca2b23d0ba | 155 | |
marcozecchini | 0:9fca2b23d0ba | 156 | # Clear serial port |
marcozecchini | 0:9fca2b23d0ba | 157 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 158 | self.serial.close() |
marcozecchini | 0:9fca2b23d0ba | 159 | self.serial = None |
marcozecchini | 0:9fca2b23d0ba | 160 | |
marcozecchini | 0:9fca2b23d0ba | 161 | # We will pool for serial to be re-mounted if it was unmounted after device reset |
marcozecchini | 0:9fca2b23d0ba | 162 | result = self.pool_for_serial_init(serial_baud, serial_timeout) # Blocking |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | # Port can be opened |
marcozecchini | 0:9fca2b23d0ba | 165 | if result: |
marcozecchini | 0:9fca2b23d0ba | 166 | self.flush() |
marcozecchini | 0:9fca2b23d0ba | 167 | return result |
marcozecchini | 0:9fca2b23d0ba | 168 | |
marcozecchini | 0:9fca2b23d0ba | 169 | def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, init_delay=0.5, loop_delay=0.25): |
marcozecchini | 0:9fca2b23d0ba | 170 | """ Functions pools for serial port readiness |
marcozecchini | 0:9fca2b23d0ba | 171 | """ |
marcozecchini | 0:9fca2b23d0ba | 172 | result = True |
marcozecchini | 0:9fca2b23d0ba | 173 | last_error = None |
marcozecchini | 0:9fca2b23d0ba | 174 | # This loop is used to check for serial port availability due to |
marcozecchini | 0:9fca2b23d0ba | 175 | # some delays and remounting when devices are being flashed with new software. |
marcozecchini | 0:9fca2b23d0ba | 176 | for i in range(pooling_loops): |
marcozecchini | 0:9fca2b23d0ba | 177 | sleep(loop_delay if i else init_delay) |
marcozecchini | 0:9fca2b23d0ba | 178 | try: |
marcozecchini | 0:9fca2b23d0ba | 179 | self.serial = Serial(self.port, baudrate=serial_baud, timeout=serial_timeout) |
marcozecchini | 0:9fca2b23d0ba | 180 | except Exception as e: |
marcozecchini | 0:9fca2b23d0ba | 181 | result = False |
marcozecchini | 0:9fca2b23d0ba | 182 | last_error = "MBED: %s"% str(e) |
marcozecchini | 0:9fca2b23d0ba | 183 | stdout.write('.') |
marcozecchini | 0:9fca2b23d0ba | 184 | stdout.flush() |
marcozecchini | 0:9fca2b23d0ba | 185 | else: |
marcozecchini | 0:9fca2b23d0ba | 186 | print "...port ready!" |
marcozecchini | 0:9fca2b23d0ba | 187 | result = True |
marcozecchini | 0:9fca2b23d0ba | 188 | break |
marcozecchini | 0:9fca2b23d0ba | 189 | if not result and last_error: |
marcozecchini | 0:9fca2b23d0ba | 190 | print last_error |
marcozecchini | 0:9fca2b23d0ba | 191 | return result |
marcozecchini | 0:9fca2b23d0ba | 192 | |
marcozecchini | 0:9fca2b23d0ba | 193 | def set_serial_timeout(self, timeout): |
marcozecchini | 0:9fca2b23d0ba | 194 | """ Wraps self.mbed.serial object timeout property |
marcozecchini | 0:9fca2b23d0ba | 195 | """ |
marcozecchini | 0:9fca2b23d0ba | 196 | result = None |
marcozecchini | 0:9fca2b23d0ba | 197 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 198 | self.serial.timeout = timeout |
marcozecchini | 0:9fca2b23d0ba | 199 | result = True |
marcozecchini | 0:9fca2b23d0ba | 200 | return result |
marcozecchini | 0:9fca2b23d0ba | 201 | |
marcozecchini | 0:9fca2b23d0ba | 202 | def serial_read(self, count=1): |
marcozecchini | 0:9fca2b23d0ba | 203 | """ Wraps self.mbed.serial object read method |
marcozecchini | 0:9fca2b23d0ba | 204 | """ |
marcozecchini | 0:9fca2b23d0ba | 205 | result = None |
marcozecchini | 0:9fca2b23d0ba | 206 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 207 | try: |
marcozecchini | 0:9fca2b23d0ba | 208 | result = self.serial.read(count) |
marcozecchini | 0:9fca2b23d0ba | 209 | except: |
marcozecchini | 0:9fca2b23d0ba | 210 | result = None |
marcozecchini | 0:9fca2b23d0ba | 211 | return result |
marcozecchini | 0:9fca2b23d0ba | 212 | |
marcozecchini | 0:9fca2b23d0ba | 213 | def serial_readline(self, timeout=5): |
marcozecchini | 0:9fca2b23d0ba | 214 | """ Wraps self.mbed.serial object read method to read one line from serial port |
marcozecchini | 0:9fca2b23d0ba | 215 | """ |
marcozecchini | 0:9fca2b23d0ba | 216 | result = '' |
marcozecchini | 0:9fca2b23d0ba | 217 | start = time() |
marcozecchini | 0:9fca2b23d0ba | 218 | while (time() - start) < timeout: |
marcozecchini | 0:9fca2b23d0ba | 219 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 220 | try: |
marcozecchini | 0:9fca2b23d0ba | 221 | c = self.serial.read(1) |
marcozecchini | 0:9fca2b23d0ba | 222 | result += c |
marcozecchini | 0:9fca2b23d0ba | 223 | except Exception as e: |
marcozecchini | 0:9fca2b23d0ba | 224 | print "MBED: %s"% str(e) |
marcozecchini | 0:9fca2b23d0ba | 225 | result = None |
marcozecchini | 0:9fca2b23d0ba | 226 | break |
marcozecchini | 0:9fca2b23d0ba | 227 | if c == '\n': |
marcozecchini | 0:9fca2b23d0ba | 228 | break |
marcozecchini | 0:9fca2b23d0ba | 229 | return result |
marcozecchini | 0:9fca2b23d0ba | 230 | |
marcozecchini | 0:9fca2b23d0ba | 231 | def serial_write(self, write_buffer): |
marcozecchini | 0:9fca2b23d0ba | 232 | """ Wraps self.mbed.serial object write method |
marcozecchini | 0:9fca2b23d0ba | 233 | """ |
marcozecchini | 0:9fca2b23d0ba | 234 | result = None |
marcozecchini | 0:9fca2b23d0ba | 235 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 236 | try: |
marcozecchini | 0:9fca2b23d0ba | 237 | result = self.serial.write(write_buffer) |
marcozecchini | 0:9fca2b23d0ba | 238 | except: |
marcozecchini | 0:9fca2b23d0ba | 239 | result = None |
marcozecchini | 0:9fca2b23d0ba | 240 | return result |
marcozecchini | 0:9fca2b23d0ba | 241 | |
marcozecchini | 0:9fca2b23d0ba | 242 | def reset_timeout(self, timeout): |
marcozecchini | 0:9fca2b23d0ba | 243 | """ Timeout executed just after reset command is issued |
marcozecchini | 0:9fca2b23d0ba | 244 | """ |
marcozecchini | 0:9fca2b23d0ba | 245 | for n in range(0, timeout): |
marcozecchini | 0:9fca2b23d0ba | 246 | sleep(1) |
marcozecchini | 0:9fca2b23d0ba | 247 | |
marcozecchini | 0:9fca2b23d0ba | 248 | def reset(self): |
marcozecchini | 0:9fca2b23d0ba | 249 | """ Calls proper reset plugin to do the job. |
marcozecchini | 0:9fca2b23d0ba | 250 | Please refer to host_test_plugins functionality |
marcozecchini | 0:9fca2b23d0ba | 251 | """ |
marcozecchini | 0:9fca2b23d0ba | 252 | # Flush serials to get only input after reset |
marcozecchini | 0:9fca2b23d0ba | 253 | self.flush() |
marcozecchini | 0:9fca2b23d0ba | 254 | if self.options.forced_reset_type: |
marcozecchini | 0:9fca2b23d0ba | 255 | result = host_tests_plugins.call_plugin('ResetMethod', self.options.forced_reset_type, disk=self.disk) |
marcozecchini | 0:9fca2b23d0ba | 256 | else: |
marcozecchini | 0:9fca2b23d0ba | 257 | result = host_tests_plugins.call_plugin('ResetMethod', 'default', serial=self.serial) |
marcozecchini | 0:9fca2b23d0ba | 258 | # Give time to wait for the image loading |
marcozecchini | 0:9fca2b23d0ba | 259 | reset_tout_s = self.options.forced_reset_timeout if self.options.forced_reset_timeout is not None else self.DEFAULT_RESET_TOUT |
marcozecchini | 0:9fca2b23d0ba | 260 | self.reset_timeout(reset_tout_s) |
marcozecchini | 0:9fca2b23d0ba | 261 | return result |
marcozecchini | 0:9fca2b23d0ba | 262 | |
marcozecchini | 0:9fca2b23d0ba | 263 | def copy_image(self, image_path=None, disk=None, copy_method=None): |
marcozecchini | 0:9fca2b23d0ba | 264 | """ Closure for copy_image_raw() method. |
marcozecchini | 0:9fca2b23d0ba | 265 | Method which is actually copying image to mbed |
marcozecchini | 0:9fca2b23d0ba | 266 | """ |
marcozecchini | 0:9fca2b23d0ba | 267 | # Set closure environment |
marcozecchini | 0:9fca2b23d0ba | 268 | image_path = image_path if image_path is not None else self.image_path |
marcozecchini | 0:9fca2b23d0ba | 269 | disk = disk if disk is not None else self.disk |
marcozecchini | 0:9fca2b23d0ba | 270 | copy_method = copy_method if copy_method is not None else self.copy_method |
marcozecchini | 0:9fca2b23d0ba | 271 | # Call proper copy method |
marcozecchini | 0:9fca2b23d0ba | 272 | result = self.copy_image_raw(image_path, disk, copy_method) |
marcozecchini | 0:9fca2b23d0ba | 273 | return result |
marcozecchini | 0:9fca2b23d0ba | 274 | |
marcozecchini | 0:9fca2b23d0ba | 275 | def copy_image_raw(self, image_path=None, disk=None, copy_method=None): |
marcozecchini | 0:9fca2b23d0ba | 276 | """ Copy file depending on method you want to use. Handles exception |
marcozecchini | 0:9fca2b23d0ba | 277 | and return code from shell copy commands. |
marcozecchini | 0:9fca2b23d0ba | 278 | """ |
marcozecchini | 0:9fca2b23d0ba | 279 | # image_path - Where is binary with target's firmware |
marcozecchini | 0:9fca2b23d0ba | 280 | if copy_method is not None: |
marcozecchini | 0:9fca2b23d0ba | 281 | # We override 'default' method with 'shell' method |
marcozecchini | 0:9fca2b23d0ba | 282 | if copy_method == 'default': |
marcozecchini | 0:9fca2b23d0ba | 283 | copy_method = 'shell' |
marcozecchini | 0:9fca2b23d0ba | 284 | else: |
marcozecchini | 0:9fca2b23d0ba | 285 | copy_method = 'shell' |
marcozecchini | 0:9fca2b23d0ba | 286 | |
marcozecchini | 0:9fca2b23d0ba | 287 | result = host_tests_plugins.call_plugin('CopyMethod', copy_method, image_path=image_path, destination_disk=disk, program_cycle_s=self.program_cycle_s, target_mcu=self.options.micro) |
marcozecchini | 0:9fca2b23d0ba | 288 | return result; |
marcozecchini | 0:9fca2b23d0ba | 289 | |
marcozecchini | 0:9fca2b23d0ba | 290 | def flush(self): |
marcozecchini | 0:9fca2b23d0ba | 291 | """ Flush serial ports |
marcozecchini | 0:9fca2b23d0ba | 292 | """ |
marcozecchini | 0:9fca2b23d0ba | 293 | result = False |
marcozecchini | 0:9fca2b23d0ba | 294 | if self.serial: |
marcozecchini | 0:9fca2b23d0ba | 295 | self.serial.flushInput() |
marcozecchini | 0:9fca2b23d0ba | 296 | self.serial.flushOutput() |
marcozecchini | 0:9fca2b23d0ba | 297 | result = True |
marcozecchini | 0:9fca2b23d0ba | 298 | return result |
marcozecchini | 0:9fca2b23d0ba | 299 | |
marcozecchini | 0:9fca2b23d0ba | 300 | |
marcozecchini | 0:9fca2b23d0ba | 301 | class HostTestResults: |
marcozecchini | 0:9fca2b23d0ba | 302 | """ Test results set by host tests |
marcozecchini | 0:9fca2b23d0ba | 303 | """ |
marcozecchini | 0:9fca2b23d0ba | 304 | def __init__(self): |
marcozecchini | 0:9fca2b23d0ba | 305 | self.RESULT_SUCCESS = 'success' |
marcozecchini | 0:9fca2b23d0ba | 306 | self.RESULT_FAILURE = 'failure' |
marcozecchini | 0:9fca2b23d0ba | 307 | self.RESULT_ERROR = 'error' |
marcozecchini | 0:9fca2b23d0ba | 308 | self.RESULT_IO_SERIAL = 'ioerr_serial' |
marcozecchini | 0:9fca2b23d0ba | 309 | self.RESULT_NO_IMAGE = 'no_image' |
marcozecchini | 0:9fca2b23d0ba | 310 | self.RESULT_IOERR_COPY = "ioerr_copy" |
marcozecchini | 0:9fca2b23d0ba | 311 | self.RESULT_PASSIVE = "passive" |
marcozecchini | 0:9fca2b23d0ba | 312 | self.RESULT_NOT_DETECTED = "not_detected" |
marcozecchini | 0:9fca2b23d0ba | 313 | self.RESULT_MBED_ASSERT = "mbed_assert" |
marcozecchini | 0:9fca2b23d0ba | 314 | |
marcozecchini | 0:9fca2b23d0ba | 315 | |
marcozecchini | 0:9fca2b23d0ba | 316 | import tools.host_tests as host_tests |
marcozecchini | 0:9fca2b23d0ba | 317 | |
marcozecchini | 0:9fca2b23d0ba | 318 | |
marcozecchini | 0:9fca2b23d0ba | 319 | class Test(HostTestResults): |
marcozecchini | 0:9fca2b23d0ba | 320 | """ Base class for host test's test runner |
marcozecchini | 0:9fca2b23d0ba | 321 | """ |
marcozecchini | 0:9fca2b23d0ba | 322 | # Select default host_test supervision (replaced after autodetection) |
marcozecchini | 0:9fca2b23d0ba | 323 | test_supervisor = host_tests.get_host_test("default") |
marcozecchini | 0:9fca2b23d0ba | 324 | |
marcozecchini | 0:9fca2b23d0ba | 325 | def __init__(self): |
marcozecchini | 0:9fca2b23d0ba | 326 | self.mbed = Mbed() |
marcozecchini | 0:9fca2b23d0ba | 327 | |
marcozecchini | 0:9fca2b23d0ba | 328 | def detect_test_config(self, verbose=False): |
marcozecchini | 0:9fca2b23d0ba | 329 | """ Detects test case configuration |
marcozecchini | 0:9fca2b23d0ba | 330 | """ |
marcozecchini | 0:9fca2b23d0ba | 331 | result = {} |
marcozecchini | 0:9fca2b23d0ba | 332 | while True: |
marcozecchini | 0:9fca2b23d0ba | 333 | line = self.mbed.serial_readline() |
marcozecchini | 0:9fca2b23d0ba | 334 | if "{start}" in line: |
marcozecchini | 0:9fca2b23d0ba | 335 | self.notify("HOST: Start test...") |
marcozecchini | 0:9fca2b23d0ba | 336 | break |
marcozecchini | 0:9fca2b23d0ba | 337 | else: |
marcozecchini | 0:9fca2b23d0ba | 338 | # Detect if this is property from TEST_ENV print |
marcozecchini | 0:9fca2b23d0ba | 339 | m = re.search('{([\w_]+);([\w\d\+ ]+)}}', line[:-1]) |
marcozecchini | 0:9fca2b23d0ba | 340 | if m and len(m.groups()) == 2: |
marcozecchini | 0:9fca2b23d0ba | 341 | # This is most likely auto-detection property |
marcozecchini | 0:9fca2b23d0ba | 342 | result[m.group(1)] = m.group(2) |
marcozecchini | 0:9fca2b23d0ba | 343 | if verbose: |
marcozecchini | 0:9fca2b23d0ba | 344 | self.notify("HOST: Property '%s' = '%s'"% (m.group(1), m.group(2))) |
marcozecchini | 0:9fca2b23d0ba | 345 | else: |
marcozecchini | 0:9fca2b23d0ba | 346 | # We can check if this is TArget Id in mbed specific format |
marcozecchini | 0:9fca2b23d0ba | 347 | m2 = re.search('^([\$]+)([a-fA-F0-9]+)', line[:-1]) |
marcozecchini | 0:9fca2b23d0ba | 348 | if m2 and len(m2.groups()) == 2: |
marcozecchini | 0:9fca2b23d0ba | 349 | if verbose: |
marcozecchini | 0:9fca2b23d0ba | 350 | target_id = m2.group(1) + m2.group(2) |
marcozecchini | 0:9fca2b23d0ba | 351 | self.notify("HOST: TargetID '%s'"% target_id) |
marcozecchini | 0:9fca2b23d0ba | 352 | self.notify(line[len(target_id):-1]) |
marcozecchini | 0:9fca2b23d0ba | 353 | else: |
marcozecchini | 0:9fca2b23d0ba | 354 | self.notify("HOST: Unknown property: %s"% line.strip()) |
marcozecchini | 0:9fca2b23d0ba | 355 | return result |
marcozecchini | 0:9fca2b23d0ba | 356 | |
marcozecchini | 0:9fca2b23d0ba | 357 | def run(self): |
marcozecchini | 0:9fca2b23d0ba | 358 | """ Test runner for host test. This function will start executing |
marcozecchini | 0:9fca2b23d0ba | 359 | test and forward test result via serial port to test suite |
marcozecchini | 0:9fca2b23d0ba | 360 | """ |
marcozecchini | 0:9fca2b23d0ba | 361 | # Copy image to device |
marcozecchini | 0:9fca2b23d0ba | 362 | self.notify("HOST: Copy image onto target...") |
marcozecchini | 0:9fca2b23d0ba | 363 | result = self.mbed.copy_image() |
marcozecchini | 0:9fca2b23d0ba | 364 | if not result: |
marcozecchini | 0:9fca2b23d0ba | 365 | self.print_result(self.RESULT_IOERR_COPY) |
marcozecchini | 0:9fca2b23d0ba | 366 | |
marcozecchini | 0:9fca2b23d0ba | 367 | # Initialize and open target's serial port (console) |
marcozecchini | 0:9fca2b23d0ba | 368 | self.notify("HOST: Initialize serial port...") |
marcozecchini | 0:9fca2b23d0ba | 369 | result = self.mbed.init_serial() |
marcozecchini | 0:9fca2b23d0ba | 370 | if not result: |
marcozecchini | 0:9fca2b23d0ba | 371 | self.print_result(self.RESULT_IO_SERIAL) |
marcozecchini | 0:9fca2b23d0ba | 372 | |
marcozecchini | 0:9fca2b23d0ba | 373 | # Reset device |
marcozecchini | 0:9fca2b23d0ba | 374 | self.notify("HOST: Reset target...") |
marcozecchini | 0:9fca2b23d0ba | 375 | result = self.mbed.reset() |
marcozecchini | 0:9fca2b23d0ba | 376 | if not result: |
marcozecchini | 0:9fca2b23d0ba | 377 | self.print_result(self.RESULT_IO_SERIAL) |
marcozecchini | 0:9fca2b23d0ba | 378 | |
marcozecchini | 0:9fca2b23d0ba | 379 | # Run test |
marcozecchini | 0:9fca2b23d0ba | 380 | try: |
marcozecchini | 0:9fca2b23d0ba | 381 | CONFIG = self.detect_test_config(verbose=True) # print CONFIG |
marcozecchini | 0:9fca2b23d0ba | 382 | |
marcozecchini | 0:9fca2b23d0ba | 383 | if "host_test_name" in CONFIG: |
marcozecchini | 0:9fca2b23d0ba | 384 | if host_tests.is_host_test(CONFIG["host_test_name"]): |
marcozecchini | 0:9fca2b23d0ba | 385 | self.test_supervisor = host_tests.get_host_test(CONFIG["host_test_name"]) |
marcozecchini | 0:9fca2b23d0ba | 386 | result = self.test_supervisor.test(self) #result = self.test() |
marcozecchini | 0:9fca2b23d0ba | 387 | |
marcozecchini | 0:9fca2b23d0ba | 388 | if result is not None: |
marcozecchini | 0:9fca2b23d0ba | 389 | self.print_result(result) |
marcozecchini | 0:9fca2b23d0ba | 390 | else: |
marcozecchini | 0:9fca2b23d0ba | 391 | self.notify("HOST: Passive mode...") |
marcozecchini | 0:9fca2b23d0ba | 392 | except Exception, e: |
marcozecchini | 0:9fca2b23d0ba | 393 | print str(e) |
marcozecchini | 0:9fca2b23d0ba | 394 | self.print_result(self.RESULT_ERROR) |
marcozecchini | 0:9fca2b23d0ba | 395 | |
marcozecchini | 0:9fca2b23d0ba | 396 | def setup(self): |
marcozecchini | 0:9fca2b23d0ba | 397 | """ Setup and check if configuration for test is |
marcozecchini | 0:9fca2b23d0ba | 398 | correct. E.g. if serial port can be opened. |
marcozecchini | 0:9fca2b23d0ba | 399 | """ |
marcozecchini | 0:9fca2b23d0ba | 400 | result = True |
marcozecchini | 0:9fca2b23d0ba | 401 | if not self.mbed.serial: |
marcozecchini | 0:9fca2b23d0ba | 402 | result = False |
marcozecchini | 0:9fca2b23d0ba | 403 | self.print_result(self.RESULT_IO_SERIAL) |
marcozecchini | 0:9fca2b23d0ba | 404 | return result |
marcozecchini | 0:9fca2b23d0ba | 405 | |
marcozecchini | 0:9fca2b23d0ba | 406 | def notify(self, message): |
marcozecchini | 0:9fca2b23d0ba | 407 | """ On screen notification function |
marcozecchini | 0:9fca2b23d0ba | 408 | """ |
marcozecchini | 0:9fca2b23d0ba | 409 | print message |
marcozecchini | 0:9fca2b23d0ba | 410 | stdout.flush() |
marcozecchini | 0:9fca2b23d0ba | 411 | |
marcozecchini | 0:9fca2b23d0ba | 412 | def print_result(self, result): |
marcozecchini | 0:9fca2b23d0ba | 413 | """ Test result unified printing function |
marcozecchini | 0:9fca2b23d0ba | 414 | """ |
marcozecchini | 0:9fca2b23d0ba | 415 | self.notify("\r\n{{%s}}\r\n{{end}}" % result) |
marcozecchini | 0:9fca2b23d0ba | 416 | |
marcozecchini | 0:9fca2b23d0ba | 417 | |
marcozecchini | 0:9fca2b23d0ba | 418 | class DefaultTestSelector(Test): |
marcozecchini | 0:9fca2b23d0ba | 419 | """ Test class with serial port initialization |
marcozecchini | 0:9fca2b23d0ba | 420 | """ |
marcozecchini | 0:9fca2b23d0ba | 421 | def __init__(self): |
marcozecchini | 0:9fca2b23d0ba | 422 | HostTestResults.__init__(self) |
marcozecchini | 0:9fca2b23d0ba | 423 | Test.__init__(self) |
marcozecchini | 0:9fca2b23d0ba | 424 | |
marcozecchini | 0:9fca2b23d0ba | 425 | if __name__ == '__main__': |
marcozecchini | 0:9fca2b23d0ba | 426 | DefaultTestSelector().run() |