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/scaffold.js
CHANGED
|
@@ -22,6 +22,7 @@ import { fileURLToPath } from "node:url";
|
|
|
22
22
|
import { DSH_FIRMWARE_DIR_NAME } from "../data/firmware-packages.js";
|
|
23
23
|
import { firmwareExtraDirs } from "./firmware-install.js";
|
|
24
24
|
import { copyFileLong, copyTreeLong, mkdirTrackedLong } from "./fs-longpath.js";
|
|
25
|
+
import { describeHalChip, LEGACY_LINKER_SCRIPT, pointLinkerFlagAt, renderIoc, renderLinkerScript, renderStartupAndDefine, resolveHalChip, } from "./hal-chip.js";
|
|
25
26
|
import { describeDroppedPlaceholder, detectVscodeFill, fillVscodeTemplate } from "./vscode-config.js";
|
|
26
27
|
export const DEFAULT_FIRMWARE_PACKAGE = "STM32Cube_FW_F1_V1.8.7";
|
|
27
28
|
export const DEFAULT_MCU_CUBE_VERSION = "6.15.0";
|
|
@@ -111,11 +112,17 @@ export function cleanupAfterFailure(targetDir, targetExistedBefore, created) {
|
|
|
111
112
|
}
|
|
112
113
|
return removed === 0 ? "" : "已清掉本次新建的 " + String(removed) + " 个空目录。";
|
|
113
114
|
}
|
|
114
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* 目录里是不是一个能拿来建工程的固件包(三条判据与 CubeMX 包管理器装的包一致)。
|
|
117
|
+
*
|
|
118
|
+
* 第三条只看 gcc 目录在不在,不再点名 startup_stm32f103xb.s:那是 C8T6 的启动文件,
|
|
119
|
+
* 用它当判据等于「不是 C8T6 的工程就别想建」。具体型号要哪个启动文件由
|
|
120
|
+
* createProjectScaffold 按芯片查(缺了会列出包里真实带的那些)。
|
|
121
|
+
*/
|
|
115
122
|
function isUsableFirmwareRoot(candidate) {
|
|
116
123
|
return (fs.existsSync(path.join(candidate, "Drivers", "STM32F1xx_HAL_Driver")) &&
|
|
117
124
|
fs.existsSync(path.join(candidate, "Drivers", "CMSIS", "Include")) &&
|
|
118
|
-
fs.existsSync(path.join(candidate, "Drivers", "CMSIS", "Device", "ST", "STM32F1xx", "Source", "Templates", "gcc"
|
|
125
|
+
fs.existsSync(path.join(candidate, "Drivers", "CMSIS", "Device", "ST", "STM32F1xx", "Source", "Templates", "gcc")));
|
|
119
126
|
}
|
|
120
127
|
/**
|
|
121
128
|
* 找固件包:先扫 CubeMX 根(官方仓库 → 插件的 dsh-firmware),再扫 extraDirs
|
|
@@ -226,8 +233,8 @@ function templateTargets(tpl, projectName) {
|
|
|
226
233
|
return targets;
|
|
227
234
|
}
|
|
228
235
|
/** 被模板/固件包直接覆盖的工程文件(Drivers/ 整棵树不在内:那是 ST 源码,不是用户文件)。 */
|
|
229
|
-
function overwriteTargetsFor(
|
|
230
|
-
return [...
|
|
236
|
+
function overwriteTargetsFor(writtenRels, facts) {
|
|
237
|
+
return [...writtenRels, facts.startup, "Core/Src/system_stm32f1xx.c", facts.linkerScript];
|
|
231
238
|
}
|
|
232
239
|
/** 备份说明(写进 warnings,卡片上第一眼就能看到原件在哪)。 */
|
|
233
240
|
export function backupWarning(backup) {
|
|
@@ -243,6 +250,12 @@ export function createProjectScaffold(request) {
|
|
|
243
250
|
const targetDir = path.resolve(request.targetDir);
|
|
244
251
|
const projectName = request.projectName ?? path.basename(targetDir);
|
|
245
252
|
const firmwarePackage = request.firmwarePackage ?? DEFAULT_FIRMWARE_PACKAGE;
|
|
253
|
+
// 芯片事实先算出来:连失败报告都要说清"本来是按哪款芯片建"。
|
|
254
|
+
const chip = resolveHalChip(request.mcu);
|
|
255
|
+
const facts = chip.facts;
|
|
256
|
+
const chipFields = () => facts === null
|
|
257
|
+
? {}
|
|
258
|
+
: { mcu: facts.mcu, startup: facts.startup, deviceDefine: facts.deviceDefine, linkerScript: facts.linkerScript };
|
|
246
259
|
const fail = (message, warnings = []) => ({
|
|
247
260
|
ok: false,
|
|
248
261
|
targetDir,
|
|
@@ -254,10 +267,14 @@ export function createProjectScaffold(request) {
|
|
|
254
267
|
durationMs: Date.now() - startedAt,
|
|
255
268
|
warnings,
|
|
256
269
|
message,
|
|
270
|
+
...chipFields(),
|
|
257
271
|
});
|
|
258
272
|
const refusal = preflightTarget({ targetDir, projectName, route: "hal", ...(request.overwrite === undefined ? {} : { overwrite: request.overwrite }) });
|
|
259
273
|
if (refusal !== null)
|
|
260
274
|
return fail(refusal);
|
|
275
|
+
// 1) 芯片:认不出 / 缺 RAM 就拒绝(reason 是人话,直接给用户看)
|
|
276
|
+
if (facts === null)
|
|
277
|
+
return fail("认不出芯片型号:" + chip.reason);
|
|
261
278
|
const tpl = templateDir();
|
|
262
279
|
if (!fs.existsSync(tpl))
|
|
263
280
|
return fail("插件模板目录缺失:" + tpl + "(重新安装插件)");
|
|
@@ -266,10 +283,65 @@ export function createProjectScaffold(request) {
|
|
|
266
283
|
return fail("找不到 CubeMX 固件包 " + firmwarePackage + "(找过:" + firmware.checked.join(";") + ")。" +
|
|
267
284
|
"固件包是 Drivers/ 与 startup 的唯一合法来源,插件不内嵌 ST 代码。");
|
|
268
285
|
}
|
|
286
|
+
// 2) 固件包里必须真有这款芯片的启动文件与设备头:认不出就停在这里,别建出个编译不过的工程
|
|
287
|
+
// (startup 是 copy 进工程的,设备头随 Drivers/ 整树拷过来,所以只校验存在性)。
|
|
288
|
+
const deviceDir = path.join(firmware.root, "Drivers", "CMSIS", "Device", "ST", "STM32F1xx");
|
|
289
|
+
const templatesDir = path.join(deviceDir, "Source", "Templates");
|
|
290
|
+
const gccDir = path.join(templatesDir, "gcc");
|
|
291
|
+
const startupSource = path.join(gccDir, facts.startup);
|
|
292
|
+
const headerSource = path.join(deviceDir, "Include", facts.deviceHeader);
|
|
293
|
+
if (!fs.existsSync(startupSource)) {
|
|
294
|
+
const available = fs.existsSync(gccDir)
|
|
295
|
+
? fs.readdirSync(gccDir).filter((name) => /^startup_.*\.s$/u.test(name)).sort()
|
|
296
|
+
: [];
|
|
297
|
+
return fail("固件包里没有 " + facts.mcu + " 需要的启动文件 " + facts.startup + ":" + startupSource +
|
|
298
|
+
"。这份固件包真实带的启动文件:" + (available.length === 0 ? "(一个都没有)" : available.join("、")));
|
|
299
|
+
}
|
|
300
|
+
if (!fs.existsSync(headerSource)) {
|
|
301
|
+
return fail("固件包里没有 " + facts.deviceDefine + " 的设备头 " + facts.deviceHeader + ":" + headerSource +
|
|
302
|
+
"。设备头随 Drivers/ 一起复制,缺它说明这份固件包不完整(换一个版本的 " + firmwarePackage + ")。");
|
|
303
|
+
}
|
|
304
|
+
// 3) 模板全部读进内存并按芯片渲染好——这一步一个字节都不写:
|
|
305
|
+
// 渲染失败只可能是模板被改过,宁可在动手前拒绝,也不留下一半 C8T6 一半新芯片的工程。
|
|
306
|
+
const vscodeFill = detectVscodeFill(request.binDirs ?? []);
|
|
307
|
+
const droppedVscode = new Set();
|
|
308
|
+
const written = [];
|
|
309
|
+
let linkerScriptText = "";
|
|
310
|
+
try {
|
|
311
|
+
// 链接脚本按芯片渲染;模板里那份(C8T6 的 64K/20K)只作渲染源 + 蓝丸的兼容副本。
|
|
312
|
+
linkerScriptText = renderLinkerScript(fs.readFileSync(path.join(tpl, LEGACY_LINKER_SCRIPT), "utf8"), facts);
|
|
313
|
+
for (const [source, destRel] of templateTargets(tpl, projectName)) {
|
|
314
|
+
// 旧名的链接脚本只在蓝丸上留一份:别的芯片写旧名 = 一份骗人的内存布局。
|
|
315
|
+
if (destRel === LEGACY_LINKER_SCRIPT && !facts.keepLegacyLinkerScript)
|
|
316
|
+
continue;
|
|
317
|
+
let text = fs.readFileSync(source, "utf8");
|
|
318
|
+
if (destRel !== ".gitignore" && destRel !== LEGACY_LINKER_SCRIPT) {
|
|
319
|
+
text = text.split("__PROJECT_NAME__").join(projectName);
|
|
320
|
+
}
|
|
321
|
+
if (destRel === projectName + ".ioc")
|
|
322
|
+
text = renderIoc(text, facts);
|
|
323
|
+
if (destRel === "cmake/gcc-arm-none-eabi.cmake" || destRel === "cmake/starm-clang.cmake") {
|
|
324
|
+
text = pointLinkerFlagAt(text, facts);
|
|
325
|
+
}
|
|
326
|
+
if (destRel === "cmake/stm32cubemx/CMakeLists.txt")
|
|
327
|
+
text = renderStartupAndDefine(text, facts);
|
|
328
|
+
if (destRel.startsWith(".vscode/")) {
|
|
329
|
+
const filled = fillVscodeTemplate(text, vscodeFill);
|
|
330
|
+
text = filled.text;
|
|
331
|
+
for (const token of filled.dropped)
|
|
332
|
+
droppedVscode.add(token);
|
|
333
|
+
}
|
|
334
|
+
written.push({ rel: destRel, text });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
return fail("没写任何文件:模板按芯片渲染失败(" + String(error.message) + ")。目标目录 " + targetDir + " 未被改动。");
|
|
339
|
+
}
|
|
340
|
+
const templateFiles = written.length;
|
|
269
341
|
// —— 写盘从这里开始:先把将被覆盖的同名文件备份;备份失败就一个字节都不写 ——
|
|
270
342
|
let backup;
|
|
271
343
|
try {
|
|
272
|
-
backup = backupBeforeOverwrite(targetDir, overwriteTargetsFor(
|
|
344
|
+
backup = backupBeforeOverwrite(targetDir, overwriteTargetsFor(written.map((item) => item.rel), facts), "scaffold");
|
|
273
345
|
}
|
|
274
346
|
catch (error) {
|
|
275
347
|
return fail("没写任何文件:覆盖前的备份失败(" + String(error.message) + ")。目标目录 " + targetDir + " 未被改动。");
|
|
@@ -277,48 +349,48 @@ export function createProjectScaffold(request) {
|
|
|
277
349
|
const backupNote = backupWarning(backup);
|
|
278
350
|
const backupWarnings = backupNote === null ? [] : [backupNote];
|
|
279
351
|
const targetExistedBefore = fs.existsSync(targetDir);
|
|
352
|
+
// npm 打包永远排除 .gitignore(它同时是 npm 的忽略过滤器):npm 安装的用户模板里必然没有。
|
|
353
|
+
// 缺它工程照建,只是 git 状态会多出 build/;warning 里说明白,别让人以为是装坏了。
|
|
354
|
+
const gitignoreMissing = !fs.existsSync(path.join(tpl, ".gitignore"));
|
|
280
355
|
const createdDirs = [];
|
|
281
|
-
const vscodeFill = detectVscodeFill(request.binDirs ?? []);
|
|
282
|
-
const droppedVscode = new Set();
|
|
283
|
-
let templateFiles = 0;
|
|
284
356
|
let driverFiles = 0;
|
|
285
357
|
try {
|
|
286
358
|
mkdirTracked(targetDir, createdDirs);
|
|
287
359
|
mkdirTracked(path.join(targetDir, "Core", "Src"), createdDirs);
|
|
288
360
|
mkdirTracked(path.join(targetDir, "Core", "Inc"), createdDirs);
|
|
289
|
-
// 1)
|
|
361
|
+
// 1) 模板快照(已按芯片渲染):CMake 工程文件 / 链接脚本 / .vscode / .ioc
|
|
290
362
|
// .vscode 里的调试/构建路径按本机探测填(模板只留占位符,绝不写死开发机的 D:/… 路径)。
|
|
291
|
-
for (const
|
|
292
|
-
|
|
293
|
-
if (destRel !== ".gitignore" && destRel !== "STM32F103XX_FLASH.ld") {
|
|
294
|
-
text = text.split("__PROJECT_NAME__").join(projectName);
|
|
295
|
-
}
|
|
296
|
-
if (destRel.startsWith(".vscode/")) {
|
|
297
|
-
const filled = fillVscodeTemplate(text, vscodeFill);
|
|
298
|
-
text = filled.text;
|
|
299
|
-
for (const token of filled.dropped)
|
|
300
|
-
droppedVscode.add(token);
|
|
301
|
-
}
|
|
302
|
-
const dest = path.join(targetDir, destRel);
|
|
363
|
+
for (const item of written) {
|
|
364
|
+
const dest = path.join(targetDir, item.rel);
|
|
303
365
|
mkdirTracked(path.dirname(dest), createdDirs);
|
|
304
|
-
fs.writeFileSync(dest, text);
|
|
305
|
-
templateFiles += 1;
|
|
366
|
+
fs.writeFileSync(dest, item.text);
|
|
306
367
|
}
|
|
307
|
-
//
|
|
368
|
+
// 链接脚本:按芯片命名的那份(蓝丸另有一份旧名兼容副本,已经作为模板项写过了)
|
|
369
|
+
fs.writeFileSync(path.join(targetDir, facts.linkerScript), linkerScriptText);
|
|
370
|
+
// 2) 固件包:Drivers 全量 + 本芯片的 startup + system(与 GUI 新工程逐字节一致)
|
|
308
371
|
driverFiles = copyTreeLong(path.join(firmware.root, "Drivers"), path.join(targetDir, "Drivers"), DRIVER_SKIP_PREFIXES, createdDirs);
|
|
309
|
-
|
|
310
|
-
copyFileLong(path.join(templatesDir, "gcc", "startup_stm32f103xb.s"), path.join(targetDir, "startup_stm32f103xb.s"));
|
|
372
|
+
copyFileLong(startupSource, path.join(targetDir, facts.startup));
|
|
311
373
|
copyFileLong(path.join(templatesDir, "system_stm32f1xx.c"), path.join(targetDir, "Core", "Src", "system_stm32f1xx.c"));
|
|
312
374
|
}
|
|
313
375
|
catch (error) {
|
|
314
376
|
const cleanup = cleanupAfterFailure(targetDir, targetExistedBefore, createdDirs);
|
|
315
|
-
return fail("写盘失败(已写 " + String(
|
|
377
|
+
return fail("写盘失败(已写 " + String(written.length) + " 个模板文件、" + String(driverFiles) + " 个固件文件):" +
|
|
316
378
|
String(error.message) + ";路径:" + targetDir + "。" + cleanup, backupWarnings);
|
|
317
379
|
}
|
|
318
380
|
const iocPath = path.join(targetDir, projectName + ".ioc");
|
|
319
381
|
const warnings = [];
|
|
320
382
|
if (backupNote !== null)
|
|
321
383
|
warnings.push(backupNote);
|
|
384
|
+
for (const note of facts.notes)
|
|
385
|
+
warnings.push(note);
|
|
386
|
+
if (!facts.keepLegacyLinkerScript) {
|
|
387
|
+
warnings.push("链接脚本按芯片命名为 " + facts.linkerScript + "(cmake 的 -T 已指到它);旧名 " + LEGACY_LINKER_SCRIPT +
|
|
388
|
+
" 只在 C8T6 上留兼容副本,别的芯片写它是错的内存布局。");
|
|
389
|
+
}
|
|
390
|
+
if (gitignoreMissing) {
|
|
391
|
+
warnings.push("插件包里没有 .gitignore(npm 打包会排除它):工程功能不受影响,只是 git 状态会多出 build/ 目录。" +
|
|
392
|
+
"想干净就在工程根目录建一个 .gitignore,内容一行:build/");
|
|
393
|
+
}
|
|
322
394
|
for (const token of droppedVscode)
|
|
323
395
|
warnings.push(describeDroppedPlaceholder(token));
|
|
324
396
|
warnings.push("没有生成 .mxproject:那是 CubeMX GUI 的工程元数据,插件不伪造;用 GUI 打开并生成一次即可补上。", "Core/ 下的 main.c 等还没生成:接着调 stm32_generate(3–4 分钟,需要 CubeMX 全权限)。");
|
|
@@ -333,7 +405,11 @@ export function createProjectScaffold(request) {
|
|
|
333
405
|
driverFiles,
|
|
334
406
|
durationMs: Date.now() - startedAt,
|
|
335
407
|
warnings,
|
|
408
|
+
mcu: facts.mcu,
|
|
409
|
+
startup: facts.startup,
|
|
410
|
+
deviceDefine: facts.deviceDefine,
|
|
411
|
+
linkerScript: facts.linkerScript,
|
|
336
412
|
message: "工程骨架已建好(" + String(templateFiles) + " 个工程文件 + " + String(driverFiles) + " 个固件文件):" + targetDir +
|
|
337
|
-
(backupNote === null ? "" : ";" + backupNote),
|
|
413
|
+
";芯片 " + describeHalChip(facts) + (backupNote === null ? "" : ";" + backupNote),
|
|
338
414
|
};
|
|
339
415
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L0:最小 zip 写入器(store,不压缩)—— 专门给 stm32_project_export 用。
|
|
3
|
+
*
|
|
4
|
+
* 为什么自己写而不用依赖:插件承诺零运行时依赖,为一个导出功能引 jszip/adm-zip 不值;
|
|
5
|
+
* 固件工程以文本为主,store 体积可接受(源码包本来就不大)。CRC32 用查表法。
|
|
6
|
+
*/
|
|
7
|
+
import { createWriteStream, readFileSync } from "node:fs";
|
|
8
|
+
/** CRC32(IEEE 0xEDB88320,查表法)。 */
|
|
9
|
+
const CRC_TABLE = (() => {
|
|
10
|
+
const table = new Uint32Array(256);
|
|
11
|
+
for (let n = 0; n < 256; n += 1) {
|
|
12
|
+
let c = n;
|
|
13
|
+
for (let k = 0; k < 8; k += 1)
|
|
14
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
15
|
+
table[n] = c >>> 0;
|
|
16
|
+
}
|
|
17
|
+
return table;
|
|
18
|
+
})();
|
|
19
|
+
export function crc32(buffer) {
|
|
20
|
+
let crc = 0xffffffff;
|
|
21
|
+
for (const byte of buffer) {
|
|
22
|
+
// Uint32Array 的索引读回 number | undefined(noUncheckedIndexedAccess);下标恒在 0..255。
|
|
23
|
+
const table = CRC_TABLE[(crc ^ byte) & 0xff];
|
|
24
|
+
crc = ((table === undefined ? 0 : table) ^ (crc >>> 8)) >>> 0;
|
|
25
|
+
}
|
|
26
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
27
|
+
}
|
|
28
|
+
/** 当前时间 → DOS 时间字段。 */
|
|
29
|
+
function dosTime(date) {
|
|
30
|
+
const time = (date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2);
|
|
31
|
+
const day = ((date.getFullYear() - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate();
|
|
32
|
+
return { time, date: day };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 写一个 store-only zip。sources: 条目名 → 磁盘路径;
|
|
36
|
+
* 值以 `__inline__\n` 开头时表示内容就是那个字符串本身(README 用)。
|
|
37
|
+
*/
|
|
38
|
+
export async function makeZip(outPath, sources) {
|
|
39
|
+
const chunks = [];
|
|
40
|
+
const entries = [];
|
|
41
|
+
let offset = 0;
|
|
42
|
+
const push = (buffer) => {
|
|
43
|
+
chunks.push(buffer);
|
|
44
|
+
offset += buffer.length;
|
|
45
|
+
};
|
|
46
|
+
const localHeader = (nameBuf, crc, bytes, time, date) => {
|
|
47
|
+
const header = Buffer.alloc(30);
|
|
48
|
+
header.writeUInt32LE(0x04034b50, 0);
|
|
49
|
+
header.writeUInt16LE(20, 4);
|
|
50
|
+
header.writeUInt16LE(0, 6);
|
|
51
|
+
header.writeUInt16LE(0, 8);
|
|
52
|
+
header.writeUInt16LE(time, 10);
|
|
53
|
+
header.writeUInt16LE(date, 12);
|
|
54
|
+
header.writeUInt32LE(crc, 14);
|
|
55
|
+
header.writeUInt32LE(bytes, 18);
|
|
56
|
+
header.writeUInt32LE(bytes, 22);
|
|
57
|
+
header.writeUInt16LE(nameBuf.length, 26);
|
|
58
|
+
header.writeUInt16LE(0, 28);
|
|
59
|
+
return header;
|
|
60
|
+
};
|
|
61
|
+
const now = new Date();
|
|
62
|
+
const { time, date } = dosTime(now);
|
|
63
|
+
for (const [name, source] of sources) {
|
|
64
|
+
const isInline = source.startsWith("__inline__\n");
|
|
65
|
+
const data = isInline ? Buffer.from(source.slice("__inline__\n".length), "utf8") : readFileSync(source);
|
|
66
|
+
const crc = crc32(data);
|
|
67
|
+
const nameBuf = Buffer.from(name, "utf8");
|
|
68
|
+
const header = localHeader(nameBuf, crc, data.length, time, date);
|
|
69
|
+
entries.push({ name, crc, bytes: data.length, offset });
|
|
70
|
+
push(header);
|
|
71
|
+
push(nameBuf);
|
|
72
|
+
push(data);
|
|
73
|
+
}
|
|
74
|
+
const centralStart = offset;
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
const nameBuf = Buffer.from(entry.name, "utf8");
|
|
77
|
+
const central = Buffer.alloc(46);
|
|
78
|
+
central.writeUInt32LE(0x02014b50, 0);
|
|
79
|
+
central.writeUInt16LE(20, 4);
|
|
80
|
+
central.writeUInt16LE(20, 6);
|
|
81
|
+
central.writeUInt16LE(0, 8);
|
|
82
|
+
central.writeUInt16LE(0, 10);
|
|
83
|
+
central.writeUInt16LE(time, 12);
|
|
84
|
+
central.writeUInt16LE(date, 14);
|
|
85
|
+
central.writeUInt32LE(entry.crc, 16);
|
|
86
|
+
central.writeUInt32LE(entry.bytes, 20);
|
|
87
|
+
central.writeUInt32LE(entry.bytes, 24);
|
|
88
|
+
central.writeUInt16LE(nameBuf.length, 28);
|
|
89
|
+
central.writeUInt16LE(0, 30);
|
|
90
|
+
central.writeUInt16LE(0, 32);
|
|
91
|
+
central.writeUInt16LE(0, 34);
|
|
92
|
+
central.writeUInt16LE(0, 36);
|
|
93
|
+
central.writeUInt32LE(0, 38);
|
|
94
|
+
central.writeUInt32LE(entry.offset, 42);
|
|
95
|
+
push(central);
|
|
96
|
+
push(nameBuf);
|
|
97
|
+
}
|
|
98
|
+
const end = Buffer.alloc(22);
|
|
99
|
+
end.writeUInt32LE(0x06054b50, 0);
|
|
100
|
+
end.writeUInt16LE(0, 4);
|
|
101
|
+
end.writeUInt16LE(0, 6);
|
|
102
|
+
end.writeUInt16LE(entries.length, 8);
|
|
103
|
+
end.writeUInt16LE(entries.length, 10);
|
|
104
|
+
end.writeUInt32LE(offset - centralStart, 12);
|
|
105
|
+
end.writeUInt32LE(centralStart, 16);
|
|
106
|
+
end.writeUInt16LE(0, 20);
|
|
107
|
+
push(end);
|
|
108
|
+
await new Promise((resolve, reject) => {
|
|
109
|
+
const stream = createWriteStream(outPath);
|
|
110
|
+
stream.on("error", reject);
|
|
111
|
+
stream.on("finish", () => resolve());
|
|
112
|
+
for (const chunk of chunks)
|
|
113
|
+
stream.write(chunk);
|
|
114
|
+
stream.end();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/** 解包对账用(测试断言用;不带压缩支持,只认 store)。 */
|
|
118
|
+
export function listZipEntries(buffer) {
|
|
119
|
+
const names = [];
|
|
120
|
+
// 找 EOCD(从尾部扫描)
|
|
121
|
+
let eocd = -1;
|
|
122
|
+
for (let i = buffer.length - 22; i >= 0; i -= 1) {
|
|
123
|
+
if (buffer.readUInt32LE(i) === 0x06054b50) {
|
|
124
|
+
eocd = i;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (eocd < 0)
|
|
129
|
+
return names;
|
|
130
|
+
const count = buffer.readUInt16LE(eocd + 10);
|
|
131
|
+
let pointer = buffer.readUInt32LE(eocd + 16);
|
|
132
|
+
for (let n = 0; n < count; n += 1) {
|
|
133
|
+
if (buffer.readUInt32LE(pointer) !== 0x02014b50)
|
|
134
|
+
break;
|
|
135
|
+
const nameLength = buffer.readUInt16LE(pointer + 28);
|
|
136
|
+
names.push(buffer.toString("utf8", pointer + 46, pointer + 46 + nameLength));
|
|
137
|
+
const extra = buffer.readUInt16LE(pointer + 30);
|
|
138
|
+
const comment = buffer.readUInt16LE(pointer + 32);
|
|
139
|
+
pointer += 46 + nameLength + extra + comment;
|
|
140
|
+
}
|
|
141
|
+
return names;
|
|
142
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import { registerIocApply } from "./tools/ioc-apply.js";
|
|
|
28
28
|
import { registerIocInspect } from "./tools/ioc-inspect.js";
|
|
29
29
|
import { registerIocPlan } from "./tools/ioc-plan.js";
|
|
30
30
|
import { registerProjectCreate } from "./tools/project-create.js";
|
|
31
|
+
import { registerProjectExport } from "./tools/project-export.js";
|
|
31
32
|
import { registerRegsRead } from "./tools/regs.js";
|
|
32
33
|
import { registerRoutePlan } from "./tools/route-plan.js";
|
|
33
34
|
import { registerWebRoutes } from "./engine/web-routes.js";
|
|
@@ -64,6 +65,7 @@ export function apply(ctx) {
|
|
|
64
65
|
registerEnvInstall(ctx);
|
|
65
66
|
registerFirmwareInstall(ctx);
|
|
66
67
|
registerProjectCreate(ctx);
|
|
68
|
+
registerProjectExport(ctx);
|
|
67
69
|
registerCodePatch(ctx);
|
|
68
70
|
registerGenerate(ctx);
|
|
69
71
|
registerIocInspect(ctx);
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* 但凡是能注册的服务就不要依赖路径约定(规格 §9)。
|
|
6
6
|
*/
|
|
7
7
|
export const STM32_SKILL_NAME = "stm32-workflow";
|
|
8
|
-
const DESCRIPTION = "
|
|
9
|
-
export const SKILL_CONTENT = "# STM32 工作流(dsh-stm32)\n\n
|
|
8
|
+
const DESCRIPTION = "STM32F1 全系(F100/101/102/103/105/107)+ dsh-stm32 的标准工作流与红线:只改 USER CODE、不许动 CubeMX 生成区、.ioc 走事务、烧录与调试分开、Serial Wire 不能关";
|
|
9
|
+
export const SKILL_CONTENT = "# STM32 工作流(dsh-stm32)\n\n适用:**STM32F1 全系**(F100/101/102/103/105/107)+ 本工程按型号生成的 CMake 工程。本机的验证板是蓝丸 STM32F103C8T6(64 KiB Flash / 20 KiB RAM,CMSIS-DAP 探针);换型号不是\"换块板子\",是一起换启动文件、设备宏与链接脚本——`stm32_project_create` / `stm32_generate` 按型号渲染(STM32F103RCT6 → `startup_stm32f103xe.s` + `STM32F103xE` + `STM32F103RCTx_FLASH.ld`),别手工替换这三样。\n\n## 〇、先定路线(每个新功能都先做这一步)\n\n动手写代码前先调 `stm32_route_plan`(`requirement` 一句话,可再给 `peripherals`):它按这台芯片与这句需求逐外设给出 HAL 与寄存器两版的代价/风险,并判出 `register-ok` / `both-viable` / `hal-only` / `unclear`。\n\n- `register-ok`:不建 `.ioc`,走寄存器版(启动文件与设备头从缓存目录取,不预置进插件)。\n- 要 HAL:先看工具链与固件包就位没有。`gcc` / `cmake` / `ninja` / `openocd` / `gdb` 可以让 `stm32_env_install` 代装;**CubeMX 与固件包插件不代装**(GUI 向导 + 要 ST 账号),照 `guides[].steps` 自己做,离线机器可以整目录拷过来。\n- `unclear`:别猜,先把需求说具体(外设、数据量、要不要中断/DMA),再重调一次。\n\n## 一、硬规范(红线)\n\n**R1 代码只准写在 USER CODE 块之间。**\n`/* USER CODE BEGIN X */` 与 `/* USER CODE END X */` 之间是 CubeMX 承诺\"重新生成时原样保留\"的唯一区域。\n想加的功能(GPIO 操作、状态机、自己的 .c 逻辑)全部写在块内。块外的任何一行都不属于你。\n\n**R2 不许改 CubeMX 生成区。** 具体包括(不是举例,是清单):\n- `Core/Src/main.c` 的 `main()`、`SystemClock_Config()`、`MX_GPIO_Init()` 等 `MX_*_Init()` 函数体;\n- `Core/Inc/main.h` 里 `/* Private defines */` 段的 `_Pin`/`_GPIO_Port` 宏;\n- `Core/Src/stm32f1xx_hal_msp.c`、`Core/Src/stm32f1xx_it.c`(中断服务函数内部若要加内容,只能加在它自带的 USER CODE 块里);\n- `Core/Inc/stm32f1xx_hal_conf.h`、`Core/Src/system_stm32f1xx.c`、本型号的启动文件(C8T6 是 `startup_stm32f103xb.s`,RCT6 是 `startup_stm32f103xe.s`,F107 是 `startup_stm32f107xc.s`)、本型号的链接脚本 `<DeviceId>_FLASH.ld`(`STM32F103C8Tx_FLASH.ld`、`STM32F103RCTx_FLASH.ld`、`STM32F107VCTx_FLASH.ld`;模板里的旧名 `STM32F103XX_FLASH.ld` 只是 C8T6 兼容副本);\n- `CMakeLists.txt`、`cmake/stm32cubemx/CMakeLists.txt`、`.mxproject`、`demo.ioc`(.ioc 只能通过事务改,见 R3)。\n理由:这些文件由 CubeMX 按 .ioc 重写;手改会被无声覆盖,而且覆盖后编译不报错(源清单写死在 `cmake/stm32cubemx/CMakeLists.txt:21-28`),故障会以\"改了不生效\"的形式出现。\n\n**R3 配置只准走 .ioc 事务:`stm32_ioc_plan` → 用户确认 → `stm32_ioc_apply`。**\n不要手改 .ioc 文本,不要开 CubeMX GUI,不要\"顺手\"改引脚/时钟。改时钟走同一条事务:`stm32_ioc_plan` 的 `clock: { targetHz }` 枚举可行的 PLL 组合、取最接近目标的那一档(不可达时显式标注「不可达,已选 X MHz(±Y)」),并把 `PLLMUL` / `SYSCLKFreq_VALUE` / `HCLKFre` 这些派生键全量重算;超限(SYSCLK > 72 MHz、APB1 > 36 MHz、ADC > 14 MHz 等)时它一个键都不写。apply 必须带 `planId`(`stm32_ioc_plan` 返回的那个计划 ID,参数名就是 planId),源文件 SHA-256 变了就拒绝。\n\n**R4 不许生成 `No_Debug` 的 SYS 配置。** 那会生成 `__HAL_AFIO_REMAP_SWJ_DISABLE()`,烧进去以后固件一上电就关掉 PA13/PA14 的 SWD 功能,**调试口永久失联**,只能靠 BOOT0 进系统 bootloader 抢救。\n.ioc 里必须有 `PA13.Signal=SYS_JTMS-SWDIO`、`PA14.Signal=SYS_JTCK-SWCLK`、`*.Mode=Serial_Wire`。ioc_inspect 报这条警告时必须先修。\n\n**R5 烧录与调试分开。**\n只烧录:`stm32_flash` 或 `/stm32-flash`(openocd `program ... verify reset exit`,烧完就跑,不进调试会话)。\n调试:`stm32_debug_run`(一次性停 main、取 pc/栈后释放)或 `/stm32-debug` + `stm32_debug_eval`(保持会话)。\n永远不要用\"能不能断下来\"来判断烧录成功——烧录成功的证据是 `** Programming Finished **` 与 `** Verified OK **`。\n\n**R6 探针只有一块。** build / flash / generate 之前先让出调试会话;我输出结束(turn/end)时插件会自动关会话,所以别在输出中途把探针攥着不说。\n\n**R7 先 build 再 flash。** 改完代码不编译就烧,烧的是旧 elf;烧完看现象会得出错误结论。\n\n**R8 结论必须带证据。** 说\"闪了\"要给寄存器/计数器证据(例如 `GPIOC_ODR` 在 0x2000/0x0000 之间翻转、`g_led_blink_count` 单调增长),不要只说\"看起来好了\"。\n\n## 二、标准流程\n\n1. `stm32_env_probe` —— 工具链齐不齐(gcc/gdb/openocd/cmake/ninja + CubeMX)。缺哪个就照它给的 `install` 装(第一条最省事,带可粘贴的 pacman / winget 命令),装完按 `verify` 自检;它还会问要不要我代装。CubeMX 是可选的:只影响建工程与重新生成,不影响编译/烧录/调试。\n2. `stm32_ioc_inspect` —— 看清引脚/时钟/调试口;有 `warnings` 先处理(尤其 Serial Wire)。\n3. 需要改引脚/时钟:`stm32_ioc_plan` → 给用户看 diff → `stm32_ioc_apply`(自动备份到 `.dsh-stm32/backups/<时间戳>-ioc/`)。时钟给 `clock: { targetHz }`(要钉死倍频就 `clock: { pllMul }`),报告里带逐外设频率与 UART 波特率误差。\n4. `stm32_generate` —— 无头重新生成(3–4 分钟,走 job)。产物 `Src/`/`Inc/` 会被搬到 `Core/Src`/`Core/Inc`,USER CODE 按标签保活,生成用的临时目录会被删掉。\n5. 写代码:**只在 USER CODE 块内**。\n6. `stm32_build`(clean 才有内存占用表)→ 0 错再往下。\n7. `stm32_flash` —— 只烧录;或 `stm32_debug_run` —— 烧录并停在 main。\n8. 验证:`stm32_regs_read`(读 GPIOC_ODR / AFIO_MAPR / 全局变量)或 `stm32_debug_eval`。\n\n## 三、命令对照\n\n| 想要 | 工具 | 斜杠命令 |\n|---|---|---|\n| 只烧录 | `stm32_flash` | `/stm32-flash` |\n| 烧录 + 调试 | `stm32_debug_run`(一次性)/ `stm32_debug_session`(保持) | `/stm32-debug` |\n| 编译 | `stm32_build` | `/stm32-build` |\n| 体检 | `stm32_doctor` | `/stm32-doctor` |\n| 释放探针 | `stm32_debug_stop` | `/stm32-stop` |\n\n## 四、踩过的坑(本机实测,别重复)\n\n1. **CubeMX 的\"假成功\"**:直接 `STM32CubeMX.exe -q script` 会 detach 出 javaw 并立刻 exit 0,stdout 是空的。必须 `jre\\bin\\java.exe -jar STM32CubeMX.exe -q <脚本>` 并重定向日志,成功标志是日志里的 `Code succesfully generated under`(CubeMX 自己把 successfully 拼错了)。启动要 90–150 s。\n2. **CLI 生成忽略工程设置**:.ioc 写着 `MainLocation=Core/Src`、`CoupleFile=false`,CLI 依然固定写 `Src/`/`Inc/` 并按外设拆文件。所以必须落地映射 + USER CODE 合并,否则核心代码会被搬丢。\n3. **USER CODE 按标签保活**:同标签用旧正文覆盖新正文;旧有新无的标签直接丢;标签不配对则那对放弃。\n4. **`mdw` 输出是 8 位裸 hex**:`0x4001100c: 00000000`;`[reg pc]` 返回 `pc (/32): 0x...`,要 `lindex 1`。\n5. **openocd 的日志走 stderr**:等 \"Listening on port\" 必须同时 drain stdout 和 stderr,否则永远等不到。\n6. **读寄存器的收尾用 `resume` 不要 `reset run`**:后者会复位芯片,计数器归零,看起来像\"变量丢了\"。\n7. **符号地址会随重链接漂移**:`g_led_blink_count` 曾在 `0x20000028`,改一行后变成 `0x2000002C`,老地址变成 `uwTick`。读符号前用 `nm` 确认,或直接读物理地址。\n8. **装插件用一条命令**:`dsh plugin --profile desktop add github:JingYu-er/dsh-stm32#main`(`lib/` 已入库,pnpm 不替 git 依赖构建也能直接用)。只有要改插件源码时才用 `link:`,且升级前先删掉 `node_modules/dsh-stm32` 这个 junction,否则 pnpm 覆盖时报 `EPERM`。\n9. **宿主输出契约**:工具返回的对象在 `additionalProperties: false` 下不能有多余键;声明为 number/object 的字段不接受 `null`(用可选字段 + 条件展开)。\n10. **ninja / piped stdio 在受限沙箱里会挂或 EPERM**:那是工具侧沙箱,不是工程问题;插件里经 `ctx.subprocess` 起的进程不受会话文件沙箱限制。\n11. **裸 `cmake -S . -B build` 会丢工具链文件**:交叉编译器只由 `CMakePresets.json` 注入(`cmake/gcc-arm-none-eabi.cmake`),不读 preset 就退回主机编译器,报一串找不到 `arm-none-eabi-gcc`。要么 `cmake --preset default`,要么显式 `-DCMAKE_TOOLCHAIN_FILE=cmake/gcc-arm-none-eabi.cmake`。\n12. **换芯片别留下旧链接脚本名**:`-T` 必须指向按型号渲染的 `<DeviceId>_FLASH.ld`(工具里的 `pointLinkerFlagAt` 会把工具链中的旧名改指到新名,手改反而绕开它);留着 `STM32F103XX_FLASH.ld` 烧到 256 KiB 的 RCT6 上就是「编译过、跑飞」。\n\n## 五、自检清单(每次交付前)\n\n- [ ] 改动全部落在 USER CODE 块内(`git diff` 里没有生成区的行)\n- [ ] .ioc 改动走过 plan → apply(有备份)\n- [ ] Serial Wire 仍在(`stm32_ioc_inspect` 的 `warnings` 为空)\n- [ ] `stm32_build` 0 error、0 unexpected warning\n- [ ] 烧录有 `** Verified OK **`\n- [ ] 行为验证有寄存器/计数器证据\n- [ ] 探针已释放(`stm32_doctor` 无残留进程)\n- [ ] 芯片型号 / 启动文件 / 设备宏 / `.ld` 四者配套(`stm32_doctor` 的芯片一致性检查无 blocker)";
|
|
10
10
|
/** 注册 skill;宿主没组合 skills 服务时直接返回。 */
|
|
11
11
|
export function registerStm32Skill(ctx) {
|
|
12
12
|
const skills = ctx.get("skills");
|
|
@@ -15,7 +15,7 @@ export function registerStm32Skill(ctx) {
|
|
|
15
15
|
skills.register({
|
|
16
16
|
name: STM32_SKILL_NAME,
|
|
17
17
|
description: DESCRIPTION,
|
|
18
|
-
whenToUse: "给
|
|
18
|
+
whenToUse: "给 STM32F1 工程(F100/101/102/103/105/107,本机验证板 F103C8T6)写代码、改引脚/时钟、重新生成、编译、烧录或调试时",
|
|
19
19
|
content: SKILL_CONTENT,
|
|
20
20
|
// 宿主读取 skill 时要求 source / provider 是字符串(register 本身不校验,
|
|
21
21
|
// 但 ctx.skills.get() 会用 validateDefinition 再验一次)。缺 source 的表现是
|
package/lib/tools/build.js
CHANGED
|
@@ -43,6 +43,10 @@ const reportSchema = {
|
|
|
43
43
|
errors: { type: "array", items: { type: "string" } },
|
|
44
44
|
warningCount: { type: "number" },
|
|
45
45
|
memoryUsage: { type: "array", items: memorySchema },
|
|
46
|
+
converted: {
|
|
47
|
+
type: "array",
|
|
48
|
+
items: { type: "object", properties: { kind: { type: "string" }, path: { type: "string" }, size: { type: "number" } }, required: ["kind", "path", "size"], additionalProperties: false },
|
|
49
|
+
},
|
|
46
50
|
elf: elfSchema,
|
|
47
51
|
message: { type: "string" },
|
|
48
52
|
logTail: { type: "string" },
|
|
@@ -110,6 +114,12 @@ export function registerBuild(ctx) {
|
|
|
110
114
|
buildDir: { type: "string", description: "显式构建目录;缺省自动推断(优先含 Debug 的 preset 目录)。" },
|
|
111
115
|
target: { type: "string", description: "只构建某个 CMake target(例如 demo.elf)。" },
|
|
112
116
|
clean: { type: "boolean", description: "先 clean 再编译(全量,慢)。" },
|
|
117
|
+
artifacts: {
|
|
118
|
+
type: "array",
|
|
119
|
+
items: { type: "string", enum: ["elf", "hex", "bin"] },
|
|
120
|
+
description: "编译成功后额外转出的产物格式(默认只有 elf):hex 给产线烧录器 / CubeProgrammer,bin 给串口 ISP 与 OTA 镜像。转换失败不判编译失败,只写 warning。",
|
|
121
|
+
additionalProperties: false,
|
|
122
|
+
},
|
|
113
123
|
foreground: { type: "boolean", description: "同步返回完整报告(默认 false,走后台 job)。" },
|
|
114
124
|
binDirs: { type: "array", items: { type: "string" }, description: "工具链目录候选,优先于默认值/环境变量。" },
|
|
115
125
|
envCheck: ENV_CHECK_SCHEMA,
|
|
@@ -158,6 +168,7 @@ export function registerBuild(ctx) {
|
|
|
158
168
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
159
169
|
...(args.target === undefined ? {} : { target: args.target }),
|
|
160
170
|
...(args.clean === undefined ? {} : { clean: args.clean }),
|
|
171
|
+
...(args.artifacts === undefined ? {} : { artifacts: args.artifacts }),
|
|
161
172
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
162
173
|
...(args.timeoutMs === undefined ? {} : { timeoutMs: args.timeoutMs }),
|
|
163
174
|
signal: exec.signal,
|
|
@@ -187,6 +198,7 @@ export function registerBuild(ctx) {
|
|
|
187
198
|
...(args.buildDir === undefined ? {} : { buildDir: args.buildDir }),
|
|
188
199
|
...(args.target === undefined ? {} : { target: args.target }),
|
|
189
200
|
...(args.clean === undefined ? {} : { clean: args.clean }),
|
|
201
|
+
...(args.artifacts === undefined ? {} : { artifacts: args.artifacts }),
|
|
190
202
|
...(args.binDirs === undefined ? {} : { binDirs: args.binDirs }),
|
|
191
203
|
...(args.timeoutMs === undefined ? {} : { timeoutMs: args.timeoutMs }),
|
|
192
204
|
signal: controller.signal,
|
package/lib/tools/cache.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 用户要能自己看见"下到哪了、对不对、占多大、能不能清",所以这四个动作必须是一等公民,
|
|
6
6
|
* 而不是藏在 error 信息里。
|
|
7
7
|
*/
|
|
8
|
-
import { cacheRootInfo, checkCacheEntry, describeCacheBytes, readCacheIndex, removeCacheEntry, } from "../engine/cache.js";
|
|
8
|
+
import { cacheRootInfo, checkCacheEntry, describeCacheBytes, pruneLegacyEntries, readCacheIndex, removeCacheEntry, resolveCacheEntry, } from "../engine/cache.js";
|
|
9
9
|
import { ensureCmsisTemplate } from "../engine/register-template.js";
|
|
10
10
|
export const CACHE_TOOL = "stm32_cache";
|
|
11
11
|
const entrySchema = {
|
|
@@ -72,8 +72,8 @@ export function registerCache(ctx) {
|
|
|
72
72
|
type: "object",
|
|
73
73
|
additionalProperties: false,
|
|
74
74
|
properties: {
|
|
75
|
-
action: { type: "string", enum: ["list", "verify", "fetch", "purge"], description: "list(默认)/ verify / fetch / purge" },
|
|
76
|
-
id: { type: "string", description: "条目 id,例如 cmsis:F1:
|
|
75
|
+
action: { type: "string", enum: ["list", "verify", "fetch", "gc", "purge"], description: "list(默认)/ verify / fetch / gc(清掉已被新 id 取代的旧条目)/ purge" },
|
|
76
|
+
id: { type: "string", description: "条目 id,例如 cmsis:F1:103xb(purge/verify 用);也认 cmsis:F1:startup_stm32f103xb、F1:B、STM32F103C8T6 这些写法。" },
|
|
77
77
|
mcu: { type: "string", description: "芯片型号,例如 STM32F103C8T6(fetch 用)。" },
|
|
78
78
|
family: { type: "string", description: "只给族时写这里,例如 F1(fetch 用;按中容量档处理)。" },
|
|
79
79
|
confirm: { type: "boolean", description: "purge 且不给 id 时(清空整个缓存)必须为 true;只删一条用 id 即可。" },
|
|
@@ -115,7 +115,10 @@ export function registerCache(ctx) {
|
|
|
115
115
|
if (action === "list" || action === "verify") {
|
|
116
116
|
const entries = listEntries(index, action === "verify");
|
|
117
117
|
if (args.id !== undefined) {
|
|
118
|
-
|
|
118
|
+
// 按"用户可能怎么写"解析(cmsis:F1:103xb / 0.1 的 startup_stm32f103xb / F1:B / 型号)。
|
|
119
|
+
const target = resolveCacheEntry(index, args.id);
|
|
120
|
+
const kept = target === null ? [] : entries.filter((entry) => entry.id === target.id);
|
|
121
|
+
const hint = target === null ? "" : target.id === args.id ? "" : "(已按别名解析成 " + target.id + ")";
|
|
119
122
|
return {
|
|
120
123
|
action,
|
|
121
124
|
ok: error === null,
|
|
@@ -124,7 +127,7 @@ export function registerCache(ctx) {
|
|
|
124
127
|
origin: info.origin,
|
|
125
128
|
totalBytes: kept.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
126
129
|
entries: kept,
|
|
127
|
-
message: (error ?? "") + (kept.length === 0 ? "缓存里没有条目 " + args.id + "。" : "缓存里有 " + String(kept.length) + "
|
|
130
|
+
message: (error ?? "") + (kept.length === 0 ? "缓存里没有条目 " + args.id + "。" : "缓存里有 " + String(kept.length) + " 个条目" + hint + "。"),
|
|
128
131
|
};
|
|
129
132
|
}
|
|
130
133
|
return {
|
|
@@ -161,13 +164,39 @@ export function registerCache(ctx) {
|
|
|
161
164
|
message: result.message,
|
|
162
165
|
};
|
|
163
166
|
}
|
|
167
|
+
if (action === "gc") {
|
|
168
|
+
const pruned = pruneLegacyEntries(process.env);
|
|
169
|
+
const after = readCacheIndex(process.env).index;
|
|
170
|
+
const entries = listEntries(after, false);
|
|
171
|
+
const parts = [];
|
|
172
|
+
if (pruned.removed.length === 0) {
|
|
173
|
+
parts.push(pruned.kept.length === 0 ? "没有需要清理的旧条目" : "旧条目还被用着(新 id 不存在),保留 " + String(pruned.kept.length) + " 个");
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
parts.push("清掉 " + String(pruned.removed.length) + " 个旧条目(" + describeCacheBytes(pruned.reclaimedBytes) + "):" + pruned.removed.join("、"));
|
|
177
|
+
}
|
|
178
|
+
if (pruned.kept.length > 0 && pruned.removed.length > 0)
|
|
179
|
+
parts.push("保留 " + String(pruned.kept.length) + " 个仍在用的旧条目");
|
|
180
|
+
return {
|
|
181
|
+
action,
|
|
182
|
+
ok: true,
|
|
183
|
+
cacheRoot: info.root,
|
|
184
|
+
indexPath: info.indexPath,
|
|
185
|
+
origin: info.origin,
|
|
186
|
+
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
187
|
+
entries,
|
|
188
|
+
message: parts.join(";") + "。",
|
|
189
|
+
};
|
|
190
|
+
}
|
|
164
191
|
if (action !== "purge") {
|
|
165
192
|
// 以前这里直接掉进 purge 分支:action 拼错一个字母(或模型传了不认识的值)就等于清空缓存(A8)。
|
|
166
|
-
return empty("不认识的 action:" + action + "(只有 list / verify / fetch / purge)。什么都没动。");
|
|
193
|
+
return empty("不认识的 action:" + action + "(只有 list / verify / fetch / gc / purge)。什么都没动。");
|
|
167
194
|
}
|
|
168
195
|
// purge
|
|
169
196
|
if (args.id !== undefined) {
|
|
170
|
-
|
|
197
|
+
// 同样先解析别名:0.1 的 id 写法也得删得掉,否则用户以为删了、下次还是旧文件。
|
|
198
|
+
const target = resolveCacheEntry(index, args.id);
|
|
199
|
+
const removed = target === null ? { removed: false, indexPath: info.indexPath } : removeCacheEntry(target.id, process.env);
|
|
171
200
|
const after = readCacheIndex(process.env).index;
|
|
172
201
|
const entries = listEntries(after, false);
|
|
173
202
|
return {
|
|
@@ -178,7 +207,9 @@ export function registerCache(ctx) {
|
|
|
178
207
|
origin: info.origin,
|
|
179
208
|
totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),
|
|
180
209
|
entries,
|
|
181
|
-
message: removed.removed
|
|
210
|
+
message: removed.removed
|
|
211
|
+
? "已删掉缓存条目 " + (target === null ? args.id : target.id) + "(目录也清了)。"
|
|
212
|
+
: "缓存里没有条目 " + args.id + ",什么都没删。",
|
|
182
213
|
};
|
|
183
214
|
}
|
|
184
215
|
const all = readCacheIndex(process.env).index.entries;
|
package/lib/tools/debug.js
CHANGED
|
@@ -35,6 +35,11 @@ const reportSchema = {
|
|
|
35
35
|
gdbCommand: { type: "string" },
|
|
36
36
|
gdbOutput: { type: "string" },
|
|
37
37
|
openOcdLogTail: { type: "string" },
|
|
38
|
+
// F2:失败时带结构化错误码(与 stm32_flash 同一套枚举)。可选字段,成功路径不出现。
|
|
39
|
+
errorCode: {
|
|
40
|
+
type: "string",
|
|
41
|
+
enum: ["", "no-probe", "probe-busy", "swd-locked", "flash-protected", "no-power", "target-mismatch", "unknown"],
|
|
42
|
+
},
|
|
38
43
|
durationMs: { type: "number" },
|
|
39
44
|
errors: { type: "array", items: { type: "string" } },
|
|
40
45
|
message: { type: "string" },
|
|
@@ -62,6 +67,8 @@ export function formatDebugReport(report) {
|
|
|
62
67
|
lines.push((report.ok ? "调试快照完成" : "调试失败") + ":" + report.message);
|
|
63
68
|
if (report.elfPath !== "")
|
|
64
69
|
lines.push("固件:" + report.elfPath);
|
|
70
|
+
if (report.errorCode !== undefined && report.errorCode !== "")
|
|
71
|
+
lines.push("错误码:" + report.errorCode);
|
|
65
72
|
if (report.port !== 0)
|
|
66
73
|
lines.push("端口:" + String(report.port) + ",断点:" + report.breakAt);
|
|
67
74
|
if (report.location !== "")
|
package/lib/tools/doctor.js
CHANGED
|
@@ -62,6 +62,14 @@ export const outputSchema = {
|
|
|
62
62
|
missingRequired: { type: "array", items: { type: "string" } },
|
|
63
63
|
tools: { type: "array", items: toolSchema },
|
|
64
64
|
processes: { type: "array", items: processSchema },
|
|
65
|
+
usb: {
|
|
66
|
+
type: "object",
|
|
67
|
+
properties: {
|
|
68
|
+
kind: { type: "string", enum: ["no-usb", "no-dap", "driver-error", "ok", "unknown"] },
|
|
69
|
+
detail: { type: "string" },
|
|
70
|
+
},
|
|
71
|
+
required: ["kind", "detail"],
|
|
72
|
+
},
|
|
65
73
|
port: { type: "number" },
|
|
66
74
|
portBusy: { type: "boolean" },
|
|
67
75
|
buildDir: { type: "string" },
|
|
@@ -74,7 +82,7 @@ export const outputSchema = {
|
|
|
74
82
|
},
|
|
75
83
|
required: [
|
|
76
84
|
"ok", "projectDir", "message", "durationMs", "missingRequired", "tools", "processes",
|
|
77
|
-
"port", "portBusy", "buildDir", "elfPath", "elfSize", "iocPath", "iocWarnings", "blockers", "notes",
|
|
85
|
+
"port", "portBusy", "buildDir", "elfPath", "elfSize", "iocPath", "iocWarnings", "usb", "blockers", "notes",
|
|
78
86
|
],
|
|
79
87
|
},
|
|
80
88
|
},
|
|
@@ -92,6 +100,7 @@ function formatReport(report) {
|
|
|
92
100
|
lines.push("可疑进程:" + report.processes.map((process) => process.name + "(" + String(process.pid) + ")").join(" "));
|
|
93
101
|
}
|
|
94
102
|
lines.push("gdb 端口:" + String(report.port) + (report.portBusy ? "(被占用)" : "(空闲)"));
|
|
103
|
+
lines.push("USB:" + report.usb.kind + " — " + report.usb.detail);
|
|
95
104
|
if (report.buildDir !== "")
|
|
96
105
|
lines.push("构建目录:" + report.buildDir);
|
|
97
106
|
if (report.elfPath !== "")
|
|
@@ -141,7 +150,7 @@ export function registerDoctor(ctx) {
|
|
|
141
150
|
message: "宿主缺少 subprocess 服务,无法体检",
|
|
142
151
|
report: {
|
|
143
152
|
ok: false, projectDir: args.projectDir ?? "", message: "宿主缺少 subprocess 服务", durationMs: 0,
|
|
144
|
-
missingRequired: ["subprocess"], tools: [], processes: [], port: args.port ?? 3333, portBusy: false,
|
|
153
|
+
missingRequired: ["subprocess"], tools: [], processes: [], usb: { kind: "unknown", detail: "宿主没有 subprocess 服务,无法诊断 USB/探针形态。" }, port: args.port ?? 3333, portBusy: false,
|
|
145
154
|
buildDir: "", elfPath: "", elfSize: 0, iocPath: "", iocWarnings: [], blockers: ["宿主缺少 subprocess 服务"], notes: [],
|
|
146
155
|
},
|
|
147
156
|
};
|