Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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