dsh-stm32 0.1.1 → 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 +52 -23
- package/README_EN.md +50 -21
- package/docs/development-notes.md +466 -4
- package/docs/reading-guide.md +738 -0
- package/lib/commands/register.js +49 -0
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +211 -42
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/stm32f1.js +186 -0
- package/lib/engine/build.js +115 -16
- package/lib/engine/cache.js +56 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/debug.js +75 -10
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +174 -30
- package/lib/engine/flash.js +95 -6
- package/lib/engine/generate.js +11 -0
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +28 -2
- package/lib/engine/msys2-install.js +332 -61
- 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 +88 -17
- package/lib/engine/scaffold.js +104 -28
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +2 -0
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +12 -0
- package/lib/tools/cache.js +39 -8
- package/lib/tools/debug.js +7 -0
- package/lib/tools/doctor.js +11 -2
- package/lib/tools/flash.js +3 -0
- package/lib/tools/ioc-plan.js +195 -4
- package/lib/tools/project-create.js +26 -1
- package/lib/tools/project-export.js +100 -0
- package/package.json +5 -3
- package/templates/register/CMakeLists.txt +19 -6
- package/templates/register/Core/Src/main.c +18 -9
- package/templates/scaffold/openocd.cfg +8 -0
package/lib/engine/doctor.js
CHANGED
|
@@ -6,14 +6,85 @@
|
|
|
6
6
|
* 不烧录、不生成、不写工程。
|
|
7
7
|
*/
|
|
8
8
|
import { createConnection } from "node:net";
|
|
9
|
-
import { readFileSync } from "node:fs";
|
|
10
|
-
import { resolve } from "node:path";
|
|
9
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
10
|
+
import { join, resolve } from "node:path";
|
|
11
|
+
import { deriveChipPinTable, findMcuDbDir } from "../data/chips.js";
|
|
11
12
|
import { describeSearchScope, discoverInstalls, discoveryDisabled, resolveSearchConfig } from "../data/search-roots.js";
|
|
13
|
+
import { resolveF1Chip } from "../data/stm32f1.js";
|
|
12
14
|
import { detectBuildDir, findElf } from "./build.js";
|
|
15
|
+
import { CUBEMX_CMAKE_REL, deviceDefineFromStartup, isDeviceDefine, listStartupFiles } from "./cmake-sources.js";
|
|
13
16
|
import { findIocFile } from "./generate.js";
|
|
14
17
|
import { summarizeIoc } from "./ioc.js";
|
|
15
18
|
import { buildTargets, probeAll } from "./probe.js";
|
|
16
19
|
import { runCapture } from "./subprocess-runner.js";
|
|
20
|
+
/**
|
|
21
|
+
* 解析 `pnputil /enum-devices /connected` 的输出,回答"这台机器上 USB 是什么形态"。
|
|
22
|
+
*
|
|
23
|
+
* 为什么要分级:云服务器(阿里云 ECS)物理上没有 USB 口,无论用户怎么插线都进不来探针;
|
|
24
|
+
* 这和"没插板 / 线只供电 / 驱动没装 / 被占"是四种完全不同的回答,混成一句"找不到探针"
|
|
25
|
+
* 会让用户白折腾。诊断只读、失败不阻塞(unknown 也会给一句手查指引)。
|
|
26
|
+
*/
|
|
27
|
+
export function diagnoseUsb(pnputilText) {
|
|
28
|
+
// 不依赖本地化字段名(中文系统的 pnputil 字段名是 GBK,读出来是乱码):
|
|
29
|
+
// 只要任意一行里出现 USB\VID_、USB\ROOT_HUB、或 VID_xxxx&PID 形态的 USB 实例,
|
|
30
|
+
// 就认定这台机器有 USB 控制器/设备;纯 ASCII 部分(USB、VID、PID、ROOT_HUB)不受编码影响。
|
|
31
|
+
const hasUsbController = /USB\\(?:VID_|ROOT_HUB|VID\w{4}&PID)/iu.test(pnputilText) ||
|
|
32
|
+
/\\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);
|
|
37
|
+
const hasDriverProblem = /(?:Problem Code|问题代码)\s*[::]?\s*(?:1[0-9]|2[0-8]|3[0-9]|4[0-3]|5[0-2])/iu.test(pnputilText);
|
|
38
|
+
if (!hasUsbController) {
|
|
39
|
+
return {
|
|
40
|
+
kind: "no-usb",
|
|
41
|
+
detail: "系统里枚举不到任何 USB 控制器:这台机器多半是云服务器/虚拟机,物理上没有 USB 口," +
|
|
42
|
+
"插线、换线、装驱动都解决不了。要么换一台有 USB 的物理机,要么用 USB over IP(usbipd/USBIP)把探针从别的机器转发过来" +
|
|
43
|
+
"(注意 SWD 对延迟敏感,转发方案只建议实验用)。",
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (hasDap && hasDriverProblem) {
|
|
47
|
+
return {
|
|
48
|
+
kind: "driver-error",
|
|
49
|
+
detail: "能看到 CMSIS-DAP 设备但驱动状态异常(Problem Code 非 0):在设备管理器里找到该设备," +
|
|
50
|
+
"重装或回退驱动(CMSIS-DAP 用 WinUSB/HID 通用驱动即可;装过 DAPLink 官方驱动的先卸载它)。",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (hasDap) {
|
|
54
|
+
return { kind: "ok", detail: "能看到 CMSIS-DAP 设备且驱动正常:探针在线,烧录/调试应当能直接找到它。" };
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
kind: "no-dap",
|
|
58
|
+
detail: "有 USB 控制器但没看到常见探针(CMSIS-DAP / ST-Link / DAPLink / CH340 桥接):检查探针有没有插好(有的线只能供电不能传数据)、" +
|
|
59
|
+
"板上调试器的跳线/开关位置、以及 BOOT0 是否接错(BOOT0=1 会进系统 bootloader,SWD 不响应)。",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export async function usbDiagnosis(subprocess, signal) {
|
|
63
|
+
// 只读枚举;pnputil 不存在(老系统)或被组策略拦时返回 unknown,绝不让诊断把 doctor 搞挂。
|
|
64
|
+
try {
|
|
65
|
+
const outcome = await runCapture(subprocess, {
|
|
66
|
+
argv: ["pnputil", "/enum-devices", "/connected"],
|
|
67
|
+
timeoutMs: 10_000,
|
|
68
|
+
maxBytes: 2 * 1024 * 1024,
|
|
69
|
+
...(signal === undefined ? {} : { signal }),
|
|
70
|
+
});
|
|
71
|
+
if (outcome.spawnError !== null || outcome.exitCode !== 0) {
|
|
72
|
+
return {
|
|
73
|
+
kind: "unknown",
|
|
74
|
+
detail: "跑不了 pnputil /enum-devices(" +
|
|
75
|
+
(outcome.spawnError !== null ? outcome.spawnError : "退出码 " + String(outcome.exitCode)) +
|
|
76
|
+
"),无法判断 USB/探针形态;可在设备管理器里人工核对。",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return diagnoseUsb(outcome.stdout + "\n" + outcome.stderr);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
return {
|
|
83
|
+
kind: "unknown",
|
|
84
|
+
detail: "USB/探针诊断异常终止(" + (error instanceof Error ? error.message : String(error)) + "),可忽略;设备管理器里人工核对。",
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
17
88
|
/** 会占着探针/端口的进程名(小写比较)。 */
|
|
18
89
|
const PROBE_PROCESS_NAMES = ["openocd.exe", "gdb-multiarch.exe", "arm-none-eabi-gdb.exe", "stm32cubemx.exe"];
|
|
19
90
|
/** 解析 `tasklist /NH /FO CSV` 的输出,只留下关心的进程。 */
|
|
@@ -53,6 +124,293 @@ export function checkPortBusy(port, timeoutMs = 600) {
|
|
|
53
124
|
socket.once("error", () => finish(false));
|
|
54
125
|
});
|
|
55
126
|
}
|
|
127
|
+
/** Gnu ld 的容量写法:20K / 0x5000 / 65536 / 64M;带 +/- 的算式不猜,返回 null。 */
|
|
128
|
+
function parseByteLength(raw) {
|
|
129
|
+
const matched = /^(0x[0-9a-f]+|\d+)\s*(k|m)?$/iu.exec(raw.trim());
|
|
130
|
+
if (matched === null)
|
|
131
|
+
return null;
|
|
132
|
+
const literal = matched[1] ?? "";
|
|
133
|
+
const base = literal.toLowerCase().startsWith("0x") ? Number.parseInt(literal, 16) : Number.parseInt(literal, 10);
|
|
134
|
+
if (!Number.isFinite(base))
|
|
135
|
+
return null;
|
|
136
|
+
const unit = (matched[2] ?? "").toLowerCase();
|
|
137
|
+
if (unit === "k")
|
|
138
|
+
return base * 1024;
|
|
139
|
+
if (unit === "m")
|
|
140
|
+
return base * 1024 * 1024;
|
|
141
|
+
return base;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 从链接脚本读 MEMORY 的 RAM/FLASH 容量。
|
|
145
|
+
*
|
|
146
|
+
* 只认 MEMORY 行(`RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K`)。
|
|
147
|
+
* `LENGTH = 20K - 1` 这类算式返回 null:算错了不如说不知道。
|
|
148
|
+
*/
|
|
149
|
+
export function parseLinkerMemory(text) {
|
|
150
|
+
const out = { ramBytes: null, flashBytes: null };
|
|
151
|
+
const withoutComments = text.replace(/\/\*[\s\S]*?\*\//gu, " ").replace(/\/\/[^\n]*/gu, " ");
|
|
152
|
+
const regionRe = /^[ \t]*([A-Za-z_][\w.]*)\s*(?:\([^)]*\))?\s*:\s*ORIGIN\s*=\s*[^,\n]+,\s*LENGTH\s*=\s*([^,\n;]+)/gmu;
|
|
153
|
+
for (const matched of withoutComments.matchAll(regionRe)) {
|
|
154
|
+
const region = (matched[1] ?? "").toUpperCase();
|
|
155
|
+
const bytes = parseByteLength(matched[2] ?? "");
|
|
156
|
+
if (bytes === null)
|
|
157
|
+
continue;
|
|
158
|
+
if (region.includes("RAM")) {
|
|
159
|
+
if (out.ramBytes === null)
|
|
160
|
+
out.ramBytes = bytes;
|
|
161
|
+
}
|
|
162
|
+
else if (region.includes("FLASH")) {
|
|
163
|
+
if (out.flashBytes === null)
|
|
164
|
+
out.flashBytes = bytes;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
/** cmake 源清单里的 STM32 设备宏(`set(MX_Defines_Syms ... STM32F103xB ...)` 那一行)。 */
|
|
170
|
+
export function deviceDefinesIn(cmakeText) {
|
|
171
|
+
// 去掉注释,并把 -DSTM32F103xB 这种带 -D 的写法还原成裸宏名(-D 紧贴 S 会顶住左边界)。
|
|
172
|
+
const withoutComments = cmakeText.replace(/#[^\n]*/gu, " ").replace(/-D(?=STM32[A-Z]\d{3}[A-Za-z])/gu, " ");
|
|
173
|
+
const out = [];
|
|
174
|
+
for (const token of withoutComments.match(/(?<![A-Za-z0-9_])STM32[A-Z]\d{3}[A-Za-z][A-Za-z0-9]?(?![A-Za-z0-9_])/gu) ?? []) {
|
|
175
|
+
if (!isDeviceDefine(token))
|
|
176
|
+
continue;
|
|
177
|
+
if (!out.includes(token))
|
|
178
|
+
out.push(token);
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
function kib(bytes) {
|
|
183
|
+
return String(Math.round((bytes / 1024) * 10) / 10) + " KiB";
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* ELF 里真正要烧进 Flash 的字节数:只算 p_paddr 落在 F1 Flash 区间(0x08000000 起 1 MiB)的 PT_LOAD。
|
|
187
|
+
*
|
|
188
|
+
* 为什么不用文件大小:开了 -g3 的工程 .elf 里塞着几百 KiB 调试信息,文件大小和 Flash 占用能差一个数量级,
|
|
189
|
+
* 拿文件大小判"超 Flash"会在正经工程上误报。看不懂 program header 时返回 null,调用方退回文件大小并说明。
|
|
190
|
+
*/
|
|
191
|
+
export function elfFlashBytes(elfPath) {
|
|
192
|
+
let buf;
|
|
193
|
+
try {
|
|
194
|
+
buf = readFileSync(elfPath);
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (buf.length < 52)
|
|
200
|
+
return null;
|
|
201
|
+
if (buf[0] !== 0x7f || buf[1] !== 0x45 || buf[2] !== 0x4c || buf[3] !== 0x46)
|
|
202
|
+
return null;
|
|
203
|
+
const isElf32 = buf[4] === 1;
|
|
204
|
+
if (buf[4] !== 1 && buf[4] !== 2)
|
|
205
|
+
return null;
|
|
206
|
+
if (buf[5] !== 1)
|
|
207
|
+
return null; // ARM 目标一律小端
|
|
208
|
+
const u16 = (at) => buf.readUInt16LE(at);
|
|
209
|
+
const u32 = (at) => buf.readUInt32LE(at);
|
|
210
|
+
const u64 = (at) => Number(buf.readBigUInt64LE(at));
|
|
211
|
+
const phoff = isElf32 ? u32(28) : u64(32);
|
|
212
|
+
const phentsize = isElf32 ? u16(42) : u16(54);
|
|
213
|
+
const phnum = isElf32 ? u16(44) : u16(56);
|
|
214
|
+
if (phoff <= 0 || phentsize === 0 || phnum === 0)
|
|
215
|
+
return null;
|
|
216
|
+
const flashBase = 0x08000000;
|
|
217
|
+
const flashTop = flashBase + 1024 * 1024;
|
|
218
|
+
let total = 0;
|
|
219
|
+
let seen = false;
|
|
220
|
+
for (let index = 0; index < phnum; index += 1) {
|
|
221
|
+
const at = phoff + index * phentsize;
|
|
222
|
+
if (at + phentsize > buf.length)
|
|
223
|
+
break;
|
|
224
|
+
if (u32(at) !== 1)
|
|
225
|
+
continue; // PT_LOAD
|
|
226
|
+
const paddr = isElf32 ? u32(at + 12) : u64(at + 24);
|
|
227
|
+
const filesz = isElf32 ? u32(at + 16) : u64(at + 32);
|
|
228
|
+
if (paddr < flashBase || paddr >= flashTop)
|
|
229
|
+
continue;
|
|
230
|
+
total += filesz;
|
|
231
|
+
seen = true;
|
|
232
|
+
}
|
|
233
|
+
return seen ? total : null;
|
|
234
|
+
}
|
|
235
|
+
/** .ioc 里的引脚名(可能带功能后缀:PC13-TAMPER-RTC、PD0-OSC_IN)归一化成 PA13/PC13/PD0。 */
|
|
236
|
+
function normalizeIocPin(pin) {
|
|
237
|
+
const head = (pin.split(/[-/]/u)[0] ?? "").trim().toUpperCase();
|
|
238
|
+
return /^P[A-K]\d{1,2}$/u.test(head) ? head : "";
|
|
239
|
+
}
|
|
240
|
+
/** 工程根下的 *_FLASH.ld 文件名(按名字排序)。 */
|
|
241
|
+
export function linkerScriptNames(projectDir) {
|
|
242
|
+
if (!existsSync(projectDir))
|
|
243
|
+
return [];
|
|
244
|
+
try {
|
|
245
|
+
return readdirSync(projectDir).filter((name) => /_FLASH\.ld$/iu.test(name)).sort();
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return [];
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** 工程根那个链接脚本:多个时优先按芯片命名的(旧通用名 STM32F103XX_FLASH.ld 排最后)。 */
|
|
252
|
+
export function findLinkerScript(projectDir) {
|
|
253
|
+
const names = linkerScriptNames(projectDir);
|
|
254
|
+
if (names.length === 0)
|
|
255
|
+
return null;
|
|
256
|
+
const preferred = names.find((name) => name.toUpperCase() !== "STM32F103XX_FLASH.LD") ?? names[0];
|
|
257
|
+
try {
|
|
258
|
+
return { path: join(projectDir, preferred), name: preferred, text: readFileSync(join(projectDir, preferred), "utf8") };
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 四项一致性检查(纯函数,runDoctor 与单测共用):
|
|
266
|
+
* ① 芯片型号 ↔ 工程根启动文件;
|
|
267
|
+
* ② 启动文件/芯片 ↔ cmake 源清单里的设备宏;
|
|
268
|
+
* ③ 链接脚本 MEMORY 的 FLASH/RAM ↔ 芯片真实容量(大了致命,小了只浪费);
|
|
269
|
+
* ④ .elf 实际大小 ↔ Flash 容量。
|
|
270
|
+
* 另外把引脚表来源(本机 db 还是内置表、有没有校验过)写进 notes。
|
|
271
|
+
*/
|
|
272
|
+
export function checkChipConsistency(input) {
|
|
273
|
+
const blockers = [];
|
|
274
|
+
const notes = [];
|
|
275
|
+
const cpn = input.cpn.trim();
|
|
276
|
+
const mcuName = (input.mcuName ?? "").trim();
|
|
277
|
+
const pins = deriveChipPinTable({ cpn, mcuName, dbMcuDir: input.dbMcuDir ?? null });
|
|
278
|
+
if (cpn !== "")
|
|
279
|
+
notes.push(...pins.notes);
|
|
280
|
+
let chip = null;
|
|
281
|
+
if (cpn !== "") {
|
|
282
|
+
const resolved = resolveF1Chip(cpn);
|
|
283
|
+
chip = resolved.chip;
|
|
284
|
+
if (chip === null) {
|
|
285
|
+
notes.push("芯片型号 " + cpn + " 没法按 F1 反推(" + resolved.reason + "):跳过型号↔启动文件/链接脚本校验");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// ① 型号 ↔ 启动文件
|
|
289
|
+
const startupFiles = [...(input.startupFiles ?? [])];
|
|
290
|
+
const startup = startupFiles[0] ?? "";
|
|
291
|
+
const startupDefine = startup === "" ? "" : deviceDefineFromStartup(startup);
|
|
292
|
+
if (startup === "") {
|
|
293
|
+
notes.push("工程根没有 startup_stm32*.s:判断不了启动文件跟芯片是否配套(CubeMX 布局的 HAL 工程根上应该有一个)");
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
if (startupFiles.length > 1) {
|
|
297
|
+
notes.push("工程根有多个启动文件(" + startupFiles.join("、") + "):按 " + startup +
|
|
298
|
+
" 判断;换过芯片请删掉旧的,否则链接期会撞重复的中断向量表");
|
|
299
|
+
}
|
|
300
|
+
if (chip !== null && startup.toLowerCase() !== chip.startup.toLowerCase()) {
|
|
301
|
+
blockers.push("芯片 " + chip.cpn + " 的启动文件应是 " + chip.startup + ",工程根却是 " + startup +
|
|
302
|
+
"(中断向量表还是别的容量档,烧进去会跑飞)");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// ② 启动文件/芯片 ↔ 设备宏(cmake 源清单)
|
|
306
|
+
const cmakeText = input.cmakeText ?? null;
|
|
307
|
+
const cmakeDefines = cmakeText === null ? [] : deviceDefinesIn(cmakeText);
|
|
308
|
+
if (cmakeText === null) {
|
|
309
|
+
notes.push("没有 " + CUBEMX_CMAKE_REL + ":跳过设备宏一致性检查(不是 ST 的 CMake 布局?)");
|
|
310
|
+
}
|
|
311
|
+
else if (cmakeDefines.length === 0) {
|
|
312
|
+
notes.push(CUBEMX_CMAKE_REL + " 里没有 STM32 设备宏(set(MX_Defines_Syms ...)):跳过设备宏一致性检查");
|
|
313
|
+
}
|
|
314
|
+
else if (startupDefine !== "" && !cmakeDefines.includes(startupDefine)) {
|
|
315
|
+
blockers.push("启动文件 " + startup + " 对应设备宏 " + startupDefine + ",但 " + CUBEMX_CMAKE_REL + " 里是 " +
|
|
316
|
+
cmakeDefines.join(" / ") +
|
|
317
|
+
"(设备宏决定 CMSIS 头的容量档与中断表,错配编出来的是不相干的工程;换芯片后重跑 stm32_generate 让 CubeMX 重写这份清单)");
|
|
318
|
+
}
|
|
319
|
+
const startupMatchesChip = chip !== null && (startup === "" || startup.toLowerCase() === chip.startup.toLowerCase());
|
|
320
|
+
if (chip !== null && startupMatchesChip && cmakeDefines.length > 0 && !cmakeDefines.includes(chip.deviceDefine)) {
|
|
321
|
+
blockers.push("芯片 " + chip.cpn + " 的设备宏应是 " + chip.deviceDefine + "," + CUBEMX_CMAKE_REL + " 里是 " + cmakeDefines.join(" / "));
|
|
322
|
+
}
|
|
323
|
+
// ③ 链接脚本 MEMORY ↔ 芯片容量
|
|
324
|
+
const linkerText = input.linkerText ?? null;
|
|
325
|
+
const memory = linkerText === null ? null : parseLinkerMemory(linkerText);
|
|
326
|
+
const linkerLabel = (input.linkerName ?? "").trim() === "" ? "链接脚本" : input.linkerName.trim();
|
|
327
|
+
if (linkerText === null) {
|
|
328
|
+
notes.push("工程根没有 *_FLASH.ld:跳过 MEMORY 容量校验(链接脚本缺失或命名不同)");
|
|
329
|
+
}
|
|
330
|
+
else if (memory !== null) {
|
|
331
|
+
if (memory.flashBytes === null) {
|
|
332
|
+
notes.push(linkerLabel + " 里读不到 FLASH 区的 LENGTH(不是 MEMORY 写法?):没校验 Flash 容量");
|
|
333
|
+
}
|
|
334
|
+
else if (chip !== null && chip.flashBytes !== null) {
|
|
335
|
+
if (memory.flashBytes > chip.flashBytes) {
|
|
336
|
+
blockers.push(linkerLabel + " 的 FLASH 是 " + kib(memory.flashBytes) + ",超过芯片 " + chip.cpn + " 的 " + kib(chip.flashBytes) +
|
|
337
|
+
"(链接器会照它放代码,超出物理容量的部分写不进去)");
|
|
338
|
+
}
|
|
339
|
+
else if (memory.flashBytes < chip.flashBytes) {
|
|
340
|
+
notes.push(linkerLabel + " 只给了 " + kib(memory.flashBytes) + " Flash,芯片 " + chip.cpn + " 有 " + kib(chip.flashBytes) +
|
|
341
|
+
":能用,但浪费了 " + kib(chip.flashBytes - memory.flashBytes) + "(按芯片重渲染 <DeviceId>_FLASH.ld 才能用满)");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (chip !== null && chip.ramBytes === null) {
|
|
345
|
+
notes.push("芯片 " + chip.cpn + " 的 RAM 大小未收录:没校验链接脚本的 RAM 上限");
|
|
346
|
+
}
|
|
347
|
+
else if (chip !== null && chip.ramBytes !== null && memory.ramBytes !== null) {
|
|
348
|
+
if (memory.ramBytes > chip.ramBytes) {
|
|
349
|
+
blockers.push(linkerLabel + " 的 RAM 是 " + kib(memory.ramBytes) + ",超过芯片 " + chip.cpn + " 的 " + kib(chip.ramBytes) +
|
|
350
|
+
"(栈/堆一涨就踩到不存在的内存)");
|
|
351
|
+
}
|
|
352
|
+
else if (memory.ramBytes < chip.ramBytes) {
|
|
353
|
+
notes.push(linkerLabel + " 只给了 " + kib(memory.ramBytes) + " RAM,芯片 " + chip.cpn + " 有 " + kib(chip.ramBytes) + ":浪费了 " + kib(chip.ramBytes - memory.ramBytes));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// ④ .elf ↔ Flash
|
|
358
|
+
const elfSize = input.elfSize ?? 0;
|
|
359
|
+
const headerBytes = input.elfPath === undefined || input.elfPath === "" ? null : elfFlashBytes(input.elfPath);
|
|
360
|
+
const elfUsed = headerBytes ?? elfSize;
|
|
361
|
+
const flashLimit = chip?.flashBytes ?? (memory === null ? null : memory.flashBytes);
|
|
362
|
+
if (elfUsed > 0 && flashLimit !== null) {
|
|
363
|
+
const usage = headerBytes === null ? ".elf 的 program header 读不出来,按文件大小 " + kib(elfUsed) + " 粗算" : ".elf 要占 " + kib(elfUsed) + " Flash";
|
|
364
|
+
if (elfUsed > flashLimit) {
|
|
365
|
+
blockers.push(usage + ",超过 Flash 容量 " + kib(flashLimit) +
|
|
366
|
+
(chip === null ? "(按链接脚本算)" : "(芯片 " + chip.cpn + ")") +
|
|
367
|
+
(headerBytes === null
|
|
368
|
+
? ":文件大小含调试信息时会偏大,跑一次 stm32_build 看 FLASH 占用再确认;真的超了就别烧"
|
|
369
|
+
: ":链接器本该拦下,多半是 .ld 跟芯片错配,别烧"));
|
|
370
|
+
}
|
|
371
|
+
else if (elfUsed / flashLimit >= 0.9) {
|
|
372
|
+
notes.push(".elf 已用 " + String(Math.round((elfUsed / flashLimit) * 100)) + "% Flash(" + kib(elfUsed) + " / " + kib(flashLimit) + "),快满了");
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
// 引脚表 ↔ .ioc 用到的引脚(只有真拿到本机 db 时才比;内置表不拿来判"引脚不存在")
|
|
376
|
+
const iocPins = [...new Set((input.iocPins ?? []).map((pin) => normalizeIocPin(pin)).filter((pin) => pin !== ""))];
|
|
377
|
+
if (pins.kind === "db" && iocPins.length > 0) {
|
|
378
|
+
const unknown = iocPins.filter((pin) => !pins.pins.includes(pin));
|
|
379
|
+
if (unknown.length > 0) {
|
|
380
|
+
notes.push(".ioc 里这些引脚不在本机 db 的 " + (mcuName === "" ? cpn : mcuName) + " 封装里:" + unknown.join("、") + "(型号或封装选错了?)");
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return { blockers, notes, chip, memory, pins };
|
|
384
|
+
}
|
|
385
|
+
/** 从工程读事实再判:runDoctor 用它把四项一致性接进报告(不 spawn、不写盘)。 */
|
|
386
|
+
export function collectChipConsistency(projectDir, options) {
|
|
387
|
+
const startupFiles = listStartupFiles(projectDir);
|
|
388
|
+
let cmakeText = null;
|
|
389
|
+
try {
|
|
390
|
+
cmakeText = readFileSync(join(projectDir, CUBEMX_CMAKE_REL), "utf8");
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
cmakeText = null;
|
|
394
|
+
}
|
|
395
|
+
const linker = findLinkerScript(projectDir);
|
|
396
|
+
const result = checkChipConsistency({
|
|
397
|
+
cpn: options.cpn,
|
|
398
|
+
mcuName: options.mcuName,
|
|
399
|
+
startupFiles,
|
|
400
|
+
cmakeText,
|
|
401
|
+
linkerText: linker === null ? null : linker.text,
|
|
402
|
+
linkerName: linker === null ? "" : linker.name,
|
|
403
|
+
elfPath: options.elfPath ?? "",
|
|
404
|
+
elfSize: options.elfSize ?? 0,
|
|
405
|
+
dbMcuDir: options.dbMcuDir ?? null,
|
|
406
|
+
iocPins: options.iocPins,
|
|
407
|
+
});
|
|
408
|
+
const names = linkerScriptNames(projectDir);
|
|
409
|
+
if (names.length > 1) {
|
|
410
|
+
result.notes.push("工程根有多个链接脚本(" + names.join("、") + "):按 " + (linker === null ? (names[0] ?? "") : linker.name) + " 校验;多余的删掉,免得 -T 指错");
|
|
411
|
+
}
|
|
412
|
+
return result;
|
|
413
|
+
}
|
|
56
414
|
async function listProcesses(subprocess, signal) {
|
|
57
415
|
const outcome = await runCapture(subprocess, {
|
|
58
416
|
argv: ["tasklist", "/NH", "/FO", "CSV"],
|
|
@@ -80,14 +438,23 @@ export async function runDoctor(subprocess, request) {
|
|
|
80
438
|
});
|
|
81
439
|
const missingRequired = tools.filter((tool) => tool.required && !tool.found).map((tool) => tool.label);
|
|
82
440
|
progress("检查残留的调试进程与端口…");
|
|
83
|
-
const [processes, portBusy] = await Promise.all([
|
|
441
|
+
const [processes, portBusy, usb] = await Promise.all([
|
|
442
|
+
listProcesses(subprocess, request.signal),
|
|
443
|
+
checkPortBusy(port),
|
|
444
|
+
usbDiagnosis(subprocess, request.signal),
|
|
445
|
+
]);
|
|
84
446
|
const buildDir = detectBuildDir(projectDir) ?? "";
|
|
85
447
|
const elfInfo = buildDir === "" ? null : findElf(buildDir);
|
|
86
448
|
const iocPath = findIocFile(projectDir) ?? "";
|
|
87
449
|
const iocWarnings = [];
|
|
450
|
+
let iocMcu = null;
|
|
451
|
+
let iocPins = [];
|
|
88
452
|
if (iocPath !== "") {
|
|
89
453
|
try {
|
|
90
|
-
|
|
454
|
+
const summary = summarizeIoc(readFileSync(iocPath, "utf8"), iocPath);
|
|
455
|
+
iocWarnings.push(...summary.warnings);
|
|
456
|
+
iocMcu = { cpn: summary.mcu.cpn, name: summary.mcu.name };
|
|
457
|
+
iocPins = summary.pins.map((pin) => pin.pin);
|
|
91
458
|
}
|
|
92
459
|
catch (error) {
|
|
93
460
|
iocWarnings.push("读不了 .ioc:" + (error instanceof Error ? error.message : String(error)));
|
|
@@ -127,10 +494,33 @@ export async function runDoctor(subprocess, request) {
|
|
|
127
494
|
}
|
|
128
495
|
if (elfInfo === null)
|
|
129
496
|
notes.push(buildDir === "" ? "找不到构建目录:先跑 stm32_build" : "构建目录里还没有 .elf:先跑 stm32_build");
|
|
497
|
+
if (usb.kind === "no-usb") {
|
|
498
|
+
blockers.push("USB:" + usb.detail);
|
|
499
|
+
}
|
|
500
|
+
else if (usb.kind !== "ok" && usb.kind !== "unknown") {
|
|
501
|
+
notes.push("USB:" + usb.detail);
|
|
502
|
+
}
|
|
503
|
+
else if (usb.kind === "ok") {
|
|
504
|
+
notes.push("USB:探针在线(CMSIS-DAP 已枚举)。");
|
|
505
|
+
}
|
|
130
506
|
if (iocPath === "")
|
|
131
507
|
notes.push("工程根目录里没有 .ioc:generate/ioc_inspect 用不了");
|
|
132
508
|
for (const warning of iocWarnings)
|
|
133
509
|
blockers.push(".ioc:" + warning);
|
|
510
|
+
// 芯片一致性:型号 ↔ 启动文件 ↔ 设备宏 ↔ .ld MEMORY ↔ .elf(没有 .ioc 就没有芯片主张,跳过)。
|
|
511
|
+
if (iocMcu !== null && iocMcu.cpn !== "") {
|
|
512
|
+
progress("核对芯片/启动文件/设备宏/链接脚本是否配套…");
|
|
513
|
+
const chipCheck = collectChipConsistency(projectDir, {
|
|
514
|
+
cpn: iocMcu.cpn,
|
|
515
|
+
mcuName: iocMcu.name,
|
|
516
|
+
elfPath: elfInfo === null ? "" : elfInfo.path,
|
|
517
|
+
elfSize: elfInfo === null ? 0 : elfInfo.size,
|
|
518
|
+
dbMcuDir: findMcuDbDir(config.cubeMxRoots),
|
|
519
|
+
iocPins,
|
|
520
|
+
});
|
|
521
|
+
blockers.push(...chipCheck.blockers);
|
|
522
|
+
notes.push(...chipCheck.notes);
|
|
523
|
+
}
|
|
134
524
|
if (blockers.length === 0)
|
|
135
525
|
notes.push("环境就绪:工具链齐全、探针没被占、.ioc 没有致命告警");
|
|
136
526
|
const message = blockers.length === 0
|
|
@@ -151,6 +541,7 @@ export async function runDoctor(subprocess, request) {
|
|
|
151
541
|
elfSize: elfInfo === null ? 0 : elfInfo.size,
|
|
152
542
|
iocPath,
|
|
153
543
|
iocWarnings,
|
|
544
|
+
usb,
|
|
154
545
|
blockers,
|
|
155
546
|
notes,
|
|
156
547
|
};
|