Skip to content

Commit 874ed49

Browse files
author
CoolUsername
committed
Fix bug by creating a temporary file with a line separator being passed to the stdin of the ST-LINK CLI
1 parent 0a51605 commit 874ed49

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

src/mbed_os_tools/test/host_tests_conn_proxy/conn_primitive_serial.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, name, port, baudrate, config):
3131
self.write_timeout = 5
3232
self.config = config
3333
self.target_id = self.config.get('target_id', None)
34+
self.mcu = self.config.get('mcu', None)
3435
self.polling_timeout = config.get('polling_timeout', 60)
3536
self.forced_reset_timeout = config.get('forced_reset_timeout', 1)
3637
self.skip_reset = config.get('skip_reset', False)
@@ -86,6 +87,7 @@ def reset_dev_via_serial(self, delay=1):
8687
reset_type,
8788
serial=self.serial,
8889
disk=disk,
90+
mcu=self.mcu,
8991
target_id=self.target_id,
9092
polling_timeout=self.config.get('polling_timeout'))
9193
# Post-reset sleep

src/mbed_os_tools/test/host_tests_plugins/host_test_plugins.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,18 @@ def check_parameters(self, capability, *args, **kwargs):
221221
return False
222222
return True
223223

224-
def run_command(self, cmd, shell=True):
224+
def run_command(self, cmd, shell=True, stdin = None, stdout = None):
225225
"""! Runs command from command line.
226226
@param cmd Command to execute
227227
@param shell True if shell command should be executed (eg. ls, ps)
228+
@param stdin A custom stdin for the process running the command (defaults to None)
229+
@param stdout A custom stdout for the process running the command (defaults to None)
228230
@details Function prints 'cmd' return code if execution failed
229231
@return True if command successfully executed
230232
"""
231233
result = True
232234
try:
233-
ret = call(cmd, shell=shell)
235+
ret = call(cmd, shell=shell, stdin=stdin, stdout=stdout)
234236
if ret:
235237
self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
236238
return False

src/mbed_os_tools/test/host_tests_plugins/module_reset_stlink.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import os
17+
import sys
18+
import tempfile
1619
from .host_test_plugins import HostTestPluginBase
20+
from .host_tests_logger import HtrunLogger
21+
22+
FIX_FILE_NAME = "enter_file.txt"
1723

1824

1925
class HostTestPluginResetMethod_Stlink(HostTestPluginBase):
@@ -45,10 +51,25 @@ def is_os_supported(self, os_name=None):
4551
def setup(self, *args, **kwargs):
4652
"""! Configure plugin, this function should be called before plugin execute() method is used.
4753
"""
54+
self.logger = HtrunLogger('STLINK_RESET_PLUGIN')
4855
# Note you need to have eACommander.exe on your system path!
4956
self.ST_LINK_CLI = 'ST-LINK_CLI.exe'
5057
return True
5158

59+
def create_stlink_fix_file(self, file_path):
60+
"""! Creates a file with a line separator
61+
This is to work around a bug in ST-LINK CLI that does not let the target run after burning it.
62+
See https://github.com/ARMmbed/mbed-os-tools/issues/147 for the details.
63+
@param file_path A path to write into this file
64+
"""
65+
try:
66+
with open(file_path, "w") as fix_file:
67+
fix_file.write(os.linesep)
68+
except (OSError, IOError):
69+
self.logger.prn_err("Error opening STLINK-PRESS-ENTER-BUG file")
70+
sys.exit(1)
71+
72+
5273
def execute(self, capability, *args, **kwargs):
5374
"""! Executes capability by name
5475
@@ -67,7 +88,25 @@ def execute(self, capability, *args, **kwargs):
6788
# ST-LINK_CLI.exe -Rst -Run
6889
cmd = [self.ST_LINK_CLI,
6990
'-Rst', '-Run']
70-
result = self.run_command(cmd)
91+
92+
# Due to the ST-LINK bug, we must press enter after burning the target
93+
# We do this here automatically by passing a file which contains an `ENTER` (line separator)
94+
# to the ST-LINK CLI as `stdin` for the running process
95+
enter_file_path = os.path.join(tempfile.gettempdir(), FIX_FILE_NAME)
96+
self.create_stlink_fix_file(enter_file_path)
97+
try:
98+
with open(enter_fix_file, 'r') as fix_file:
99+
std_args = {'stdin' : fix_file}
100+
if 'stdin' in kwargs:
101+
std_args['stdin'] = kwargs['stdin']
102+
if 'stdout' in kwargs:
103+
std_args['stdout'] = kwargs['stdout']
104+
105+
result = self.run_command(cmd, **std_args)
106+
except (OSError, IOError):
107+
self.logger.prn_err("Error opening STLINK-PRESS-ENTER-BUG file")
108+
sys.exit(1)
109+
71110
return result
72111

73112

src/mbed_os_tools/test/host_tests_runner/host_test_default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def callback__notify_prn(key, value, timestamp):
164164
"digest" : "serial",
165165
"port" : self.mbed.port,
166166
"baudrate" : self.mbed.serial_baud,
167+
"mcu" : self.mbed.mcu,
167168
"program_cycle_s" : self.options.program_cycle_s,
168169
"reset_type" : self.options.forced_reset_type,
169170
"target_id" : self.options.target_id,

src/mbed_os_tools/test/host_tests_runner/mbed_base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, options):
3636
self.logger = HtrunLogger('MBED')
3737
# Options related to copy / reset mbed device
3838
self.port = self.options.port
39+
self.mcu = self.options.micro
3940
self.disk = self.options.disk
4041
self.target_id = self.options.target_id
4142
self.image_path = self.options.image_path.strip('"') if self.options.image_path is not None else ''
@@ -80,7 +81,7 @@ def __init__(self, options):
8081
self.logger.prn_err("Test configuration JSON Unexpected error:", str(e))
8182
raise
8283

83-
def copy_image(self, image_path=None, disk=None, copy_method=None, port=None, retry_copy=5):
84+
def copy_image(self, image_path=None, disk=None, copy_method=None, port=None, mcu=None, retry_copy=5):
8485
"""! Closure for copy_image_raw() method.
8586
@return Returns result from copy plugin
8687
"""
@@ -167,6 +168,8 @@ def check_flash_error(target_id, disk, initial_remount_count):
167168
copy_method = self.copy_method
168169
if not port:
169170
port = self.port
171+
if not mcu:
172+
mcu = self.mcu
170173
if not retry_copy:
171174
retry_copy = self.retry_copy
172175
target_id = self.target_id
@@ -182,7 +185,7 @@ def check_flash_error(target_id, disk, initial_remount_count):
182185
for count in range(0, retry_copy):
183186
initial_remount_count = get_remount_count(disk)
184187
# Call proper copy method
185-
result = self.copy_image_raw(image_path, disk, copy_method, port)
188+
result = self.copy_image_raw(image_path, disk, copy_method, port, mcu)
186189
sleep(self.program_cycle_s)
187190
if not result:
188191
continue
@@ -191,7 +194,7 @@ def check_flash_error(target_id, disk, initial_remount_count):
191194
break
192195
return result
193196

194-
def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None):
197+
def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None, mcu=None):
195198
"""! Copy file depending on method you want to use. Handles exception
196199
and return code from shell copy commands.
197200
@return Returns result from copy plugin
@@ -209,6 +212,7 @@ def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None
209212
result = ht_plugins.call_plugin('CopyMethod',
210213
copy_method,
211214
image_path=image_path,
215+
mcu=mcu,
212216
serial=port,
213217
destination_disk=disk,
214218
target_id=self.target_id,

0 commit comments

Comments
 (0)