diff options
author | Tom Hughes <tomhughes@chromium.org> | 2020-05-07 16:28:43 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-05-15 19:18:16 +0000 |
commit | c828740d6050fc40cf1b881374679ebf0e4b5666 (patch) | |
tree | d82b5e916e1668a83163e09b1348a2c5b1dcf28a /util/flash_jlink.py | |
parent | 263b54489f08557690791fe7f0aaed634ddc6a2e (diff) | |
download | chrome-ec-c828740d6050fc40cf1b881374679ebf0e4b5666.tar.gz |
util: Script for flashing via J-Link
BRANCH=none
BUG=none
TEST=make BOARD=bloonchipper -j && \
./util/flash_jlink.py --board bloonchipper
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: Ida382ade28451eaab62ca852337256740b81d30f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2191292
Commit-Queue: Diana Z <dzigterman@chromium.org>
Reviewed-by: Diana Z <dzigterman@chromium.org>
Diffstat (limited to 'util/flash_jlink.py')
-rwxr-xr-x | util/flash_jlink.py | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/util/flash_jlink.py b/util/flash_jlink.py new file mode 100755 index 0000000000..160c25e9df --- /dev/null +++ b/util/flash_jlink.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Flashes firmware using Segger J-Link. + +This script requires Segger hardware attached via JTAG/SWD. +""" +import argparse +import logging +import os +import subprocess +import sys +import tempfile + +JLINK_COMMANDS = ''' +r +loadfile {FIRMWARE} +go +exit +''' + + +class BoardConfig: + def __init__(self, interface, device): + self.interface = interface + self.device = device + + +SWD_INTERFACE = 'SWD' +DRAGONCLAW_CONFIG = BoardConfig(interface=SWD_INTERFACE, device='STM32F412CG') +ICETOWER_CONFIG = BoardConfig(interface=SWD_INTERFACE, device='STM32H743ZI') + +BOARD_CONFIGS = { + 'dragonclaw': DRAGONCLAW_CONFIG, + 'bloonchipper': DRAGONCLAW_CONFIG, + 'dartmonkey': ICETOWER_CONFIG, + 'icetower': ICETOWER_CONFIG, +} + + +def create_jlink_command_file(firmware_file): + tmp = tempfile.NamedTemporaryFile() + tmp.write(JLINK_COMMANDS.format(FIRMWARE=firmware_file).encode('utf-8')) + tmp.flush() + return tmp + + +def flash(jlink_exe, ip, device, interface, cmd_file): + cmd = [ + jlink_exe, + '-ip', ip, + '-device', device, + '-if', interface, + '-speed', 'auto', + '-autoconnect', '1', + '-CommandFile', cmd_file + ] + logging.debug('Running command: "%s"', ' '.join(cmd)) + subprocess.run(cmd) + + +def main(): + parser = argparse.ArgumentParser() + + default_jlink = './JLink_Linux_V670e_x86_64/JLinkExe' + parser.add_argument( + '--jlink', '-j', + help='JLinkExe path (default: ' + default_jlink + ')', + default=default_jlink) + + default_ip = '127.0.0.1:2551' + parser.add_argument( + '--ip', '-n', + help='IP address of J-Link or machine running JLinkRemoteServerCLExe ' + '(default: ' + default_ip + ')', + default=default_ip) + + default_board = 'bloonchipper' + parser.add_argument( + '--board', '-b', + help='Board (default: ' + default_board + ')', + default=default_board) + + default_firmware = os.path.join('./build', default_board, 'ec.bin') + parser.add_argument( + '--image', '-i', + help='Firmware binary (default: ' + default_firmware + ')', + default=default_firmware) + + log_level_choices = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] + parser.add_argument( + '--log_level', '-l', + choices=log_level_choices, + default='DEBUG' + ) + + args = parser.parse_args() + logging.basicConfig(level=args.log_level) + + if args.board not in BOARD_CONFIGS: + logging.error('Unable to find a config for board: "%s"', args.board) + sys.exit(1) + + config = BOARD_CONFIGS[args.board] + + args.image = os.path.realpath(args.image) + args.jlink = os.path.realpath(args.jlink) + + cmd_file = create_jlink_command_file(args.image) + flash(args.jlink, args.ip, config.device, config.interface, cmd_file.name) + cmd_file.close() + + +if __name__ == '__main__': + sys.exit(main()) |