dsh-stm32 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/README.md +92 -40
  2. package/README_EN.md +85 -36
  3. package/assets/README.md +85 -0
  4. package/assets/VERSION +1 -0
  5. package/assets/device/c0/chips.json +1108 -0
  6. package/assets/device/f0/chips.json +1570 -0
  7. package/assets/device/f1/chips.json +1238 -0
  8. package/assets/device/f1/svd/100.json +943 -0
  9. package/assets/device/f1/svd/101.json +901 -0
  10. package/assets/device/f1/svd/102.json +522 -0
  11. package/assets/device/f1/svd/103.json +1139 -0
  12. package/assets/device/f1/svd/105.json +1117 -0
  13. package/assets/device/f1/svd/107.json +1195 -0
  14. package/assets/device/f2/chips.json +674 -0
  15. package/assets/device/f3/chips.json +1318 -0
  16. package/assets/device/f4/chips.json +2942 -0
  17. package/assets/device/f7/chips.json +1710 -0
  18. package/assets/device/g0/chips.json +2102 -0
  19. package/assets/device/g4/chips.json +2494 -0
  20. package/assets/device/h5/chips.json +898 -0
  21. package/assets/device/h7/chips.json +1948 -0
  22. package/assets/device/l0/chips.json +2494 -0
  23. package/assets/device/l1/chips.json +1234 -0
  24. package/assets/device/l4/chips.json +2130 -0
  25. package/assets/device/l4+/chips.json +856 -0
  26. package/assets/device/l5/chips.json +226 -0
  27. package/assets/device/u0/chips.json +688 -0
  28. package/assets/device/u3/chips.json +310 -0
  29. package/assets/device/u5/chips.json +884 -0
  30. package/assets/device/wba/chips.json +506 -0
  31. package/assets/device/wl/chips.json +184 -0
  32. package/assets/headers/f1/stm32f100xb.h +5972 -0
  33. package/assets/headers/f1/stm32f100xe.h +6587 -0
  34. package/assets/headers/f1/stm32f101x6.h +5315 -0
  35. package/assets/headers/f1/stm32f101xb.h +5446 -0
  36. package/assets/headers/f1/stm32f101xe.h +6503 -0
  37. package/assets/headers/f1/stm32f101xg.h +6713 -0
  38. package/assets/headers/f1/stm32f102x6.h +6448 -0
  39. package/assets/headers/f1/stm32f102xb.h +6570 -0
  40. package/assets/headers/f1/stm32f103x6.h +10109 -0
  41. package/assets/headers/f1/stm32f103xb.h +10240 -0
  42. package/assets/headers/f1/stm32f103xe.h +11767 -0
  43. package/assets/headers/f1/stm32f103xg.h +11951 -0
  44. package/assets/headers/f1/stm32f105xc.h +14340 -0
  45. package/assets/headers/f1/stm32f107xc.h +15253 -0
  46. package/assets/headers/f1/stm32f1xx.h +273 -0
  47. package/assets/headers/f1/system_stm32f1xx.h +96 -0
  48. package/assets/ioc/family/C0.ioc +82 -0
  49. package/assets/ioc/family/F0.ioc +71 -0
  50. package/assets/ioc/family/F2.ioc +91 -0
  51. package/assets/ioc/family/F3.ioc +94 -0
  52. package/assets/ioc/family/F4.ioc +91 -0
  53. package/assets/ioc/family/F7.ioc +102 -0
  54. package/assets/ioc/family/G0.ioc +90 -0
  55. package/assets/ioc/family/G4.ioc +110 -0
  56. package/assets/ioc/family/H5.ioc +116 -0
  57. package/assets/ioc/family/H7.ioc +133 -0
  58. package/assets/ioc/family/L0.ioc +92 -0
  59. package/assets/ioc/family/L1.ioc +87 -0
  60. package/assets/ioc/family/L4+.ioc +92 -0
  61. package/assets/ioc/family/L4.ioc +93 -0
  62. package/assets/ioc/family/L5.ioc +91 -0
  63. package/assets/ioc/family/U0.ioc +103 -0
  64. package/assets/ioc/family/U3.ioc +113 -0
  65. package/assets/ioc/family/U5.ioc +102 -0
  66. package/assets/ioc/family/WBA.ioc +100 -0
  67. package/assets/ioc/family/WL.ioc +93 -0
  68. package/assets/ioc/minimal/STM32F100C8Tx.ioc +113 -0
  69. package/assets/ioc/minimal/STM32F100RCTx.ioc +113 -0
  70. package/assets/ioc/minimal/STM32F101C4Tx.ioc +113 -0
  71. package/assets/ioc/minimal/STM32F101C8Tx.ioc +113 -0
  72. package/assets/ioc/minimal/STM32F101RCTx.ioc +113 -0
  73. package/assets/ioc/minimal/STM32F101RFTx.ioc +113 -0
  74. package/assets/ioc/minimal/STM32F102C4Tx.ioc +113 -0
  75. package/assets/ioc/minimal/STM32F102C8Tx.ioc +113 -0
  76. package/assets/ioc/minimal/STM32F103C4Tx.ioc +113 -0
  77. package/assets/ioc/minimal/STM32F103C8Tx.ioc +113 -0
  78. package/assets/ioc/minimal/STM32F103RCTx.ioc +113 -0
  79. package/assets/ioc/minimal/STM32F103RFTx.ioc +113 -0
  80. package/assets/ioc/minimal/STM32F105RCTx.ioc +113 -0
  81. package/assets/ioc/minimal/STM32F107RCTx.ioc +113 -0
  82. package/assets/ld/f1/STM32F100C8Tx_FLASH.ld +249 -0
  83. package/assets/ld/f1/STM32F100RCTx_FLASH.ld +249 -0
  84. package/assets/ld/f1/STM32F101C4Tx_FLASH.ld +249 -0
  85. package/assets/ld/f1/STM32F101C8Tx_FLASH.ld +249 -0
  86. package/assets/ld/f1/STM32F101RCTx_FLASH.ld +249 -0
  87. package/assets/ld/f1/STM32F101RFTx_FLASH.ld +249 -0
  88. package/assets/ld/f1/STM32F102C4Tx_FLASH.ld +249 -0
  89. package/assets/ld/f1/STM32F102C8Tx_FLASH.ld +249 -0
  90. package/assets/ld/f1/STM32F103C4Tx_FLASH.ld +249 -0
  91. package/assets/ld/f1/STM32F103C8Tx_FLASH.ld +249 -0
  92. package/assets/ld/f1/STM32F103RCTx_FLASH.ld +249 -0
  93. package/assets/ld/f1/STM32F103RFTx_FLASH.ld +249 -0
  94. package/assets/ld/f1/STM32F105RCTx_FLASH.ld +249 -0
  95. package/assets/ld/f1/STM32F107RCTx_FLASH.ld +249 -0
  96. package/assets/manifest.json +1130 -0
  97. package/assets/startup/f1/startup_stm32f100xb.s +407 -0
  98. package/assets/startup/f1/startup_stm32f100xe.s +449 -0
  99. package/assets/startup/f1/startup_stm32f101x6.s +322 -0
  100. package/assets/startup/f1/startup_stm32f101xb.s +338 -0
  101. package/assets/startup/f1/startup_stm32f101xe.s +423 -0
  102. package/assets/startup/f1/startup_stm32f101xg.s +439 -0
  103. package/assets/startup/f1/startup_stm32f102x6.s +330 -0
  104. package/assets/startup/f1/startup_stm32f102xb.s +346 -0
  105. package/assets/startup/f1/startup_stm32f103x6.s +348 -0
  106. package/assets/startup/f1/startup_stm32f103xb.s +364 -0
  107. package/assets/startup/f1/startup_stm32f103xe.s +470 -0
  108. package/assets/startup/f1/startup_stm32f103xg.s +466 -0
  109. package/assets/startup/f1/startup_stm32f105xc.s +463 -0
  110. package/assets/startup/f1/startup_stm32f107xc.s +472 -0
  111. package/assets/templates/family/C0.ioc +82 -0
  112. package/assets/templates/family/F0.ioc +71 -0
  113. package/assets/templates/family/F2.ioc +91 -0
  114. package/assets/templates/family/F3.ioc +94 -0
  115. package/assets/templates/family/F4.ioc +91 -0
  116. package/assets/templates/family/F7.ioc +102 -0
  117. package/assets/templates/family/G0.ioc +90 -0
  118. package/assets/templates/family/G4.ioc +110 -0
  119. package/assets/templates/family/H5.ioc +116 -0
  120. package/assets/templates/family/H7.ioc +133 -0
  121. package/assets/templates/family/L0.ioc +92 -0
  122. package/assets/templates/family/L1.ioc +87 -0
  123. package/assets/templates/family/L4+.ioc +92 -0
  124. package/assets/templates/family/L4.ioc +93 -0
  125. package/assets/templates/family/L5.ioc +91 -0
  126. package/assets/templates/family/U0.ioc +103 -0
  127. package/assets/templates/family/U3.ioc +113 -0
  128. package/assets/templates/family/U5.ioc +102 -0
  129. package/assets/templates/family/WB.ioc +81 -0
  130. package/assets/templates/family/WBA.ioc +100 -0
  131. package/assets/templates/family/WL.ioc +93 -0
  132. package/docs/reading-guide.md +94 -71
  133. package/lib/data/chips.js +38 -51
  134. package/lib/data/clock-db.js +830 -0
  135. package/lib/data/clock-f1.js +12 -45
  136. package/lib/data/cmsis-sources.js +264 -17
  137. package/lib/data/db-presets.js +252 -0
  138. package/lib/data/device-facts.js +274 -0
  139. package/lib/data/family.js +515 -0
  140. package/lib/data/firmware-packages.js +562 -0
  141. package/lib/data/install-guides.js +2 -2
  142. package/lib/data/ioc-presets.js +248 -0
  143. package/lib/data/ioc-schema.js +1058 -0
  144. package/lib/data/rm-topics/stm32c0.json +227 -0
  145. package/lib/data/rm-topics.js +103 -0
  146. package/lib/data/search-roots.js +42 -1
  147. package/lib/data/stm32f1.js +95 -150
  148. package/lib/data/svd.js +643 -0
  149. package/lib/data/toolchain-config.js +0 -14
  150. package/lib/engine/assets.js +659 -0
  151. package/lib/engine/breakpoints.js +297 -0
  152. package/lib/engine/clock-derived.js +101 -0
  153. package/lib/engine/clock-direct.js +313 -0
  154. package/lib/engine/clock-plan.js +96 -52
  155. package/lib/engine/clock-pllmn.js +319 -0
  156. package/lib/engine/cmake-sources.js +67 -13
  157. package/lib/engine/cube-mx-guard.js +44 -0
  158. package/lib/engine/cube-programmer.js +137 -0
  159. package/lib/engine/cubemx-db.js +273 -0
  160. package/lib/engine/debug-session.js +576 -22
  161. package/lib/engine/debug.js +99 -46
  162. package/lib/engine/doctor.js +162 -19
  163. package/lib/engine/facts-acquire.js +416 -0
  164. package/lib/engine/facts.js +134 -0
  165. package/lib/engine/firmware-install.js +176 -5
  166. package/lib/engine/firmware-official.js +451 -0
  167. package/lib/engine/flash.js +317 -76
  168. package/lib/engine/gdb-mi.js +476 -0
  169. package/lib/engine/generate.js +9 -2
  170. package/lib/engine/hal-chip.js +124 -41
  171. package/lib/engine/ioc-plan.js +1247 -63
  172. package/lib/engine/ioc-rules.js +698 -0
  173. package/lib/engine/ioc-verify.js +706 -0
  174. package/lib/engine/ioc.js +43 -4
  175. package/lib/engine/msys2-install.js +4 -4
  176. package/lib/engine/probe-error.js +231 -0
  177. package/lib/engine/probe-session.js +347 -0
  178. package/lib/engine/probe.js +18 -3
  179. package/lib/engine/register-clock.js +41 -0
  180. package/lib/engine/register-gpio.js +115 -0
  181. package/lib/engine/register-linker.js +12 -7
  182. package/lib/engine/register-template.js +130 -45
  183. package/lib/engine/regs.js +43 -8
  184. package/lib/engine/rm-extract.js +144 -0
  185. package/lib/engine/rm-sources.js +360 -0
  186. package/lib/engine/rm-text.js +56 -0
  187. package/lib/engine/rtt.js +758 -0
  188. package/lib/engine/scaffold.js +185 -18
  189. package/lib/engine/toolchain-gate.js +0 -4
  190. package/lib/engine/watch.js +227 -0
  191. package/lib/index.js +12 -2
  192. package/lib/skill/stm32-workflow.js +2 -2
  193. package/lib/tools/assets.js +272 -0
  194. package/lib/tools/cache.js +8 -0
  195. package/lib/tools/debug-session.js +41 -6
  196. package/lib/tools/debug.js +41 -11
  197. package/lib/tools/doctor.js +65 -4
  198. package/lib/tools/firmware-install.js +31 -6
  199. package/lib/tools/flash.js +47 -10
  200. package/lib/tools/generate.js +25 -0
  201. package/lib/tools/ioc-apply.js +2 -0
  202. package/lib/tools/ioc-inspect.js +2 -0
  203. package/lib/tools/ioc-plan.js +59 -2
  204. package/lib/tools/ioc-verify.js +237 -0
  205. package/lib/tools/project-create.js +63 -3
  206. package/lib/tools/regs.js +453 -61
  207. package/lib/tools/route-plan.js +3 -0
  208. package/lib/tools/rtt.js +470 -0
  209. package/lib/tools/watch.js +256 -0
  210. package/package.json +5 -4
  211. package/templates/openocd/stm32c0x.cfg +83 -0
  212. package/templates/openocd/stm32u0x.cfg +64 -0
  213. package/templates/register/CMakeLists.txt +8 -2
  214. package/templates/register/Core/Inc/rtt.h +138 -0
  215. package/templates/register/Core/Src/main.c +42 -25
  216. package/templates/register/Core/Src/rtt.c +373 -0
  217. package/templates/scaffold/RTT.md +90 -0
  218. package/templates/scaffold/STM32F103XX_FLASH.ld +3 -0
  219. package/templates/scaffold/cmake/user/CMakeLists.txt +7 -0
  220. package/templates/scaffold/openocd.cfg +5 -3
  221. package/templates/scaffold/vscode/launch.json +8 -4
  222. package/docs/development-notes.md +0 -2116
  223. package/docs/dsh-stm32-plugin-/350/247/204/346/240/274.md +0 -673
  224. package/docs/logic-audit.md +0 -607
  225. package/lib/data/f1-ram.generated.js +0 -34
@@ -10,8 +10,11 @@ import { createConnection } from "node:net";
10
10
  import { dirname, isAbsolute, resolve } from "node:path";
11
11
  import { resolveSearchConfig } from "../data/search-roots.js";
12
12
  import { detectBuildDir, elfSelectionNote, findElf } from "./build.js";
13
- import { usbDiagnosis } from "./doctor.js";
14
- import { HINT_BUSY, HINT_DP, HINT_PROBE, defaultConfigs, errorCodeOfLines, findProjectConfig, probeMissingLines, quoteForDisplay, toOpenOcdPath, usbNoteFor, } from "./flash.js";
13
+ import { liveProbeChoice, openocdTransportArgs, usbDiagnosis } from "./doctor.js";
14
+ import { chooseOpenocdTarget, readProjectFamily } from "../data/family.js";
15
+ import { findProjectConfig, quoteForDisplay, toOpenOcdPath } from "./flash.js";
16
+ import { collectBreakpointRequests, planBreakpoints, quotaOf, resolveBreakpointBudget } from "./breakpoints.js";
17
+ import { classifyProbeFailure, firstProblemLine, probeMissingLines, usbNoteFor } from "./probe-error.js";
15
18
  import { resolveToolIn } from "./probe.js";
16
19
  import { runStreaming } from "./subprocess-runner.js";
17
20
  const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
@@ -131,39 +134,50 @@ function keyLogTail(lines, maxLines) {
131
134
  kept.add(index);
132
135
  return [...kept].sort((left, right) => left - right).map((index) => lines[index]).join("\n");
133
136
  }
134
- /** errorCode → 一句给人看的下一步。 */
135
- function debugHintFor(code) {
136
- if (code === "no-probe")
137
- return HINT_PROBE;
138
- if (code === "probe-busy")
139
- return HINT_BUSY;
140
- if (code === "swd-locked")
141
- return HINT_DP;
142
- if (code === "no-power")
143
- return "目标板可能没供电:检查 3V3/GND 与 BOOT 跳线。";
144
- return null;
145
- }
146
- /** 日志里第一条真正说明问题的行(给 message 用,避免只报"起不来")。 */
147
- function firstProblemLine(lines) {
148
- const hit = lines.find((line) => /^Error|unable to find|no device found|couldn't bind|cannot open|invalid/i.test(line));
149
- return (hit ?? lines[lines.length - 1] ?? "").trim();
150
- }
151
137
  export async function runDebugRun(subprocess, request) {
152
138
  const projectDir = resolve(request.projectDir);
153
139
  const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
154
140
  const startedAt = Date.now();
155
141
  const notes = [];
156
142
  const errors = [];
143
+ // B4:断点预算先算清楚(纯计算 + 读 .ioc/chips.json,不碰探针),下面所有报告共用同一份配额。
144
+ const budgetInfo = resolveBreakpointBudget(projectDir);
145
+ const collected = collectBreakpointRequests({
146
+ ...(request.breakAt === undefined ? {} : { breakAt: request.breakAt }),
147
+ ...(request.breakpoints === undefined ? {} : { breakpoints: request.breakpoints }),
148
+ ...(request.preCommands === undefined ? {} : { preCommands: request.preCommands }),
149
+ });
150
+ const plan = planBreakpoints(collected.requested, budgetInfo.budget);
157
151
  const empty = {
158
152
  ok: false, projectDir, elfPath: "", port: 0, breakAt: request.breakAt ?? "main",
159
153
  hitBreakpoint: false, location: "", registers: [], backtrace: [], locals: [],
160
- gdbCommand: "", gdbOutput: "", openOcdLogTail: "", durationMs: 0, errors, message: "", notes,
154
+ gdbCommand: "", gdbOutput: "", openOcdLogTail: "", durationMs: 0,
155
+ breakpoints: quotaOf(plan.used.code, plan.budget.code),
156
+ watchpoints: quotaOf(plan.used.data, plan.budget.data),
157
+ // 还没走到探针那一步(找不到 .elf / 缺工具链)就不算探针故障。
158
+ probeError: null,
159
+ errors, message: "", notes,
161
160
  };
162
161
  const elfPath = resolveElfPath(projectDir, request);
163
162
  if (elfPath === null) {
164
163
  errors.push("找不到 .elf(先跑 stm32_build)");
165
164
  return { ...empty, message: "没调试:找不到 .elf", durationMs: Date.now() - startedAt };
166
165
  }
166
+ // B4:超预算就到此为止——不 spawn openocd(探针不动),也不让 gdb 报一串
167
+ // "Cannot insert breakpoint" 再把整次调试带崩。文案直接给"超了几个"和"删哪个/换什么"。
168
+ if (!plan.ok) {
169
+ errors.push("断点超预算:" + plan.detail);
170
+ notes.push(...collected.notes);
171
+ notes.push(budgetInfo.note);
172
+ notes.push(plan.suggestion);
173
+ return {
174
+ ...empty,
175
+ elfPath,
176
+ message: "没调试:断点超预算(硬件断点 " + String(plan.used.code) + "/" + String(plan.budget.code)
177
+ + ",观察点 " + String(plan.used.data) + "/" + String(plan.budget.data) + ")",
178
+ durationMs: Date.now() - startedAt,
179
+ };
180
+ }
167
181
  const selectionNote = elfSelectionNote(dirname(elfPath));
168
182
  if (selectionNote !== "")
169
183
  notes.push(selectionNote);
@@ -173,7 +187,7 @@ export async function runDebugRun(subprocess, request) {
173
187
  const gdb = await resolveToolIn(subprocess, ["gdb-multiarch", "arm-none-eabi-gdb"], config.binDirs);
174
188
  if (openOcd === null || gdb === null) {
175
189
  errors.push(openOcd === null ? "找不到 openocd" : "找不到 gdb(gdb-multiarch 或 arm-none-eabi-gdb)");
176
- return { ...empty, elfPath, message: "没调试:缺工具链(先跑 stm32_env_probe)", durationMs: Date.now() - startedAt };
190
+ return { ...empty, elfPath, message: "没调试:缺工具链(先跑 stm32_doctor)", durationMs: Date.now() - startedAt };
177
191
  }
178
192
  const port = request.port ?? (await pickPort(3333));
179
193
  if (request.port === undefined && port !== 3333)
@@ -184,9 +198,27 @@ export async function runDebugRun(subprocess, request) {
184
198
  openOcdArgs.push("-f", toOpenOcdPath(projectConfig));
185
199
  }
186
200
  else {
187
- const fallback = defaultConfigs();
188
- openOcdArgs.push("-f", request.interfaceConfig ?? fallback.interfaceConfig, "-f", request.targetConfig ?? fallback.targetConfig);
189
- notes.push("工程里没有 openocd 配置文件,回退到 CMSIS-DAP + stm32f1x");
201
+ // 按 .ioc 的 Mcu.Family 选 target:换族之后不能再用 F1 的脚本硬连。
202
+ const family = readProjectFamily(projectDir);
203
+ const choice = chooseOpenocdTarget(family);
204
+ if (choice.error !== "") {
205
+ errors.push(choice.error);
206
+ return {
207
+ ...empty,
208
+ elfPath,
209
+ message: "没调试:" + (family === null ? "认不出芯片族" : "本机 openocd 没有 " + family.id + " 的 target 脚本"),
210
+ durationMs: Date.now() - startedAt,
211
+ };
212
+ }
213
+ const live = request.interfaceConfig === undefined ? await liveProbeChoice(subprocess, request.signal) : null;
214
+ const interfaceConfig = request.interfaceConfig ?? (live !== null && live.swdProbes.length > 0 ? live.cfg : "interface/cmsis-dap.cfg");
215
+ const targetConfig = request.targetConfig ?? choice.targetConfig;
216
+ openOcdArgs.push("-f", interfaceConfig, ...openocdTransportArgs(interfaceConfig), "-f", targetConfig);
217
+ notes.push("工程里没有 openocd 配置文件,回退到 " + interfaceConfig + " + " + targetConfig);
218
+ if (family !== null)
219
+ notes.push("芯片族按 .ioc 判定:" + family.id + "(内核 " + family.core + ")");
220
+ if (choice.note !== "")
221
+ notes.push(choice.note);
190
222
  }
191
223
  if (request.adapterSpeed !== undefined)
192
224
  openOcdArgs.push("-c", "adapter speed " + String(request.adapterSpeed));
@@ -208,26 +240,31 @@ export async function runDebugRun(subprocess, request) {
208
240
  // F2:旧版只回"openocd 起不来"+15 行尾巴,真正的 adapter init 失败常被挤掉。
209
241
  // 现在先归类(与 flash 同一套规则),再把有信息量的行留下,并顺手做一次 USB 形态诊断。
210
242
  const lines = listening.text.split(/\r?\n/u).filter((line) => line.trim() !== "");
211
- const errorCode = errorCodeOfLines(lines);
212
- const hint = debugHintFor(errorCode);
213
- if (hint !== null)
214
- notes.push(hint);
215
- if (probeMissingLines(lines).length > 0) {
243
+ // 此时 openocd 还没结算,退出码未知(给 null);归类规则与 flash 完全同一份。
244
+ const probeError = classifyProbeFailure({ lines, exitCode: null, stage: "debug" });
245
+ if (probeError.kind !== "unknown")
246
+ notes.push(probeError.hint);
247
+ if (probeError.kind === "no-probe" && probeMissingLines(lines).length > 0) {
216
248
  const usb = await usbDiagnosis(subprocess, request.signal);
217
249
  const extra = usbNoteFor(usb.kind, usb.detail);
218
250
  if (extra !== null)
219
251
  notes.push(extra);
220
252
  }
221
253
  const problem = firstProblemLine(lines);
222
- errors.push("openocd 起不来(errorCode=" + (errorCode === "" ? "unknown" : errorCode) + "):" +
223
- (problem === "" ? "15 s 内没开始监听 " + String(port) : problem));
254
+ errors.push("openocd 起不来:" + (problem === "" ? "15 s 内没开始监听 " + String(port) : problem));
224
255
  return {
225
- ...empty, elfPath, port, openOcdLogTail: keyLogTail(lines, 40), errorCode,
226
- message: "没调试:openocd 起不来" + (errorCode === "" ? "" : "(" + errorCode + ")"),
256
+ ...empty, elfPath, port, openOcdLogTail: keyLogTail(lines, 40), probeError,
257
+ message: "没调试:openocd 起不来",
227
258
  durationMs: Date.now() - startedAt,
228
259
  };
229
260
  }
230
261
  const breakAt = request.breakAt ?? "main";
262
+ // B4:显式的额外断点(breakAt 已经单独处理;这里只补 breakpoints 参数里的)。
263
+ const explicit = collectBreakpointRequests({
264
+ ...(request.breakpoints === undefined ? {} : { breakpoints: request.breakpoints }),
265
+ }).requested;
266
+ // breakAt=none 但有额外断点时也要 continue,否则那些断点永远不会命中。
267
+ const shouldContinue = (breakAt !== "none" && breakAt !== "") || explicit.length > 0;
231
268
  const gdbArgs = [gdb, "-q", "-batch", "-nx"];
232
269
  const ex = (command) => {
233
270
  gdbArgs.push("-ex", command);
@@ -240,9 +277,11 @@ export async function runDebugRun(subprocess, request) {
240
277
  ex("monitor reset halt");
241
278
  if (breakAt !== "none" && breakAt !== "")
242
279
  ex("break " + breakAt);
280
+ for (const bp of explicit)
281
+ ex((bp.kind === "data" ? "watch " : "break ") + bp.location);
243
282
  for (const command of request.preCommands ?? [])
244
283
  ex(command);
245
- if (breakAt !== "none" && breakAt !== "")
284
+ if (shouldContinue)
246
285
  ex("continue");
247
286
  ex("info registers pc sp lr xpsr");
248
287
  ex("bt");
@@ -250,7 +289,7 @@ export async function runDebugRun(subprocess, request) {
250
289
  for (const command of request.postCommands ?? ["monitor reset run", "detach"])
251
290
  ex(command);
252
291
  const gdbCommand = gdbArgs.map(quoteForDisplay).join(" ");
253
- request.onProgress?.("gdb:" + breakAt + (breakAt === "none" ? "" : " 断点"));
292
+ request.onProgress?.("gdb:" + breakAt + (shouldContinue ? " 断点" : ""));
254
293
  const gdbOutcome = await runStreaming(subprocess, {
255
294
  argv: gdbArgs,
256
295
  cwd: projectDir,
@@ -277,22 +316,34 @@ export async function runDebugRun(subprocess, request) {
277
316
  && !gdbOutcome.timedOut
278
317
  && gdbOutcome.exitCode === 0
279
318
  && probe.errors.length === 0
280
- && (breakAt === "none" || probe.hitBreakpoint);
319
+ && (!shouldContinue || probe.hitBreakpoint);
281
320
  if (ok)
282
321
  notes.push("已在 " + (probe.location === "" ? breakAt : probe.location) + " 停住并抓过快照;收尾是 monitor reset run + detach,芯片现在自由运行,探针已释放");
283
322
  if (!ok && probe.hitBreakpoint)
284
323
  notes.push("命中了断点,但收尾阶段有问题");
285
- if (breakAt !== "none" && !probe.hitBreakpoint && errors.length === 0) {
286
- errors.push("没有停在 " + breakAt + ":可能是复位后没跑到这里,或符号对不上(.elf 与板上的固件不一致)");
324
+ if (shouldContinue && !probe.hitBreakpoint && errors.length === 0) {
325
+ const where = breakAt !== "none" && breakAt !== "" ? breakAt : "这 " + String(explicit.length) + " 个断点";
326
+ errors.push("没有停在 " + where + ":可能是复位后没跑到这里,或符号对不上(.elf 与板上的固件不一致)");
327
+ }
328
+ // B4:只要这次真设了断点,就把预算和来源写进 notes(6/4 是芯片事实,不是插件随手定的)。
329
+ if (collected.requested.length > 0) {
330
+ notes.push(...collected.notes);
331
+ notes.push("断点预算:" + String(plan.used.code) + "/" + String(plan.budget.code) + " 个硬件断点、"
332
+ + String(plan.used.data) + "/" + String(plan.budget.data) + " 个观察点(" + budgetInfo.note + ")");
287
333
  }
288
- // F2:失败时给结构化错误码 + 可执行提示(成功时留空,报告不带噪音)。
289
- let errorCode = "";
334
+ // B2:失败时只归一次类(成功时 probeError 是 null,报告里不带噪音)。
335
+ let probeError = null;
290
336
  if (!ok) {
291
- errorCode = errorCodeOfLines(allLines);
292
- const hint = debugHintFor(errorCode);
293
- if (hint !== null)
294
- notes.push(hint);
295
- if (errorCode === "no-probe" && probeMissingLines(allLines).length > 0) {
337
+ probeError = classifyProbeFailure({
338
+ lines: allLines,
339
+ exitCode: gdbOutcome.exitCode,
340
+ stage: "debug",
341
+ timedOut: gdbOutcome.timedOut,
342
+ spawnError: gdbOutcome.spawnError,
343
+ });
344
+ if (probeError.kind !== "unknown")
345
+ notes.push(probeError.hint);
346
+ if (probeError.kind === "no-probe" && probeMissingLines(allLines).length > 0) {
296
347
  const usb = await usbDiagnosis(subprocess, request.signal);
297
348
  const extra = usbNoteFor(usb.kind, usb.detail);
298
349
  if (extra !== null)
@@ -313,7 +364,9 @@ export async function runDebugRun(subprocess, request) {
313
364
  gdbCommand,
314
365
  gdbOutput: gdbText,
315
366
  openOcdLogTail: keyLogTail(allLines, 40),
316
- ...(errorCode === "" ? {} : { errorCode }),
367
+ breakpoints: quotaOf(plan.used.code, plan.budget.code),
368
+ watchpoints: quotaOf(plan.used.data, plan.budget.data),
369
+ probeError,
317
370
  durationMs: Date.now() - startedAt,
318
371
  errors,
319
372
  message: ok
@@ -17,46 +17,122 @@ import { findIocFile } from "./generate.js";
17
17
  import { summarizeIoc } from "./ioc.js";
18
18
  import { buildTargets, probeAll } from "./probe.js";
19
19
  import { runCapture } from "./subprocess-runner.js";
20
+ /** ST-LINK 全系 VID_0483 的这些 PID(openocd interface/stlink.cfg 的 hla_vid_pid 实测同一张表)。 */
21
+ const STLINK_PIDS = new Set(["3744", "3748", "374b", "374d", "374e", "374f", "3752", "3753", "3754"]);
22
+ /** DAPLink 系(板载探针常以 CMSIS-DAP 名义枚举,openocd 却走 cmsis-dap.cfg)。 */
23
+ const DAPLINK_PIDS = new Set(["f111"]);
20
24
  /**
21
- * 解析 `pnputil /enum-devices /connected` 的输出,回答"这台机器上 USB 是什么形态"。
22
- *
23
- * 为什么要分级:云服务器(阿里云 ECS)物理上没有 USB 口,无论用户怎么插线都进不来探针;
24
- * 这和"没插板 / 线只供电 / 驱动没装 / 被占"是四种完全不同的回答,混成一句"找不到探针"
25
- * 会让用户白折腾。诊断只读、失败不阻塞(unknown 也会给一句手查指引)。
25
+ * SEGGER 的 USB VID(J-Link / J-Trace 全系共用,PID 随型号变:靠 VID 认,不列举 PID)。
26
+ * openocd 的 interface/jlink.cfg 只有 `adapter driver jlink`——没有 vid_pid 表、也不选 transport,
27
+ * 所以 J-Link 必须由调用方追加 `-c "transport select swd"`(否则默认 jtag,SWD 目标连不上)。
28
+ */
29
+ const SEGGER_VID = "1366";
30
+ /**
31
+ * 从 pnputil 输出解析所有 USB 设备的 VID:PID 并分类。
32
+ * 只依赖 ASCII(USB\VID_xxxx&PID_yyyy),中文系统的本地化字段不参与匹配(历史教训)。
26
33
  */
34
+ export function parseProbeVidPid(text) {
35
+ const out = [];
36
+ const seen = new Set();
37
+ for (const m of text.matchAll(/USB\\VID_([0-9A-Fa-f]{4})&PID_([0-9A-Fa-f]{4})/gi)) {
38
+ const vid = (m[1] ?? "").toLowerCase();
39
+ const pid = (m[2] ?? "").toLowerCase();
40
+ const key = vid + ":" + pid;
41
+ if (seen.has(key))
42
+ continue;
43
+ seen.add(key);
44
+ let kind = "unknown";
45
+ let label = "USB 设备 " + key;
46
+ if (vid === "0483") {
47
+ if (STLINK_PIDS.has(pid)) {
48
+ kind = "st-link";
49
+ label = "ST-Link(VID:PID " + key + ")";
50
+ }
51
+ else {
52
+ kind = "unknown";
53
+ label = "ST 设备 " + key + "(不在 ST-LINK PID 表,多半是串口/DFU)";
54
+ }
55
+ }
56
+ else if (vid === SEGGER_VID) {
57
+ kind = "jlink";
58
+ label = "J-Link(VID:PID " + key + ",SEGGER)";
59
+ }
60
+ else if (vid === "0d28") {
61
+ kind = "cmsis-dap";
62
+ label = "CMSIS-DAP(VID:PID " + key + ")";
63
+ }
64
+ else if (vid === "c251") {
65
+ kind = "daplink";
66
+ label = "DAPLink(VID:PID " + key + ",走 CMSIS-DAP 协议)";
67
+ }
68
+ else if (vid === "1a86") {
69
+ kind = "ch341-serial";
70
+ label = "CH341 串口桥(VID:PID " + key + ",不是 SWD 探针)";
71
+ }
72
+ out.push({ vid, pid, kind, label });
73
+ }
74
+ return out;
75
+ }
76
+ /** 实见探针 → openocd 该用哪个 interface 配置。ST-LINK 优先(官方探针,且 hla 与 cmsis-dap 互斥)。 */
77
+ export function preferredInterfaceConfig(probes) {
78
+ if (probes.some((p) => p.kind === "st-link")) {
79
+ return { cfg: "interface/stlink-dap.cfg", why: "USB 里实见 ST-LINK(0483:37xx);走 dapdirect 而不是 hla——C0/U0 这类新族的 target 脚本用的是 swj-dp.tcl 那一套,hla 会报 transport 不支持" };
80
+ }
81
+ if (probes.some((p) => p.kind === "jlink")) {
82
+ return { cfg: "interface/jlink.cfg", why: "USB 里实见 SEGGER J-Link(1366:xxxx);openocd 的 jlink 驱动默认选 jtag,SWD 目标必须再跟一条 -c \"transport select swd\"" };
83
+ }
84
+ if (probes.some((p) => p.kind === "cmsis-dap" || p.kind === "daplink"))
85
+ return { cfg: "interface/cmsis-dap.cfg", why: "USB 里实见 CMSIS-DAP/DAPLink" };
86
+ return { cfg: "interface/cmsis-dap.cfg", why: "没认出已知探针,维持历史默认 cmsis-dap" };
87
+ }
27
88
  export function diagnoseUsb(pnputilText) {
28
89
  // 不依赖本地化字段名(中文系统的 pnputil 字段名是 GBK,读出来是乱码):
29
90
  // 只要任意一行里出现 USB\VID_、USB\ROOT_HUB、或 VID_xxxx&PID 形态的 USB 实例,
30
- // 就认定这台机器有 USB 控制器/设备;纯 ASCII 部分(USB、VID、PID、ROOT_HUB)不受编码影响。
91
+ // 就认定这台机器有 USB 控制器/设备;纯 ASCII 部分不受编码影响。
31
92
  const hasUsbController = /USB\\(?:VID_|ROOT_HUB|VID\w{4}&PID)/iu.test(pnputilText) ||
32
93
  /\\USB\\/u.test(pnputilText);
33
- // VID 集(2a):CMSIS-DAP 是 ARM 官方 VID 0x0D28;ST-Link 全系 VID_0483;
34
- // DAPLink 是 VID_C251;CH340 串口桥接的国产探针常见 VID_1A86。ASCII 本体不受编码影响。
35
- const hasDap = /CMSIS-DAP|VID_0D28|VID_0483|VID_C251|VID_1A86/iu.test(pnputilText) ||
36
- /\\usb\\vid_(?:0d28|0483|c251|1a86)/iu.test(pnputilText);
94
+ const probes = parseProbeVidPid(pnputilText);
95
+ const swdProbes = probes.filter((p) => p.kind === "st-link" || p.kind === "jlink" || p.kind === "cmsis-dap" || p.kind === "daplink");
96
+ // CH341/CH340 串口桥:2a 扩表的历史契约——"板载探针走串口透传"的形态真实存在,算在线;
97
+ // 但它不是 SWD 驱动,不参与 interface 选择(preferredInterfaceConfig 也不认它)。
98
+ const serialBridges = probes.filter((p) => p.kind === "ch341-serial");
37
99
  const hasDriverProblem = /(?:Problem Code|问题代码)\s*[::]?\s*(?:1[0-9]|2[0-8]|3[0-9]|4[0-3]|5[0-2])/iu.test(pnputilText);
100
+ const list = swdProbes.map((p) => p.label).join("、");
38
101
  if (!hasUsbController) {
39
102
  return {
40
103
  kind: "no-usb",
104
+ probes,
41
105
  detail: "系统里枚举不到任何 USB 控制器:这台机器多半是云服务器/虚拟机,物理上没有 USB 口," +
42
106
  "插线、换线、装驱动都解决不了。要么换一台有 USB 的物理机,要么用 USB over IP(usbipd/USBIP)把探针从别的机器转发过来" +
43
107
  "(注意 SWD 对延迟敏感,转发方案只建议实验用)。",
44
108
  };
45
109
  }
46
- if (hasDap && hasDriverProblem) {
110
+ if ((swdProbes.length > 0 || serialBridges.length > 0) && hasDriverProblem) {
47
111
  return {
48
112
  kind: "driver-error",
49
- detail: "能看到 CMSIS-DAP 设备但驱动状态异常(Problem Code 非 0):在设备管理器里找到该设备," +
50
- "重装或回退驱动(CMSIS-DAP 用 WinUSB/HID 通用驱动即可;装过 DAPLink 官方驱动的先卸载它)。",
113
+ probes,
114
+ detail: "能看到探针/桥设备(" + (list || serialBridges.map((p) => p.label).join("、")) + ")但驱动状态异常(Problem Code 非 0):设备管理器里重装/回退驱动(CMSIS-DAP 与 ST-Link 走 WinUSB/HID 通用驱动即可)。",
51
115
  };
52
116
  }
53
- if (hasDap) {
54
- return { kind: "ok", detail: "能看到 CMSIS-DAP 设备且驱动正常:探针在线,烧录/调试应当能直接找到它。" };
117
+ if (swdProbes.length > 0) {
118
+ // 文案必须报出具体类型:ST-Link 说成 CMSIS-DAP 会让人写错 openocd interface(2026-11-08 实测教训)
119
+ const iface = preferredInterfaceConfig(swdProbes);
120
+ return { kind: "ok", probes, detail: "探针在线:" + list + ";openocd 应用 " + iface.cfg + "。" };
121
+ }
122
+ if (serialBridges.length > 0) {
123
+ return {
124
+ kind: "ok",
125
+ probes,
126
+ detail: "USB 里只有串口桥(" + serialBridges.map((p) => p.label).join("、") + "):串口烧录/bootloader 通路可用;" +
127
+ "若指望 SWD,需要探针本身(ST-LINK / J-Link / CMSIS-DAP / DAPLink)出现在枚举里,或板载 DAP 的 USB 口接好。",
128
+ };
55
129
  }
56
130
  return {
57
131
  kind: "no-dap",
58
- detail: "有 USB 控制器但没看到常见探针(CMSIS-DAP / ST-Link / DAPLink / CH340 桥接):检查探针有没有插好(有的线只能供电不能传数据)、" +
59
- "板上调试器的跳线/开关位置、以及 BOOT0 是否接错(BOOT0=1 会进系统 bootloader,SWD 不响应)。",
132
+ probes,
133
+ detail: "有 USB 控制器但没看到 SWD 探针(ST-LINK 0483:3744-54 / J-Link 1366 / CMSIS-DAP 0D28 / DAPLink C251)" +
134
+ (probes.length > 0 ? ";看到的其他 USB 设备:" + probes.map((p) => p.label).join("、") : "") +
135
+ ":检查探针有没有插好(有的线只能供电不能传数据)、板上调试器的跳线/开关位置、以及 BOOT0 是否接错(BOOT0=1 会进系统 bootloader,SWD 不响应)。",
60
136
  };
61
137
  }
62
138
  export async function usbDiagnosis(subprocess, signal) {
@@ -71,6 +147,7 @@ export async function usbDiagnosis(subprocess, signal) {
71
147
  if (outcome.spawnError !== null || outcome.exitCode !== 0) {
72
148
  return {
73
149
  kind: "unknown",
150
+ probes: [],
74
151
  detail: "跑不了 pnputil /enum-devices(" +
75
152
  (outcome.spawnError !== null ? outcome.spawnError : "退出码 " + String(outcome.exitCode)) +
76
153
  "),无法判断 USB/探针形态;可在设备管理器里人工核对。",
@@ -81,10 +158,70 @@ export async function usbDiagnosis(subprocess, signal) {
81
158
  catch (error) {
82
159
  return {
83
160
  kind: "unknown",
161
+ probes: [],
84
162
  detail: "USB/探针诊断异常终止(" + (error instanceof Error ? error.message : String(error)) + "),可忽略;设备管理器里人工核对。",
85
163
  };
86
164
  }
87
165
  }
166
+ /**
167
+ * 引擎各处(flash / debug / regs / 会话)兜底 interface 的统一入口。
168
+ *
169
+ * 刻意**不做模块级缓存**:第一版把 60 s 的结果缓存在模块里,同进程连烧两颗不同芯片时
170
+ * 第二颗会拿到第一颗的探针结论(重试方向还会因此算反),测试里表现为用例互相污染。
171
+ * 现在一次工具调用只探一次(调用方自己复用返回值),跨调用永远重新看。
172
+ */
173
+ export async function liveProbeChoice(subprocess, signal) {
174
+ const usb = await usbDiagnosis(subprocess, signal);
175
+ const swd = usb.probes.filter((p) => p.kind === "st-link" || p.kind === "jlink" || p.kind === "cmsis-dap" || p.kind === "daplink");
176
+ const pref = preferredInterfaceConfig(swd);
177
+ return { ...pref, probes: usb.probes, swdProbes: swd };
178
+ }
179
+ /**
180
+ * interface 配置决定的额外 openocd 参数(插在 interface 与 target 之间)。
181
+ *
182
+ * 只有 J-Link 需要:interface/jlink.cfg 不选 transport,openocd 会默认 jtag,
183
+ * 结果是「No J-Link device found」或者连上了却读不到 SWD 目标。其余 interface
184
+ * 自带 transport(cmsis-dap 走 dapdirect_swd、stlink-dap 同理),多写一条反而报错。
185
+ */
186
+ export function openocdTransportArgs(interfaceConfig) {
187
+ return /jlink/iu.test(interfaceConfig) ? ["-c", "transport select swd"] : [];
188
+ }
189
+ export function capabilitiesOf(tools) {
190
+ const found = new Set(tools.filter((tool) => tool.found).map((tool) => tool.id));
191
+ const has = (id) => found.has(id);
192
+ return {
193
+ build: has("gcc") && has("cmake") && has("ninja"),
194
+ flash: has("openocd"),
195
+ debug: has("openocd") && has("gdb"),
196
+ generate: has("cubemx") && has("cubemx-java"),
197
+ };
198
+ }
199
+ export function recommendationsOf(tools, missingRequired, capabilities) {
200
+ const lines = [];
201
+ const missing = tools.filter((tool) => tool.required && !tool.found);
202
+ if (missing.length === 0) {
203
+ lines.push("必需组件齐全:" + (capabilities.build ? "可以编译" : "但编译还缺组件") + "、" +
204
+ (capabilities.flash ? "可以只烧录" : "烧录还缺 openocd") + "、" +
205
+ (capabilities.debug ? "可以烧录并调试" : "调试还缺 gdb") + "。");
206
+ }
207
+ else {
208
+ lines.push("缺 " + missingRequired.join("、") + "。按下面的顺序装,装一项就少一项:");
209
+ for (const item of missing) {
210
+ const step = item.install[0] ?? item.hint;
211
+ lines.push(item.label + ":" + step + "(自检:" + item.verify + ")");
212
+ }
213
+ lines.push("需要我帮你装吗?回一句「装 gcc」「装 openocd」或「把缺的都装上」,我就照上面的命令在这台机器上装,装完再跑一次 stm32_doctor 复测;你也可以自己装完再跑一次。");
214
+ }
215
+ const cubeMx = tools.find((item) => item.id === "cubemx");
216
+ const cubeMxJava = tools.find((item) => item.id === "cubemx-java");
217
+ if (cubeMx !== undefined && !cubeMx.found) {
218
+ lines.push("CubeMX 是可选的:不影响编译、烧录、调试和改 USER CODE,只影响建工程(stm32_project_create)与改 .ioc 后的重新生成(stm32_generate)。要建工程时再装,步骤见上面 cubemx / cubemx-java 两条。");
219
+ }
220
+ else if (cubeMx !== undefined && cubeMx.found && cubeMxJava !== undefined && !cubeMxJava.found) {
221
+ lines.push("CubeMX 装上了但找不到内置 JRE:无头生成(stm32_generate / stm32_project_create)会失败,建议用官网安装包重装一次(只拷 exe 不够)。");
222
+ }
223
+ return lines;
224
+ }
88
225
  /** 会占着探针/端口的进程名(小写比较)。 */
89
226
  const PROBE_PROCESS_NAMES = ["openocd.exe", "gdb-multiarch.exe", "arm-none-eabi-gdb.exe", "stm32cubemx.exe"];
90
227
  /** 解析 `tasklist /NH /FO CSV` 的输出,只留下关心的进程。 */
@@ -267,7 +404,7 @@ export function findLinkerScript(projectDir) {
267
404
  * ② 启动文件/芯片 ↔ cmake 源清单里的设备宏;
268
405
  * ③ 链接脚本 MEMORY 的 FLASH/RAM ↔ 芯片真实容量(大了致命,小了只浪费);
269
406
  * ④ .elf 实际大小 ↔ Flash 容量。
270
- * 另外把引脚表来源(本机 db 还是内置表、有没有校验过)写进 notes。
407
+ * 另外把引脚表来源(本机 db 还是 unknown、有没有校验过)写进 notes。
271
408
  */
272
409
  export function checkChipConsistency(input) {
273
410
  const blockers = [];
@@ -372,7 +509,7 @@ export function checkChipConsistency(input) {
372
509
  notes.push(".elf 已用 " + String(Math.round((elfUsed / flashLimit) * 100)) + "% Flash(" + kib(elfUsed) + " / " + kib(flashLimit) + "),快满了");
373
510
  }
374
511
  }
375
- // 引脚表 ↔ .ioc 用到的引脚(只有真拿到本机 db 时才比;内置表不拿来判"引脚不存在")
512
+ // 引脚表 ↔ .ioc 用到的引脚(只有真拿到本机 db 时才比:没有内置表可退,没 db 就不判引脚存在性)
376
513
  const iocPins = [...new Set((input.iocPins ?? []).map((pin) => normalizeIocPin(pin)).filter((pin) => pin !== ""))];
377
514
  if (pins.kind === "db" && iocPins.length > 0) {
378
515
  const unknown = iocPins.filter((pin) => !pins.pins.includes(pin));
@@ -533,6 +670,12 @@ export async function runDoctor(subprocess, request) {
533
670
  durationMs: Date.now() - startedAt,
534
671
  missingRequired,
535
672
  tools,
673
+ capabilities: capabilitiesOf(tools),
674
+ recommendations: recommendationsOf(tools, missingRequired, capabilitiesOf(tools)),
675
+ searched: [
676
+ "工具链搜索范围:" + describeSearchScope(config.binDirs, "PATH"),
677
+ "CubeMX 搜索范围:" + describeSearchScope(config.cubeMxRoots),
678
+ ],
536
679
  processes,
537
680
  port,
538
681
  portBusy,