zephyr/scripts/support/runner/nrfjprog.py
Marti Bolivar 7a3462de7a 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-12-15 09:57:30 -05:00

91 lines
2.8 KiB
Python

# Copyright (c) 2017 Linaro Limited.
#
# SPDX-License-Identifier: Apache-2.0
'''Runner for flashing with nrfjprog.'''
import sys
from .core import ZephyrBinaryRunner, RunnerCaps
class NrfJprogBinaryRunner(ZephyrBinaryRunner):
'''Runner front-end for nrfjprog.'''
def __init__(self, hex_, family, debug=False):
super(NrfJprogBinaryRunner, self).__init__(debug=debug)
self.hex_ = hex_
self.family = family
@classmethod
def name(cls):
return 'nrfjprog'
@classmethod
def capabilities(cls):
return RunnerCaps(commands={'flash'})
@classmethod
def do_add_parser(cls, parser):
parser.add_argument('--nrf-family', required=True,
choices=['NRF51', 'NRF52'],
help='family of nRF MCU')
@classmethod
def create_from_args(cls, args):
return NrfJprogBinaryRunner(args.kernel_hex, args.nrf_family,
debug=args.verbose)
def get_board_snr_from_user(self):
snrs = self.check_output(['nrfjprog', '--ids'])
snrs = snrs.decode(sys.getdefaultencoding()).strip().splitlines()
if len(snrs) == 1:
return snrs[0]
print('There are multiple boards connected.')
for i, snr in enumerate(snrs, 1):
print('{}. {}'.format(i, snr))
p = 'Please select one with desired serial number (1-{}): '.format(
len(snrs))
while True:
value = input(p)
try:
value = int(value)
except ValueError:
continue
if 1 <= value <= len(snrs):
break
return snrs[value - 1]
def do_run(self, command, **kwargs):
board_snr = self.get_board_snr_from_user()
print('Flashing file: {}'.format(self.hex_))
commands = [
['nrfjprog', '--eraseall', '-f', self.family, '--snr', board_snr],
['nrfjprog', '--program', self.hex_, '-f', self.family, '--snr',
board_snr],
]
if self.family == 'NRF52':
commands.extend([
# Set reset pin
['nrfjprog', '--memwr', '0x10001200', '--val', '0x00000015',
'-f', self.family, '--snr', board_snr],
['nrfjprog', '--memwr', '0x10001204', '--val', '0x00000015',
'-f', self.family, '--snr', board_snr],
['nrfjprog', '--reset', '-f', self.family, '--snr', board_snr],
])
commands.append(['nrfjprog',
'--pinreset',
'-f', self.family,
'--snr', board_snr])
for cmd in commands:
self.check_call(cmd)
print('Board with serial number {} flashed successfully.'.format(
board_snr))