|
5 | 5 | '''Runner for debugging with J-Link.''' |
6 | 6 |
|
7 | 7 | import argparse |
8 | | -import glob |
9 | 8 | import ipaddress |
10 | 9 | import logging |
11 | 10 | import os |
@@ -110,41 +109,34 @@ def tool_opt_help(cls) -> str: |
110 | 109 | return "Additional options for JLink Commander, e.g. '-autoconnect 1'" |
111 | 110 |
|
112 | 111 | @staticmethod |
113 | | - def find_jlink(): |
| 112 | + def default_jlink(): |
114 | 113 | global DEFAULT_JLINK_EXE |
115 | 114 |
|
116 | 115 | if sys.platform == 'win32': |
117 | 116 | # JLink.exe can collide with the JDK executable of the same name |
118 | | - # Look in the usual locations before falling back to $PATH |
119 | | - for root in [os.environ["ProgramFiles"], os.environ["ProgramFiles(x86)"], str(Path.home())]: # noqa SIM112 |
120 | | - # SEGGER folder can contain a single "JLink" folder |
121 | | - _direct = Path(root) / "SEGGER" / "JLink" / "JLink.exe" |
122 | | - if _direct.exists(): |
123 | | - DEFAULT_JLINK_EXE = str(_direct) |
124 | | - else: |
125 | | - # SEGGER folder can contain multiple versions such as: |
126 | | - # JLink_V796b |
127 | | - # JLink_V796t |
128 | | - # JLink_V798c |
129 | | - # Find the latest version |
130 | | - _versions = glob.glob(str(Path(root) / "SEGGER" / "JLink_V*")) |
131 | | - if len(_versions) == 0: |
132 | | - continue |
133 | | - _expected_jlink = Path(_versions[-1]) / "JLink.exe" |
134 | | - if not _expected_jlink.exists(): |
135 | | - continue |
136 | | - DEFAULT_JLINK_EXE = str(_expected_jlink) |
137 | | - break |
138 | | - else: |
139 | | - # Not found in the normal locations, hope that $PATH is correct |
| 117 | + # Locate the executable using the registry |
| 118 | + try: |
| 119 | + import winreg |
| 120 | + |
| 121 | + # Note that when multiple JLink versions are installed on the |
| 122 | + # machine this points to the one that was installed |
| 123 | + # last, and not to the latest version. |
| 124 | + key = winreg.OpenKeyEx( |
| 125 | + winreg.HKEY_CURRENT_USER, r"Software\SEGGER\J-Link") |
| 126 | + DEFAULT_JLINK_EXE = ( |
| 127 | + Path(winreg.QueryValueEx(key, "InstallPath")[0]) |
| 128 | + / "JLink.exe") |
| 129 | + except Exception: |
| 130 | + # Not found via the registry, hope that $PATH is correct |
140 | 131 | DEFAULT_JLINK_EXE = "JLink.exe" |
141 | 132 | else: |
142 | 133 | DEFAULT_JLINK_EXE = "JLinkExe" |
143 | 134 |
|
144 | 135 | @classmethod |
145 | 136 | def do_add_parser(cls, parser): |
146 | 137 |
|
147 | | - cls.find_jlink() |
| 138 | + # Find the default JLink executable |
| 139 | + cls.default_jlink() |
148 | 140 |
|
149 | 141 | # Required: |
150 | 142 | parser.add_argument('--device', required=True, help='device name') |
@@ -287,6 +279,7 @@ def do_run(self, command, **kwargs): |
287 | 279 | # version of the tools we're using. |
288 | 280 | self.commander = os.fspath( |
289 | 281 | Path(self.require(self.commander)).resolve()) |
| 282 | + self.logger.debug(f'JLink executable: {self.commander}') |
290 | 283 | self.logger.info(f'JLink version: {self.jlink_version_str}') |
291 | 284 |
|
292 | 285 | rtos = self.thread_info_enabled and self.supports_thread_info |
|
0 commit comments