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
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L2:cmake-sources —— 让 cmake/stm32cubemx/CMakeLists.txt 跟着工程的实际内容更新。
|
|
3
|
+
*
|
|
4
|
+
* 背景(审计后补的老洞):这份文件由脚手架从 templates/scaffold 拷进来,之后 CubeMX 不再重写它。
|
|
5
|
+
* 实测证据:demo 工程里它的 mtime 与模板拷贝时间相同,而 Core/Src/main.c 已经更新过一轮。
|
|
6
|
+
* 于是它一直是「C8T6 + GPIO」的快照:设备宏写死 STM32F103xB、启动文件写死 startup_stm32f103xb.s、
|
|
7
|
+
* HAL 模块清单只有 gpio/dma/pwr/flash/exti/cortex/rcc 那一圈。只要在 CubeMX 里开一个 UART/CAN/USB,
|
|
8
|
+
* 编译能过、链接报 undefined reference to HAL_UART_Init —— 而源清单里根本没有那个 .c,
|
|
9
|
+
* 报错信息完全不指向真正的病因,这是最费时间的一类坑。
|
|
10
|
+
*
|
|
11
|
+
* 做法:只动三处(设备宏、应用源清单、驱动源清单),文件里其余的行原样保留。
|
|
12
|
+
* - 设备宏与启动文件:从工程根真实存在的 startup_stm32*.s 反推(stm32f103xb → STM32F103xB),
|
|
13
|
+
* 不查表、不猜型号:文件在,就说明事实如此。
|
|
14
|
+
* - HAL 模块:以 Core/Inc/stm32f1xx_hal_conf.h 里 CubeMX 自己写的 HAL_xxx_MODULE_ENABLED 为准,
|
|
15
|
+
* 再并上 Core 源码里 #include "stm32f1xx_hal_xxx.h" 的模块;只有 Drivers/.../Src 下真有 .c 的才写进去。
|
|
16
|
+
* - 已有条目只要文件还在就保持原顺序(用户工程的 diff 最小),缺的追加在末尾。
|
|
17
|
+
* 幂等:算出来和文件里一模一样时一个字节都不写。
|
|
18
|
+
*/
|
|
19
|
+
import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { basename, join } from "node:path";
|
|
21
|
+
import { readTextFile } from "./text-file.js";
|
|
22
|
+
/** ST 的 CMake 布局里那份「CubeMX 生成」的源清单。 */
|
|
23
|
+
export const CUBEMX_CMAKE_REL = "cmake/stm32cubemx/CMakeLists.txt";
|
|
24
|
+
/** CMSIS 的设备宏,例如 STM32F103xB / STM32F107xC。 */
|
|
25
|
+
const DEVICE_DEFINE_RE = /^STM32[A-Z]\d{3}[A-Za-z][A-Za-z0-9]?$/u;
|
|
26
|
+
/** 启动文件名,例如 startup_stm32f103xb.s。 */
|
|
27
|
+
const STARTUP_RE = /^startup_stm32[a-z0-9]+\.s$/iu;
|
|
28
|
+
/** 块里的条目路径:${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/main.c → Core/Src/main.c。 */
|
|
29
|
+
const ENTRY_RE = /\$\{CMAKE_CURRENT_SOURCE_DIR\}\/\.\.\/\.\.\/([^"\s)]+)/u;
|
|
30
|
+
export function isDeviceDefine(token) {
|
|
31
|
+
return DEVICE_DEFINE_RE.test(token);
|
|
32
|
+
}
|
|
33
|
+
/** 工程根上的启动文件(排序后返回,保证多次调用结果一致)。 */
|
|
34
|
+
export function listStartupFiles(projectDir) {
|
|
35
|
+
if (!existsSync(projectDir))
|
|
36
|
+
return [];
|
|
37
|
+
try {
|
|
38
|
+
return readdirSync(projectDir)
|
|
39
|
+
.filter((name) => STARTUP_RE.test(name))
|
|
40
|
+
.sort();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** 选工程根那个启动文件:唯一就是它;多个按字典序取第一个并说明(芯片换了要人删掉旧的)。 */
|
|
47
|
+
export function pickStartupFile(projectDir) {
|
|
48
|
+
const files = listStartupFiles(projectDir);
|
|
49
|
+
if (files.length === 0)
|
|
50
|
+
return null;
|
|
51
|
+
const first = files[0];
|
|
52
|
+
if (files.length === 1)
|
|
53
|
+
return { file: first, notes: [] };
|
|
54
|
+
return {
|
|
55
|
+
file: first,
|
|
56
|
+
notes: [
|
|
57
|
+
"工程根有多个启动文件(" + files.join("、") + "),按字典序用了 " + first + ";",
|
|
58
|
+
"换过芯片的话请删掉不用的那个,否则链接期会撞重复的中断向量表。",
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** startup_stm32f103xb.s → stm32f103xb。 */
|
|
63
|
+
export function startupStem(file) {
|
|
64
|
+
return basename(file).replace(/^startup_/iu, "").replace(/\.s$/iu, "").toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
/** stm32f103xb → STM32F103xB(就是 CMSIS 头里那个宏,也是 cmake 源清单里那一行)。 */
|
|
67
|
+
export function deviceDefineFromStartup(file) {
|
|
68
|
+
return startupStem(file).toUpperCase().replace(/X(?=.$)/u, "x");
|
|
69
|
+
}
|
|
70
|
+
/** stm32f103xb → { token: stm32f1xx, upper: STM32F1xx };认不出返回 null。 */
|
|
71
|
+
export function familyTokensFromStartup(file) {
|
|
72
|
+
const matched = /^stm32([a-z]\d)/u.exec(startupStem(file));
|
|
73
|
+
if (matched === null)
|
|
74
|
+
return null;
|
|
75
|
+
const code = matched[1];
|
|
76
|
+
return { token: "stm32" + code + "xx", upper: "STM32" + code.toUpperCase() + "xx" };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 抹掉注释再匹配。
|
|
80
|
+
*
|
|
81
|
+
* CubeMX 的 hal_conf 里,没用到的模块是一行被注释掉的 `/*#define HAL_ADC_MODULE_ENABLED *\/`——
|
|
82
|
+
* 不剥注释的话正则照样命中,会把整个 HAL 的 30 多个 .c 全编进去(老洞修复时就踩了这个)。
|
|
83
|
+
*/
|
|
84
|
+
/**
|
|
85
|
+
* 这个文件主要用哪种行尾。
|
|
86
|
+
*
|
|
87
|
+
* text-file 的 eol 是「含 CRLF 就算 CRLF」,遇到 CRLF/LF 混排的文件会把整个文件翻成 CRLF,
|
|
88
|
+
* 于是明明内容没变,diff 却是全文件重写。这里改成按多数行尾走。
|
|
89
|
+
*/
|
|
90
|
+
function dominantEol(text) {
|
|
91
|
+
let crlf = 0;
|
|
92
|
+
let lf = 0;
|
|
93
|
+
for (const line of text.split("\n")) {
|
|
94
|
+
if (line.endsWith("\r"))
|
|
95
|
+
crlf += 1;
|
|
96
|
+
else
|
|
97
|
+
lf += 1;
|
|
98
|
+
}
|
|
99
|
+
return crlf > lf ? "\r\n" : "\n";
|
|
100
|
+
}
|
|
101
|
+
function stripComments(text) {
|
|
102
|
+
return text.replace(/\/\*[\s\S]*?\*\//gu, " ").replace(/\/\/[^\n]*/gu, " ");
|
|
103
|
+
}
|
|
104
|
+
function readTextOrNull(path) {
|
|
105
|
+
try {
|
|
106
|
+
return existsSync(path) ? readFileSync(path, "utf8") : null;
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 这个工程用到了哪些 HAL 模块。
|
|
114
|
+
*
|
|
115
|
+
* 主依据是 stm32f1xx_hal_conf.h:CubeMX 只把真正用到的外设模块的 HAL_xxx_MODULE_ENABLED 打开,
|
|
116
|
+
* 那份文件就是它对「要编译哪些 .c」的声明(不要扫 hal_conf 里的 #include —— 那里每个模块都列了一遍,
|
|
117
|
+
* 只是被 #ifdef 包住,全当「用到」会把整个 HAL 都编进去)。
|
|
118
|
+
* 再并上 Core 源码里显式 #include 的模块,兜住手写代码的情况。
|
|
119
|
+
*/
|
|
120
|
+
export function halModulesInUse(projectDir, family) {
|
|
121
|
+
const modules = new Set();
|
|
122
|
+
const notes = [];
|
|
123
|
+
const confPath = join(projectDir, "Core", "Inc", family.token + "_hal_conf.h");
|
|
124
|
+
const conf = readTextOrNull(confPath);
|
|
125
|
+
if (conf === null) {
|
|
126
|
+
notes.push("没有 " + family.token + "_hal_conf.h,模块清单只能靠源码里的 #include 推");
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
for (const matched of stripComments(conf).matchAll(/#define\s+HAL_([A-Z0-9_]+)_MODULE_ENABLED/gu)) {
|
|
130
|
+
modules.add(matched[1].toLowerCase());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const includeRe = new RegExp("#include\\s+[\"<]" + family.token + "_hal_([a-z0-9_]+)\\.h[\">]", "gu");
|
|
134
|
+
const scanDirs = [
|
|
135
|
+
[join(projectDir, "Core", "Src"), /\.c$/iu],
|
|
136
|
+
[join(projectDir, "Core", "Inc"), /\.h$/iu],
|
|
137
|
+
];
|
|
138
|
+
const skip = confPath.toLowerCase();
|
|
139
|
+
for (const [dir, filter] of scanDirs) {
|
|
140
|
+
if (!existsSync(dir))
|
|
141
|
+
continue;
|
|
142
|
+
let names;
|
|
143
|
+
try {
|
|
144
|
+
names = readdirSync(dir).filter((name) => filter.test(name));
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
for (const name of names) {
|
|
150
|
+
const path = join(dir, name);
|
|
151
|
+
if (path.toLowerCase() === skip)
|
|
152
|
+
continue;
|
|
153
|
+
const text = readTextOrNull(path);
|
|
154
|
+
if (text === null)
|
|
155
|
+
continue;
|
|
156
|
+
for (const matched of stripComments(text).matchAll(includeRe))
|
|
157
|
+
modules.add(matched[1].toLowerCase());
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const sorted = [...modules].sort();
|
|
161
|
+
if (sorted.length === 0)
|
|
162
|
+
notes.push("没读到任何 HAL 模块(hal_conf 与 Core 源码里都没有),驱动源清单会保持原样");
|
|
163
|
+
return { modules: sorted, notes };
|
|
164
|
+
}
|
|
165
|
+
/** 收集「这个工程到底该编哪些文件」的事实;缺启动文件时返回 error。 */
|
|
166
|
+
export function collectCmakeSources(projectDir) {
|
|
167
|
+
const startup = pickStartupFile(projectDir);
|
|
168
|
+
if (startup === null) {
|
|
169
|
+
return { error: "工程根没有 startup_stm32*.s(不是脚手架建的 HAL 工程?),无从反推芯片与源清单" };
|
|
170
|
+
}
|
|
171
|
+
const family = familyTokensFromStartup(startup.file);
|
|
172
|
+
if (family === null)
|
|
173
|
+
return { error: "认不出启动文件 " + startup.file + " 属于哪个 STM32 家族" };
|
|
174
|
+
const deviceDefine = deviceDefineFromStartup(startup.file);
|
|
175
|
+
const notes = [...startup.notes];
|
|
176
|
+
const moduleFacts = halModulesInUse(projectDir, family);
|
|
177
|
+
notes.push(...moduleFacts.notes);
|
|
178
|
+
const driverDir = "Drivers/" + family.upper + "_HAL_Driver/Src";
|
|
179
|
+
const exists = (rel) => existsSync(join(projectDir, rel));
|
|
180
|
+
const driverFiles = [];
|
|
181
|
+
const push = (rel) => {
|
|
182
|
+
if (exists(rel) && !driverFiles.includes(rel))
|
|
183
|
+
driverFiles.push(rel);
|
|
184
|
+
};
|
|
185
|
+
const systemSrc = "Core/Src/system_" + family.token + ".c";
|
|
186
|
+
push(systemSrc);
|
|
187
|
+
push(driverDir + "/" + family.token + "_hal.c");
|
|
188
|
+
const missing = [];
|
|
189
|
+
for (const module of moduleFacts.modules) {
|
|
190
|
+
const base = driverDir + "/" + family.token + "_hal_" + module + ".c";
|
|
191
|
+
if (!exists(base)) {
|
|
192
|
+
missing.push(module);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
driverFiles.push(base);
|
|
196
|
+
const ex = driverDir + "/" + family.token + "_hal_" + module + "_ex.c";
|
|
197
|
+
if (exists(ex) && !driverFiles.includes(ex))
|
|
198
|
+
driverFiles.push(ex);
|
|
199
|
+
}
|
|
200
|
+
if (missing.length > 0) {
|
|
201
|
+
notes.push("这些模块在 stm32f1xx_hal_conf.h 里是开着的,但固件包的 " + driverDir + " 下没有对应 .c:" +
|
|
202
|
+
missing.join("、") +
|
|
203
|
+
"(先确认 Drivers 目录是不是完整拷贝)");
|
|
204
|
+
}
|
|
205
|
+
const appFiles = [];
|
|
206
|
+
const appCandidates = [
|
|
207
|
+
"Core/Src/main.c",
|
|
208
|
+
"Core/Src/" + family.token + "_it.c",
|
|
209
|
+
"Core/Src/" + family.token + "_hal_msp.c",
|
|
210
|
+
"Core/Src/sysmem.c",
|
|
211
|
+
"Core/Src/syscalls.c",
|
|
212
|
+
startup.file,
|
|
213
|
+
];
|
|
214
|
+
for (const rel of appCandidates) {
|
|
215
|
+
if (exists(rel))
|
|
216
|
+
appFiles.push(rel);
|
|
217
|
+
}
|
|
218
|
+
return { startup: startup.file, deviceDefine, family, modules: moduleFacts.modules, driverDir, driverFiles, appFiles, notes };
|
|
219
|
+
}
|
|
220
|
+
function findBlock(lines, varName) {
|
|
221
|
+
const openRe = new RegExp("^\\s*set\\(\\s*" + varName + "\\s*$", "u");
|
|
222
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
223
|
+
if (!openRe.test(lines[i]))
|
|
224
|
+
continue;
|
|
225
|
+
for (let j = i + 1; j < lines.length; j += 1) {
|
|
226
|
+
if (lines[j].trim() === ")")
|
|
227
|
+
return { open: i, close: j };
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
function entryPaths(lines, block) {
|
|
234
|
+
const out = [];
|
|
235
|
+
for (let i = block.open + 1; i < block.close; i += 1) {
|
|
236
|
+
const matched = ENTRY_RE.exec(lines[i]);
|
|
237
|
+
if (matched !== null)
|
|
238
|
+
out.push(matched[1]);
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
/** 把一组相对路径渲染成 cmake 块里的条目行(缩进照模板:4 个空格)。 */
|
|
243
|
+
function entryLines(paths) {
|
|
244
|
+
return paths.map((rel) => " ${CMAKE_CURRENT_SOURCE_DIR}/../../" + rel);
|
|
245
|
+
}
|
|
246
|
+
function splice(lines, block, body) {
|
|
247
|
+
return [...lines.slice(0, block.open + 1), ...body, ...lines.slice(block.close)];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 应用源清单:保留原有顺序,去掉已经不存在的文件,启动文件必须与事实一致(换芯片时换掉旧的)。
|
|
251
|
+
*/
|
|
252
|
+
function rewriteAppBlock(lines, block, facts, projectDir) {
|
|
253
|
+
const added = [];
|
|
254
|
+
const dropped = [];
|
|
255
|
+
const kept = [];
|
|
256
|
+
for (const rel of entryPaths(lines, block)) {
|
|
257
|
+
const isStartup = STARTUP_RE.test(basename(rel));
|
|
258
|
+
if (!existsSync(join(projectDir, rel))) {
|
|
259
|
+
dropped.push(rel);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (isStartup && basename(rel) !== facts.startup) {
|
|
263
|
+
dropped.push(rel);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (kept.includes(rel))
|
|
267
|
+
continue;
|
|
268
|
+
kept.push(rel);
|
|
269
|
+
}
|
|
270
|
+
for (const rel of facts.appFiles) {
|
|
271
|
+
if (kept.includes(rel))
|
|
272
|
+
continue;
|
|
273
|
+
kept.push(rel);
|
|
274
|
+
added.push(rel);
|
|
275
|
+
}
|
|
276
|
+
if (kept.length === 0)
|
|
277
|
+
return { lines, added: [], dropped: [] };
|
|
278
|
+
return { lines: splice(lines, block, entryLines(kept)), added, dropped };
|
|
279
|
+
}
|
|
280
|
+
/** 驱动源清单:同样保留顺序,缺的追加;只剩一个空块时不改(宁可不写也不写坏)。 */
|
|
281
|
+
function rewriteDriverBlock(lines, block, facts, projectDir) {
|
|
282
|
+
const added = [];
|
|
283
|
+
const dropped = [];
|
|
284
|
+
const kept = [];
|
|
285
|
+
for (const rel of entryPaths(lines, block)) {
|
|
286
|
+
if (!existsSync(join(projectDir, rel))) {
|
|
287
|
+
dropped.push(rel);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (kept.includes(rel))
|
|
291
|
+
continue;
|
|
292
|
+
kept.push(rel);
|
|
293
|
+
}
|
|
294
|
+
for (const rel of facts.driverFiles) {
|
|
295
|
+
if (kept.includes(rel))
|
|
296
|
+
continue;
|
|
297
|
+
kept.push(rel);
|
|
298
|
+
added.push(rel);
|
|
299
|
+
}
|
|
300
|
+
if (kept.length === 0)
|
|
301
|
+
return { lines, added: [], dropped: [] };
|
|
302
|
+
return { lines: splice(lines, block, entryLines(kept)), added, dropped };
|
|
303
|
+
}
|
|
304
|
+
/** 设备宏:把块里已有的设备宏换成事实值;一个都没有就插在 USE_HAL_DRIVER 后面。 */
|
|
305
|
+
function rewriteDefinesBlock(lines, block, facts) {
|
|
306
|
+
const body = [];
|
|
307
|
+
let found = false;
|
|
308
|
+
for (let i = block.open + 1; i < block.close; i += 1) {
|
|
309
|
+
const line = lines[i];
|
|
310
|
+
const token = line.trim();
|
|
311
|
+
if (isDeviceDefine(token)) {
|
|
312
|
+
body.push(line.replace(token, facts.deviceDefine));
|
|
313
|
+
found = true;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
body.push(line);
|
|
317
|
+
}
|
|
318
|
+
if (!found) {
|
|
319
|
+
const insertAt = body.findIndex((line) => line.includes("USE_HAL_DRIVER"));
|
|
320
|
+
body.splice(insertAt >= 0 ? insertAt + 1 : 0, 0, "\t" + facts.deviceDefine);
|
|
321
|
+
}
|
|
322
|
+
return { lines: splice(lines, block, body), changed: true };
|
|
323
|
+
}
|
|
324
|
+
function emptyResult(path, message, notes = []) {
|
|
325
|
+
return { ok: false, path, changed: false, message, notes, startup: "", deviceDefine: "", modules: [], added: [], dropped: [] };
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* 重算并(默认)写回 cmake/stm32cubemx/CMakeLists.txt。
|
|
329
|
+
* 只动三处 managed 块,其余行原样保留;内容没变就不写盘。
|
|
330
|
+
*/
|
|
331
|
+
export function refreshCubemxCmakeLists(projectDir, options = {}) {
|
|
332
|
+
const cmakePath = join(projectDir, CUBEMX_CMAKE_REL);
|
|
333
|
+
if (!existsSync(cmakePath)) {
|
|
334
|
+
return emptyResult(cmakePath, "工程里没有 " + CUBEMX_CMAKE_REL + "(不是 ST 的 CMake 布局),跳过");
|
|
335
|
+
}
|
|
336
|
+
const facts = collectCmakeSources(projectDir);
|
|
337
|
+
if ("error" in facts)
|
|
338
|
+
return emptyResult(cmakePath, facts.error);
|
|
339
|
+
const file = readTextFile(cmakePath);
|
|
340
|
+
if (!file.ok)
|
|
341
|
+
return emptyResult(cmakePath, "读不了 " + CUBEMX_CMAKE_REL + "(" + file.error + "),跳过");
|
|
342
|
+
if (file.encoding !== "utf8" && file.encoding !== "utf8-bom") {
|
|
343
|
+
return emptyResult(cmakePath, CUBEMX_CMAKE_REL + " 的编码是 " + file.encoding + ",为避免写坏,跳过");
|
|
344
|
+
}
|
|
345
|
+
const eol = dominantEol(file.text);
|
|
346
|
+
const original = file.text.split(/\r?\n/u);
|
|
347
|
+
const endedWithEol = /\r?\n$/u.test(file.text);
|
|
348
|
+
// 按行切完末尾会多出一个空串;不摘掉它,每跑一次就多一个空行(幂等就断了)。
|
|
349
|
+
if (endedWithEol)
|
|
350
|
+
original.pop();
|
|
351
|
+
let lines = original;
|
|
352
|
+
// 家族目录名(Drivers/STM32F1xx_HAL_Driver、CMSIS/Device/ST/STM32F1xx)跟着启动文件走。
|
|
353
|
+
const familyRe = /STM32[A-Z]\dXX/gu;
|
|
354
|
+
lines = lines.map((line) => line.replace(familyRe, facts.family.upper));
|
|
355
|
+
const notes = [...facts.notes];
|
|
356
|
+
const added = [];
|
|
357
|
+
const dropped = [];
|
|
358
|
+
const defines = findBlock(lines, "MX_Defines_Syms");
|
|
359
|
+
if (defines === null)
|
|
360
|
+
return emptyResult(cmakePath, "找不到 set(MX_Defines_Syms ...) 块,格式不是 CubeMX 那套,跳过");
|
|
361
|
+
const afterDefines = rewriteDefinesBlock(lines, defines, facts);
|
|
362
|
+
lines = afterDefines.lines;
|
|
363
|
+
const apps = findBlock(lines, "MX_Application_Src");
|
|
364
|
+
if (apps !== null) {
|
|
365
|
+
const rewritten = rewriteAppBlock(lines, apps, facts, projectDir);
|
|
366
|
+
lines = rewritten.lines;
|
|
367
|
+
added.push(...rewritten.added);
|
|
368
|
+
dropped.push(...rewritten.dropped);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
notes.push("没有 set(MX_Application_Src ...) 块,应用源清单未改动");
|
|
372
|
+
}
|
|
373
|
+
const drivers = findBlock(lines, "STM32_Drivers_Src");
|
|
374
|
+
if (drivers !== null) {
|
|
375
|
+
const rewritten = rewriteDriverBlock(lines, drivers, facts, projectDir);
|
|
376
|
+
lines = rewritten.lines;
|
|
377
|
+
added.push(...rewritten.added);
|
|
378
|
+
dropped.push(...rewritten.dropped);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
notes.push("没有 set(STM32_Drivers_Src ...) 块,驱动源清单未改动");
|
|
382
|
+
}
|
|
383
|
+
if (dropped.length > 0) {
|
|
384
|
+
notes.push("从源清单里摘掉了磁盘上已不存在的条目:" + dropped.join("、"));
|
|
385
|
+
}
|
|
386
|
+
const next = lines.join(eol) + (endedWithEol ? eol : "");
|
|
387
|
+
const base = {
|
|
388
|
+
ok: true,
|
|
389
|
+
path: cmakePath,
|
|
390
|
+
startup: facts.startup,
|
|
391
|
+
deviceDefine: facts.deviceDefine,
|
|
392
|
+
modules: facts.modules,
|
|
393
|
+
added,
|
|
394
|
+
dropped,
|
|
395
|
+
notes,
|
|
396
|
+
};
|
|
397
|
+
// 只比内容,不比行尾:文件里 CRLF 与 LF 混着时,按一种行尾重写会让整个文件都变「脏」。
|
|
398
|
+
const contentSame = original.length === lines.length && original.every((line, index) => line === lines[index]);
|
|
399
|
+
if (contentSame) {
|
|
400
|
+
return { ...base, changed: false, message: "已是最新(" + facts.startup + " / " + facts.deviceDefine + " / " + String(facts.modules.length) + " 个 HAL 模块)" };
|
|
401
|
+
}
|
|
402
|
+
if (options.dryRun === true) {
|
|
403
|
+
return { ...base, changed: true, message: "将要更新(dryRun,未写盘)" };
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
writeFileSync(cmakePath, next, "utf8");
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
410
|
+
return emptyResult(cmakePath, "写不了 " + cmakePath + ":" + message, notes);
|
|
411
|
+
}
|
|
412
|
+
return {
|
|
413
|
+
...base,
|
|
414
|
+
changed: true,
|
|
415
|
+
message: "已更新(" + facts.startup + " / " + facts.deviceDefine + ",新增 " + String(added.length) + " 条)",
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
/** 给报告/日志用的一句话。 */
|
|
419
|
+
export function describeRefresh(result) {
|
|
420
|
+
if (!result.ok)
|
|
421
|
+
return "cmake 源清单:未处理 —— " + result.message;
|
|
422
|
+
if (!result.changed)
|
|
423
|
+
return "cmake 源清单:" + result.message + "(" + CUBEMX_CMAKE_REL + ")";
|
|
424
|
+
return ("cmake 源清单:" +
|
|
425
|
+
result.message +
|
|
426
|
+
(result.added.length === 0 ? "" : ";新增 " + result.added.join("、")) +
|
|
427
|
+
(result.dropped.length === 0 ? "" : ";摘掉 " + result.dropped.join("、")));
|
|
428
|
+
}
|
package/lib/engine/debug.js
CHANGED
|
@@ -7,10 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { existsSync, statSync } from "node:fs";
|
|
9
9
|
import { createConnection } from "node:net";
|
|
10
|
-
import { isAbsolute, resolve } from "node:path";
|
|
10
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
11
11
|
import { resolveSearchConfig } from "../data/search-roots.js";
|
|
12
|
-
import { detectBuildDir, findElf } from "./build.js";
|
|
13
|
-
import {
|
|
12
|
+
import { detectBuildDir, elfSelectionNote, findElf } from "./build.js";
|
|
13
|
+
import { usbDiagnosis } from "./doctor.js";
|
|
14
|
+
import { HINT_BUSY, HINT_DP, HINT_PROBE, defaultConfigs, errorCodeOfLines, findProjectConfig, probeMissingLines, quoteForDisplay, toOpenOcdPath, usbNoteFor, } from "./flash.js";
|
|
14
15
|
import { resolveToolIn } from "./probe.js";
|
|
15
16
|
import { runStreaming } from "./subprocess-runner.js";
|
|
16
17
|
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
@@ -115,6 +116,38 @@ export async function waitForListening(handle, timeoutMs, onText) {
|
|
|
115
116
|
drain();
|
|
116
117
|
return { ok: false, text };
|
|
117
118
|
}
|
|
119
|
+
/** 失败时 log tail 的优先级:Error/adapter 那几行比"最后 15 行"有用得多。 */
|
|
120
|
+
function keyLogTail(lines, maxLines) {
|
|
121
|
+
const important = /error|unable to find|no device found|adapter|cmsis-dap|listening|couldn't bind|jtag|cannot/i;
|
|
122
|
+
const kept = new Set();
|
|
123
|
+
for (let index = lines.length - 1; index >= 0 && kept.size < maxLines; index -= 1) {
|
|
124
|
+
const line = lines[index];
|
|
125
|
+
if (line === undefined)
|
|
126
|
+
continue;
|
|
127
|
+
if (important.test(line))
|
|
128
|
+
kept.add(index);
|
|
129
|
+
}
|
|
130
|
+
for (let index = lines.length - 1; index >= 0 && kept.size < maxLines; index -= 1)
|
|
131
|
+
kept.add(index);
|
|
132
|
+
return [...kept].sort((left, right) => left - right).map((index) => lines[index]).join("\n");
|
|
133
|
+
}
|
|
134
|
+
/** errorCode → 一句给人看的下一步。 */
|
|
135
|
+
function debugHintFor(code) {
|
|
136
|
+
if (code === "no-probe")
|
|
137
|
+
return HINT_PROBE;
|
|
138
|
+
if (code === "probe-busy")
|
|
139
|
+
return HINT_BUSY;
|
|
140
|
+
if (code === "swd-locked")
|
|
141
|
+
return HINT_DP;
|
|
142
|
+
if (code === "no-power")
|
|
143
|
+
return "目标板可能没供电:检查 3V3/GND 与 BOOT 跳线。";
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
/** 日志里第一条真正说明问题的行(给 message 用,避免只报"起不来")。 */
|
|
147
|
+
function firstProblemLine(lines) {
|
|
148
|
+
const hit = lines.find((line) => /^Error|unable to find|no device found|couldn't bind|cannot open|invalid/i.test(line));
|
|
149
|
+
return (hit ?? lines[lines.length - 1] ?? "").trim();
|
|
150
|
+
}
|
|
118
151
|
export async function runDebugRun(subprocess, request) {
|
|
119
152
|
const projectDir = resolve(request.projectDir);
|
|
120
153
|
const config = resolveSearchConfig(process.env, request.binDirs === undefined ? {} : { binDirs: request.binDirs });
|
|
@@ -131,6 +164,9 @@ export async function runDebugRun(subprocess, request) {
|
|
|
131
164
|
errors.push("找不到 .elf(先跑 stm32_build)");
|
|
132
165
|
return { ...empty, message: "没调试:找不到 .elf", durationMs: Date.now() - startedAt };
|
|
133
166
|
}
|
|
167
|
+
const selectionNote = elfSelectionNote(dirname(elfPath));
|
|
168
|
+
if (selectionNote !== "")
|
|
169
|
+
notes.push(selectionNote);
|
|
134
170
|
const openOcd = request.openOcdPath !== undefined && request.openOcdPath !== ""
|
|
135
171
|
? request.openOcdPath
|
|
136
172
|
: await resolveToolIn(subprocess, ["openocd"], config.binDirs);
|
|
@@ -169,13 +205,26 @@ export async function runDebugRun(subprocess, request) {
|
|
|
169
205
|
request.onProgress?.(text);
|
|
170
206
|
});
|
|
171
207
|
if (!listening.ok) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
208
|
+
// F2:旧版只回"openocd 起不来"+15 行尾巴,真正的 adapter init 失败常被挤掉。
|
|
209
|
+
// 现在先归类(与 flash 同一套规则),再把有信息量的行留下,并顺手做一次 USB 形态诊断。
|
|
210
|
+
const lines = listening.text.split(/\r?\n/u).filter((line) => line.trim() !== "");
|
|
211
|
+
const errorCode = errorCodeOfLines(lines);
|
|
212
|
+
const hint = debugHintFor(errorCode);
|
|
213
|
+
if (hint !== null)
|
|
214
|
+
notes.push(hint);
|
|
215
|
+
if (probeMissingLines(lines).length > 0) {
|
|
216
|
+
const usb = await usbDiagnosis(subprocess, request.signal);
|
|
217
|
+
const extra = usbNoteFor(usb.kind, usb.detail);
|
|
218
|
+
if (extra !== null)
|
|
219
|
+
notes.push(extra);
|
|
220
|
+
}
|
|
221
|
+
const problem = firstProblemLine(lines);
|
|
222
|
+
errors.push("openocd 起不来(errorCode=" + (errorCode === "" ? "unknown" : errorCode) + "):" +
|
|
223
|
+
(problem === "" ? "15 s 内没开始监听 " + String(port) : problem));
|
|
176
224
|
return {
|
|
177
|
-
...empty, elfPath, port, openOcdLogTail:
|
|
178
|
-
message: "没调试:openocd 起不来"
|
|
225
|
+
...empty, elfPath, port, openOcdLogTail: keyLogTail(lines, 40), errorCode,
|
|
226
|
+
message: "没调试:openocd 起不来" + (errorCode === "" ? "" : "(" + errorCode + ")"),
|
|
227
|
+
durationMs: Date.now() - startedAt,
|
|
179
228
|
};
|
|
180
229
|
}
|
|
181
230
|
const breakAt = request.breakAt ?? "main";
|
|
@@ -222,6 +271,7 @@ export async function runDebugRun(subprocess, request) {
|
|
|
222
271
|
errors.push("gdb 超时(" + String(request.timeoutMs ?? 45_000) + " ms):断点没命中或目标没在跑");
|
|
223
272
|
if (gdbOutcome.exitCode !== 0)
|
|
224
273
|
errors.push("gdb 退出码 " + String(gdbOutcome.exitCode));
|
|
274
|
+
const allLines = [...openOcdText.split(/\r?\n/u), ...gdbText.split(/\r?\n/u)].filter((line) => line.trim() !== "");
|
|
225
275
|
// A26:ok 必须和 errors 一致 —— 退出码非 0、gdb 报的错(Cannot access memory 之类)都不算成功。
|
|
226
276
|
const ok = gdbOutcome.spawnError === null
|
|
227
277
|
&& !gdbOutcome.timedOut
|
|
@@ -235,6 +285,20 @@ export async function runDebugRun(subprocess, request) {
|
|
|
235
285
|
if (breakAt !== "none" && !probe.hitBreakpoint && errors.length === 0) {
|
|
236
286
|
errors.push("没有停在 " + breakAt + ":可能是复位后没跑到这里,或符号对不上(.elf 与板上的固件不一致)");
|
|
237
287
|
}
|
|
288
|
+
// F2:失败时给结构化错误码 + 可执行提示(成功时留空,报告不带噪音)。
|
|
289
|
+
let errorCode = "";
|
|
290
|
+
if (!ok) {
|
|
291
|
+
errorCode = errorCodeOfLines(allLines);
|
|
292
|
+
const hint = debugHintFor(errorCode);
|
|
293
|
+
if (hint !== null)
|
|
294
|
+
notes.push(hint);
|
|
295
|
+
if (errorCode === "no-probe" && probeMissingLines(allLines).length > 0) {
|
|
296
|
+
const usb = await usbDiagnosis(subprocess, request.signal);
|
|
297
|
+
const extra = usbNoteFor(usb.kind, usb.detail);
|
|
298
|
+
if (extra !== null)
|
|
299
|
+
notes.push(extra);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
238
302
|
return {
|
|
239
303
|
ok,
|
|
240
304
|
projectDir,
|
|
@@ -248,7 +312,8 @@ export async function runDebugRun(subprocess, request) {
|
|
|
248
312
|
locals: probe.locals,
|
|
249
313
|
gdbCommand,
|
|
250
314
|
gdbOutput: gdbText,
|
|
251
|
-
openOcdLogTail:
|
|
315
|
+
openOcdLogTail: keyLogTail(allLines, 40),
|
|
316
|
+
...(errorCode === "" ? {} : { errorCode }),
|
|
252
317
|
durationMs: Date.now() - startedAt,
|
|
253
318
|
errors,
|
|
254
319
|
message: ok
|