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.
- package/README.md +127 -29
- package/README_EN.md +125 -29
- package/client/client.js +682 -0
- package/docs/development-notes.md +1385 -5
- package/docs/logic-audit.md +607 -0
- package/docs/reading-guide.md +738 -0
- package/lib/client.js +682 -0
- package/lib/commands/register.js +214 -12
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +316 -0
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/firmware-packages.js +352 -0
- package/lib/data/install-guides.js +190 -0
- package/lib/data/search-roots.js +135 -15
- package/lib/data/stm32f1.js +186 -0
- package/lib/data/toolchain-config.js +310 -0
- package/lib/engine/build.js +121 -16
- package/lib/engine/cache.js +336 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/code-patch.js +18 -6
- package/lib/engine/debug-session.js +163 -4
- package/lib/engine/debug.js +84 -19
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +287 -0
- package/lib/engine/firmware-install.js +668 -0
- package/lib/engine/flash.js +145 -15
- package/lib/engine/fs-longpath.js +107 -0
- package/lib/engine/generate-cache.js +118 -0
- package/lib/engine/generate-script.js +30 -8
- package/lib/engine/generate.js +296 -14
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +47 -8
- package/lib/engine/msys2-install.js +1870 -0
- package/lib/engine/probe.js +57 -6
- package/lib/engine/project-export.js +129 -0
- package/lib/engine/register-clock.js +132 -0
- package/lib/engine/register-linker.js +129 -0
- package/lib/engine/register-template.js +449 -0
- package/lib/engine/regs.js +18 -4
- package/lib/engine/route-plan.js +553 -0
- package/lib/engine/scaffold.js +303 -72
- package/lib/engine/subprocess-runner.js +2 -0
- package/lib/engine/tar.js +250 -0
- package/lib/engine/text-file.js +150 -0
- package/lib/engine/thirdparty.js +243 -0
- package/lib/engine/tool-env.js +51 -0
- package/lib/engine/toolchain-gate.js +386 -0
- package/lib/engine/vscode-config.js +99 -0
- package/lib/engine/web-api.js +375 -0
- package/lib/engine/web-routes.js +100 -0
- package/lib/engine/xcube-catalog.js +102 -0
- package/lib/engine/xcube.js +254 -0
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +44 -2
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +55 -3
- package/lib/tools/cache.js +250 -0
- package/lib/tools/code-patch.js +29 -2
- package/lib/tools/debug-session.js +56 -14
- package/lib/tools/debug.js +43 -2
- package/lib/tools/doctor.js +16 -3
- package/lib/tools/env-install.js +472 -0
- package/lib/tools/env-probe.js +6 -2
- package/lib/tools/firmware-install.js +255 -0
- package/lib/tools/flash.js +41 -2
- package/lib/tools/generate.js +96 -10
- package/lib/tools/ioc-apply.js +27 -11
- package/lib/tools/ioc-inspect.js +4 -2
- package/lib/tools/ioc-plan.js +209 -11
- package/lib/tools/project-create.js +229 -21
- package/lib/tools/project-dir.js +95 -0
- package/lib/tools/project-export.js +100 -0
- package/lib/tools/regs.js +82 -7
- package/lib/tools/route-plan.js +467 -0
- package/package.json +11 -4
- package/templates/register/CMakeLists.txt +52 -0
- package/templates/register/Core/Src/main.c +72 -0
- package/templates/scaffold/openocd.cfg +8 -0
- package/templates/scaffold/vscode/launch.json +8 -6
- package/templates/scaffold/vscode/settings.json +5 -11
package/lib/commands/register.js
CHANGED
|
@@ -5,33 +5,91 @@
|
|
|
5
5
|
* 而人想按一下就烧,最省事的路径是 /stm32-flash —— 宿主直接执行 handler,不进模型、
|
|
6
6
|
* 不消耗对话,结果像一条系统消息一样贴回对话里。
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* 六条命令:
|
|
9
9
|
* /stm32-flash [<工程目录>] 只烧录(program + verify + reset + exit),不进调试会话
|
|
10
10
|
* /stm32-debug [<工程目录>] 烧录后起调试会话并停在 main,可继续读寄存器
|
|
11
11
|
* /stm32-stop 提前关掉调试会话,把探针还给 VS Code
|
|
12
12
|
* /stm32-build [<工程目录>] [--clean] 编译(默认走 CMake+Ninja 的 build/Debug)
|
|
13
13
|
* /stm32-doctor [<工程目录>] 体检:工具链 / 残留进程 / 端口 / .elf / .ioc 告警
|
|
14
|
+
* /stm32-env [<工程目录>] 工具链体检卡:编译/烧录/调试/生成各缺什么、装在哪、怎么装
|
|
15
|
+
* /stm32-setup [<工程目录>] 一条龙:体检 → 出计划 → 真装(要确认)→ 自检,每步给下一句
|
|
14
16
|
*
|
|
15
17
|
* 工程目录缺省取会话当前工作目录;也可以直接写路径(相对路径按当前目录解析)。
|
|
18
|
+
* 带空格的路径要整段用引号包起来(`"C:/Program Files/.../demo proj"`):切词是引号感知的,
|
|
19
|
+
* 引号内的空白不当分隔符;引号没闭合会明确报「引号没闭合」,不会把半截路径当工程目录。
|
|
16
20
|
*/
|
|
17
21
|
import { resolve } from "node:path";
|
|
18
22
|
import { runBuild } from "../engine/build.js";
|
|
19
23
|
import { debugSessions, releaseProbeFor } from "../engine/debug-session.js";
|
|
20
24
|
import { runDoctor } from "../engine/doctor.js";
|
|
21
25
|
import { runFlash } from "../engine/flash.js";
|
|
26
|
+
import { ensureToolchain } from "../engine/toolchain-gate.js";
|
|
22
27
|
import { formatFlashReport } from "../tools/flash.js";
|
|
28
|
+
/**
|
|
29
|
+
* 引号感知切词(审计 B7)。
|
|
30
|
+
*
|
|
31
|
+
* 为什么不能"先按空白 split 再剥引号":`"C:/Program Files/ST/demo proj"` 在第一步就被切成两段,
|
|
32
|
+
* 之后再怎么剥都拼不回原路径 —— projectDir 只剩半截,用户看到的却是「找不到 CMakeLists.txt」,
|
|
33
|
+
* 只会以为工程坏了。所以改成一次扫描:引号内不切词、顺手把引号吃掉。
|
|
34
|
+
* 引号没闭合直接抛错:静默按空白切等于把"用户写错了"伪装成"工程坏了"。
|
|
35
|
+
*
|
|
36
|
+
* 引号内不做反斜杠转义:Windows 路径里 `\` 到处都是(C:\Users\...),当转义符只会把路径吃掉;
|
|
37
|
+
* 需要字面引号时用另一种引号把整段包起来。
|
|
38
|
+
*/
|
|
39
|
+
export function tokenizeCommandLine(rawInput) {
|
|
40
|
+
const tokens = [];
|
|
41
|
+
let text = "";
|
|
42
|
+
// pending 与 text 分开记:`""` 也是个"已经开始的 token",而连续空白不该产出空 token。
|
|
43
|
+
let pending = false;
|
|
44
|
+
let quoted = false;
|
|
45
|
+
let quote = null;
|
|
46
|
+
for (const char of rawInput) {
|
|
47
|
+
if (quote !== null) {
|
|
48
|
+
if (char === quote) {
|
|
49
|
+
quote = null;
|
|
50
|
+
quoted = true;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
text += char;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (char === '"' || char === "'") {
|
|
57
|
+
quote = char;
|
|
58
|
+
pending = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (/\s/u.test(char)) {
|
|
62
|
+
if (pending) {
|
|
63
|
+
tokens.push({ text, quoted });
|
|
64
|
+
text = "";
|
|
65
|
+
pending = false;
|
|
66
|
+
quoted = false;
|
|
67
|
+
}
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
text += char;
|
|
71
|
+
pending = true;
|
|
72
|
+
}
|
|
73
|
+
if (quote !== null) {
|
|
74
|
+
throw new Error("引号没闭合:少了收尾的 " + quote + " —— " + rawInput.trim() + "(带空格的路径请整段用引号包起来)");
|
|
75
|
+
}
|
|
76
|
+
if (pending)
|
|
77
|
+
tokens.push({ text, quoted });
|
|
78
|
+
return tokens;
|
|
79
|
+
}
|
|
23
80
|
/** 把 `[<目录>] [--flag]` 解析成工程目录 + 开关;目录缺省用会话工作目录。 */
|
|
24
81
|
async function parseInvocation(ctx, invocation) {
|
|
25
82
|
const flags = new Set();
|
|
26
83
|
const positional = [];
|
|
27
|
-
for (const token of (invocation.rawInput ?? "")
|
|
28
|
-
if (token === "")
|
|
84
|
+
for (const token of tokenizeCommandLine(invocation.rawInput ?? "")) {
|
|
85
|
+
if (token.text === "")
|
|
29
86
|
continue;
|
|
30
|
-
|
|
31
|
-
|
|
87
|
+
// 引号里的 "--clean" 是目录名的一部分(真有人建这种目录),不能当开关吃掉。
|
|
88
|
+
if (!token.quoted && token.text.startsWith("--")) {
|
|
89
|
+
flags.add(token.text.slice(2).toLowerCase());
|
|
32
90
|
continue;
|
|
33
91
|
}
|
|
34
|
-
positional.push(token.
|
|
92
|
+
positional.push(token.text);
|
|
35
93
|
}
|
|
36
94
|
const workingDirectory = ctx.get("workingDirectory");
|
|
37
95
|
const cwd = workingDirectory === undefined ? process.cwd() : await workingDirectory.ensure(invocation.agent, invocation.signal);
|
|
@@ -100,7 +158,71 @@ function flashText(report, released) {
|
|
|
100
158
|
function err(text) {
|
|
101
159
|
return { kind: "error", text };
|
|
102
160
|
}
|
|
103
|
-
/**
|
|
161
|
+
/**
|
|
162
|
+
* 命令层也先过一遍门禁(审计 B8)。
|
|
163
|
+
*
|
|
164
|
+
* 为什么命令要单独过:/stm32-build、/stm32-flash、/stm32-debug 不经过工具层,缺 gcc 或 openocd 时
|
|
165
|
+
* 用户只得到一屏 cmake / spawn 原始报错,与工具入口那句「缺 X,装法是 Y」差得太远;
|
|
166
|
+
* 而 /stm32-env 摆在那儿,却没人会先想到它。
|
|
167
|
+
*
|
|
168
|
+
* 为什么固定 envCheck=warn:命令是"人已经决定要干"的动作,门禁没资格替人拦掉,
|
|
169
|
+
* 但必须把「缺什么、怎么装」放在输出第一行。门禁自带 60 s 探测缓存,连着敲也不贵。
|
|
170
|
+
*/
|
|
171
|
+
async function commandGateWarning(ctx, capability, signal) {
|
|
172
|
+
const gate = await ensureToolchain(ctx, {
|
|
173
|
+
capability,
|
|
174
|
+
envCheck: "warn",
|
|
175
|
+
...(signal === undefined ? {} : { signal }),
|
|
176
|
+
});
|
|
177
|
+
return gate.ok ? null : gate.message;
|
|
178
|
+
}
|
|
179
|
+
/** 门禁警告放第一行;kind 原样保留 —— 命令已经跑完了,改成 error 会让调用方误判"什么都没干"。 */
|
|
180
|
+
function withGateWarning(warning, result) {
|
|
181
|
+
if (warning === null)
|
|
182
|
+
return result;
|
|
183
|
+
const text = result.text ?? "";
|
|
184
|
+
return { kind: result.kind, text: text === "" ? warning : warning + "\n\n" + text };
|
|
185
|
+
}
|
|
186
|
+
/** 体检卡上的四项能力,顺序就是用户干的顺序:先能编译,再能烧、能调,最后才生成。 */
|
|
187
|
+
const ENV_CAPABILITIES = ["build", "flash", "debug", "generate"];
|
|
188
|
+
const ENV_CAPABILITY_LABEL = {
|
|
189
|
+
build: "编译",
|
|
190
|
+
flash: "烧录",
|
|
191
|
+
debug: "调试",
|
|
192
|
+
generate: "生成代码",
|
|
193
|
+
};
|
|
194
|
+
/** 组件 id → 卡片上写的名字(齐全时点名用;缺的时候用门禁给的 label,那里写得更细)。 */
|
|
195
|
+
const ENV_COMPONENT_LABEL = {
|
|
196
|
+
gcc: "gcc",
|
|
197
|
+
cmake: "CMake",
|
|
198
|
+
ninja: "Ninja",
|
|
199
|
+
openocd: "OpenOCD",
|
|
200
|
+
gdb: "GDB",
|
|
201
|
+
cubemx: "CubeMX",
|
|
202
|
+
"cubemx-java": "CubeMX 内置 JRE",
|
|
203
|
+
};
|
|
204
|
+
/** 四行体检结论:行的顺序固定,缺件时把第一条安装动作贴在后面(可直接粘贴)。 */
|
|
205
|
+
function envText(rows, configPath) {
|
|
206
|
+
const lines = ["工具链体检(配置:" + configPath + ")"];
|
|
207
|
+
let anyMissing = false;
|
|
208
|
+
for (const row of rows) {
|
|
209
|
+
const label = ENV_CAPABILITY_LABEL[row.capability];
|
|
210
|
+
if (row.gate.ok) {
|
|
211
|
+
const names = row.gate.checked.map((id) => ENV_COMPONENT_LABEL[id] ?? id).join("、");
|
|
212
|
+
// 单项时"OpenOCD 都在"读着别扭,按项数选量词。
|
|
213
|
+
lines.push(label + " ✔ " + names + (row.gate.checked.length > 1 ? " 都在" : " 在"));
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
anyMissing = true;
|
|
217
|
+
const missing = row.gate.missing.map((item) => item.label).join("、");
|
|
218
|
+
const install = row.gate.missing[0]?.install[0];
|
|
219
|
+
lines.push(label + " ✘ 缺 " + missing + (install === undefined ? "" : " —— " + install));
|
|
220
|
+
}
|
|
221
|
+
// 缺件也是体检结果,不是命令错误;所以两种结尾都是 success。
|
|
222
|
+
lines.push(anyMissing ? "要装:回「装」,我跑 stm32_env_install 出计划(先不下载)" : "都齐了:/stm32-build 就能编译");
|
|
223
|
+
return { kind: "success", text: lines.join("\n") };
|
|
224
|
+
}
|
|
225
|
+
/** 注册六条斜杠命令;宿主没组合 commands 服务时直接返回(工具不受影响)。 */
|
|
104
226
|
export function registerCommands(ctx) {
|
|
105
227
|
const commands = ctx.commands;
|
|
106
228
|
if (commands === undefined)
|
|
@@ -115,10 +237,11 @@ export function registerCommands(ctx) {
|
|
|
115
237
|
const subprocess = needSubprocess(ctx);
|
|
116
238
|
if (typeof subprocess === "string")
|
|
117
239
|
return err(subprocess);
|
|
240
|
+
const warning = await commandGateWarning(ctx, "flash", invocation.signal);
|
|
118
241
|
const { projectDir } = await parseInvocation(ctx, invocation);
|
|
119
242
|
const released = await releaseProbeFor("stm32-flash");
|
|
120
243
|
const report = await runFlash(subprocess, { projectDir, signal: invocation.signal });
|
|
121
|
-
return flashText(report, released);
|
|
244
|
+
return withGateWarning(warning, flashText(report, released));
|
|
122
245
|
}
|
|
123
246
|
catch (error) {
|
|
124
247
|
return err("烧录命令异常:" + (error instanceof Error ? error.message : String(error)));
|
|
@@ -135,17 +258,18 @@ export function registerCommands(ctx) {
|
|
|
135
258
|
const subprocess = needSubprocess(ctx);
|
|
136
259
|
if (typeof subprocess === "string")
|
|
137
260
|
return err(subprocess);
|
|
261
|
+
const warning = await commandGateWarning(ctx, "debug", invocation.signal);
|
|
138
262
|
const { projectDir } = await parseInvocation(ctx, invocation);
|
|
139
263
|
await releaseProbeFor("stm32-debug");
|
|
140
264
|
const flash = await runFlash(subprocess, { projectDir, signal: invocation.signal });
|
|
141
265
|
if (!flash.ok)
|
|
142
|
-
return flashText(flash, null);
|
|
266
|
+
return withGateWarning(warning, flashText(flash, null));
|
|
143
267
|
// 会话管理器由工具层 configure;命令层也自己 configure 一次,
|
|
144
268
|
// 这样"只注册命令不注册工具"的场合(测试、精简组合)同样能起会话。
|
|
145
269
|
debugSessions.configure(subprocess, []);
|
|
146
270
|
const session = await debugSessions.start({ projectDir, reset: "halt", idleTtlMs: 180_000, hardTtlMs: 10 * 60_000 });
|
|
147
271
|
if (!session.ok) {
|
|
148
|
-
return err(formatFlashReport(flash).join("\n") + "\n\n调试会话起不来:" + session.message);
|
|
272
|
+
return withGateWarning(warning, err(formatFlashReport(flash).join("\n") + "\n\n调试会话起不来:" + session.message));
|
|
149
273
|
}
|
|
150
274
|
const stopped = await debugSessions.eval({ commands: ["break main", "continue", "bt"], keepHalted: true, timeoutMs: 30_000 });
|
|
151
275
|
const location = /^#0\s+(.+)$/mu.exec(stopped.output)?.[1] ?? (stopped.ok ? "已停在 main" : "没能停在 main:" + stopped.message);
|
|
@@ -155,7 +279,7 @@ export function registerCommands(ctx) {
|
|
|
155
279
|
lines.push("端口:" + String(session.port) + ",探针由调试会话占着(空闲 " + String(Math.round(session.idleTtlMs / 1000)) + " s 自动关)");
|
|
156
280
|
lines.push("· 读变量/寄存器:stm32_debug_eval(每次调用续租)");
|
|
157
281
|
lines.push("· 想把探针交还给 VS Code:/stm32-stop,或者等我这一轮说完(turn/end 会自动关)");
|
|
158
|
-
return { kind: "success", text: lines.join("\n") };
|
|
282
|
+
return withGateWarning(warning, { kind: "success", text: lines.join("\n") });
|
|
159
283
|
}
|
|
160
284
|
catch (error) {
|
|
161
285
|
return err("调试命令异常:" + (error instanceof Error ? error.message : String(error)));
|
|
@@ -186,10 +310,11 @@ export function registerCommands(ctx) {
|
|
|
186
310
|
const subprocess = needSubprocess(ctx);
|
|
187
311
|
if (typeof subprocess === "string")
|
|
188
312
|
return err(subprocess);
|
|
313
|
+
const warning = await commandGateWarning(ctx, "build", invocation.signal);
|
|
189
314
|
const { projectDir, flags } = await parseInvocation(ctx, invocation);
|
|
190
315
|
await releaseProbeFor("stm32-build");
|
|
191
316
|
const report = await runBuild(subprocess, { projectDir, clean: flags.has("clean"), signal: invocation.signal });
|
|
192
|
-
return buildText(report);
|
|
317
|
+
return withGateWarning(warning, buildText(report));
|
|
193
318
|
}
|
|
194
319
|
catch (error) {
|
|
195
320
|
return err("编译命令异常:" + (error instanceof Error ? error.message : String(error)));
|
|
@@ -215,4 +340,81 @@ export function registerCommands(ctx) {
|
|
|
215
340
|
}
|
|
216
341
|
},
|
|
217
342
|
});
|
|
343
|
+
commands.register({
|
|
344
|
+
definitionId: "dsh-stm32/stm32-setup",
|
|
345
|
+
name: "stm32-setup",
|
|
346
|
+
description: "一条龙:体检 → 出计划 → 真装(要确认)→ 自检;缺什么补什么,齐了就直接说齐了",
|
|
347
|
+
input: { hint: "[<工程目录>] [装]" },
|
|
348
|
+
handler: async (invocation) => {
|
|
349
|
+
try {
|
|
350
|
+
const subprocess = needSubprocess(ctx);
|
|
351
|
+
if (typeof subprocess === "string")
|
|
352
|
+
return err(subprocess);
|
|
353
|
+
const wantsInstall = /(^|\s)装(\s|$)/u.test(invocation.rawInput.trim());
|
|
354
|
+
const lines = ["stm32-setup:"];
|
|
355
|
+
// 1) 体检(与 /stm32-env 同一套门禁)
|
|
356
|
+
const rows = [];
|
|
357
|
+
for (const capability of ENV_CAPABILITIES) {
|
|
358
|
+
const gate = await ensureToolchain(ctx, { capability, envCheck: "warn", signal: invocation.signal });
|
|
359
|
+
rows.push({ capability, gate });
|
|
360
|
+
}
|
|
361
|
+
const allOk = rows.every((row) => row.gate.ok);
|
|
362
|
+
if (allOk) {
|
|
363
|
+
lines.push("1/3 体检:四项能力全齐,不用装。");
|
|
364
|
+
lines.push("2/3 下一步:/stm32-build 就能编译(板子接好时 /stm32-doctor 再看一眼硬件)。");
|
|
365
|
+
return { kind: "success", text: lines.join("\n") };
|
|
366
|
+
}
|
|
367
|
+
// 2) 缺件 → 出计划(与 stm32_env_install 同一个引擎,零副作用)
|
|
368
|
+
const { planOrInstallMsys2 } = await import("../engine/msys2-install.js");
|
|
369
|
+
const plan = await planOrInstallMsys2(subprocess, { mode: "plan", writePath: true, ...(invocation.signal === undefined ? {} : { signal: invocation.signal }) });
|
|
370
|
+
lines.push("2/3 计划:" + plan.message);
|
|
371
|
+
if (!wantsInstall) {
|
|
372
|
+
lines.push("3/3 确认:回「/stm32-setup 装」开始真装(只补缺的包,不升级已装的);装完会自动自检。");
|
|
373
|
+
return { kind: "success", text: lines.join("\n") };
|
|
374
|
+
}
|
|
375
|
+
// 3) 真装(第二次带「装」才走到这里)
|
|
376
|
+
const install = await planOrInstallMsys2(subprocess, { mode: "install", writePath: true, ...(invocation.signal === undefined ? {} : { signal: invocation.signal }) });
|
|
377
|
+
if (!install.ok) {
|
|
378
|
+
lines.push("3/3 安装未完成:" + install.message);
|
|
379
|
+
lines.push(" 下一步:" + install.nextStep);
|
|
380
|
+
return { kind: "success", text: lines.join("\n") };
|
|
381
|
+
}
|
|
382
|
+
lines.push("3/3 安装完成:" + install.message);
|
|
383
|
+
lines.push(" 下一步:/stm32-build 编译;/stm32-doctor 做全量体检。");
|
|
384
|
+
return { kind: "success", text: lines.join("\n") };
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
return err("stm32-setup 异常:" + (error instanceof Error ? error.message : String(error)));
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
commands.register({
|
|
392
|
+
definitionId: "dsh-stm32/stm32-env",
|
|
393
|
+
name: "stm32-env",
|
|
394
|
+
description: "工具链体检:编译/烧录/调试/生成各缺什么、装在哪、怎么装",
|
|
395
|
+
input: { hint: "[<工程目录>]" },
|
|
396
|
+
handler: async (invocation) => {
|
|
397
|
+
try {
|
|
398
|
+
const subprocess = needSubprocess(ctx);
|
|
399
|
+
if (typeof subprocess === "string")
|
|
400
|
+
return err(subprocess);
|
|
401
|
+
// 体检只问"这台机器能不能干这四件事",不看工程目录;hint 里仍留着 [<工程目录>],
|
|
402
|
+
// 是因为用户习惯性地带路径,不想让人以为参数被吞了。
|
|
403
|
+
// envCheck=warn:缺件也要拿到结论 + 安装建议,不能第一项缺就中断体检。
|
|
404
|
+
// 门禁自带 60 s 探测缓存,四项连着查只真探一遍。
|
|
405
|
+
const rows = [];
|
|
406
|
+
let configPath = "";
|
|
407
|
+
for (const capability of ENV_CAPABILITIES) {
|
|
408
|
+
const gate = await ensureToolchain(ctx, { capability, envCheck: "warn", signal: invocation.signal });
|
|
409
|
+
if (configPath === "")
|
|
410
|
+
configPath = gate.configPath;
|
|
411
|
+
rows.push({ capability, gate });
|
|
412
|
+
}
|
|
413
|
+
return envText(rows, configPath);
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
return err("工具链体检异常:" + (error instanceof Error ? error.message : String(error)));
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
});
|
|
218
420
|
}
|
package/lib/data/chips.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* L1:芯片型号 →
|
|
2
|
+
* L1:芯片型号 → 可用引脚表。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 两条来源,优先本机 CubeMX 的数据库:
|
|
5
|
+
* 1. `<CubeMX 安装根>/db/mcu/<Mcu.Name>.xml` 里的 `<Pin Name="PA1" Position="3"/>`——
|
|
6
|
+
* Position>0 才是真实封装引脚(Position=0 是虚拟引脚,例如 VP_SYS_VS_Systick 的占位)。
|
|
7
|
+
* 名字常带功能后缀(`PA0-WKUP`、`PC13-TAMPER-RTC`、`PD0-OSC_IN`),这里归一化成 PA0/PC13/PD0。
|
|
8
|
+
* 2. 内置表(CHIPS):机器上没装 CubeMX 时兜底,能跑但**没有跟本机 db 校验过**,
|
|
9
|
+
* deriveChipPinTable 会在 notes 里把这件事说清楚。
|
|
10
|
+
*
|
|
11
|
+
* 放行语义(从第一天就在,不能变):isKnownPin 只认内置表;未知型号、虚拟引脚(VP_*)一律 true。
|
|
12
|
+
* 校验是为了拦住手误,不是为了当数据源;没校验过的东西绝不当"不存在"。
|
|
7
13
|
*/
|
|
14
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
8
16
|
function port(prefix, from, to) {
|
|
9
17
|
const out = [];
|
|
10
18
|
for (let index = from; index <= to; index += 1)
|
|
@@ -34,3 +42,125 @@ export function isKnownPin(cpn, pin) {
|
|
|
34
42
|
return chip.pins.includes(pin.toUpperCase());
|
|
35
43
|
}
|
|
36
44
|
export const KNOWN_CHIPS = CHIPS;
|
|
45
|
+
// ── 本机 CubeMX db/mcu 派生 ─────────────────────────────────────────────────
|
|
46
|
+
/** db 里 Pin 的 Name 形如 `PA0-WKUP` / `PC13-TAMPER-RTC` / `PD0-OSC_IN` / `PA13` / `VDD_1`。 */
|
|
47
|
+
function normalizeDbPinName(raw) {
|
|
48
|
+
const head = (raw.split(/[-/]/u)[0] ?? "").trim().toUpperCase();
|
|
49
|
+
return /^P[A-K]\d{1,2}$/u.test(head) ? head : "";
|
|
50
|
+
}
|
|
51
|
+
function pinOrder(pin) {
|
|
52
|
+
const matched = /^P([A-K])(\d{1,2})$/u.exec(pin);
|
|
53
|
+
return [matched?.[1] ?? "", Number(matched?.[2] ?? 0)];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 解析 db/mcu/<Mcu.Name>.xml,取出这个封装上真实存在的 GPIO 引脚(PA0…,已排序去重)。
|
|
57
|
+
*
|
|
58
|
+
* 只认 Position>0:CubeMX 把虚拟引脚也写进 <Pin>,Position=0,拿来当"这个型号有这根脚"会误判。
|
|
59
|
+
* 兼容多行标签(真实 db 里属性经常换行)与自闭合/带子节点的写法。
|
|
60
|
+
*/
|
|
61
|
+
export function parseMcuDbXmlPins(xml) {
|
|
62
|
+
const pins = [];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
for (const tag of xml.match(/<Pin\b[^>]*>/gu) ?? []) {
|
|
65
|
+
const name = /\bName\s*=\s*"([^"]*)"/u.exec(tag)?.[1] ?? "";
|
|
66
|
+
const positionText = /\bPosition\s*=\s*"([^"]*)"/u.exec(tag)?.[1] ?? "";
|
|
67
|
+
const position = Number.parseInt(positionText.trim(), 10);
|
|
68
|
+
if (!Number.isFinite(position) || position <= 0)
|
|
69
|
+
continue;
|
|
70
|
+
const pin = normalizeDbPinName(name);
|
|
71
|
+
if (pin === "" || seen.has(pin))
|
|
72
|
+
continue;
|
|
73
|
+
seen.add(pin);
|
|
74
|
+
pins.push(pin);
|
|
75
|
+
}
|
|
76
|
+
pins.sort((left, right) => {
|
|
77
|
+
const [leftPort, leftNumber] = pinOrder(left);
|
|
78
|
+
const [rightPort, rightNumber] = pinOrder(right);
|
|
79
|
+
if (leftPort !== rightPort)
|
|
80
|
+
return leftPort < rightPort ? -1 : 1;
|
|
81
|
+
return leftNumber - rightNumber;
|
|
82
|
+
});
|
|
83
|
+
return pins;
|
|
84
|
+
}
|
|
85
|
+
/** 本机 CubeMX 的 db/mcu 目录(<CubeMX 安装根>/db/mcu);没有返回 null。 */
|
|
86
|
+
export function findMcuDbDir(cubeMxRoots) {
|
|
87
|
+
for (const root of cubeMxRoots ?? []) {
|
|
88
|
+
if (root.trim() === "")
|
|
89
|
+
continue;
|
|
90
|
+
const dir = join(root, "db", "mcu");
|
|
91
|
+
if (existsSync(dir))
|
|
92
|
+
return dir;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 在 db/mcu 下按候选文件名找 XML(CubeMX 用 Mcu.Name,例如 STM32F103C(8-B)Tx.xml;
|
|
98
|
+
* 也可能有人按完整型号存成 STM32F103RCT6.xml),读到就解析,读不到返回 null。
|
|
99
|
+
*/
|
|
100
|
+
export function readMcuDbPins(dbMcuDir, names) {
|
|
101
|
+
if (dbMcuDir === null || dbMcuDir === undefined || dbMcuDir.trim() === "")
|
|
102
|
+
return null;
|
|
103
|
+
for (const name of names) {
|
|
104
|
+
const wanted = name.trim();
|
|
105
|
+
if (wanted === "")
|
|
106
|
+
continue;
|
|
107
|
+
const xmlPath = join(dbMcuDir, wanted + ".xml");
|
|
108
|
+
if (!existsSync(xmlPath))
|
|
109
|
+
continue;
|
|
110
|
+
try {
|
|
111
|
+
const pins = parseMcuDbXmlPins(readFileSync(xmlPath, "utf8"));
|
|
112
|
+
if (pins.length > 0)
|
|
113
|
+
return { xmlPath, pins };
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// 读不了/不是合法 db:退回内置表,notes 里会说明没校验。
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 型号 → 引脚表(db 优先,退回内置表)。
|
|
123
|
+
*
|
|
124
|
+
* "没校验"这件事必须写进 notes:内置表只收了 C8T6 一款,别的型号拿它当"真实引脚"会误判,
|
|
125
|
+
* 所以内置表路径只说"这里是内置表,未与本机 db 校验",不假装校验过。
|
|
126
|
+
*/
|
|
127
|
+
export function deriveChipPinTable(input) {
|
|
128
|
+
const cpn = input.cpn.trim();
|
|
129
|
+
const mcuName = (input.mcuName ?? "").trim();
|
|
130
|
+
const names = [mcuName, cpn].filter((name) => name !== "");
|
|
131
|
+
const dbHit = readMcuDbPins(input.dbMcuDir ?? null, names);
|
|
132
|
+
if (dbHit !== null) {
|
|
133
|
+
return {
|
|
134
|
+
kind: "db",
|
|
135
|
+
cpn,
|
|
136
|
+
mcuName,
|
|
137
|
+
pins: dbHit.pins,
|
|
138
|
+
notes: [
|
|
139
|
+
"引脚表:来自本机 CubeMX db(" + dbHit.xmlPath + "," + String(dbHit.pins.length) + " 个封装引脚)",
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const builtin = findChip(cpn);
|
|
144
|
+
const why = input.dbMcuDir === null || input.dbMcuDir === undefined
|
|
145
|
+
? "本机没找到 CubeMX 的 db/mcu"
|
|
146
|
+
: "db/mcu 下没有 " + (names.length === 0 ? "该型号" : names.join(" / ")) + ".xml";
|
|
147
|
+
if (builtin !== null) {
|
|
148
|
+
return {
|
|
149
|
+
kind: "builtin",
|
|
150
|
+
cpn,
|
|
151
|
+
mcuName,
|
|
152
|
+
pins: builtin.pins,
|
|
153
|
+
notes: ["引脚表:内置表(" + String(builtin.pins.length) + " 个引脚,未与本机 db 校验:" + why + ")"],
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
kind: "unknown",
|
|
158
|
+
cpn,
|
|
159
|
+
mcuName,
|
|
160
|
+
pins: [],
|
|
161
|
+
notes: [
|
|
162
|
+
"引脚表:型号 " + (cpn === "" ? "(空)" : cpn) + " 不在内置表,也没有本机 CubeMX db(未与本机 db 校验:" + why +
|
|
163
|
+
");未知引脚一律放行(isKnownPin 语义不变)",
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* STM32F1 时钟树事实表(只放"书上写的数字",不放算法)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一张表:F1 的时钟上限、PLL 可选倍频、外设挂在哪条总线,这些是芯片事实,
|
|
5
|
+
* 而"给定目标频率怎么挑组合"是策略(src/engine/clock-plan.ts)。事实和策略分开,
|
|
6
|
+
* 以后扩 F0/F4 只换这张表,算法不用动。
|
|
7
|
+
*
|
|
8
|
+
* 依据:RM0008 §7(RCC)、DS5319(F103x8/xB)电气特性。F1 全系(value / connectivity)
|
|
9
|
+
* 的 SYSCLK 上限都是 72 MHz;connectivity(105/107)多一级 PREDIV1,本插件暂不规划。
|
|
10
|
+
*/
|
|
11
|
+
export const F1_PLL_SOURCES = [
|
|
12
|
+
{
|
|
13
|
+
id: "hse",
|
|
14
|
+
label: "HSE 8 MHz",
|
|
15
|
+
iocValue: "RCC_PLLSOURCE_HSE",
|
|
16
|
+
defaultHz: 8_000_000,
|
|
17
|
+
selectable: true,
|
|
18
|
+
note: "外部晶振(板上 8 MHz,值料号 HSE 范围 4–16 MHz)。精度由晶振决定,通常 ≤50 ppm,UART 波特率靠它就够准。",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: "hsi-div2",
|
|
22
|
+
label: "HSI÷2 4 MHz",
|
|
23
|
+
iocValue: "RCC_PLLSOURCE_HSI_DIV2",
|
|
24
|
+
defaultHz: 4_000_000,
|
|
25
|
+
selectable: true,
|
|
26
|
+
note: "内部 RC 8 MHz ÷2。全温区 ±1%,UART 波特率会跟着漂,只在不接晶振时用。",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: "hsi",
|
|
30
|
+
label: "HSI 8 MHz",
|
|
31
|
+
iocValue: "RCC_PLLSOURCE_HSI",
|
|
32
|
+
defaultHz: 8_000_000,
|
|
33
|
+
selectable: false,
|
|
34
|
+
note: "F1 值料号的 PLLSRC 只有 HSE / HSI÷2 两档;HSI 直接进 PLL 是互联型(105/107)经 PREDIV1 才有的路,本插件不规划。",
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
export function pllSourceById(id) {
|
|
38
|
+
return F1_PLL_SOURCES.find((source) => source.id === id) ?? null;
|
|
39
|
+
}
|
|
40
|
+
export const F1_PLL_MULS = Array.from({ length: 15 }, (_unused, index) => {
|
|
41
|
+
const mul = index + 2;
|
|
42
|
+
return { mul, iocValue: "RCC_PLL_MUL" + String(mul) };
|
|
43
|
+
});
|
|
44
|
+
export function pllMulValue(mul) {
|
|
45
|
+
return "RCC_PLL_MUL" + String(mul);
|
|
46
|
+
}
|
|
47
|
+
/** 从 RCC.PLLMUL 的值(RCC_PLL_MUL9)反解倍频;认不出来返回 null。 */
|
|
48
|
+
export function pllMulFromValue(value) {
|
|
49
|
+
const matched = /^RCC_PLL_MUL(\d{1,2})$/u.exec(value.trim());
|
|
50
|
+
if (matched === null)
|
|
51
|
+
return null;
|
|
52
|
+
const mul = Number.parseInt(matched[1], 10);
|
|
53
|
+
return Number.isFinite(mul) && mul >= 2 && mul <= 16 ? mul : null;
|
|
54
|
+
}
|
|
55
|
+
/** AHB / APB1 / APB2 分频:÷1、2、4、8、16(RM0008 RCC_CFGR.HPRE/PPRE1/PPRE2)。 */
|
|
56
|
+
export const F1_APB_DIVS = [1, 2, 4, 8, 16].map((div) => ({
|
|
57
|
+
div,
|
|
58
|
+
iocValue: "RCC_HCLK_DIV" + String(div),
|
|
59
|
+
}));
|
|
60
|
+
/** ADC 预分频:÷2、4、6、8(RCC_CFGR.ADCPRE)。 */
|
|
61
|
+
export const F1_ADC_DIVS = [2, 4, 6, 8].map((div) => ({
|
|
62
|
+
div,
|
|
63
|
+
iocValue: "RCC_ADCPCLK2_DIV" + String(div),
|
|
64
|
+
}));
|
|
65
|
+
/** USB 时钟:只能从 PLLCLK ÷1.5 来,所以 PLLCLK 必须是 72 MHz(→48 MHz)。 */
|
|
66
|
+
export const F1_USB_PRESCALER = {
|
|
67
|
+
iocValue: "RCC_USBCLKSOURCE_PLL_DIV1_5",
|
|
68
|
+
hz: 48_000_000,
|
|
69
|
+
requiresPllClkHz: 72_000_000,
|
|
70
|
+
note: "F1 的 USB 只有 PLLCLK÷1.5 一路:PLLCLK=72 MHz 时 48 MHz 可用,其它频率下 USB 跑不起来。",
|
|
71
|
+
};
|
|
72
|
+
export function apbDivValue(div) {
|
|
73
|
+
return "RCC_HCLK_DIV" + String(div);
|
|
74
|
+
}
|
|
75
|
+
export function adcDivValue(div) {
|
|
76
|
+
return "RCC_ADCPCLK2_DIV" + String(div);
|
|
77
|
+
}
|
|
78
|
+
export function apbDivFromValue(value) {
|
|
79
|
+
const matched = /^RCC_HCLK_DIV(\d{1,2})$/u.exec(value.trim());
|
|
80
|
+
if (matched === null)
|
|
81
|
+
return null;
|
|
82
|
+
const div = Number.parseInt(matched[1], 10);
|
|
83
|
+
return [1, 2, 4, 8, 16].includes(div) ? div : null;
|
|
84
|
+
}
|
|
85
|
+
export function adcDivFromValue(value) {
|
|
86
|
+
const matched = /^RCC_ADCPCLK2_DIV(\d{1,2})$/u.exec(value.trim());
|
|
87
|
+
if (matched === null)
|
|
88
|
+
return null;
|
|
89
|
+
const div = Number.parseInt(matched[1], 10);
|
|
90
|
+
return [2, 4, 6, 8].includes(div) ? div : null;
|
|
91
|
+
}
|
|
92
|
+
/** 上限(Hz)。USB 不是"上限"而是"必须等于":必须正好 48 MHz。 */
|
|
93
|
+
export const F1_CLOCK_LIMITS = {
|
|
94
|
+
sysclkHz: 72_000_000,
|
|
95
|
+
apb1Hz: 36_000_000,
|
|
96
|
+
apb2Hz: 72_000_000,
|
|
97
|
+
adcHz: 14_000_000,
|
|
98
|
+
usbHz: 48_000_000,
|
|
99
|
+
};
|
|
100
|
+
/** 上限的人读标签,报告里拼错误消息用。 */
|
|
101
|
+
export const F1_CLOCK_LIMIT_LABELS = {
|
|
102
|
+
sysclkHz: "SYSCLK",
|
|
103
|
+
apb1Hz: "APB1",
|
|
104
|
+
apb2Hz: "APB2",
|
|
105
|
+
adcHz: "ADC",
|
|
106
|
+
usbHz: "USB",
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* 外设 → 总线(RM0008 的 memory map 与 RCC 使能寄存器分组):
|
|
110
|
+
* APB2 = USART1 / SPI1 / ADC1-3 / TIM1 / TIM8 / GPIO / AFIO;
|
|
111
|
+
* APB1 = USART2-5 / SPI2-3 / I2C1-2 / TIM2-7 / USB / CAN / DAC。
|
|
112
|
+
* 这里只列报告要展示的那批,够覆盖 F1 值料号常见的用法。
|
|
113
|
+
*/
|
|
114
|
+
export const F1_PERIPHERAL_CLOCKS = [
|
|
115
|
+
{ id: "USART1", label: "USART1", bus: "apb2", kind: "usart" },
|
|
116
|
+
{ id: "USART2", label: "USART2", bus: "apb1", kind: "usart" },
|
|
117
|
+
{ id: "USART3", label: "USART3", bus: "apb1", kind: "usart" },
|
|
118
|
+
{ id: "SPI1", label: "SPI1", bus: "apb2", kind: "spi" },
|
|
119
|
+
{ id: "SPI2", label: "SPI2", bus: "apb1", kind: "spi" },
|
|
120
|
+
{ id: "I2C1", label: "I2C1", bus: "apb1", kind: "i2c" },
|
|
121
|
+
{ id: "I2C2", label: "I2C2", bus: "apb1", kind: "i2c" },
|
|
122
|
+
{ id: "ADC1", label: "ADC1/2", bus: "apb2", kind: "adc" },
|
|
123
|
+
{ id: "TIM1", label: "TIM1", bus: "apb2", kind: "tim" },
|
|
124
|
+
{ id: "TIM2", label: "TIM2", bus: "apb1", kind: "tim" },
|
|
125
|
+
{ id: "TIM3", label: "TIM3", bus: "apb1", kind: "tim" },
|
|
126
|
+
{ id: "TIM4", label: "TIM4", bus: "apb1", kind: "tim" },
|
|
127
|
+
{ id: "USB", label: "USB", bus: "apb1", kind: "usb" },
|
|
128
|
+
{ id: "Cortex", label: "Cortex-SysTick", bus: "ahb", kind: "core" },
|
|
129
|
+
];
|
|
130
|
+
/** UART 波特率整除误差默认算这几档(F1 值料号常用档)。 */
|
|
131
|
+
export const F1_BAUD_LIST = [9600, 19200, 38400, 57600, 115200, 230400];
|
|
132
|
+
/**
|
|
133
|
+
* templates/scaffold/demo.ioc 里 RCC.IPParameters 列出的那 20 个键。
|
|
134
|
+
* 改时钟时必须成套重算——只改 RCC.PLLMUL 会让 CubeMX 下次打开按旧 Freq_Value 生成
|
|
135
|
+
* SystemClock_Config,HAL_RCC_ClockConfig 直接算错(这是本插件最容易埋雷的地方)。
|
|
136
|
+
*/
|
|
137
|
+
export const F1_RCC_IP_KEYS = [
|
|
138
|
+
"ADCFreqValue",
|
|
139
|
+
"AHBFreq_Value",
|
|
140
|
+
"APB1CLKDivider",
|
|
141
|
+
"APB1Freq_Value",
|
|
142
|
+
"APB1TimFreq_Value",
|
|
143
|
+
"APB2Freq_Value",
|
|
144
|
+
"APB2TimFreq_Value",
|
|
145
|
+
"FCLKCortexFreq_Value",
|
|
146
|
+
"FamilyName",
|
|
147
|
+
"HCLKFreq_Value",
|
|
148
|
+
"MCOFreq_Value",
|
|
149
|
+
"PLLCLKFreq_Value",
|
|
150
|
+
"PLLMCOFreq_Value",
|
|
151
|
+
"PLLMUL",
|
|
152
|
+
"PLLSourceVirtual",
|
|
153
|
+
"SYSCLKFreq_VALUE",
|
|
154
|
+
"SYSCLKSource",
|
|
155
|
+
"TimSysFreq_Value",
|
|
156
|
+
"USBFreq_Value",
|
|
157
|
+
"VCOOutput2Freq_Value",
|
|
158
|
+
];
|