dsh-stm32 0.1.3 → 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 (221) hide show
  1. package/README.md +52 -38
  2. package/README_EN.md +44 -33
  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 +37 -35
  133. package/lib/data/chips.js +3 -3
  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 +239 -18
  137. package/lib/data/db-presets.js +252 -0
  138. package/lib/data/device-facts.js +99 -26
  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 +15 -14
  148. package/lib/data/svd.js +32 -16
  149. package/lib/data/toolchain-config.js +0 -14
  150. package/lib/engine/assets.js +191 -456
  151. package/lib/engine/breakpoints.js +66 -11
  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 +423 -17
  161. package/lib/engine/debug.js +25 -6
  162. package/lib/engine/doctor.js +160 -17
  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 +290 -21
  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 +1244 -60
  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 +25 -6
  177. package/lib/engine/probe-session.js +50 -14
  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 +101 -40
  183. package/lib/engine/regs.js +41 -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 +188 -30
  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 +4 -2
  192. package/lib/skill/stm32-workflow.js +2 -2
  193. package/lib/tools/assets.js +39 -108
  194. package/lib/tools/cache.js +2 -2
  195. package/lib/tools/debug-session.js +2 -2
  196. package/lib/tools/doctor.js +55 -5
  197. package/lib/tools/firmware-install.js +31 -6
  198. package/lib/tools/flash.js +32 -4
  199. package/lib/tools/generate.js +22 -0
  200. package/lib/tools/ioc-inspect.js +1 -1
  201. package/lib/tools/ioc-plan.js +56 -2
  202. package/lib/tools/ioc-verify.js +237 -0
  203. package/lib/tools/project-create.js +41 -5
  204. package/lib/tools/regs.js +8 -8
  205. package/lib/tools/route-plan.js +1 -1
  206. package/lib/tools/rtt.js +157 -34
  207. package/lib/tools/watch.js +256 -0
  208. package/package.json +5 -4
  209. package/templates/openocd/stm32c0x.cfg +83 -0
  210. package/templates/openocd/stm32u0x.cfg +64 -0
  211. package/templates/register/CMakeLists.txt +2 -2
  212. package/templates/register/Core/Inc/rtt.h +37 -2
  213. package/templates/register/Core/Src/main.c +17 -25
  214. package/templates/register/Core/Src/rtt.c +85 -9
  215. package/templates/scaffold/RTT.md +6 -2
  216. package/templates/scaffold/STM32F103XX_FLASH.ld +3 -0
  217. package/templates/scaffold/openocd.cfg +5 -3
  218. package/templates/scaffold/vscode/launch.json +8 -4
  219. package/docs/development-notes.md +0 -2487
  220. package/docs/dsh-stm32-plugin-/350/247/204/346/240/274.md +0 -673
  221. package/docs/logic-audit.md +0 -607
@@ -7,9 +7,11 @@
7
7
  */
8
8
  import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
9
9
  import { basename, dirname, isAbsolute, resolve } from "node:path";
10
+ import { chooseOpenocdTarget, readProjectFamily } from "../data/family.js";
10
11
  import { describeSearchScope, resolveSearchConfig } from "../data/search-roots.js";
11
- import { usbDiagnosis } from "./doctor.js";
12
- import { classifyProbeFailure, probeMissingLines, usbNoteFor } from "./probe-error.js";
12
+ import { liveProbeChoice, usbDiagnosis } from "./doctor.js";
13
+ import { cubeProgrammerBackendNote, findCubeProgrammerCli, runCubeProgrammerFlash } from "./cube-programmer.js";
14
+ import { classifyProbeFailure, makeProbeError, probeMissingLines, usbNoteFor } from "./probe-error.js";
13
15
  import { detectBuildDir, elfSelectionNote, findElf } from "./build.js";
14
16
  import { findIocFile } from "./generate.js";
15
17
  import { summarizeIoc } from "./ioc.js";
@@ -126,9 +128,15 @@ export function quoteForDisplay(item) {
126
128
  return item;
127
129
  return item.includes('"') ? "'" + item + "'" : '"' + item + '"';
128
130
  }
129
- export function defaultConfigs() {
130
- // 只支持 CMSIS-DAP + STM32F1(规格 §13 已定:探针只 CMSIS-DAP,芯片只 F103C8T6)。
131
- return { interfaceConfig: "interface/cmsis-dap.cfg", targetConfig: "target/stm32f1x.cfg" };
131
+ /**
132
+ * 内置兜底配置:探针只 CMSIS-DAP(规格 §13),target 按芯片族取。
133
+ *
134
+ * family 省略/null 时保持历史行为回退 stm32f1x。真正的"按族选 target + 选不出来就拒绝"
135
+ * 走 chooseOpenocdTarget(data/family.ts)——这里只负责拼相对路径。
136
+ */
137
+ export function defaultConfigs(family) {
138
+ const target = family === undefined || family === null ? "stm32f1x" : (family.openocdTarget ?? "stm32f1x");
139
+ return { interfaceConfig: "interface/cmsis-dap.cfg", targetConfig: "target/" + target + ".cfg" };
132
140
  }
133
141
  export function findProjectConfig(projectDir, explicit) {
134
142
  if (explicit !== undefined && explicit !== "") {
@@ -194,23 +202,27 @@ export function parseFlashLog(text) {
194
202
  }
195
203
  return { programmed, verified, errors, noise };
196
204
  }
197
- export async function runFlash(subprocess, request) {
198
- const projectDir = resolve(request.projectDir);
199
- const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
205
+ /**
206
+ * 两条后端共用的前置:找 .elf、按 .ioc 过 SWD 守卫、把"烧的是哪一个"说清楚。
207
+ * 后端只决定"字节怎么进 Flash";这些判断与后端无关——CubeProgrammer 同样会把 SWD 烧失联。
208
+ */
209
+ function prepareFlashTarget(projectDir, request, startedAt) {
200
210
  const notes = [];
201
- const startedAt = Date.now();
202
211
  const elfResolved = resolveElf(projectDir, request);
203
212
  if (elfResolved.elf === null) {
204
213
  return {
205
- ok: false, projectDir, elfPath: "", elfSize: 0, command: "", args: [], configs: [],
206
- durationMs: Date.now() - startedAt, programmed: false, verified: false,
207
- probeError: null,
208
- errors: [elfResolved.note], message: "没烧:找不到要烧的 .elf", logTail: "", notes,
214
+ ok: false,
215
+ report: {
216
+ ok: false, projectDir, elfPath: "", elfSize: 0, command: "", args: [], configs: [],
217
+ durationMs: Date.now() - startedAt, programmed: false, verified: false,
218
+ probeError: null,
219
+ errors: [elfResolved.note], message: "没烧:找不到要烧的 .elf", logTail: "", notes,
220
+ },
209
221
  };
210
222
  }
211
223
  const elfPath = elfResolved.elf;
212
224
  const elfSize = statSync(elfPath).size;
213
- // F1:多个 .elf 并存时说清"烧的是哪一个、为什么是它"。
225
+ // 多个 .elf 并存时说清"烧的是哪一个、为什么是它"。
214
226
  const selectionNote = elfSelectionNote(dirname(elfPath));
215
227
  if (selectionNote !== "")
216
228
  notes.push(selectionNote);
@@ -219,15 +231,100 @@ export async function runFlash(subprocess, request) {
219
231
  if (!guard.blocked && guard.notes.length > 0)
220
232
  notes.push(...guard.notes);
221
233
  if (guard.blocked) {
234
+ return {
235
+ ok: false,
236
+ report: {
237
+ ok: false, projectDir, elfPath, elfSize, command: "", args: [], configs: [],
238
+ durationMs: Date.now() - startedAt, programmed: false, verified: false,
239
+ // 守卫拒绝不是探针故障:这次连烧录器都没起,probeError 保持 null。
240
+ probeError: null,
241
+ errors: guard.errors, message: "没烧:.ioc 里没启用 Serial Wire(SWD 会被关掉)",
242
+ logTail: "", notes: [...notes, ...guard.notes], guard: "no-debug",
243
+ },
244
+ };
245
+ }
246
+ return { ok: true, projectDir, elfPath, elfSize, notes };
247
+ }
248
+ /**
249
+ * CLI 的失败文本不在 openocd 的错误字符串里,classifyProbeFailure 认不出,
250
+ * 这里只归两类有把握的:没看见 ST-Link(换探针/插好)与连不上目标(供电/接线/芯片)。
251
+ */
252
+ function cliFailureProbeError(errors) {
253
+ const text = errors.join("\n");
254
+ if (/No ST-Link detected|no st-link/iu.test(text)) {
255
+ return makeProbeError("no-probe", { cause: "CubeProgrammer 没看到 ST-LINK", evidence: errors.slice(0, 3) });
256
+ }
257
+ if (/Unable to connect|Cannot connect/iu.test(text)) {
258
+ return makeProbeError("target-mismatch", { cause: "CubeProgrammer 连不上目标", evidence: errors.slice(0, 3) });
259
+ }
260
+ return null;
261
+ }
262
+ /** CubeProgrammer 后端(ST-LINK 官方 CLI):不经过 openocd,也不依赖 target/flash 脚本。 */
263
+ async function runCubeProgrammerBackend(subprocess, request, probeNote) {
264
+ const projectDir = resolve(request.projectDir);
265
+ const startedAt = Date.now();
266
+ const target = prepareFlashTarget(projectDir, request, startedAt);
267
+ if (!target.ok)
268
+ return target.report;
269
+ const { elfPath, elfSize } = target;
270
+ const notes = target.notes;
271
+ if (probeNote !== "")
272
+ notes.unshift(probeNote);
273
+ const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
274
+ const cli = findCubeProgrammerCli(config.cubePrgBins);
275
+ if (cli === null) {
276
+ const searched = describeSearchScope(config.cubePrgBins, "STM32_CUBE_PRG_ROOT");
222
277
  return {
223
278
  ok: false, projectDir, elfPath, elfSize, command: "", args: [], configs: [],
224
279
  durationMs: Date.now() - startedAt, programmed: false, verified: false,
225
- // 守卫拒绝不是探针故障:这次连 openocd 都没起,probeError 保持 null。
226
280
  probeError: null,
227
- errors: guard.errors, message: "没烧:.ioc 里没启用 Serial Wire(SWD 会被关掉)",
228
- logTail: "", notes: [...notes, ...guard.notes], guard: "no-debug",
281
+ errors: ["没找到 STM32_Programmer_CLI.exe(查过 " + searched + ")"],
282
+ message: "没烧:backend=cubeprogrammer 需要 STM32CubeProgrammer 的 CLI,本机没找到" +
283
+ "(下载安装:https://www.st.com/en/development-tools/stm32cubeprog.html;默认路径即可被自动发现,装在别处就设 STM32_CUBE_PRG_ROOT)",
284
+ logTail: "", notes,
229
285
  };
230
286
  }
287
+ const alt = await runCubeProgrammerFlash(subprocess, {
288
+ projectDir,
289
+ elfPath,
290
+ cliPath: cli,
291
+ ...(request.adapterSpeed === undefined ? {} : { adapterSpeed: request.adapterSpeed }),
292
+ runAfterReset: request.runAfterReset !== false,
293
+ timeoutMs: request.timeoutMs ?? 120_000,
294
+ ...(request.signal === undefined ? {} : { signal: request.signal }),
295
+ ...(request.onProgress === undefined ? {} : { onProgress: request.onProgress }),
296
+ });
297
+ if (alt.ok)
298
+ notes.push(cubeProgrammerBackendNote(cli));
299
+ return {
300
+ ok: alt.ok,
301
+ projectDir,
302
+ elfPath,
303
+ elfSize,
304
+ command: alt.command,
305
+ args: alt.args,
306
+ configs: [],
307
+ durationMs: Date.now() - startedAt,
308
+ ...(alt.exitCode === null ? {} : { exitCode: alt.exitCode }),
309
+ programmed: alt.programmed,
310
+ verified: alt.verified,
311
+ probeError: alt.ok ? null : cliFailureProbeError(alt.errors),
312
+ errors: alt.errors,
313
+ message: alt.message,
314
+ logTail: alt.logTail,
315
+ notes,
316
+ };
317
+ }
318
+ /** openocd 后端主体;ifaceOverride 只在工程没有自己的 openocd.cfg 时参与兜底 interface 选择。 */
319
+ async function runOpenocdFlash(subprocess, request, ifaceOverride) {
320
+ const projectDir = resolve(request.projectDir);
321
+ const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
322
+ const startedAt = Date.now();
323
+ const target = prepareFlashTarget(projectDir, request, startedAt);
324
+ if (!target.ok)
325
+ return target.report;
326
+ const { elfPath, elfSize } = target;
327
+ const notes = target.notes;
231
328
  const openOcd = request.openOcdPath !== undefined && request.openOcdPath !== ""
232
329
  ? request.openOcdPath
233
330
  : await resolveToolIn(subprocess, ["openocd"], config.binDirs);
@@ -249,13 +346,48 @@ export async function runFlash(subprocess, request) {
249
346
  configs.push(toOpenOcdPath(projectConfig));
250
347
  }
251
348
  else {
252
- const fallback = defaultConfigs();
253
- configs.push(request.interfaceConfig ?? fallback.interfaceConfig, request.targetConfig ?? fallback.targetConfig);
349
+ // 按 .ioc 的 Mcu.Family 选 target:F1 仍是 stm32f1x,换了族不会再拿 F1 的脚本硬连。
350
+ const family = readProjectFamily(projectDir);
351
+ const choice = chooseOpenocdTarget(family);
352
+ if (choice.error !== "") {
353
+ return {
354
+ ok: false, projectDir, elfPath, elfSize, command: "", args: [], configs: [],
355
+ durationMs: Date.now() - startedAt, programmed: false, verified: false,
356
+ probeError: null,
357
+ errors: [choice.error],
358
+ message: "没烧:" + (family === null ? "认不出芯片族" : "本机 openocd 没有 " + family.id + " 的 target 脚本"),
359
+ logTail: "",
360
+ notes,
361
+ };
362
+ }
363
+ configs.push(request.interfaceConfig ?? ifaceOverride ?? "interface/cmsis-dap.cfg", request.targetConfig ?? choice.targetConfig);
254
364
  notes.push("工程里没有 openocd 配置文件,回退到 " + configs.join(" + "));
365
+ if (family !== null)
366
+ notes.push("芯片族按 .ioc 判定:" + family.id + "(内核 " + family.core + ")");
367
+ if (choice.note !== "")
368
+ notes.push(choice.note);
255
369
  }
256
370
  const args = [openOcd];
257
- for (const cfg of configs)
371
+ for (let i = 0; i < configs.length; i += 1) {
372
+ const cfg = configs[i] ?? "";
258
373
  args.push("-f", cfg);
374
+ // J-Link 的 interface/jlink.cfg 只写 `adapter driver jlink`:不显式选 transport 时 openocd
375
+ // 默认 jtag,SWD 目标会报 "No J-Link device found"。工程自带 cfg 时按它 source 的 interface 判断。
376
+ if (projectConfig !== null && i === 0) {
377
+ let projectText = "";
378
+ try {
379
+ projectText = readFileSync(projectConfig, "utf8");
380
+ }
381
+ catch {
382
+ projectText = "";
383
+ }
384
+ if (/interface\/jlink\.cfg/u.test(projectText))
385
+ args.push("-c", "transport select swd");
386
+ }
387
+ else if (/jlink/iu.test(cfg)) {
388
+ args.push("-c", "transport select swd");
389
+ }
390
+ }
259
391
  if (request.adapterSpeed !== undefined)
260
392
  args.push("-c", "adapter speed " + String(request.adapterSpeed));
261
393
  args.push("-c", buildProgramCommand(elfPath, { reset: request.runAfterReset !== false }));
@@ -294,7 +426,13 @@ export async function runFlash(subprocess, request) {
294
426
  const probeError = ok
295
427
  ? null
296
428
  : classifyProbeFailure({
297
- lines: [...errors, ...verdict.noise],
429
+ // 分类要看**完整日志**,不能只看解析出的 Error 行:target 不匹配这类故障,
430
+ // openocd 只打一行 `Warn : Cannot identify target as a STM32 family.`(2026-10-31 实测,
431
+ // 只看 errors 时就落到 unknown)。errors 里还有我们自己合成的行(spawn 失败/超时),排在后面。
432
+ lines: [
433
+ ...log.split(/\r?\n/u).map((line) => line.trim()).filter((line) => line !== ""),
434
+ ...errors,
435
+ ],
298
436
  exitCode: outcome.exitCode,
299
437
  stage: "flash",
300
438
  timedOut: outcome.timedOut,
@@ -322,3 +460,134 @@ export async function runFlash(subprocess, request) {
322
460
  errors, message, logTail: log.split(/\r?\n/u).slice(-25).join("\n"), notes,
323
461
  };
324
462
  }
463
+ /**
464
+ * 烧录编排器(2026-11-08 复盘后的两层修复):
465
+ *
466
+ * 1. **interface 先探后选**:以前兜底硬编码 cmsis-dap(规格 §13 的旧前提),插 ST-Link 的
467
+ * 用户直接被带偏——openocd 报 "unable to find a matching CMSIS-DAP device",还得人肉猜。
468
+ * 现在先 usbDiagnosis(VID:PID 分流)按实见探针选 interface;工程自带 openocd.cfg 时不插手
469
+ * (那是用户显式配置,只在失败后提示改哪一行)。
470
+ * 2. **换 interface 自动重试一次**:仍报 probe-interface-mismatch 就切另一个驱动再跑。
471
+ * 3. **CubeProgrammer 兜底**:openocd 两条都失败、实见探针是 ST-LINK、本机装了 CubeProgrammer、
472
+ * 且失败不是「换后端也没用」类(swd-locked/no-power/flash-protected)时,走 CLI 第二后端。
473
+ * DAPLink 用户没有这条腿(CLI 只认 ST-LINK),报告里如实说。
474
+ */
475
+ export async function runFlash(subprocess, request) {
476
+ const backend = request.backend ?? "auto";
477
+ const hasProjectCfg = findProjectConfig(request.projectDir, request.configFile) !== null;
478
+ let ifaceOverride;
479
+ let probeNote = "";
480
+ let sawStLink = false;
481
+ let sawDapFamily = false;
482
+ let sawJlink = false;
483
+ // CLI 后端也要知道探针类型(它只认 ST-LINK),所以不能只按 interfaceConfig 是否显式给来决定探不探。
484
+ if (request.interfaceConfig === undefined || backend === "cubeprogrammer") {
485
+ const live = await liveProbeChoice(subprocess, request.signal);
486
+ sawStLink = live.swdProbes.some((p) => p.kind === "st-link");
487
+ sawDapFamily = live.swdProbes.some((p) => p.kind === "cmsis-dap" || p.kind === "daplink");
488
+ sawJlink = live.swdProbes.some((p) => p.kind === "jlink");
489
+ if (live.swdProbes.length > 0) {
490
+ if (request.interfaceConfig === undefined && !hasProjectCfg)
491
+ ifaceOverride = live.cfg;
492
+ probeNote = "探针实见:" + live.swdProbes.map((p) => p.label).join("、") +
493
+ (backend === "cubeprogrammer"
494
+ ? ""
495
+ : hasProjectCfg
496
+ ? "(工程自带 openocd.cfg,interface 以它为准)"
497
+ : ";interface 用 " + live.cfg + "(" + live.why + ")");
498
+ }
499
+ }
500
+ if (backend === "cubeprogrammer") {
501
+ // 实见的是 DAPLink/CMSIS-DAP:CLI 这条路根本不通,别让它跑一遍再报一句含糊的 "no ST-Link"。
502
+ if (sawDapFamily && !sawStLink) {
503
+ return {
504
+ ok: false, projectDir: resolve(request.projectDir), elfPath: "", elfSize: 0, command: "", args: [], configs: [],
505
+ durationMs: 0, programmed: false, verified: false, probeError: null,
506
+ errors: ["CubeProgrammer CLI 的 SWD 路径只认 ST-LINK,实见的是 CMSIS-DAP/DAPLink 类探针"],
507
+ message: "没烧:backend=cubeprogrammer 只认 ST-LINK;这块探针请用 backend=auto(openocd)",
508
+ logTail: "", notes: probeNote === "" ? [] : [probeNote],
509
+ };
510
+ }
511
+ return runCubeProgrammerBackend(subprocess, request, probeNote);
512
+ }
513
+ let report = await runOpenocdFlash(subprocess, request, ifaceOverride);
514
+ if (probeNote !== "")
515
+ report.notes.unshift(probeNote);
516
+ // 2) 接口不匹配 → 换另一个 interface 重试一次(仅插件兜底配置的工程;手写 cfg 的让用户按 hint 改)
517
+ if (!report.ok && !hasProjectCfg && report.probeError !== null && report.probeError.kind === "probe-interface-mismatch") {
518
+ // 换哪条由**实见探针**决定,不是"把当前值取反":取反在探针结论过期/缺失时会把重试引向
519
+ // 与真实硬件无关的方向(CI 上 2 条用例就是这么挂的)。实见 ST-LINK 就先试 stlink。
520
+ const firstIface = ifaceOverride ?? "interface/cmsis-dap.cfg";
521
+ const guided = sawStLink ? "interface/stlink-dap.cfg" : sawDapFamily ? "interface/cmsis-dap.cfg" : sawJlink ? "interface/jlink.cfg" : "";
522
+ const other = guided !== "" && guided !== firstIface
523
+ ? guided
524
+ : firstIface === "interface/cmsis-dap.cfg" ? "interface/stlink-dap.cfg" : "interface/cmsis-dap.cfg";
525
+ const firstMessage = report.message;
526
+ const retry = await runOpenocdFlash(subprocess, request, other);
527
+ // notes 归属:retry 是新报告——探针实见行、换接口说明、第一次失败原因都得挂到它身上
528
+ // (第一版把"自动换…"push 进了旧 report,随对象丢弃,测试当场抓到)。
529
+ if (probeNote !== "")
530
+ retry.notes.unshift(probeNote);
531
+ retry.notes.push("openocd 报探针驱动不匹配,自动换 " + other + " 重试一次(第 1 次:" + firstMessage + ")");
532
+ report = retry;
533
+ }
534
+ // 3) CubeProgrammer 第二后端
535
+ const futile = report.probeError !== null && ["swd-locked", "no-power", "flash-protected", "probe-busy"].includes(report.probeError.kind);
536
+ const fallbackAllowed = backend !== "openocd" && !futile && report.elfPath !== "" && sawStLink;
537
+ if (!report.ok && fallbackAllowed) {
538
+ const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
539
+ const cli = findCubeProgrammerCli(config.cubePrgBins);
540
+ if (cli !== null) {
541
+ report.notes.push(report.configs.length === 0
542
+ ? "openocd 根本没起来(没有可用的 target 脚本),直接试 CubeProgrammer CLI(ST-LINK 后端)…"
543
+ : "openocd 没烧成(" + String(report.configs.length) + " 个配置都试过),改试 CubeProgrammer CLI(ST-LINK 后端)…");
544
+ const alt = await runCubeProgrammerFlash(subprocess, {
545
+ projectDir: request.projectDir,
546
+ elfPath: report.elfPath,
547
+ cliPath: cli,
548
+ ...(request.adapterSpeed === undefined ? {} : { adapterSpeed: request.adapterSpeed }),
549
+ runAfterReset: request.runAfterReset !== false,
550
+ timeoutMs: request.timeoutMs ?? 120_000,
551
+ ...(request.signal === undefined ? {} : { signal: request.signal }),
552
+ ...(request.onProgress === undefined ? {} : { onProgress: request.onProgress }),
553
+ });
554
+ if (alt.ok) {
555
+ return {
556
+ ok: true,
557
+ projectDir: report.projectDir,
558
+ elfPath: report.elfPath,
559
+ elfSize: report.elfSize,
560
+ command: alt.command,
561
+ args: alt.args,
562
+ configs: [],
563
+ durationMs: report.durationMs + alt.durationMs,
564
+ ...(alt.exitCode === null ? {} : { exitCode: alt.exitCode }),
565
+ programmed: alt.programmed,
566
+ verified: alt.verified,
567
+ probeError: null,
568
+ errors: [],
569
+ message: alt.message,
570
+ logTail: alt.logTail,
571
+ notes: [...report.notes, cubeProgrammerBackendNote(cli)],
572
+ };
573
+ }
574
+ report.notes.push("CubeProgrammer 兜底也没成:" + alt.message + "(查过 " + alt.searched + ")");
575
+ }
576
+ else {
577
+ report.notes.push("实见 ST-LINK 但本机没装 STM32CubeProgrammer(CLI 没找到):装上它,ST-Link 就多一条不依赖 openocd target 脚本的后端" +
578
+ "(backend=cubeprogrammer 可直连;H5/WBA/WB0/WL3 只能走它)。下载:https://www.st.com/en/development-tools/stm32cubeprog.html");
579
+ }
580
+ }
581
+ if (!report.ok && sawJlink && !sawStLink) {
582
+ // J-Link 也没有 CLI 腿(CubeProgrammer 的 SWD 路径只认 ST-LINK)
583
+ report.notes.push("J-Link 没有 CubeProgrammer 兜底腿(CLI 只认 ST-LINK):openocd 的 jlink 驱动需要 `-c \"transport select swd\"`,插着 J-Link 时若仍失败,先确认 J-Link 驱动(SEGGER 的 USB Driver)与 openocd 版本。");
584
+ }
585
+ if (!report.ok && !futile && report.probeError !== null && report.probeError.kind === "no-probe" && sawDapFamily && !sawStLink) {
586
+ // DAPLink 用户走不了 CLI 这条腿,别让人以为还有救
587
+ const config = resolveSearchConfig(process.env, {});
588
+ if (findCubeProgrammerCli(config.cubePrgBins) !== null) {
589
+ report.notes.push("本机装了 CubeProgrammer,但它的 SWD 路径只认 ST-LINK:你插的是 CMSIS-DAP/DAPLink 类探针,这条路帮不上(docs §79)。");
590
+ }
591
+ }
592
+ return report;
593
+ }