28
28
from sys import stdout
29
29
30
30
class Mbed :
31
- """
32
- Base class for a host driven test
31
+ """ Base class for a host driven test
33
32
"""
34
33
def __init__ (self ):
35
34
parser = OptionParser ()
@@ -63,8 +62,17 @@ def __init__(self):
63
62
dest = "forced_reset_type" ,
64
63
help = "Forces different type of reset" )
65
64
65
+ parser .add_option ("-R" , "--reset-timeout" ,
66
+ dest = "forced_reset_timeout" ,
67
+ metavar = "NUMBER" ,
68
+ type = "int" ,
69
+ help = "When forcing a reset using option -r you can set up after reset timeout in seconds" )
70
+
66
71
(self .options , _ ) = parser .parse_args ()
67
72
73
+ self .DEFAULT_RESET_TOUT = 2
74
+ self .DEFAULT_TOUT = 10
75
+
68
76
if self .options .port is None :
69
77
raise Exception ("The serial port of the target mbed have to be provided as command line arguments" )
70
78
@@ -73,10 +81,12 @@ def __init__(self):
73
81
self .extra_port = self .options .extra
74
82
self .extra_serial = None
75
83
self .serial = None
76
- self .timeout = 10 if self .options .timeout is None else self .options .timeout
77
- print 'Mbed : "%s" "%s"' % (self .port , self .disk )
84
+ self .timeout = self . DEFAULT_TOUT if self .options .timeout is None else self .options .timeout
85
+ print 'Host test instrumentation on port : "%s" with serial: "%s"' % (self .port , self .disk )
78
86
79
87
def init_serial (self , baud = 9600 , extra_baud = 9600 ):
88
+ """ Initialize serial port. Function will return error is port can't be opened or initialized
89
+ """
80
90
result = True
81
91
try :
82
92
self .serial = Serial (self .port , timeout = 1 )
@@ -91,8 +101,18 @@ def init_serial(self, baud=9600, extra_baud=9600):
91
101
self .flush ()
92
102
return result
93
103
104
+ def serial_timeout (self , timeout ):
105
+ """ Wraps self.mbed.serial object timeout property
106
+ """
107
+ result = None
108
+ if self .serial :
109
+ self .serial .timeout = timeout
110
+ result = True
111
+ return result
112
+
94
113
def serial_read (self , count = 1 ):
95
- """ Wraps self.mbed.serial object read method """
114
+ """ Wraps self.mbed.serial object read method
115
+ """
96
116
result = None
97
117
if self .serial :
98
118
try :
@@ -102,23 +122,24 @@ def serial_read(self, count=1):
102
122
return result
103
123
104
124
def serial_write (self , write_buffer ):
105
- """ Wraps self.mbed.serial object write method """
106
- result = - 1
125
+ """ Wraps self.mbed.serial object write method
126
+ """
127
+ result = None
107
128
if self .serial :
108
129
try :
109
130
result = self .serial .write (write_buffer )
110
131
except :
111
- result = - 1
132
+ result = None
112
133
return result
113
134
114
135
def safe_sendBreak (self , serial ):
115
136
""" Wraps serial.sendBreak() to avoid serial::serialposix.py exception on Linux
116
- Traceback (most recent call last):
117
- File "make.py", line 189, in <module>
118
- serial.sendBreak()
119
- File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 511, in sendBreak
120
- termios.tcsendbreak(self.fd, int(duration/0.25))
121
- error: (32, 'Broken pipe')
137
+ Traceback (most recent call last):
138
+ File "make.py", line 189, in <module>
139
+ serial.sendBreak()
140
+ File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 511, in sendBreak
141
+ termios.tcsendbreak(self.fd, int(duration/0.25))
142
+ error: (32, 'Broken pipe')
122
143
"""
123
144
result = True
124
145
try :
@@ -132,24 +153,38 @@ def safe_sendBreak(self, serial):
132
153
result = False
133
154
return result
134
155
135
- def touch_file (self , path , name ):
136
- with os .open (path , 'a' ):
156
+ def touch_file (self , path ):
157
+ """ Touch file and set timestamp to items
158
+ """
159
+ with open (path , 'a' ):
137
160
os .utime (path , None )
138
161
162
+ def reset_timeout (self , timeout ):
163
+ """ Timeout executed just after reset command is issued
164
+ """
165
+ for n in range (0 , timeout ):
166
+ sleep (1 )
167
+
139
168
def reset (self ):
140
- """ reboot.txt - startup from standby state, reboots when in run mode.
169
+ """ Reset function. Supports 'standard' send break command via Mbed's CDC,
170
+ also handles other reset modes.
171
+ E.g. reset by touching file with specific file name:
172
+ reboot.txt - startup from standby state, reboots when in run mode.
141
173
shutdown.txt - shutdown from run mode
142
- reset.txt - reset fpga during run mode """
143
- if self . options . forced_reset_type :
144
- path = os . path . join ([ self .disk , self .options .forced_reset_type .lower ()])
145
- if self .options .forced_reset_type .endswith ( '.txt' ):
146
- self .touch_file (path )
174
+ reset.txt - reset FPGA during run mode
175
+ """
176
+ if self .options . forced_reset_type and self .options .forced_reset_type .endswith ( '.txt' ):
177
+ reset_file_path = os . path . join ( self .disk , self . options .forced_reset_type .lower ())
178
+ self .touch_file (reset_file_path )
147
179
else :
148
180
self .safe_sendBreak (self .serial ) # Instead of serial.sendBreak()
149
181
# Give time to wait for the image loading
150
- sleep (2 )
182
+ reset_tout_s = self .options .forced_reset_timeout if self .options .forced_reset_timeout is not None else self .DEFAULT_RESET_TOUT
183
+ self .reset_timeout (reset_tout_s )
151
184
152
185
def flush (self ):
186
+ """ Flush serial ports
187
+ """
153
188
self .serial .flushInput ()
154
189
self .serial .flushOutput ()
155
190
if self .extra_serial :
@@ -158,10 +193,15 @@ def flush(self):
158
193
159
194
160
195
class Test :
196
+ """ Baseclass for host test's test runner
197
+ """
161
198
def __init__ (self ):
162
199
self .mbed = Mbed ()
163
200
164
201
def run (self ):
202
+ """ Test runner for host test. This function will start executing
203
+ test and forward test result via serial port to test suite
204
+ """
165
205
try :
166
206
result = self .test ()
167
207
self .print_result ("success" if result else "failure" )
@@ -170,31 +210,40 @@ def run(self):
170
210
self .print_result ("error" )
171
211
172
212
def setup (self ):
173
- """ Setup and check if configuration for test is correct. E.g. if serial port can be opened """
213
+ """ Setup and check if configuration for test is correct. E.g. if serial port can be opened
214
+ """
174
215
result = True
175
216
if not self .mbed .serial :
176
217
result = False
177
218
self .print_result ("ioerr_serial" )
178
219
return result
179
220
180
221
def notify (self , message ):
181
- """ On screen notification function """
222
+ """ On screen notification function
223
+ """
182
224
print message
183
225
stdout .flush ()
184
226
185
227
def print_result (self , result ):
186
- """ Test result unified printing function """
228
+ """ Test result unified printing function
229
+ """
187
230
self .notify ("\n {%s}\n {end}" % result )
188
231
189
232
190
233
class DefaultTest (Test ):
234
+ """ Test class with serial port initialization
235
+ """
191
236
def __init__ (self ):
192
237
Test .__init__ (self )
193
238
serial_init_res = self .mbed .init_serial ()
194
239
self .mbed .reset ()
195
240
196
241
197
242
class Simple (DefaultTest ):
243
+ """ Simple, basic host test's test runner waiting for serial port
244
+ output from MUT, no supervision over test running in MUT is executed.
245
+ Just waiting for result
246
+ """
198
247
def run (self ):
199
248
try :
200
249
while True :
@@ -207,5 +256,6 @@ def run(self):
207
256
except KeyboardInterrupt , _ :
208
257
print "\n [CTRL+c] exit"
209
258
259
+
210
260
if __name__ == '__main__' :
211
261
Simple ().run ()
0 commit comments