Skip to content

Commit c13a4f8

Browse files
authored
Merge pull request #148 from embeddedteam103/workaround_stlink_plugin_enter_bug
ST-LINK Plugin bugfix: Create temp file containing an Enter tab and pass it to ST-LINK CLI
2 parents 32aab52 + 859cd8b commit c13a4f8

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,17 @@ 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):
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)
228229
@details Function prints 'cmd' return code if execution failed
229230
@return True if command successfully executed
230231
"""
231232
result = True
232233
try:
233-
ret = call(cmd, shell=shell)
234+
ret = call(cmd, shell=shell, stdin=stdin)
234235
if ret:
235236
self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
236237
return False

src/mbed_os_tools/test/host_tests_plugins/module_reset_stlink.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +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
1720

21+
FIX_FILE_NAME = "enter_file.txt"
22+
1823

1924
class HostTestPluginResetMethod_Stlink(HostTestPluginBase):
2025

@@ -49,6 +54,20 @@ def setup(self, *args, **kwargs):
4954
self.ST_LINK_CLI = 'ST-LINK_CLI.exe'
5055
return True
5156

57+
def create_stlink_fix_file(self, file_path):
58+
"""! Creates a file with a line separator
59+
This is to work around a bug in ST-LINK CLI that does not let the target run after burning it.
60+
See https://github.com/ARMmbed/mbed-os-tools/issues/147 for the details.
61+
@param file_path A path to write into this file
62+
"""
63+
try:
64+
with open(file_path, "w") as fix_file:
65+
fix_file.write(os.linesep)
66+
except (OSError, IOError):
67+
self.print_plugin_error("Error opening STLINK-PRESS-ENTER-BUG file")
68+
sys.exit(1)
69+
70+
5271
def execute(self, capability, *args, **kwargs):
5372
"""! Executes capability by name
5473
@@ -67,7 +86,20 @@ def execute(self, capability, *args, **kwargs):
6786
# ST-LINK_CLI.exe -Rst -Run
6887
cmd = [self.ST_LINK_CLI,
6988
'-Rst', '-Run']
70-
result = self.run_command(cmd)
89+
90+
# Due to the ST-LINK bug, we must press enter after burning the target
91+
# We do this here automatically by passing a file which contains an `ENTER` (line separator)
92+
# to the ST-LINK CLI as `stdin` for the running process
93+
enter_file_path = os.path.join(tempfile.gettempdir(), FIX_FILE_NAME)
94+
self.create_stlink_fix_file(enter_file_path)
95+
try:
96+
with open(enter_file_path, 'r') as fix_file:
97+
stdin_arg = kwargs.get('stdin', fix_file)
98+
result = self.run_command(cmd, stdin = stdin_arg)
99+
except (OSError, IOError):
100+
self.print_plugin_error("Error opening STLINK-PRESS-ENTER-BUG file")
101+
sys.exit(1)
102+
71103
return result
72104

73105

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
"""
@@ -172,6 +173,8 @@ def check_flash_error(target_id, disk, initial_remount_count):
172173
copy_method = self.copy_method
173174
if not port:
174175
port = self.port
176+
if not mcu:
177+
mcu = self.mcu
175178
if not retry_copy:
176179
retry_copy = self.retry_copy
177180
target_id = self.target_id
@@ -187,7 +190,7 @@ def check_flash_error(target_id, disk, initial_remount_count):
187190
for count in range(0, retry_copy):
188191
initial_remount_count = get_remount_count(disk)
189192
# Call proper copy method
190-
result = self.copy_image_raw(image_path, disk, copy_method, port)
193+
result = self.copy_image_raw(image_path, disk, copy_method, port, mcu)
191194
sleep(self.program_cycle_s)
192195
if not result:
193196
continue
@@ -196,7 +199,7 @@ def check_flash_error(target_id, disk, initial_remount_count):
196199
break
197200
return result
198201

199-
def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None):
202+
def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None, mcu=None):
200203
"""! Copy file depending on method you want to use. Handles exception
201204
and return code from shell copy commands.
202205
@return Returns result from copy plugin
@@ -214,6 +217,7 @@ def copy_image_raw(self, image_path=None, disk=None, copy_method=None, port=None
214217
result = ht_plugins.call_plugin('CopyMethod',
215218
copy_method,
216219
image_path=image_path,
220+
mcu=mcu,
217221
serial=port,
218222
destination_disk=disk,
219223
target_id=self.target_id,

0 commit comments

Comments
 (0)