|
| 1 | +# Copyright (c) 2025, Microchip Technology Inc. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +'''Runner for flashing PIC devices with ipecmd.''' |
| 6 | + |
| 7 | +import os |
| 8 | +import platform |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from runners.core import RunnerCaps, ZephyrBinaryRunner |
| 13 | + |
| 14 | +COMMON_LOCATIONS = ["/opt", "/usr/local", "/home", "C:\\Program Files"] |
| 15 | + |
| 16 | + |
| 17 | +class IpecmdBinaryRunner(ZephyrBinaryRunner): |
| 18 | + '''Runner front-end for Microchip's dspic33a_curiosity.''' |
| 19 | + |
| 20 | + def __init__(self, cfg, device, flash_tool): |
| 21 | + super().__init__(cfg) |
| 22 | + self.elf = cfg.elf_file |
| 23 | + self.ipecmd_cmd = None |
| 24 | + self.mplabx_base = None |
| 25 | + self.java_bin = None |
| 26 | + self.ipecmd_jar = None |
| 27 | + if platform.system() == 'Linux' or platform.system() == 'Windows': |
| 28 | + self.mplabx_base = self.find_mplabx_base() |
| 29 | + if not self.mplabx_base: |
| 30 | + print("Error: Could not locate mplabx base directory") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + version_path = self.find_latest_version_dir(self.mplabx_base) |
| 34 | + if not version_path: |
| 35 | + print("Error: No MPLAB X version directories found") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + self.java_bin = self.find_java_bin(version_path) |
| 39 | + if not self.java_bin or not os.access(self.java_bin, os.X_OK): |
| 40 | + print("Error: Java executable not found or not executable") |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + self.ipecmd_jar = self.find_ipecmd_jar(version_path) |
| 44 | + if not self.ipecmd_jar: |
| 45 | + print(f"Error: ipecmd.jar not found in {version_path}/mplab_platform/mplab_ipe/") |
| 46 | + sys.exit(1) |
| 47 | + else: |
| 48 | + print(f'ipecmd: {self.ipecmd_jar}') |
| 49 | + self.app_bin = cfg.bin_file |
| 50 | + print(f'bin file: {cfg.bin_file}') |
| 51 | + self.hex_file = cfg.hex_file |
| 52 | + print(f'hex file: {cfg.hex_file}') |
| 53 | + self.device = device |
| 54 | + self.flash_tool = flash_tool |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def name(cls): |
| 58 | + return 'ipecmd' |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def capabilities(cls): |
| 62 | + return RunnerCaps(commands={'flash'}, erase=True, reset=True) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def do_add_parser(cls, parser): |
| 66 | + # Required |
| 67 | + parser.add_argument('--device', required=True, help='soc') |
| 68 | + parser.add_argument('--flash-tool', required=True, help='hardware tool to program') |
| 69 | + |
| 70 | + parser.set_defaults(reset=True) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def do_create(cls, cfg, args): |
| 74 | + return IpecmdBinaryRunner(cfg, args.device, args.flash_tool) |
| 75 | + |
| 76 | + def do_run(self, command, **kwargs): |
| 77 | + print("***************Flashing*************") |
| 78 | + self.ensure_output('hex') |
| 79 | + |
| 80 | + self.logger.info(f'Flashing file: {self.hex_file}') |
| 81 | + if self.hex_file is not None: |
| 82 | + self.logger.info(f'flash tool: {self.flash_tool}, Device: {self.device}') |
| 83 | + self.logger.info(f'flash cmd: {self.ipecmd_jar}') |
| 84 | + cmd = [ |
| 85 | + str(self.java_bin), |
| 86 | + '-jar', |
| 87 | + str(self.ipecmd_jar), |
| 88 | + '-TP' + self.flash_tool, |
| 89 | + '-P' + self.device, |
| 90 | + '-M', |
| 91 | + '-F' + self.hex_file, |
| 92 | + '-OL', |
| 93 | + ] |
| 94 | + self.require(cmd[0]) |
| 95 | + self.check_call(cmd) |
| 96 | + else: |
| 97 | + print("E: No HEX file found") |
| 98 | + |
| 99 | + def find_mplabx_base(self): |
| 100 | + for base in COMMON_LOCATIONS: |
| 101 | + for root, dirs, _ in os.walk(base): |
| 102 | + for d in dirs: |
| 103 | + if d.startswith("mplabx") or d.startswith("MPLABX"): |
| 104 | + return Path(root) / d |
| 105 | + return None |
| 106 | + |
| 107 | + def find_latest_version_dir(self, mplabx_base): |
| 108 | + versions = sorted([p for p in mplabx_base.glob("v*/") if p.is_dir()]) |
| 109 | + return versions[-1] if versions else None |
| 110 | + |
| 111 | + def find_java_bin(self, version_path): |
| 112 | + if platform.system() == 'Linux': |
| 113 | + java_dirs = list(version_path.glob("sys/java/*/bin/java")) |
| 114 | + elif platform.system() == 'Windows': |
| 115 | + java_dirs = list(version_path.glob("sys/java/*/bin/java.exe")) |
| 116 | + else: |
| 117 | + self.logger.error("Platform not supported") |
| 118 | + sys.exit(1) |
| 119 | + return java_dirs[0] if java_dirs else None |
| 120 | + |
| 121 | + def find_ipecmd_jar(self, version_path): |
| 122 | + ipe_dir = version_path / "mplab_platform/mplab_ipe" |
| 123 | + for root, _, files in os.walk(ipe_dir): |
| 124 | + if "ipecmd.jar" in files: |
| 125 | + return Path(root) / "ipecmd.jar" |
| 126 | + return None |
0 commit comments