Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jlink: custom flashing support #79372

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions scripts/west_commands/runners/jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class JLinkBinaryRunner(ZephyrBinaryRunner):
def __init__(self, cfg, device, dev_id=None,
commander=DEFAULT_JLINK_EXE,
dt_flash=True, erase=True, reset=False,
iface='swd', speed='auto',
iface='swd', speed='auto', flash_script = None,
loader=None,
gdbserver='JLinkGDBServer',
gdb_host='',
Expand All @@ -69,6 +69,7 @@ def __init__(self, cfg, device, dev_id=None,
self.device = device
self.dev_id = dev_id
self.commander = commander
self.flash_script = flash_script
self.dt_flash = dt_flash
self.erase = erase
self.reset = reset
Expand Down Expand Up @@ -120,6 +121,8 @@ def do_add_parser(cls, parser):
help='interface to use, default is swd')
parser.add_argument('--speed', default='auto',
help='interface speed, default is autodetect')
parser.add_argument('--flash-script', default=None,
help='Custom flashing script, default is None')
parser.add_argument('--tui', default=False, action='store_true',
help='if given, GDB uses -tui')
parser.add_argument('--gdbserver', default='JLinkGDBServer',
Expand Down Expand Up @@ -152,6 +155,7 @@ def do_create(cls, cfg, args):
erase=args.erase,
reset=args.reset,
iface=args.iface, speed=args.speed,
flash_script=args.flash_script,
gdbserver=args.gdbserver,
loader=args.loader,
gdb_host=args.gdb_host,
Expand Down Expand Up @@ -331,9 +335,7 @@ def do_run(self, command, **kwargs):
else:
self.run_client(client_cmd)

def flash(self, **kwargs):

loader_details = ""
def get_default_flash_commands(self):
lines = [
'ExitOnError 1', # Treat any command-error as fatal
'r', # Reset and halt the target
Expand Down Expand Up @@ -406,16 +408,12 @@ def flash(self, **kwargs):

self.logger.debug('JLink commander script:\n' +
'\n'.join(lines))
return flash_file, lines

# Don't use NamedTemporaryFile: the resulting file can't be
# opened again on Windows.
with tempfile.TemporaryDirectory(suffix='jlink') as d:
fname = os.path.join(d, 'runner.jlink')
with open(fname, 'wb') as f:
f.writelines(bytes(line + '\n', 'utf-8') for line in lines)
if self.supports_loader and self.loader:
loader_details = "?" + self.loader

def run_flash_cmd(self, fname, flash_file, **kwargs):
loader_details = ""
if self.supports_loader and self.loader:
loader_details = "?" + self.loader
cmd = (
[self.commander]
+ (
Expand All @@ -432,8 +430,24 @@ def flash(self, **kwargs):
+ self.tool_opt
)

if flash_file:
self.logger.info(f'Flashing file: {flash_file}')
kwargs = {}
if not self.logger.isEnabledFor(logging.DEBUG):
kwargs['stdout'] = subprocess.DEVNULL
self.check_call(cmd, **kwargs)
kwargs = {}
if not self.logger.isEnabledFor(logging.DEBUG):
kwargs['stdout'] = subprocess.DEVNULL
self.check_call(cmd, **kwargs)

def flash(self, **kwargs):
fname = self.flash_script
if fname is None:
# Don't use NamedTemporaryFile: the resulting file can't be
# opened again on Windows.
with tempfile.TemporaryDirectory(suffix='jlink') as d:
flash_file, lines = self.get_default_flash_commands()
fname = os.path.join(d, 'runner.jlink')
with open(fname, 'wb') as f:
f.writelines(bytes(line + '\n', 'utf-8') for line in lines)

self.run_flash_cmd(fname, flash_file, **kwargs)
else:
self.run_flash_cmd(fname, None, **kwargs)
Loading