zephyr/scripts/support/runner/dfu.py

81 lines
2.7 KiB
Python
Raw Normal View History

# Copyright (c) 2017 Linaro Limited.
#
# SPDX-License-Identifier: Apache-2.0
'''Runner for flashing with dfu-util.'''
import sys
import time
scripts: runner: use arguments, not environment vars The various runners (flash/debug scripts) use environment variables to take arguments. This is legacy behavior which is not desirable. Use command line arguments instead. Note: this leaves more general environment variables with publicly documented behavior in place for now, for compatibility, e.g.: ZEPHYR_FLASH_OVER_DFU, OPENSDA_FW, ESP_IDF_PATH, PYOCD_DAPARG For example, when using dfu-util to flash arduino_101, instead of setting DFUUTIL_PID, DFUUTIL_ALT, and DFUUTIL_IMG environment variables, have the script invocation look like this: python3 .../zephyr_flash_debug.py dfu-util flash \ [common arguments omitted] \ --pid=8087:0aba --alt=x86_app \ --img=.../build/zephyr/zephyr.bin Make similar changes for other runners (openocd, etc.) and targets (debug, debugserver). To implement this in the scripts: - have the individual scripts/support/runner/some-runner.py files register their own command line arguments - teach them to construct instances from arguments, not the environment - have zephyr_flash_debug.py request runners to register command line argument parsers, and handle arguments In the build system: - add a new board_runner_args() extension function that board.cmake files can use to add to the zephyr_flash_debug.py command line - adjust cmake/flash/CMakeLists.txt to invoke with arguments - add new helper include files for each runner (like boards/common/dfu-util.board.cmake, etc.), which add default options as needed and then add on overrides from board_runner_args() calls - update board.cmake files to use the new includes and extension This implied some tweaking when using openocd to make the CMake string escaping and unescaping work properly. Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-11-16 17:45:38 -05:00
from .core import ZephyrBinaryRunner, RunnerCaps
class DfuUtilBinaryRunner(ZephyrBinaryRunner):
'''Runner front-end for dfu-util.'''
def __init__(self, pid, alt, img, dfuse=None, exe='dfu-util', debug=False):
super(DfuUtilBinaryRunner, self).__init__(debug=debug)
self.alt = alt
self.img = img
self.dfuse = dfuse
self.cmd = [exe, '-d,{}'.format(pid)]
try:
self.list_pattern = ', alt={},'.format(int(self.alt))
except ValueError:
self.list_pattern = ', name="{}",'.format(self.alt)
@classmethod
def name(cls):
return 'dfu-util'
@classmethod
def capabilities(cls):
return RunnerCaps(commands={'flash'})
scripts: runner: use arguments, not environment vars The various runners (flash/debug scripts) use environment variables to take arguments. This is legacy behavior which is not desirable. Use command line arguments instead. Note: this leaves more general environment variables with publicly documented behavior in place for now, for compatibility, e.g.: ZEPHYR_FLASH_OVER_DFU, OPENSDA_FW, ESP_IDF_PATH, PYOCD_DAPARG For example, when using dfu-util to flash arduino_101, instead of setting DFUUTIL_PID, DFUUTIL_ALT, and DFUUTIL_IMG environment variables, have the script invocation look like this: python3 .../zephyr_flash_debug.py dfu-util flash \ [common arguments omitted] \ --pid=8087:0aba --alt=x86_app \ --img=.../build/zephyr/zephyr.bin Make similar changes for other runners (openocd, etc.) and targets (debug, debugserver). To implement this in the scripts: - have the individual scripts/support/runner/some-runner.py files register their own command line arguments - teach them to construct instances from arguments, not the environment - have zephyr_flash_debug.py request runners to register command line argument parsers, and handle arguments In the build system: - add a new board_runner_args() extension function that board.cmake files can use to add to the zephyr_flash_debug.py command line - adjust cmake/flash/CMakeLists.txt to invoke with arguments - add new helper include files for each runner (like boards/common/dfu-util.board.cmake, etc.), which add default options as needed and then add on overrides from board_runner_args() calls - update board.cmake files to use the new includes and extension This implied some tweaking when using openocd to make the CMake string escaping and unescaping work properly. Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-11-16 17:45:38 -05:00
@classmethod
def do_add_parser(cls, parser):
# Required:
parser.add_argument("--pid", required=True,
help="USB VID:PID of the board")
parser.add_argument("--alt", required=True,
help="interface alternate setting number or name")
# Optional:
parser.add_argument("--img",
help="binary to flash, default is --kernel-bin")
parser.add_argument("--dfuse-addr", default=None,
help='''target address if the board is a DfuSe
device; ignored it not present''')
parser.add_argument('--dfu-util', default='dfu-util',
help='dfu-util executable; defaults to "dfu-util"')
scripts: runner: use arguments, not environment vars The various runners (flash/debug scripts) use environment variables to take arguments. This is legacy behavior which is not desirable. Use command line arguments instead. Note: this leaves more general environment variables with publicly documented behavior in place for now, for compatibility, e.g.: ZEPHYR_FLASH_OVER_DFU, OPENSDA_FW, ESP_IDF_PATH, PYOCD_DAPARG For example, when using dfu-util to flash arduino_101, instead of setting DFUUTIL_PID, DFUUTIL_ALT, and DFUUTIL_IMG environment variables, have the script invocation look like this: python3 .../zephyr_flash_debug.py dfu-util flash \ [common arguments omitted] \ --pid=8087:0aba --alt=x86_app \ --img=.../build/zephyr/zephyr.bin Make similar changes for other runners (openocd, etc.) and targets (debug, debugserver). To implement this in the scripts: - have the individual scripts/support/runner/some-runner.py files register their own command line arguments - teach them to construct instances from arguments, not the environment - have zephyr_flash_debug.py request runners to register command line argument parsers, and handle arguments In the build system: - add a new board_runner_args() extension function that board.cmake files can use to add to the zephyr_flash_debug.py command line - adjust cmake/flash/CMakeLists.txt to invoke with arguments - add new helper include files for each runner (like boards/common/dfu-util.board.cmake, etc.), which add default options as needed and then add on overrides from board_runner_args() calls - update board.cmake files to use the new includes and extension This implied some tweaking when using openocd to make the CMake string escaping and unescaping work properly. Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-11-16 17:45:38 -05:00
@classmethod
def create_from_args(cls, args):
if args.img is None:
args.img = args.kernel_bin
return DfuUtilBinaryRunner(args.pid, args.alt, args.img,
dfuse=args.dfuse_addr, exe=args.dfu_util,
debug=args.verbose)
def find_device(self):
cmd = list(self.cmd) + ['-l']
output = self.check_output(cmd)
output = output.decode(sys.getdefaultencoding())
return self.list_pattern in output
def do_run(self, command, **kwargs):
reset = 0
if not self.find_device():
reset = 1
print('Please reset your board to switch to DFU mode...')
while not self.find_device():
time.sleep(0.1)
cmd = list(self.cmd)
if self.dfuse is not None:
cmd.extend(['-s', '{}:leave'.format(self.dfuse)])
cmd.extend(['-a', self.alt, '-D', self.img])
self.check_call(cmd)
if reset:
print('Now reset your board again to switch back to runtime mode.')