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/fetch.js
CHANGED
|
@@ -8,10 +8,130 @@
|
|
|
8
8
|
*
|
|
9
9
|
* 只用全局 fetch(Node 18+ 自带),不引运行时依赖;测试可注入 fetchImpl。
|
|
10
10
|
*/
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
11
12
|
import fs from "node:fs";
|
|
12
13
|
import path from "node:path";
|
|
13
14
|
const HTML_HEAD_BYTES = 512;
|
|
15
|
+
/** F3c:10 s 内平均不足 1 KB/s 就 abort。半截文件留在 .part 里,下一秒从断点续。 */
|
|
16
|
+
const DEFAULT_STALL_MS = 10_000;
|
|
17
|
+
const DEFAULT_MIN_BYTES_PER_SEC = 1024;
|
|
14
18
|
let partSeq = 0;
|
|
19
|
+
/** 单个 .part 文件的大小;没有就是 0。 */
|
|
20
|
+
function partSize(file) {
|
|
21
|
+
try {
|
|
22
|
+
return fs.statSync(file).size;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 下一次文件到 partPath(F3c):支持从 startAt 字节续传。
|
|
30
|
+
*
|
|
31
|
+
* 三个必须的边界:
|
|
32
|
+
* 1. 服务器回 200 而不是 206 = 它忽略了 Range,必须**从头写**(截断),不能接在旧字节后面;
|
|
33
|
+
* 2. 10 s 观察窗口内平均速率不足 1 KB/s 就 abort(半截文件留着,交给上层续传或换源);
|
|
34
|
+
* 3. 外层 signal 取消要能立刻停(不能让一个 30 s 的下载拖住取消)。
|
|
35
|
+
*/
|
|
36
|
+
async function streamOnce(url, partPath, startAt, fetchImpl, options) {
|
|
37
|
+
const stallMs = options.stallMs ?? DEFAULT_STALL_MS;
|
|
38
|
+
const minRate = options.minBytesPerSec ?? DEFAULT_MIN_BYTES_PER_SEC;
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const overall = setTimeout(() => controller.abort(), options.timeoutMs ?? 30_000);
|
|
41
|
+
const onAbort = () => controller.abort();
|
|
42
|
+
if (options.signal !== undefined) {
|
|
43
|
+
if (options.signal.aborted)
|
|
44
|
+
controller.abort();
|
|
45
|
+
else
|
|
46
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
47
|
+
}
|
|
48
|
+
let slow = false;
|
|
49
|
+
let lastProgress = Date.now();
|
|
50
|
+
let windowStart = Date.now();
|
|
51
|
+
let windowBytes = 0;
|
|
52
|
+
const watchdog = setInterval(() => {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
if (now - lastProgress > stallMs) {
|
|
55
|
+
slow = true;
|
|
56
|
+
controller.abort();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const elapsed = now - windowStart;
|
|
60
|
+
if (elapsed >= stallMs) {
|
|
61
|
+
if ((windowBytes * 1000) / elapsed < minRate) {
|
|
62
|
+
slow = true;
|
|
63
|
+
controller.abort();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
windowStart = now;
|
|
67
|
+
windowBytes = 0;
|
|
68
|
+
}
|
|
69
|
+
}, Math.min(1000, Math.max(100, Math.floor(stallMs / 4))));
|
|
70
|
+
try {
|
|
71
|
+
const headers = {};
|
|
72
|
+
if (startAt > 0)
|
|
73
|
+
headers["Range"] = "bytes=" + String(startAt) + "-";
|
|
74
|
+
const response = await fetchImpl(url, { headers, signal: controller.signal, redirect: "follow" });
|
|
75
|
+
if (startAt > 0 && response.status === 416) {
|
|
76
|
+
return { ok: false, bytes: startAt, reason: "服务器说 Range 越界(416)", slow: false };
|
|
77
|
+
}
|
|
78
|
+
if (!response.ok && response.status !== 206) {
|
|
79
|
+
return { ok: false, bytes: 0, reason: "HTTP " + String(response.status), slow: false };
|
|
80
|
+
}
|
|
81
|
+
// 只有 206 才是"接着上次的字节";200 说明它整份重发,必须截断重写。
|
|
82
|
+
const append = startAt > 0 && response.status === 206;
|
|
83
|
+
const body = response.body;
|
|
84
|
+
if (body === null)
|
|
85
|
+
return { ok: false, bytes: 0, reason: "响应没有 body", slow: false };
|
|
86
|
+
const reader = body.getReader();
|
|
87
|
+
const fd = fs.openSync(partPath, append ? "a" : "w");
|
|
88
|
+
let written = append ? startAt : 0;
|
|
89
|
+
try {
|
|
90
|
+
for (;;) {
|
|
91
|
+
const chunk = await reader.read();
|
|
92
|
+
if (chunk.done)
|
|
93
|
+
break;
|
|
94
|
+
const value = chunk.value;
|
|
95
|
+
if (value === undefined)
|
|
96
|
+
continue;
|
|
97
|
+
fs.writeSync(fd, value);
|
|
98
|
+
written += value.byteLength;
|
|
99
|
+
windowBytes += value.byteLength;
|
|
100
|
+
lastProgress = Date.now();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
fs.closeSync(fd);
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, bytes: written, reason: "", slow: false };
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
const name = error instanceof Error ? error.name : "";
|
|
110
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
111
|
+
if (slow) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
bytes: partSize(partPath),
|
|
115
|
+
reason: "低速/停滞(" + String(Math.round(stallMs / 1000)) + " s 内平均不足 " + String(minRate) + " B/s)",
|
|
116
|
+
slow: true,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const timedOut = name === "AbortError";
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
bytes: partSize(partPath),
|
|
123
|
+
reason: timedOut
|
|
124
|
+
? "超时/取消(" + String(options.timeoutMs ?? 30_000) + " ms)"
|
|
125
|
+
: message,
|
|
126
|
+
slow: false,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(overall);
|
|
131
|
+
clearInterval(watchdog);
|
|
132
|
+
options.signal?.removeEventListener?.("abort", onAbort);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
15
135
|
function looksLikeHtml(buffer) {
|
|
16
136
|
const head = buffer.subarray(0, HTML_HEAD_BYTES).toString("utf8").trimStart().toLowerCase();
|
|
17
137
|
return head.startsWith("<!doctype html") || head.startsWith("<html");
|
|
@@ -31,48 +151,76 @@ function removeQuietly(file) {
|
|
|
31
151
|
// 临时文件删不掉不影响这次的结论。
|
|
32
152
|
}
|
|
33
153
|
}
|
|
34
|
-
/**
|
|
154
|
+
/**
|
|
155
|
+
* 下载单个文件(多候选,F3c:同一源内断点续传)。失败不抛,把原因写进 outcome
|
|
156
|
+
* —— 上层要一次性报告所有失败。
|
|
157
|
+
*
|
|
158
|
+
* 为什么"同一个源内续传、换源必须从零":同一条线路上的 Range 续的是同一份文件;
|
|
159
|
+
* 换到另一个源时,镜像与上游的字节不保证是同一份(同步时点不同),接起来必然是坏文件。
|
|
160
|
+
*/
|
|
35
161
|
export async function fetchOne(target, destRoot, options = {}) {
|
|
36
162
|
const attempts = [];
|
|
37
163
|
const minBytes = target.minBytes ?? 16;
|
|
38
164
|
const dest = path.join(destRoot, target.path);
|
|
39
165
|
const fetchImpl = options.fetchImpl ?? fetch;
|
|
166
|
+
partSeq += 1;
|
|
167
|
+
const tag = String(process.pid) + "-" + String(partSeq);
|
|
168
|
+
const part = dest + ".part-" + tag;
|
|
169
|
+
const old = dest + ".old-" + tag;
|
|
170
|
+
// 取消判定包一层:直接比较 signal.aborted 会被上面的分支narrow 成 false,续传那行的条件就永远不成立。
|
|
171
|
+
const isCancelled = () => options.signal?.aborted === true;
|
|
40
172
|
for (const url of target.urls) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (options.signal !== undefined) {
|
|
45
|
-
if (options.signal.aborted)
|
|
46
|
-
controller.abort();
|
|
47
|
-
else
|
|
48
|
-
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
173
|
+
if (isCancelled()) {
|
|
174
|
+
attempts.push(url + " → 已取消(没再试)");
|
|
175
|
+
break;
|
|
49
176
|
}
|
|
50
177
|
try {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
178
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// 目录建不了会在写盘那一步报出来,这里不抢戏。
|
|
182
|
+
}
|
|
183
|
+
removeQuietly(part);
|
|
184
|
+
let streamed = await streamOnce(url, part, 0, fetchImpl, options);
|
|
185
|
+
// 只有"太慢/停滞"才值得在同一源上续传一次:断点已落在 .part 里,再从那里接着要。
|
|
186
|
+
if (!streamed.ok && streamed.slow && streamed.bytes > 0 && !isCancelled()) {
|
|
187
|
+
attempts.push(url + " → " + streamed.reason + "," + String(streamed.bytes) + " 字节已落盘,续传重试");
|
|
188
|
+
streamed = await streamOnce(url, part, streamed.bytes, fetchImpl, options);
|
|
189
|
+
}
|
|
190
|
+
if (!streamed.ok) {
|
|
191
|
+
attempts.push(url + " → " + streamed.reason);
|
|
192
|
+
removeQuietly(part);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const buffer = fs.readFileSync(part);
|
|
197
|
+
// 校验不过时必须连 .part 一起删(T1):早先只 continue,42 MB 的半成品会留在缓存目录里,
|
|
198
|
+
// 既不参与续传(换源从零)也没人清,几次失败就攒出几百 MB 垃圾。
|
|
199
|
+
const reject = (reason) => {
|
|
200
|
+
attempts.push(url + " → " + reason);
|
|
201
|
+
removeQuietly(part);
|
|
202
|
+
};
|
|
57
203
|
if (buffer.byteLength < minBytes) {
|
|
58
|
-
|
|
204
|
+
reject("只有 " + String(buffer.byteLength) + " 字节(< " + String(minBytes) + ",不像真文件)");
|
|
59
205
|
continue;
|
|
60
206
|
}
|
|
61
207
|
if (looksLikeHtml(buffer)) {
|
|
62
|
-
|
|
208
|
+
reject("拿回来是网页(代理拦截或 404 页),不是文件");
|
|
63
209
|
continue;
|
|
64
210
|
}
|
|
65
211
|
const missed = missingMarkers(buffer, target.markers ?? []);
|
|
66
212
|
if (missed.length > 0) {
|
|
67
|
-
|
|
213
|
+
reject("内容对不上(找不到 " + missed.join(" / ") + "),不是要的那个文件");
|
|
68
214
|
continue;
|
|
69
215
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
216
|
+
if (target.sha256 !== undefined && target.sha256 !== "") {
|
|
217
|
+
const actual = createHash("sha256").update(buffer).digest("hex");
|
|
218
|
+
if (actual !== target.sha256.toLowerCase()) {
|
|
219
|
+
reject("sha256 对不上(期望 " + target.sha256.toLowerCase().slice(0, 16) + "…,实际 " + actual.slice(0, 16) +
|
|
220
|
+
"…)。多半是上游 master 挪动了内容:用真实 URL 重新实测哈希并更新 cmsis-sources。");
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
76
224
|
// 先给旧文件改名、再把新课搬上来(A15):旧写法是「先删旧文件再 rename」,
|
|
77
225
|
// 第二步失败(Windows 上杀软扫描、目录只读、权限刚变)就新旧一起没了。
|
|
78
226
|
let movedOld = false;
|
|
@@ -101,13 +249,9 @@ export async function fetchOne(target, destRoot, options = {}) {
|
|
|
101
249
|
return { path: target.path, ok: true, bytes: buffer.byteLength, url, error: "", attempts };
|
|
102
250
|
}
|
|
103
251
|
catch (error) {
|
|
104
|
-
const name = error instanceof Error ? error.name : "";
|
|
105
252
|
const message = error instanceof Error ? error.message : String(error);
|
|
106
|
-
attempts.push(url + " → " +
|
|
107
|
-
|
|
108
|
-
finally {
|
|
109
|
-
clearTimeout(timer);
|
|
110
|
-
options.signal?.removeEventListener?.("abort", onAbort);
|
|
253
|
+
attempts.push(url + " → " + message);
|
|
254
|
+
removeQuietly(part);
|
|
111
255
|
}
|
|
112
256
|
}
|
|
113
257
|
return { path: target.path, ok: false, bytes: 0, url: "", error: "所有源都没下下来:" + attempts.join(";"), attempts };
|
package/lib/engine/flash.js
CHANGED
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
* 跑完 reset run 就退出,探针立刻释放,不留后台 server,也不停核。
|
|
7
7
|
*/
|
|
8
8
|
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
9
|
-
import { basename, isAbsolute, resolve } from "node:path";
|
|
9
|
+
import { basename, dirname, isAbsolute, resolve } from "node:path";
|
|
10
10
|
import { describeSearchScope, resolveSearchConfig } from "../data/search-roots.js";
|
|
11
|
-
import {
|
|
11
|
+
import { usbDiagnosis } from "./doctor.js";
|
|
12
|
+
import { detectBuildDir, elfSelectionNote, findElf } from "./build.js";
|
|
12
13
|
import { findIocFile } from "./generate.js";
|
|
13
14
|
import { summarizeIoc } from "./ioc.js";
|
|
14
15
|
import { resolveToolIn } from "./probe.js";
|
|
@@ -42,6 +43,37 @@ export function checkNoDebugGuard(projectDir, allowNoDebug = false, explicitIoc
|
|
|
42
43
|
: names.length === 0
|
|
43
44
|
? "工程里没有 .ioc"
|
|
44
45
|
: "工程里有 " + String(names.length) + " 个 .ioc(" + names.join("、") + "),分不清用哪个";
|
|
46
|
+
// 2c:没有 .ioc(寄存器工程就是这样)时退回看 openocd.cfg 的 transport——
|
|
47
|
+
// 插件生成的 cfg 固定是 hla_swd;用户换成 jtag 的 cfg 就给警告,守卫从此也覆盖寄存器工程。
|
|
48
|
+
const cfg = findProjectConfig(projectDir);
|
|
49
|
+
if (cfg !== null) {
|
|
50
|
+
try {
|
|
51
|
+
const cfgText = readFileSync(cfg, "utf8");
|
|
52
|
+
const isSwd = /transport\s+select\s+(?:hla_)?swd/iu.test(cfgText);
|
|
53
|
+
const isJtag = /transport\s+select\s+jtag/iu.test(cfgText);
|
|
54
|
+
if (isSwd) {
|
|
55
|
+
return {
|
|
56
|
+
blocked: false,
|
|
57
|
+
errors: [],
|
|
58
|
+
notes: ["SWD 守卫(openocd.cfg 版):transport 是 swd,探针侧不会关调试口;固件侧仍可能自己关 SWD,烧录前自行确认固件没动 AFIO_MAPR。"],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (isJtag) {
|
|
62
|
+
return {
|
|
63
|
+
blocked: false,
|
|
64
|
+
errors: [],
|
|
65
|
+
notes: [
|
|
66
|
+
"SWD 守卫(openocd.cfg 版):openocd.cfg 选了 jtag 而不是 swd。F103 的 JTAG 引脚与 SWD 引脚复用," +
|
|
67
|
+
"用 JTAG 烧录前确认固件没有把 PA13/PA14 改作他用。",
|
|
68
|
+
],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return { blocked: false, errors: [], notes: ["SWD 守卫没生效:" + why + ";openocd.cfg 里也没有 transport select,无法判断。"] };
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return { blocked: false, errors: [], notes: ["读不了 openocd.cfg(" + cfg + "),跳过 SWD 守卫"] };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
45
77
|
return {
|
|
46
78
|
blocked: false,
|
|
47
79
|
errors: [],
|
|
@@ -126,6 +158,29 @@ export function resolveElf(projectDir, request) {
|
|
|
126
158
|
return { elf: null, buildDir, note: "构建目录里没有 .elf(先跑 stm32_build)" };
|
|
127
159
|
return { elf: elfInfo.path, buildDir, note: "" };
|
|
128
160
|
}
|
|
161
|
+
/** 按日志行特征归码(2d):一行命中即返回,多类并存时按先见者优先。 */
|
|
162
|
+
export function errorCodeOfLines(lines) {
|
|
163
|
+
for (const raw of lines) {
|
|
164
|
+
const line = raw.toLowerCase();
|
|
165
|
+
if (/unable to find a matching cmsis-dap device|no device found/.test(line))
|
|
166
|
+
return "no-probe";
|
|
167
|
+
if (/adapter speed could not be limited|target is openocd and must be halted/.test(line))
|
|
168
|
+
return "probe-busy";
|
|
169
|
+
// 真板子取证(2026-10-28):两个 openocd 同时抓同一个 CMSIS-DAP 时,openocd 不会报"忙",
|
|
170
|
+
// 而是两边交替收发导致 DAP 事务错位——满屏 transfer count mismatch / command mismatch。
|
|
171
|
+
if (/cmsis-dap (transfer count mismatch|command mismatch)|cmsis-dap command cmd_\w+ failed|libusb_error_busy/.test(line))
|
|
172
|
+
return "probe-busy";
|
|
173
|
+
if (/error connecting dp|cannot read idr|ap lock|sticky error/.test(line))
|
|
174
|
+
return "swd-locked";
|
|
175
|
+
if (/flash programming|write protected|read protected|memory protected/.test(line))
|
|
176
|
+
return "flash-protected";
|
|
177
|
+
if (/target voltage may be too low|no power/.test(line))
|
|
178
|
+
return "no-power";
|
|
179
|
+
if (/device id|cannot identify|unexpected idcode/.test(line))
|
|
180
|
+
return "target-mismatch";
|
|
181
|
+
}
|
|
182
|
+
return "unknown";
|
|
183
|
+
}
|
|
129
184
|
/** 认 openocd 的 ** Programming Finished ** / ** Verified OK ** 与 Error: 行。 */
|
|
130
185
|
export function parseFlashLog(text) {
|
|
131
186
|
const programmed = /\*\*\s*Programming Finished\s*\*\*/u.test(text);
|
|
@@ -155,10 +210,23 @@ export function parseFlashLog(text) {
|
|
|
155
210
|
else
|
|
156
211
|
errors.push(line);
|
|
157
212
|
}
|
|
158
|
-
|
|
213
|
+
const flat = [...errors, ...noise];
|
|
214
|
+
const errorCode = succeeded ? "" : errorCodeOfLines(flat.length === 0 ? [text] : flat);
|
|
215
|
+
return { programmed, verified, errors, noise, errorCode };
|
|
216
|
+
}
|
|
217
|
+
export const HINT_DP = "连不上芯片(Error connecting DP):固件里可能关了 SWD(__HAL_AFIO_REMAP_SWJ_DISABLE),需要 BOOT0=1 重启进系统 bootloader 再烧,或按住复位键在连接瞬间松开。";
|
|
218
|
+
export const HINT_PROBE = "找不到 CMSIS-DAP 探针:检查 USB、是否被 openocd/调试会话占用(先停掉正在跑的调试)。";
|
|
219
|
+
export const HINT_BUSY = "探针被别的进程同时访问(两个 openocd 抢同一个 CMSIS-DAP 时不会报错,而是满屏 transfer count mismatch):先关掉调试会话或另一个 openocd,再烧一次。";
|
|
220
|
+
/** openocd 报"找不到设备"的那几行错误行(2a 的判定基准,纯函数便于单测)。 */
|
|
221
|
+
export function probeMissingLines(errors) {
|
|
222
|
+
return errors.filter((line) => /CMSIS-DAP device|no device found/iu.test(line));
|
|
223
|
+
}
|
|
224
|
+
/** USB 形态诊断的结论该以什么 note 呈现(unknown 静默:诊断失败不能把失败报告搞得更吵)。 */
|
|
225
|
+
export function usbNoteFor(kind, detail) {
|
|
226
|
+
if (kind === "unknown")
|
|
227
|
+
return null;
|
|
228
|
+
return "USB 形态:" + detail;
|
|
159
229
|
}
|
|
160
|
-
const HINT_DP = "连不上芯片(Error connecting DP):固件里可能关了 SWD(__HAL_AFIO_REMAP_SWJ_DISABLE),需要 BOOT0=1 重启进系统 bootloader 再烧,或按住复位键在连接瞬间松开。";
|
|
161
|
-
const HINT_PROBE = "找不到 CMSIS-DAP 探针:检查 USB、是否被 openocd/调试会话占用(先停掉正在跑的调试)。";
|
|
162
230
|
export async function runFlash(subprocess, request) {
|
|
163
231
|
const projectDir = resolve(request.projectDir);
|
|
164
232
|
const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
|
|
@@ -174,6 +242,10 @@ export async function runFlash(subprocess, request) {
|
|
|
174
242
|
}
|
|
175
243
|
const elfPath = elfResolved.elf;
|
|
176
244
|
const elfSize = statSync(elfPath).size;
|
|
245
|
+
// F1:多个 .elf 并存时说清"烧的是哪一个、为什么是它"。
|
|
246
|
+
const selectionNote = elfSelectionNote(dirname(elfPath));
|
|
247
|
+
if (selectionNote !== "")
|
|
248
|
+
notes.push(selectionNote);
|
|
177
249
|
// A5:先看 .ioc,别把 SWD 烧死。
|
|
178
250
|
const guard = checkNoDebugGuard(projectDir, request.allowNoDebug === true, request.iocPath ?? "");
|
|
179
251
|
if (!guard.blocked && guard.notes.length > 0)
|
|
@@ -243,8 +315,16 @@ export async function runFlash(subprocess, request) {
|
|
|
243
315
|
}
|
|
244
316
|
if (errors.some((line) => /Error connecting DP/u.test(line)))
|
|
245
317
|
notes.push(HINT_DP);
|
|
246
|
-
|
|
318
|
+
const probeMissing = probeMissingLines(errors);
|
|
319
|
+
if (probeMissing.length > 0) {
|
|
247
320
|
notes.push(HINT_PROBE);
|
|
321
|
+
// 2a:找不到设备时顺手做一次 USB 形态诊断——"没插"与"这台机器根本没 USB"是两种答案。
|
|
322
|
+
// 诊断失败静默(unknown 不进 notes),绝不能让诊断把失败报告搞得更吵。
|
|
323
|
+
const usb = await usbDiagnosis(subprocess, request.signal);
|
|
324
|
+
const extra = usbNoteFor(usb.kind, usb.detail);
|
|
325
|
+
if (extra !== null)
|
|
326
|
+
notes.push(extra);
|
|
327
|
+
}
|
|
248
328
|
const ok = outcome.exitCode === 0 && outcome.spawnError === null && !outcome.timedOut && verdict.programmed && errors.length === 0;
|
|
249
329
|
if (!ok && verdict.programmed && !verdict.verified)
|
|
250
330
|
notes.push("写入成功但校验没过,建议重试或降低 adapter speed");
|
|
@@ -253,11 +333,20 @@ export async function runFlash(subprocess, request) {
|
|
|
253
333
|
const message = ok
|
|
254
334
|
? "烧录成功(" + ((Date.now() - startedAt) / 1000).toFixed(1) + " s" + (verdict.verified ? ",已校验" : "") + ")"
|
|
255
335
|
: "烧录失败:" + (errors[0] ?? "openocd 退出码 " + String(outcome.exitCode));
|
|
336
|
+
// 2d:errorCode 供模型分流(重试 / 换线 / 停会话 / 直接问人)。
|
|
337
|
+
let errorCode = verdict.errorCode;
|
|
338
|
+
if (!ok && (outcome.spawnError !== null || outcome.timedOut))
|
|
339
|
+
errorCode = "unknown";
|
|
340
|
+
if (!ok && errorCode === "swd-locked" && errors.some((line) => /Error connecting DP/u.test(line)))
|
|
341
|
+
errorCode = "swd-locked";
|
|
342
|
+
if (!ok && errorCode === "probe-busy")
|
|
343
|
+
notes.push(HINT_BUSY);
|
|
256
344
|
return {
|
|
257
345
|
ok, projectDir, elfPath, elfSize, command, args, configs,
|
|
258
346
|
durationMs: Date.now() - startedAt,
|
|
259
347
|
...(outcome.exitCode === null ? {} : { exitCode: outcome.exitCode }),
|
|
260
348
|
programmed: verdict.programmed, verified: verdict.verified,
|
|
349
|
+
...(errorCode === "" ? {} : { errorCode }),
|
|
261
350
|
errors, message, logTail: log.split(/\r?\n/u).slice(-25).join("\n"), notes,
|
|
262
351
|
};
|
|
263
352
|
}
|
package/lib/engine/generate.js
CHANGED
|
@@ -16,6 +16,7 @@ import { firmwareNeedFromIoc, firmwareSourceLabel, locateFirmwarePackage, } from
|
|
|
16
16
|
import { describeSearchScope, resolveSearchConfig } from "../data/search-roots.js";
|
|
17
17
|
import { buildGenerateScript, parseGenerateLog } from "./generate-script.js";
|
|
18
18
|
import { firmwareExtraDirs } from "./firmware-install.js";
|
|
19
|
+
import { describeRefresh, refreshCubemxCmakeLists } from "./cmake-sources.js";
|
|
19
20
|
import { cacheHit, cacheKeyFor, entryFromFiles, readCache, writeCache } from "./generate-cache.js";
|
|
20
21
|
import { cubeMxUserHome, needsThirdpartyPacks, parkThirdpartyDb } from "./thirdparty.js";
|
|
21
22
|
import { listGeneratedFiles, mapGeneratedPath } from "./layout.js";
|
|
@@ -366,6 +367,9 @@ export async function runGenerate(subprocess, request) {
|
|
|
366
367
|
const hit = cacheHit(readCache(stateDir), cacheKey.key, projectDir);
|
|
367
368
|
if (hit.hit && hit.entry !== null) {
|
|
368
369
|
const cached = hit.entry;
|
|
370
|
+
// 老洞(审计后补):cmake/stm32cubemx/CMakeLists.txt 一直是脚手架快照,CubeMX 从不重写它。
|
|
371
|
+
// 跳过的这次也要把它对齐一次 —— 否则用户只改了 Core 里的代码(.ioc 没动)时,源清单永远缺模块。
|
|
372
|
+
const cmake = refreshCubemxCmakeLists(projectDir);
|
|
369
373
|
return {
|
|
370
374
|
ok: true,
|
|
371
375
|
projectDir,
|
|
@@ -398,6 +402,8 @@ export async function runGenerate(subprocess, request) {
|
|
|
398
402
|
"想强制重新生成就传 skipIfUnchanged=false(会再跑一次 CubeMX,3–4 分钟)。",
|
|
399
403
|
"生成缓存只认 .ioc / CubeMX 版本 / 布局:换了固件包内容(例如重下同一个包)也要传 skipIfUnchanged=false。",
|
|
400
404
|
"缓存文件:.dsh-stm32/" + "generate-cache.json" + ";删掉它等效于强制重跑。",
|
|
405
|
+
describeRefresh(cmake),
|
|
406
|
+
...cmake.notes,
|
|
401
407
|
],
|
|
402
408
|
logTail: "",
|
|
403
409
|
};
|
|
@@ -574,6 +580,11 @@ export async function runGenerate(subprocess, request) {
|
|
|
574
580
|
});
|
|
575
581
|
}
|
|
576
582
|
const notes = [...applied.notes];
|
|
583
|
+
// 老洞:CubeMX 不写 cmake/stm32cubemx/CMakeLists.txt(实测 mtime 停留在脚手架拷贝那一刻),
|
|
584
|
+
// 这份源清单必须由我们按磁盘事实对齐,否则开了 UART/CAN 会链接期 undefined reference。
|
|
585
|
+
const cmake = refreshCubemxCmakeLists(projectDir, request.dryRun === true ? { dryRun: true } : {});
|
|
586
|
+
notes.push(describeRefresh(cmake));
|
|
587
|
+
notes.push(...cmake.notes);
|
|
577
588
|
if (verdict.knownNoise.length > 0) {
|
|
578
589
|
notes.push("CubeMX 噪音 " + String(verdict.knownNoise.length) + " 条(与本芯片无关,已忽略)");
|
|
579
590
|
}
|