dsh-stm32 0.1.0 → 0.1.2

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 (82) hide show
  1. package/README.md +127 -29
  2. package/README_EN.md +125 -29
  3. package/client/client.js +682 -0
  4. package/docs/development-notes.md +1385 -5
  5. package/docs/logic-audit.md +607 -0
  6. package/docs/reading-guide.md +738 -0
  7. package/lib/client.js +682 -0
  8. package/lib/commands/register.js +214 -12
  9. package/lib/data/chips.js +134 -4
  10. package/lib/data/clock-f1.js +158 -0
  11. package/lib/data/cmsis-sources.js +316 -0
  12. package/lib/data/f1-ram.generated.js +34 -0
  13. package/lib/data/firmware-packages.js +352 -0
  14. package/lib/data/install-guides.js +190 -0
  15. package/lib/data/search-roots.js +135 -15
  16. package/lib/data/stm32f1.js +186 -0
  17. package/lib/data/toolchain-config.js +310 -0
  18. package/lib/engine/build.js +121 -16
  19. package/lib/engine/cache.js +336 -0
  20. package/lib/engine/clock-plan.js +608 -0
  21. package/lib/engine/cmake-sources.js +428 -0
  22. package/lib/engine/code-patch.js +18 -6
  23. package/lib/engine/debug-session.js +163 -4
  24. package/lib/engine/debug.js +84 -19
  25. package/lib/engine/doctor.js +395 -4
  26. package/lib/engine/fetch.js +287 -0
  27. package/lib/engine/firmware-install.js +668 -0
  28. package/lib/engine/flash.js +145 -15
  29. package/lib/engine/fs-longpath.js +107 -0
  30. package/lib/engine/generate-cache.js +118 -0
  31. package/lib/engine/generate-script.js +30 -8
  32. package/lib/engine/generate.js +296 -14
  33. package/lib/engine/hal-chip.js +230 -0
  34. package/lib/engine/ioc-plan.js +47 -8
  35. package/lib/engine/msys2-install.js +1870 -0
  36. package/lib/engine/probe.js +57 -6
  37. package/lib/engine/project-export.js +129 -0
  38. package/lib/engine/register-clock.js +132 -0
  39. package/lib/engine/register-linker.js +129 -0
  40. package/lib/engine/register-template.js +449 -0
  41. package/lib/engine/regs.js +18 -4
  42. package/lib/engine/route-plan.js +553 -0
  43. package/lib/engine/scaffold.js +303 -72
  44. package/lib/engine/subprocess-runner.js +2 -0
  45. package/lib/engine/tar.js +250 -0
  46. package/lib/engine/text-file.js +150 -0
  47. package/lib/engine/thirdparty.js +243 -0
  48. package/lib/engine/tool-env.js +51 -0
  49. package/lib/engine/toolchain-gate.js +386 -0
  50. package/lib/engine/vscode-config.js +99 -0
  51. package/lib/engine/web-api.js +375 -0
  52. package/lib/engine/web-routes.js +100 -0
  53. package/lib/engine/xcube-catalog.js +102 -0
  54. package/lib/engine/xcube.js +254 -0
  55. package/lib/engine/zip.js +142 -0
  56. package/lib/index.js +44 -2
  57. package/lib/skill/stm32-workflow.js +3 -3
  58. package/lib/tools/build.js +55 -3
  59. package/lib/tools/cache.js +250 -0
  60. package/lib/tools/code-patch.js +29 -2
  61. package/lib/tools/debug-session.js +56 -14
  62. package/lib/tools/debug.js +43 -2
  63. package/lib/tools/doctor.js +16 -3
  64. package/lib/tools/env-install.js +472 -0
  65. package/lib/tools/env-probe.js +6 -2
  66. package/lib/tools/firmware-install.js +255 -0
  67. package/lib/tools/flash.js +41 -2
  68. package/lib/tools/generate.js +96 -10
  69. package/lib/tools/ioc-apply.js +27 -11
  70. package/lib/tools/ioc-inspect.js +4 -2
  71. package/lib/tools/ioc-plan.js +209 -11
  72. package/lib/tools/project-create.js +229 -21
  73. package/lib/tools/project-dir.js +95 -0
  74. package/lib/tools/project-export.js +100 -0
  75. package/lib/tools/regs.js +82 -7
  76. package/lib/tools/route-plan.js +467 -0
  77. package/package.json +11 -4
  78. package/templates/register/CMakeLists.txt +52 -0
  79. package/templates/register/Core/Src/main.c +72 -0
  80. package/templates/scaffold/openocd.cfg +8 -0
  81. package/templates/scaffold/vscode/launch.json +8 -6
  82. package/templates/scaffold/vscode/settings.json +5 -11
@@ -15,6 +15,7 @@ import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameS
15
15
  import { basename, isAbsolute, join, resolve, sep } from "node:path";
16
16
  import { findIocFile, STATE_DIR } from "./generate.js";
17
17
  import { parseIocEntries, summarizeIoc } from "./ioc.js";
18
+ import { emptyClockPlan, formatHz, planClock } from "./clock-plan.js";
18
19
  import { isKnownPin } from "../data/chips.js";
19
20
  export function sha256Hex(text) {
20
21
  return createHash("sha256").update(text, "utf8").digest("hex");
@@ -120,6 +121,16 @@ function readPinTable(entries, errors, warnings) {
120
121
  }
121
122
  return { order, entries, errors, warnings };
122
123
  }
124
+ /** 两串引脚名是否完全一致(顺序也要一致):没变就别重写 Mcu.PinN,保持原字节。 */
125
+ function samePinOrder(left, right) {
126
+ if (left.length !== right.length)
127
+ return false;
128
+ for (let index = 0; index < left.length; index += 1) {
129
+ if (left[index] !== right[index])
130
+ return false;
131
+ }
132
+ return true;
133
+ }
123
134
  /** 把引脚列表整体重编号写回 entries(新增/删除都可能让下标变化,重写最稳)。 */
124
135
  function writePinOrder(entries, order) {
125
136
  const previous = Number.parseInt(entries.get("Mcu.PinsNb") ?? "0", 10);
@@ -225,6 +236,7 @@ export function planIocChanges(request) {
225
236
  const empty = {
226
237
  ok: false, projectDir, iocPath: iocPath ?? "", planId: "", sourceSha256: "", candidateSha256: "",
227
238
  changes: [], warnings: [], notes: [], errors: [], message: "",
239
+ clockPlan: emptyClockPlan("还没读到 .ioc,时钟没算"),
228
240
  };
229
241
  if (iocPath === null || !existsSync(iocPath)) {
230
242
  return { ...empty, errors: ["找不到 .ioc(工程根目录下必须有且只有一个 *.ioc,或用 iocPath 指定)"], message: "没生成计划:找不到 .ioc" };
@@ -239,16 +251,19 @@ export function planIocChanges(request) {
239
251
  const after = new Map(before);
240
252
  const chipCpn = before.get("Mcu.CPN") ?? "";
241
253
  const pinTable = readPinTable(after, errors, warnings);
254
+ // 先记下原始引脚表:只有真的变了才重写 Mcu.PinN,顺序没变就一个字节都不碰。
255
+ const originalOrder = [...pinTable.order];
242
256
  for (const pin of request.pins ?? [])
243
257
  addPin(pinTable, pin, { cpn: chipCpn });
244
- // 引脚表变化过才重写 Mcu.PinN(否则一个键都不碰)。
245
- const pinsTouched = (request.pins ?? []).length > 0 || request.serialWire === true;
246
258
  if (request.serialWire === true)
247
259
  ensureSerialWire(pinTable);
248
- if (pinsTouched) {
249
- const remaining = pinTable.order.filter((pin) => after.get(pin + ".Signal") !== undefined || pin.startsWith("VP_"));
250
- writePinOrder(after, remaining);
251
- }
260
+ // 引脚表变化过才重写 Mcu.PinN。
261
+ // 为什么不再按"有没有 .Signal"过滤:F1 的 PB2 在 CubeMX 里只有 PB2.Mode=B2-BOOT1(BOOT1 是复用功能,
262
+ // 不带 .Signal)。按 .Signal 过滤会把它从 Mcu.PinN 里静默剔除——PinsNb 缩水,PB2.Mode / PB2.GPIO_*
263
+ // 成了没有引脚指向的孤儿键,CubeMX 下次打开就丢这段配置。所以:已在表里的引脚一个都不删,只追加。
264
+ // 虚拟引脚(VP_*)的清理归 ensureSerialWire 管,这里不掺和。
265
+ if (!samePinOrder(originalOrder, pinTable.order))
266
+ writePinOrder(after, pinTable.order);
252
267
  for (const item of request.entries ?? []) {
253
268
  const key = item.key.trim();
254
269
  if (key === "" || key.includes("=")) {
@@ -269,6 +284,29 @@ export function planIocChanges(request) {
269
284
  }
270
285
  after.delete(trimmed);
271
286
  }
287
+ // 时钟:.ioc 里的一段键,和 entries 一起算(不单开工具)。复用 after,所以同一调用里的
288
+ // entries(例如 RCC.MCOSource)对时钟规划是可见的;规划出的键也是"候选文件"的一部分。
289
+ const clockPlan = request.clock === undefined
290
+ ? planClock(undefined, { entries: after })
291
+ : planClock(request.clock, { entries: after });
292
+ if (request.clock === undefined) {
293
+ const achieved = clockPlan.achieved;
294
+ notes.push("时钟(只读,未请求改动):SYSCLK " + formatHz(achieved.sysclkHz) +
295
+ " / APB1 " + formatHz(achieved.apb1Hz) +
296
+ " / APB2 " + formatHz(achieved.apb2Hz) +
297
+ " / ADC " + formatHz(achieved.adcHz));
298
+ }
299
+ else {
300
+ warnings.push(...clockPlan.warnings);
301
+ if (clockPlan.note !== "")
302
+ notes.push("时钟:" + clockPlan.note);
303
+ for (const item of clockPlan.entries)
304
+ after.set(item.key, item.value);
305
+ for (const key of clockPlan.unset)
306
+ after.delete(key);
307
+ for (const error of clockPlan.errors)
308
+ errors.push("时钟:" + error);
309
+ }
272
310
  // 安全闸:这两条是踩过坑的硬约束,不满足就不给 plan。
273
311
  const candidateText = renderIocText(source, compareEntries(before, after));
274
312
  const summary = summarizeIoc(candidateText, iocPath);
@@ -279,11 +317,11 @@ export function planIocChanges(request) {
279
317
  errors.push("改完之后 ProjectManager.KeepUserCode=false:重新生成会丢掉 USER CODE 区,拒绝。");
280
318
  }
281
319
  if (errors.length > 0) {
282
- return { ...empty, iocPath, sourceSha256, errors, warnings, notes, message: "没生成计划:" + errors[0] };
320
+ return { ...empty, iocPath, sourceSha256, errors, warnings, notes, clockPlan, message: "没生成计划:" + errors[0] };
283
321
  }
284
322
  const changes = compareEntries(before, after);
285
323
  if (changes.length === 0) {
286
- return { ...empty, iocPath, sourceSha256, warnings, notes, message: "没有变化:这些键本来就是这个值" };
324
+ return { ...empty, iocPath, sourceSha256, warnings, notes, clockPlan, message: "没有变化:这些键本来就是这个值" };
287
325
  }
288
326
  const candidateSha256 = sha256Hex(candidateText);
289
327
  const planId = sha256Hex(sourceSha256 + "\n" + candidateSha256).slice(0, 16);
@@ -310,6 +348,7 @@ export function planIocChanges(request) {
310
348
  errors: [],
311
349
  message: "已生成计划 " + planId + ":" + String(changes.length) + " 个键变化(新增 " + String(added) + "、删除 " + String(removedCount) + "、修改 " + String(changes.length - added - removedCount) + ")",
312
350
  summaryAfter: summary,
351
+ clockPlan,
313
352
  };
314
353
  }
315
354
  export function loadPlan(projectDir, planId) {