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/build.js
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
9
9
|
import { join, resolve } from "node:path";
|
|
10
10
|
import { resolveSearchConfig } from "../data/search-roots.js";
|
|
11
|
-
import { runStreaming } from "./subprocess-runner.js";
|
|
11
|
+
import { runCapture, runStreaming } from "./subprocess-runner.js";
|
|
12
|
+
import { resolveToolIn } from "./probe.js";
|
|
12
13
|
import { envWithBinDirs } from "./tool-env.js";
|
|
13
14
|
const DEFAULT_TIMEOUT_MS = 300_000;
|
|
14
15
|
function toBytes(value, unit) {
|
|
@@ -138,22 +139,85 @@ export function pickConfigurePreset(projectDir) {
|
|
|
138
139
|
});
|
|
139
140
|
return sorted[0] ?? null;
|
|
140
141
|
}
|
|
141
|
-
/**
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
/** CMakeCache 里记的工程名(CMAKE_PROJECT_NAME:<TYPE>=<名字>);读不到返回空串。 */
|
|
143
|
+
function readCmakeProjectName(buildDir) {
|
|
144
|
+
try {
|
|
145
|
+
const text = readFileSync(join(buildDir, "CMakeCache.txt"), "utf8");
|
|
146
|
+
for (const raw of text.split(/\r?\n/u)) {
|
|
147
|
+
const match = /^CMAKE_PROJECT_NAME:[A-Z]+=(.+)$/u.exec(raw.trim());
|
|
148
|
+
if (match !== null && match[1] !== undefined && match[1].trim() !== "")
|
|
149
|
+
return match[1].trim();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// 没有缓存文件或读不了:退回"最新的那个",不影响出结果。
|
|
154
|
+
}
|
|
155
|
+
return "";
|
|
156
|
+
}
|
|
157
|
+
/** 构建目录里全部 .elf,按修改时间从新到旧。 */
|
|
158
|
+
export function elfCandidates(buildDir) {
|
|
159
|
+
let names;
|
|
160
|
+
try {
|
|
161
|
+
names = readdirSync(buildDir).filter((name) => name.toLowerCase().endsWith(".elf"));
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const name of names) {
|
|
168
|
+
const file = join(buildDir, name);
|
|
169
|
+
try {
|
|
170
|
+
const stat = statSync(file);
|
|
171
|
+
out.push({ name, path: file, size: stat.size, mtimeMs: stat.mtimeMs });
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// 刚被删掉或被独占:跳过,下一次再找。
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
out.sort((left, right) => right.mtimeMs - left.mtimeMs);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
function pickElf(buildDir) {
|
|
181
|
+
const candidates = elfCandidates(buildDir);
|
|
182
|
+
if (candidates.length === 0)
|
|
144
183
|
return null;
|
|
145
|
-
const
|
|
146
|
-
if (
|
|
184
|
+
const projectName = readCmakeProjectName(buildDir).toLowerCase();
|
|
185
|
+
if (projectName !== "") {
|
|
186
|
+
const exact = candidates.find((item) => item.name.toLowerCase() === projectName + ".elf");
|
|
187
|
+
if (exact !== undefined)
|
|
188
|
+
return { chosen: exact, candidates, byProjectName: true };
|
|
189
|
+
}
|
|
190
|
+
const newest = candidates[0];
|
|
191
|
+
if (newest === undefined)
|
|
147
192
|
return null;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
193
|
+
return { chosen: newest, candidates, byProjectName: false };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 构建目录里的 .elf(F1)。
|
|
197
|
+
*
|
|
198
|
+
* 旧规则是"与构建目录同名的优先",但构建目录叫 Debug、产物叫 demo.elf,两者永不相等,
|
|
199
|
+
* 于是退化成 readdir 顺序——改过工程名重新生成之后,会把旧固件当产物烧进板子。
|
|
200
|
+
* 现在的顺序:CMakeCache 里记的工程名(最权威)→ 修改时间最新(多产物时唯一有意义的判据)。
|
|
201
|
+
*/
|
|
202
|
+
export function findElf(buildDir) {
|
|
203
|
+
const picked = pickElf(buildDir);
|
|
204
|
+
if (picked === null)
|
|
205
|
+
return null;
|
|
206
|
+
const { chosen } = picked;
|
|
207
|
+
return { path: chosen.path, size: chosen.size, mtimeMs: chosen.mtimeMs };
|
|
208
|
+
}
|
|
209
|
+
/** 多个 .elf 并存时给一句说明(只有一个时返回空串,报告不加噪音)。 */
|
|
210
|
+
export function elfSelectionNote(buildDir) {
|
|
211
|
+
const picked = pickElf(buildDir);
|
|
212
|
+
if (picked === null || picked.candidates.length < 2)
|
|
213
|
+
return "";
|
|
214
|
+
const stamp = (ms) => new Date(ms).toISOString().slice(11, 19);
|
|
215
|
+
const others = picked.candidates.filter((item) => item.name !== picked.chosen.name);
|
|
216
|
+
const list = others.slice(0, 4).map((item) => item.name + "(" + stamp(item.mtimeMs) + ")").join("、");
|
|
217
|
+
return ("构建目录里有 " + String(picked.candidates.length) + " 个 .elf,用了 " + picked.chosen.name +
|
|
218
|
+
"(" + stamp(picked.chosen.mtimeMs) + "," +
|
|
219
|
+
(picked.byProjectName ? "按 CMakeCache 的工程名选" : "按修改时间最新选") +
|
|
220
|
+
");没动的 " + String(others.length) + " 个:" + list);
|
|
157
221
|
}
|
|
158
222
|
/** 解析 cmake 可执行文件:优先显式 binDirs(msys64),否则交给 PATH。 */
|
|
159
223
|
function resolveCmake(subprocess, binDirs) {
|
|
@@ -293,8 +357,12 @@ export async function runBuild(subprocess, request) {
|
|
|
293
357
|
logTail: tail(log, 30),
|
|
294
358
|
};
|
|
295
359
|
const notes = [...preNotes];
|
|
296
|
-
if (elf !== null)
|
|
360
|
+
if (elf !== null) {
|
|
297
361
|
notes.push("产物:" + elf.path + "(" + String(elf.size) + " B)");
|
|
362
|
+
const selectionNote = elfSelectionNote(buildDir);
|
|
363
|
+
if (selectionNote !== "")
|
|
364
|
+
notes.push(selectionNote);
|
|
365
|
+
}
|
|
298
366
|
if (memoryUsage.length > 0)
|
|
299
367
|
notes.push("占用:" + memoryUsage.map((item) => item.region + " " + String(item.percent) + "%").join(","));
|
|
300
368
|
else if (elf !== null)
|
|
@@ -318,9 +386,40 @@ export async function runBuild(subprocess, request) {
|
|
|
318
386
|
if (cacheMismatch !== undefined) {
|
|
319
387
|
return fail(request, "build 目录是从别处复制或移动过来的:CMakeCache.txt 里记的绝对路径对不上(cmake 报错但退出码是 0,不拦会误报成功)。先 clean 或删掉 " + base.buildDir + " 再编译。", { ...base, errors, notes });
|
|
320
388
|
}
|
|
389
|
+
// 2b:按请求转换 hex / bin。objcopy 失败不判 build 失败——编译本身成功了,
|
|
390
|
+
// 转不出来只降级为 warning(给 CubeProgrammer 用户一句怎么自己转)。
|
|
391
|
+
const wanted = new Set(request.artifacts ?? []);
|
|
392
|
+
const converted = [];
|
|
393
|
+
if (elf !== null && (wanted.has("hex") || wanted.has("bin"))) {
|
|
394
|
+
const objcopy = await resolveToolIn(subprocess, ["arm-none-eabi-objcopy"], config.binDirs, request.signal);
|
|
395
|
+
for (const kind of ["hex", "bin"]) {
|
|
396
|
+
if (!wanted.has(kind))
|
|
397
|
+
continue;
|
|
398
|
+
if (objcopy === null) {
|
|
399
|
+
notes.push("要 " + kind + " 但找不到 arm-none-eabi-objcopy(工具链不完整);跳过转换,elf 仍可用");
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const out = elf.path.replace(/\.elf$/u, "") + (kind === "hex" ? ".hex" : ".bin");
|
|
403
|
+
const conv = await runCapture(subprocess, {
|
|
404
|
+
argv: [objcopy, "-O", kind === "hex" ? "ihex" : "binary", elf.path, out],
|
|
405
|
+
cwd: buildDir,
|
|
406
|
+
timeoutMs: 60_000,
|
|
407
|
+
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
408
|
+
});
|
|
409
|
+
if (conv.spawnError === null && conv.exitCode === 0 && existsSync(out)) {
|
|
410
|
+
const size = statSync(out).size;
|
|
411
|
+
converted.push({ kind, path: out, size });
|
|
412
|
+
notes.push(kind + ":" + out + "(" + String(size) + " B)");
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
notes.push(kind + " 转换失败(" + (conv.spawnError ?? "退出码 " + String(conv.exitCode)) + ");elf 仍可用");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
321
419
|
return {
|
|
322
420
|
ok: true,
|
|
323
421
|
...base,
|
|
422
|
+
...(converted.length === 0 ? {} : { converted }),
|
|
324
423
|
notes,
|
|
325
424
|
message: "编译成功(" + (durationMs / 1000).toFixed(1) + " s," + String(errors.length) + " 个错误、" + String(base.warningCount) + " 个警告)",
|
|
326
425
|
};
|
package/lib/engine/cache.js
CHANGED
|
@@ -12,6 +12,7 @@ import crypto from "node:crypto";
|
|
|
12
12
|
import fs from "node:fs";
|
|
13
13
|
import os from "node:os";
|
|
14
14
|
import path from "node:path";
|
|
15
|
+
import { cmsisEntryAliases } from "../data/cmsis-sources.js";
|
|
15
16
|
export const CACHE_INDEX_VERSION = 1;
|
|
16
17
|
/** 缓存根目录:环境变量优先,其次 LOCALAPPDATA,再退 USERPROFILE、临时目录。 */
|
|
17
18
|
export function cacheRootInfo(env = process.env) {
|
|
@@ -224,6 +225,61 @@ export function removeCacheEntry(id, env = process.env) {
|
|
|
224
225
|
export function findCacheEntry(index, kind, id) {
|
|
225
226
|
return index.entries.find((item) => item.kind === kind && item.id === id) ?? null;
|
|
226
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* 按"用户可能怎么写"找缓存条目:先精确命中 id,再按 cmsis 条目的别名找。
|
|
230
|
+
*
|
|
231
|
+
* 为什么需要:条目 id 的规范写法变过(0.1 是 cmsis:F1:startup_stm32f103xb,0.2 是
|
|
232
|
+
* cmsis:F1:103xb),而用户是照着文档/旧报错/记忆敲的(cmsis:F1:startup_stm32f103xb、
|
|
233
|
+
* F1:B、STM32F103C8T6 都可能)。只做精确匹配的后果是:缓存里明明有一份健康的 14 个文件,
|
|
234
|
+
* 却被判"没缓存"重下一遍;purge 更是删不掉——用户以为删了,下次还是旧文件。
|
|
235
|
+
*/
|
|
236
|
+
export function resolveCacheEntry(index, ref) {
|
|
237
|
+
const direct = findCacheEntry(index, "cmsis", ref);
|
|
238
|
+
if (direct !== null)
|
|
239
|
+
return direct;
|
|
240
|
+
for (const candidate of cmsisEntryAliases(ref)) {
|
|
241
|
+
const found = findCacheEntry(index, "cmsis", candidate);
|
|
242
|
+
if (found !== null)
|
|
243
|
+
return found;
|
|
244
|
+
}
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
/** 0.1 的老 id:cmsis:F1:startup_stm32f103xb(0.2 是 cmsis:F1:103xb)。 */
|
|
248
|
+
const LEGACY_CMSIS_ID = /^cmsis:([A-Za-z0-9]+):startup_(stm32f1\d{2}[0-9a-z]+)$/u;
|
|
249
|
+
/**
|
|
250
|
+
* 清掉「已被新 id 取代」的旧缓存条目。
|
|
251
|
+
*
|
|
252
|
+
* 为什么要专门做:0.2 把 id 从 cmsis:F1:startup_stm32f103xb 改成 cmsis:F1:103xb,
|
|
253
|
+
* resolveCacheEntry 靠别名让老条目照样能命中——于是同一份模板可能在磁盘上留两份
|
|
254
|
+
* (每份 11 个文件、几百 KB)。只有「新 id 真的存在」时才删旧的,否则删了下次还得重下。
|
|
255
|
+
*/
|
|
256
|
+
export function pruneLegacyEntries(env = process.env) {
|
|
257
|
+
const read = readCacheIndex(env);
|
|
258
|
+
const index = read.index;
|
|
259
|
+
const removed = [];
|
|
260
|
+
const kept = [];
|
|
261
|
+
let reclaimedBytes = 0;
|
|
262
|
+
for (const entry of index.entries) {
|
|
263
|
+
const match = LEGACY_CMSIS_ID.exec(entry.id);
|
|
264
|
+
if (match === null)
|
|
265
|
+
continue;
|
|
266
|
+
const family = match[1] ?? "";
|
|
267
|
+
const stem = match[2] ?? "";
|
|
268
|
+
const line = stem.slice(6, 9);
|
|
269
|
+
const group = stem.slice(9);
|
|
270
|
+
const newId = "cmsis:" + family + ":" + line + group;
|
|
271
|
+
if (findCacheEntry(index, "cmsis", newId) === null) {
|
|
272
|
+
kept.push(entry.id);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
const result = removeCacheEntry(entry.id, env);
|
|
276
|
+
if (result.removed) {
|
|
277
|
+
removed.push(entry.id);
|
|
278
|
+
reclaimedBytes += entry.totalBytes;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return { removed, kept, reclaimedBytes };
|
|
282
|
+
}
|
|
227
283
|
/**
|
|
228
284
|
* 校验条目里的文件还在不在、内容对不对。
|
|
229
285
|
* 有 sha256 的条目比内容哈希(A38):字节数只能挡半截文件,挡不住"同样大小、内容被换掉"。
|