1# Copyright (c) 2024 tinyVision.ai Inc.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5"""Runner for the ecpprog programming tool for Lattice FPGAs."""
6# https://github.com/gregdavill/ecpprog
7
8from runners.core import BuildConfiguration, RunnerCaps, ZephyrBinaryRunner
9
10
11class EcpprogBinaryRunner(ZephyrBinaryRunner):
12    """Runner front-end for programming the FPGA flash at some offset."""
13
14    def __init__(self, cfg, device=None):
15        super().__init__(cfg)
16        self.device = device
17
18    @classmethod
19    def name(cls):
20        return "ecpprog"
21
22    @classmethod
23    def capabilities(cls):
24        return RunnerCaps(commands={"flash"})
25
26    @classmethod
27    def do_add_parser(cls, parser):
28        parser.add_argument(
29            "--device", dest="device", help="Device identifier such as i:<vid>:<pid>"
30        )
31
32    @classmethod
33    def do_create(cls, cfg, args):
34        return EcpprogBinaryRunner(cfg, device=args.device)
35
36    def do_run(self, command, **kwargs):
37        build_conf = BuildConfiguration(self.cfg.build_dir)
38        load_offset = build_conf.get("CONFIG_FLASH_LOAD_OFFSET", 0)
39        command = ("ecpprog", "-o", hex(load_offset), self.cfg.bin_file)
40        self.logger.debug(" ".join(command))
41        self.check_call(command)
42