west: add flash runner for DediProg

This adds a flash runner for DediProg using the dpcmd command.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2019-10-24 16:26:37 -07:00 committed by Anas Nashif
parent ca5c829a84
commit fca4ead397
4 changed files with 152 additions and 1 deletions

View File

@ -12,6 +12,7 @@ from runners.core import ZephyrBinaryRunner, MissingProgram
# flake8: noqa: F401
from runners import arc
from runners import bossac
from runners import dediprog
from runners import dfu
from runners import esp32
from runners import hifive1

View File

@ -0,0 +1,76 @@
# Copyright (c) 2017 Linaro Limited.
# Copyright (c) 2019 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
'''Dediprog (dpcmd) flash only runner for SPI chips.'''
import platform
import subprocess
from runners.core import ZephyrBinaryRunner, RunnerCaps
DPCMD_EXE = 'dpcmd.exe' if platform.system() == 'Windows' else 'dpcmd'
DEFAULT_MAX_RETRIES = 3
class DediProgBinaryRunner(ZephyrBinaryRunner):
'''Runner front-end for DediProg (dpcmd).'''
def __init__(self, cfg, spi_image, vcc, retries):
super(DediProgBinaryRunner, self).__init__(cfg)
self.spi_image = spi_image
self.vcc = vcc
self.dpcmd_retries = retries
@classmethod
def name(cls):
return 'dediprog'
@classmethod
def capabilities(cls):
return RunnerCaps(commands={'flash'})
@classmethod
def do_add_parser(cls, parser):
parser.add_argument('--spi-image', required=True,
help='path to SPI image')
parser.add_argument('--vcc',
help='VCC (0=3.5V, 1=2.5V, 2=1.8V)')
parser.add_argument('--retries', default=5,
help='Number of retries (default 5)')
@classmethod
def create(cls, cfg, args):
return DediProgBinaryRunner(cfg,
spi_image=args.spi_image,
vcc=args.vcc,
retries=args.retries)
def do_run(self, command, **kwargs):
self.require(DPCMD_EXE)
cmd_flash = [DPCMD_EXE, '--auto', self.spi_image]
if self.vcc:
cmd_flash.append('--vcc')
cmd_flash.append(self.vcc)
cmd_flash.append('--silent')
cmd_flash.append('--verify')
try:
max_retries = int(self.dpcmd_retries)
except ValueError:
max_retries = DEFAULT_MAX_RETRIES
retries = 0
while retries <= max_retries:
try:
self.check_call(cmd_flash)
except subprocess.CalledProcessError as cpe:
retries += 1
if retries > max_retries:
raise cpe
else:
continue
break

View File

@ -0,0 +1,73 @@
# Copyright (c) 2018 Foundries.io
# Copyright (c) 2019 Nordic Semiconductor ASA.
#
# SPDX-License-Identifier: Apache-2.0
import argparse
import platform
from unittest.mock import patch, call
import pytest
from runners.dediprog import DediProgBinaryRunner
from conftest import RC_KERNEL_BIN
DPCMD_EXE = 'dpcmd.exe' if platform.system() == 'Windows' else 'dpcmd'
EXPECTED_COMMAND = {
(RC_KERNEL_BIN, None):
[DPCMD_EXE,
'--auto', RC_KERNEL_BIN,
'--silent', '--verify'],
(RC_KERNEL_BIN, '0'):
[DPCMD_EXE,
'--auto', RC_KERNEL_BIN, '--vcc', '0',
'--silent', '--verify'],
(RC_KERNEL_BIN, '1'):
[DPCMD_EXE,
'--auto', RC_KERNEL_BIN, '--vcc', '1',
'--silent', '--verify'],
}
def require_patch(program):
assert program in [DPCMD_EXE]
def id_fn(tc):
return 'spi_image={},vcc={}'.format(*tc)
@pytest.mark.parametrize('tc', [
(RC_KERNEL_BIN, None),
(RC_KERNEL_BIN, '0'),
(RC_KERNEL_BIN, '1'),
], ids=id_fn)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.core.ZephyrBinaryRunner.check_call')
def test_dediprog_init(cc, req, tc, runner_config):
'''Test commands using a runner created by constructor.'''
spi_image, vcc = tc
runner = DediProgBinaryRunner(runner_config, spi_image=spi_image,
vcc=vcc, retries=0)
runner.run('flash')
assert cc.call_args_list == [call(EXPECTED_COMMAND[tc])]
@pytest.mark.parametrize('tc', [
(RC_KERNEL_BIN, None),
(RC_KERNEL_BIN, '0'),
(RC_KERNEL_BIN, '1'),
], ids=id_fn)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.core.ZephyrBinaryRunner.check_call')
def test_dediprog_create(cc, req, tc, runner_config):
'''Test commands using a runner created from command line parameters.'''
spi_image, vcc = tc
args = ['--spi-image', spi_image, '--retries', '0']
if vcc:
args.extend(['--vcc', vcc])
parser = argparse.ArgumentParser()
DediProgBinaryRunner.add_parser(parser)
arg_namespace = parser.parse_args(args)
runner = DediProgBinaryRunner.create(runner_config, arg_namespace)
runner.run('flash')
assert cc.call_args_list == [call(EXPECTED_COMMAND[tc])]

View File

@ -14,5 +14,6 @@ def test_runner_imports():
runner_names = set(r.name() for r in ZephyrBinaryRunner.get_runners())
expected = set(('arc-nsim', 'bossac', 'dfu-util', 'em-starterkit', 'esp32',
'hifive1', 'jlink', 'nios2', 'nrfjprog', 'openocd', 'pyocd',
'qemu', 'xtensa', 'intel_s1000', 'blackmagicprobe'))
'qemu', 'xtensa', 'intel_s1000', 'blackmagicprobe',
'dediprog'))
assert runner_names == expected