Mbed Host Tests
module_reset_mbed.py
Go to the documentation of this file.
1"""
2mbed SDK
3Copyright (c) 2011-2015 ARM Limited
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
18"""
19
20import re
21import pkg_resources
22from .host_test_plugins import HostTestPluginBase
23
24
26
27 # Plugin interface
28 name = 'HostTestPluginResetMethod_Mbed'
29 type = 'ResetMethod'
30 stable = True
31 capabilities = ['default']
32 required_parameters = ['serial']
33
34 def __init__(self):
35 """! ctor
36 @details We can check module version by referring to version attribute
37 import pkg_resources
38 print pkg_resources.require("mbed-host-tests")[0].version
39 '2.7'
40 """
41 HostTestPluginBase.__init__(self)
42 self.re_float = re.compile("^\d+\.\d+")
43 pyserial_version = pkg_resources.require("pyserial")[0].version
44 self.pyserial_version = self.get_pyserial_version(pyserial_version)
45 self.is_pyserial_v3 = float(self.pyserial_version) >= 3.0
46
47 def get_pyserial_version(self, pyserial_version):
48 """! Retrieve pyserial module version
49 @return Returns float with pyserial module number
50 """
51 version = 3.0
52 m = self.re_float.search(pyserial_version)
53 if m:
54 try:
55 version = float(m.group(0))
56 except ValueError:
57 version = 3.0 # We will assume you've got latest (3.0+)
58 return version
59
60 def safe_sendBreak(self, serial):
61 """! Closure for pyserial version dependant API calls
62 """
63 if self.is_pyserial_v3:
64 return self._safe_sendBreak_v3_0(serial)
65 return self._safe_sendBreak_v2_7(serial)
66
67 def _safe_sendBreak_v2_7(self, serial):
68 """! pyserial 2.7 API implementation of sendBreak/setBreak
69 @details
70 Below API is deprecated for pyserial 3.x versions!
71 http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.sendBreak
72 http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.setBreak
73 """
74 result = True
75 try:
76 serial.sendBreak()
77 except:
78 # In Linux a termios.error is raised in sendBreak and in setBreak.
79 # The following setBreak() is needed to release the reset signal on the target mcu.
80 try:
81 serial.setBreak(False)
82 except:
83 result = False
84 return result
85
86 def _safe_sendBreak_v3_0(self, serial):
87 """! pyserial 3.x API implementation of send_brea / break_condition
88 @details
89 http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.send_break
90 http://pyserial.readthedocs.org/en/latest/pyserial_api.html#serial.Serial.break_condition
91 """
92 result = True
93 try:
94 serial.send_break()
95 except:
96 # In Linux a termios.error is raised in sendBreak and in setBreak.
97 # The following break_condition = False is needed to release the reset signal on the target mcu.
98 try:
99 serial.break_condition = False
100 except Exception as e:
101 self.print_plugin_error("Error while doing 'serial.break_condition = False' : %s"% str(e))
102 result = False
103 return result
104
105 def setup(self, *args, **kwargs):
106 """! Configure plugin, this function should be called before plugin execute() method is used.
107 """
108 return True
109
110 def execute(self, capability, *args, **kwargs):
111 """! Executes capability by name
112
113 @param capability Capability name
114 @param args Additional arguments
115 @param kwargs Additional arguments
116 @details Each capability e.g. may directly just call some command line program or execute building pythonic function
117 @return Capability call return value
118 """
119 if not kwargs['serial']:
120 self.print_plugin_error("Error: serial port not set (not opened?)")
121 return False
122
123 result = False
124 if self.check_parameters(capability, *args, **kwargs) is True:
125 if kwargs['serial']:
126 if capability == 'default':
127 serial = kwargs['serial']
128 result = self.safe_sendBreak(serial)
129 return result
130
131
133 """! Returns plugin available in this module
134 """
print_plugin_error(self, text)
Interface helper methods - overload only if you need to have custom behaviour.
check_parameters(self, capability, *args, **kwargs)
This function should be ran each time we call execute() to check if none of the required parameters i...
get_pyserial_version(self, pyserial_version)
Retrieve pyserial module version.
setup(self, *args, **kwargs)
Configure plugin, this function should be called before plugin execute() method is used.
execute(self, capability, *args, **kwargs)
Executes capability by name.
safe_sendBreak(self, serial)
Closure for pyserial version dependant API calls.
_safe_sendBreak_v3_0(self, serial)
pyserial 3.x API implementation of send_brea / break_condition
_safe_sendBreak_v2_7(self, serial)
pyserial 2.7 API implementation of sendBreak/setBreak
load_plugin()
Returns plugin available in this module.