Mbed Host Tests
host_functional.py
Go to the documentation of this file.
1 """
2 mbed SDK
3 Copyright (c) 2011-2016 ARM Limited
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 
17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
18 """
19 
20 import sys
21 import json
22 from time import sleep
23 from serial import Serial, SerialException
24 from mbed_host_tests import host_tests_plugins, DEFAULT_BAUD_RATE
25 
26 
27 def flash_dev(disk=None,
28  image_path=None,
29  copy_method='default',
30  port=None,
31  program_cycle_s=4):
32  """! Flash device using pythonic interface
33  @param disk Switch -d <disk>
34  @param image_path Switch -f <image_path>
35  @param copy_method Switch -c <copy_method> (default: shell)
36  @param port Switch -p <port>
37  """
38  if copy_method == 'default':
39  copy_method = 'shell'
40  result = False
41  result = host_tests_plugins.call_plugin('CopyMethod',
42  copy_method,
43  image_path=image_path,
44  serial=port,
45  destination_disk=disk)
46  sleep(program_cycle_s)
47  return result
48 
49 def reset_dev(port=None,
50  disk=None,
51  reset_type='default',
52  reset_timeout=1,
53  serial_port=None,
54  baudrate=DEFAULT_BAUD_RATE,
55  timeout=1,
56  verbose=False):
57  """! Reset device using pythonic interface
58  @param port Switch -p <port>
59  @param disk Switch -d <disk>
60  @param reset_type Switch -r <reset_type>
61  @param reset_timeout Switch -R <reset_timeout>
62  @param serial_port Serial port handler, set to None if you want this function to open serial
63 
64  @param baudrate Serial port baudrate
65  @param timeout Serial port timeout
66  @param verbose Verbose mode
67  """
68 
69  result = False
70  if not serial_port:
71  try:
72  with Serial(port, baudrate=baudrate, timeout=timeout) as serial_port:
73  result = host_tests_plugins.call_plugin('ResetMethod',
74  reset_type,
75  serial=serial_port,
76  disk=disk)
77  sleep(reset_timeout)
78  except SerialException as e:
79  if verbose:
80  print("%s" % (str(e)))
81  result = False
82  return result
83 
85  disk,
86  reset_type=None,
87  baudrate=None,
88  timeout=1,
89  verbose=False):
90  """! Resets platforms and prints serial port output
91  @detail Mix with switch -r RESET_TYPE and -p PORT for versatility
92  """
93  if not reset_type:
94  reset_type = 'default'
95 
96  port_config = port.split(':')
97  if len(port_config) == 2:
98  # -p COM4:115200
99  port = port_config[0]
100  baudrate = int(port_config[1]) if not baudrate else baudrate
101  elif len(port_config) == 3:
102  # -p COM4:115200:0.5
103  port = port_config[0]
104  baudrate = int(port_config[1]) if not baudrate else baudrate
105  timeout = float(port_config[2])
106 
107  # Use default baud rate value if not set
108  if not baudrate:
109  baudrate = DEFAULT_BAUD_RATE
110 
111  if verbose:
112  print("mbedhtrun: serial port configuration: %s:%s:%s"% (port, str(baudrate), str(timeout)))
113 
114  try:
115  serial_port = Serial(port, baudrate=baudrate, timeout=timeout)
116  except Exception as e:
117  print("mbedhtrun: %s" % (str(e)))
118  print(json.dumps({
119  "port" : port,
120  "disk" : disk,
121  "baudrate" : baudrate,
122  "timeout" : timeout,
123  "reset_type" : reset_type,
124  }, indent=4))
125  return False
126 
127  serial_port.flush()
128  # Reset using one of the plugins
129  result = host_tests_plugins.call_plugin('ResetMethod', reset_type, serial=serial_port, disk=disk)
130  if not result:
131  print("mbedhtrun: reset plugin failed")
132  print(json.dumps({
133  "port" : port,
134  "disk" : disk,
135  "baudrate" : baudrate,
136  "timeout" : timeout,
137  "reset_type" : reset_type
138  }, indent=4))
139  return False
140 
141  print("mbedhtrun: serial dump started (use ctrl+c to break)")
142  try:
143  while True:
144  test_output = serial_port.read(512)
145  if test_output:
146  sys.stdout.write('%s'% test_output)
147  if "{end}" in test_output:
148  if verbose:
149  print()
150  print("mbedhtrun: stopped (found '{end}' terminator)")
151  break
152  except KeyboardInterrupt:
153  print("ctrl+c break")
154 
155  serial_port.close()
156  return True
def reset_dev(port=None, disk=None, reset_type='default', reset_timeout=1, serial_port=None, baudrate=DEFAULT_BAUD_RATE, timeout=1, verbose=False)
Reset device using pythonic interface.
def handle_send_break_cmd(port, disk, reset_type=None, baudrate=None, timeout=1, verbose=False)
Resets platforms and prints serial port output @detail Mix with switch -r RESET_TYPE and -p PORT for ...
def flash_dev(disk=None, image_path=None, copy_method='default', port=None, program_cycle_s=4)
Flash device using pythonic interface.