Skip to content

Commit 3ebc2e4

Browse files
Marti BolivarAnas Nashif
Marti Bolivar
authored and
Anas Nashif
committed
scripts: runner: dfu-util: support DT-based flashing for DfuSe
Enable DT support in the dfu-util flasher when the target is a DfuSe (DFU + ST extensions) device. Untangling DfuSe-specific options (currently, the default is 'leave', to immediately start running after the flashing is done) from the actual address makes this cleaner, and sets up a subsequent patch to let callers set DfuSe options. It also lets us fix an unnecessary printline when flashing DfuSe devices. There's no need to reset those, since the 'leave' modifier starts execution immediately. Signed-off-by: Marti Bolivar <[email protected]>
1 parent 3830bc5 commit 3ebc2e4

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

boards/arm/96b_carbon/board.cmake

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
board_runner_args(dfu-util "--pid=0483:df11" "--alt=0")
2-
board_runner_args(dfu-util "--dfuse-addr=${CONFIG_FLASH_BASE_ADDRESS}")
1+
board_runner_args(dfu-util "--pid=0483:df11" "--alt=0" "--dfuse")
32

43
include($ENV{ZEPHYR_BASE}/boards/common/dfu-util.board.cmake)

scripts/support/runner/dfu.py

+43-12
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,44 @@
44

55
'''Runner for flashing with dfu-util.'''
66

7+
from collections import namedtuple
8+
import os
79
import sys
810
import time
911

10-
from .core import ZephyrBinaryRunner, RunnerCaps
12+
from .core import ZephyrBinaryRunner, RunnerCaps, BuildConfiguration
13+
14+
15+
DfuSeConfig = namedtuple('DfuSeConfig', ['address', 'options'])
1116

1217

1318
class DfuUtilBinaryRunner(ZephyrBinaryRunner):
1419
'''Runner front-end for dfu-util.'''
1520

16-
def __init__(self, pid, alt, img, dfuse=None, exe='dfu-util', debug=False):
21+
def __init__(self, pid, alt, img, exe='dfu-util',
22+
dfuse_config=None, debug=False):
1723
super(DfuUtilBinaryRunner, self).__init__(debug=debug)
1824
self.alt = alt
1925
self.img = img
20-
self.dfuse = dfuse
2126
self.cmd = [exe, '-d,{}'.format(pid)]
2227
try:
2328
self.list_pattern = ', alt={},'.format(int(self.alt))
2429
except ValueError:
2530
self.list_pattern = ', name="{}",'.format(self.alt)
2631

32+
if dfuse_config is None:
33+
self.dfuse = False
34+
else:
35+
self.dfuse = True
36+
self.dfuse_config = dfuse_config
37+
2738
@classmethod
2839
def name(cls):
2940
return 'dfu-util'
3041

3142
@classmethod
3243
def capabilities(cls):
33-
return RunnerCaps(commands={'flash'})
44+
return RunnerCaps(commands={'flash'}, flash_addr=True)
3445

3546
@classmethod
3647
def do_add_parser(cls, parser):
@@ -43,18 +54,27 @@ def do_add_parser(cls, parser):
4354
# Optional:
4455
parser.add_argument("--img",
4556
help="binary to flash, default is --kernel-bin")
46-
parser.add_argument("--dfuse-addr", default=None,
47-
help='''target address if the board is a DfuSe
48-
device; ignored it not present''')
57+
parser.add_argument("--dfuse", default=False, action='store_true',
58+
help='''set if target is a DfuSe device;
59+
implies --dt-flash.''')
4960
parser.add_argument('--dfu-util', default='dfu-util',
5061
help='dfu-util executable; defaults to "dfu-util"')
5162

5263
@classmethod
5364
def create_from_args(cls, args):
5465
if args.img is None:
5566
args.img = args.kernel_bin
67+
68+
if args.dfuse:
69+
args.dt_flash = True # --dfuse implies --dt-flash.
70+
build_conf = BuildConfiguration(os.getcwd())
71+
dcfg = DfuSeConfig(address=cls.get_flash_address(args, build_conf),
72+
options="leave")
73+
else:
74+
dcfg = None
75+
5676
return DfuUtilBinaryRunner(args.pid, args.alt, args.img,
57-
dfuse=args.dfuse_addr, exe=args.dfu_util,
77+
exe=args.dfu_util, dfuse_config=dcfg,
5878
debug=args.verbose)
5979

6080
def find_device(self):
@@ -64,17 +84,28 @@ def find_device(self):
6484
return self.list_pattern in output
6585

6686
def do_run(self, command, **kwargs):
67-
reset = 0
87+
reset = False
6888
if not self.find_device():
69-
reset = 1
89+
reset = True
7090
print('Please reset your board to switch to DFU mode...')
7191
while not self.find_device():
7292
time.sleep(0.1)
7393

7494
cmd = list(self.cmd)
75-
if self.dfuse is not None:
76-
cmd.extend(['-s', '{}:leave'.format(self.dfuse)])
95+
if self.dfuse:
96+
# http://dfu-util.sourceforge.net/dfuse.html
97+
dcfg = self.dfuse_config
98+
addr_opts = hex(dcfg.address) + ':' + dcfg.options
99+
cmd.extend(['-s', addr_opts])
77100
cmd.extend(['-a', self.alt, '-D', self.img])
78101
self.check_call(cmd)
102+
103+
if self.dfuse and 'leave' in dcfg.options.split(':'):
104+
# Normal DFU devices generally need to be reset to switch
105+
# back to the flashed program.
106+
#
107+
# DfuSe targets do as well, except when 'leave' is given
108+
# as an option.
109+
reset = False
79110
if reset:
80111
print('Now reset your board again to switch back to runtime mode.')

0 commit comments

Comments
 (0)