dsh-stm32 0.1.2 → 0.1.4
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 +92 -40
- package/README_EN.md +85 -36
- package/assets/README.md +85 -0
- package/assets/VERSION +1 -0
- package/assets/device/c0/chips.json +1108 -0
- package/assets/device/f0/chips.json +1570 -0
- package/assets/device/f1/chips.json +1238 -0
- package/assets/device/f1/svd/100.json +943 -0
- package/assets/device/f1/svd/101.json +901 -0
- package/assets/device/f1/svd/102.json +522 -0
- package/assets/device/f1/svd/103.json +1139 -0
- package/assets/device/f1/svd/105.json +1117 -0
- package/assets/device/f1/svd/107.json +1195 -0
- package/assets/device/f2/chips.json +674 -0
- package/assets/device/f3/chips.json +1318 -0
- package/assets/device/f4/chips.json +2942 -0
- package/assets/device/f7/chips.json +1710 -0
- package/assets/device/g0/chips.json +2102 -0
- package/assets/device/g4/chips.json +2494 -0
- package/assets/device/h5/chips.json +898 -0
- package/assets/device/h7/chips.json +1948 -0
- package/assets/device/l0/chips.json +2494 -0
- package/assets/device/l1/chips.json +1234 -0
- package/assets/device/l4/chips.json +2130 -0
- package/assets/device/l4+/chips.json +856 -0
- package/assets/device/l5/chips.json +226 -0
- package/assets/device/u0/chips.json +688 -0
- package/assets/device/u3/chips.json +310 -0
- package/assets/device/u5/chips.json +884 -0
- package/assets/device/wba/chips.json +506 -0
- package/assets/device/wl/chips.json +184 -0
- package/assets/headers/f1/stm32f100xb.h +5972 -0
- package/assets/headers/f1/stm32f100xe.h +6587 -0
- package/assets/headers/f1/stm32f101x6.h +5315 -0
- package/assets/headers/f1/stm32f101xb.h +5446 -0
- package/assets/headers/f1/stm32f101xe.h +6503 -0
- package/assets/headers/f1/stm32f101xg.h +6713 -0
- package/assets/headers/f1/stm32f102x6.h +6448 -0
- package/assets/headers/f1/stm32f102xb.h +6570 -0
- package/assets/headers/f1/stm32f103x6.h +10109 -0
- package/assets/headers/f1/stm32f103xb.h +10240 -0
- package/assets/headers/f1/stm32f103xe.h +11767 -0
- package/assets/headers/f1/stm32f103xg.h +11951 -0
- package/assets/headers/f1/stm32f105xc.h +14340 -0
- package/assets/headers/f1/stm32f107xc.h +15253 -0
- package/assets/headers/f1/stm32f1xx.h +273 -0
- package/assets/headers/f1/system_stm32f1xx.h +96 -0
- package/assets/ioc/family/C0.ioc +82 -0
- package/assets/ioc/family/F0.ioc +71 -0
- package/assets/ioc/family/F2.ioc +91 -0
- package/assets/ioc/family/F3.ioc +94 -0
- package/assets/ioc/family/F4.ioc +91 -0
- package/assets/ioc/family/F7.ioc +102 -0
- package/assets/ioc/family/G0.ioc +90 -0
- package/assets/ioc/family/G4.ioc +110 -0
- package/assets/ioc/family/H5.ioc +116 -0
- package/assets/ioc/family/H7.ioc +133 -0
- package/assets/ioc/family/L0.ioc +92 -0
- package/assets/ioc/family/L1.ioc +87 -0
- package/assets/ioc/family/L4+.ioc +92 -0
- package/assets/ioc/family/L4.ioc +93 -0
- package/assets/ioc/family/L5.ioc +91 -0
- package/assets/ioc/family/U0.ioc +103 -0
- package/assets/ioc/family/U3.ioc +113 -0
- package/assets/ioc/family/U5.ioc +102 -0
- package/assets/ioc/family/WBA.ioc +100 -0
- package/assets/ioc/family/WL.ioc +93 -0
- package/assets/ioc/minimal/STM32F100C8Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F100RCTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F101C4Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F101C8Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F101RCTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F101RFTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F102C4Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F102C8Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F103C4Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F103C8Tx.ioc +113 -0
- package/assets/ioc/minimal/STM32F103RCTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F103RFTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F105RCTx.ioc +113 -0
- package/assets/ioc/minimal/STM32F107RCTx.ioc +113 -0
- package/assets/ld/f1/STM32F100C8Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F100RCTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F101C4Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F101C8Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F101RCTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F101RFTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F102C4Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F102C8Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F103C4Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F103C8Tx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F103RCTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F103RFTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F105RCTx_FLASH.ld +249 -0
- package/assets/ld/f1/STM32F107RCTx_FLASH.ld +249 -0
- package/assets/manifest.json +1130 -0
- package/assets/startup/f1/startup_stm32f100xb.s +407 -0
- package/assets/startup/f1/startup_stm32f100xe.s +449 -0
- package/assets/startup/f1/startup_stm32f101x6.s +322 -0
- package/assets/startup/f1/startup_stm32f101xb.s +338 -0
- package/assets/startup/f1/startup_stm32f101xe.s +423 -0
- package/assets/startup/f1/startup_stm32f101xg.s +439 -0
- package/assets/startup/f1/startup_stm32f102x6.s +330 -0
- package/assets/startup/f1/startup_stm32f102xb.s +346 -0
- package/assets/startup/f1/startup_stm32f103x6.s +348 -0
- package/assets/startup/f1/startup_stm32f103xb.s +364 -0
- package/assets/startup/f1/startup_stm32f103xe.s +470 -0
- package/assets/startup/f1/startup_stm32f103xg.s +466 -0
- package/assets/startup/f1/startup_stm32f105xc.s +463 -0
- package/assets/startup/f1/startup_stm32f107xc.s +472 -0
- package/assets/templates/family/C0.ioc +82 -0
- package/assets/templates/family/F0.ioc +71 -0
- package/assets/templates/family/F2.ioc +91 -0
- package/assets/templates/family/F3.ioc +94 -0
- package/assets/templates/family/F4.ioc +91 -0
- package/assets/templates/family/F7.ioc +102 -0
- package/assets/templates/family/G0.ioc +90 -0
- package/assets/templates/family/G4.ioc +110 -0
- package/assets/templates/family/H5.ioc +116 -0
- package/assets/templates/family/H7.ioc +133 -0
- package/assets/templates/family/L0.ioc +92 -0
- package/assets/templates/family/L1.ioc +87 -0
- package/assets/templates/family/L4+.ioc +92 -0
- package/assets/templates/family/L4.ioc +93 -0
- package/assets/templates/family/L5.ioc +91 -0
- package/assets/templates/family/U0.ioc +103 -0
- package/assets/templates/family/U3.ioc +113 -0
- package/assets/templates/family/U5.ioc +102 -0
- package/assets/templates/family/WB.ioc +81 -0
- package/assets/templates/family/WBA.ioc +100 -0
- package/assets/templates/family/WL.ioc +93 -0
- package/docs/reading-guide.md +94 -71
- package/lib/data/chips.js +38 -51
- package/lib/data/clock-db.js +830 -0
- package/lib/data/clock-f1.js +12 -45
- package/lib/data/cmsis-sources.js +264 -17
- package/lib/data/db-presets.js +252 -0
- package/lib/data/device-facts.js +274 -0
- package/lib/data/family.js +515 -0
- package/lib/data/firmware-packages.js +562 -0
- package/lib/data/install-guides.js +2 -2
- package/lib/data/ioc-presets.js +248 -0
- package/lib/data/ioc-schema.js +1058 -0
- package/lib/data/rm-topics/stm32c0.json +227 -0
- package/lib/data/rm-topics.js +103 -0
- package/lib/data/search-roots.js +42 -1
- package/lib/data/stm32f1.js +95 -150
- package/lib/data/svd.js +643 -0
- package/lib/data/toolchain-config.js +0 -14
- package/lib/engine/assets.js +659 -0
- package/lib/engine/breakpoints.js +297 -0
- package/lib/engine/clock-derived.js +101 -0
- package/lib/engine/clock-direct.js +313 -0
- package/lib/engine/clock-plan.js +96 -52
- package/lib/engine/clock-pllmn.js +319 -0
- package/lib/engine/cmake-sources.js +67 -13
- package/lib/engine/cube-mx-guard.js +44 -0
- package/lib/engine/cube-programmer.js +137 -0
- package/lib/engine/cubemx-db.js +273 -0
- package/lib/engine/debug-session.js +576 -22
- package/lib/engine/debug.js +99 -46
- package/lib/engine/doctor.js +162 -19
- package/lib/engine/facts-acquire.js +416 -0
- package/lib/engine/facts.js +134 -0
- package/lib/engine/firmware-install.js +176 -5
- package/lib/engine/firmware-official.js +451 -0
- package/lib/engine/flash.js +317 -76
- package/lib/engine/gdb-mi.js +476 -0
- package/lib/engine/generate.js +9 -2
- package/lib/engine/hal-chip.js +124 -41
- package/lib/engine/ioc-plan.js +1247 -63
- package/lib/engine/ioc-rules.js +698 -0
- package/lib/engine/ioc-verify.js +706 -0
- package/lib/engine/ioc.js +43 -4
- package/lib/engine/msys2-install.js +4 -4
- package/lib/engine/probe-error.js +231 -0
- package/lib/engine/probe-session.js +347 -0
- package/lib/engine/probe.js +18 -3
- package/lib/engine/register-clock.js +41 -0
- package/lib/engine/register-gpio.js +115 -0
- package/lib/engine/register-linker.js +12 -7
- package/lib/engine/register-template.js +130 -45
- package/lib/engine/regs.js +43 -8
- package/lib/engine/rm-extract.js +144 -0
- package/lib/engine/rm-sources.js +360 -0
- package/lib/engine/rm-text.js +56 -0
- package/lib/engine/rtt.js +758 -0
- package/lib/engine/scaffold.js +185 -18
- package/lib/engine/toolchain-gate.js +0 -4
- package/lib/engine/watch.js +227 -0
- package/lib/index.js +12 -2
- package/lib/skill/stm32-workflow.js +2 -2
- package/lib/tools/assets.js +272 -0
- package/lib/tools/cache.js +8 -0
- package/lib/tools/debug-session.js +41 -6
- package/lib/tools/debug.js +41 -11
- package/lib/tools/doctor.js +65 -4
- package/lib/tools/firmware-install.js +31 -6
- package/lib/tools/flash.js +47 -10
- package/lib/tools/generate.js +25 -0
- package/lib/tools/ioc-apply.js +2 -0
- package/lib/tools/ioc-inspect.js +2 -0
- package/lib/tools/ioc-plan.js +59 -2
- package/lib/tools/ioc-verify.js +237 -0
- package/lib/tools/project-create.js +63 -3
- package/lib/tools/regs.js +453 -61
- package/lib/tools/route-plan.js +3 -0
- package/lib/tools/rtt.js +470 -0
- package/lib/tools/watch.js +256 -0
- package/package.json +5 -4
- package/templates/openocd/stm32c0x.cfg +83 -0
- package/templates/openocd/stm32u0x.cfg +64 -0
- package/templates/register/CMakeLists.txt +8 -2
- package/templates/register/Core/Inc/rtt.h +138 -0
- package/templates/register/Core/Src/main.c +42 -25
- package/templates/register/Core/Src/rtt.c +373 -0
- package/templates/scaffold/RTT.md +90 -0
- package/templates/scaffold/STM32F103XX_FLASH.ld +3 -0
- package/templates/scaffold/cmake/user/CMakeLists.txt +7 -0
- package/templates/scaffold/openocd.cfg +5 -3
- package/templates/scaffold/vscode/launch.json +8 -4
- package/docs/development-notes.md +0 -2116
- package/docs/dsh-stm32-plugin-/350/247/204/346/240/274.md +0 -673
- package/docs/logic-audit.md +0 -607
- package/lib/data/f1-ram.generated.js +0 -34
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* facts 获取自动化:SVD(结构)与 RM(语义载体)的取得与降级。
|
|
3
|
+
*
|
|
4
|
+
* SVD 上游取证(2026-11-07,GitHub API 实测):github.com/modm-io/cmsis-svd-stm32,
|
|
5
|
+
* default_branch=main、261★、未归档、pushed_at=2026-07-09。布局 <族小写>/<LINE>.svd,
|
|
6
|
+
* 例如 stm32c0/STM32C031.svd(API contents 实测:C0 6 份、F1 5 份、L4 16 份、H5 10 份,命名同规则)。
|
|
7
|
+
* 本机 D:/JingYu/STM32/cmsis-svd-stm32 的 origin 就是该仓。
|
|
8
|
+
*
|
|
9
|
+
* 为什么 SVD 不锁 sha256:本机旧快照(e79021a)与 main 当前内容对同名文件已经漂移
|
|
10
|
+
* (STM32C031.svd 两边字节不同),上游每次 "Update STM32 SVD files" 都可能改。
|
|
11
|
+
* 这里用 minBytes + markers 认「是不是 SVD」,把**实际取到**的 sha256 记进 facts.sources:
|
|
12
|
+
* 可追溯,但不作为失败条件。
|
|
13
|
+
*
|
|
14
|
+
* RM 通道(两条,都不猜):
|
|
15
|
+
* · 本机 PDF:--rm-pdf <路径>,只哈希登记,不解析(语义抽取是下一项工作)。
|
|
16
|
+
* · 显式 URL:--rm-url <https 直链>,下载后按 %PDF 特征校验。
|
|
17
|
+
* 不手搓「RM 号 → st.com 长 slug」表:实测 rm0490 长 slug 直链 200(11.5 MB application/pdf)、
|
|
18
|
+
* 短链 rm0490.pdf 404 —— slug 含整篇标题,只能人从官网页面拿,写死进插件就是失真快照。
|
|
19
|
+
* 语义仍来自人工抽取的 rm-facts JSON({doc, items});拿不到就 semantics=0,
|
|
20
|
+
* facts.needs 说明缺什么,工具层(flash 门禁/describeFacts)自动走只读降级。
|
|
21
|
+
*
|
|
22
|
+
* 联网禁止:opts.allowNetwork=false 或环境变量 DSH_STM32_OFFLINE=1 时只走本地,
|
|
23
|
+
* 本地也没有 → 明确失败并把试过的路径写进 attempts。
|
|
24
|
+
*/
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import fs from "node:fs";
|
|
27
|
+
import path from "node:path";
|
|
28
|
+
import { fetchOne } from "./fetch.js";
|
|
29
|
+
import { acquireRmViaSitemap, acquireRmViaStmcu } from "./rm-sources.js";
|
|
30
|
+
import { factsRoot, partToFamilyLine, readFactsFor } from "./facts.js";
|
|
31
|
+
import { loadRmTopics } from "../data/rm-topics.js";
|
|
32
|
+
import { defaultPdfTextBridge } from "./rm-text.js";
|
|
33
|
+
import { extractRmFacts, assembleRmFacts } from "./rm-extract.js";
|
|
34
|
+
/** SVD 上游仓库与引用(实测见文件头)。 */
|
|
35
|
+
export const SVD_REPO = "modm-io/cmsis-svd-stm32";
|
|
36
|
+
export const SVD_REF = "main";
|
|
37
|
+
/** facts 关注的寄存器(RM/SVD 交叉验证启动与调试门禁就用这三个)。 */
|
|
38
|
+
export const FACTS_REGISTERS = ["FLASH_OPTR", "FLASH_ACR", "FLASH_SECR"];
|
|
39
|
+
function nodeText(node, tag) {
|
|
40
|
+
const m = node.match(new RegExp("<" + tag + ">([^<]*)</" + tag + ">"));
|
|
41
|
+
const value = m?.[1] ?? "";
|
|
42
|
+
return value.replace(/\s+/gu, " ").trim();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 从 SVD XML 抽 FLASH 外设的寄存器结构(含枚举)。
|
|
46
|
+
*
|
|
47
|
+
* 收集策略两级(2026-11-07 实测教训):
|
|
48
|
+
* A. 首选按外设收:<peripheral> 的 name==FLASH 下的**全部** <register>。
|
|
49
|
+
* modm 的寄存器命名逐族不同——C0 带前缀(FLASH_ACR/FLASH_OPTR/FLASH_SECR)、
|
|
50
|
+
* F1 不带(ACR/OBR/CR…);写死名字清单等于拿 C0 约定套全族,F1 会解析出 0 寄存器
|
|
51
|
+
* (自愈真链路就是这么炸的)。名字清单只是兜底口径,不是主口径。
|
|
52
|
+
* B. 退回全局按 FACTS_REGISTERS 名单过滤:微型片段 SVD 可能没有 peripheral 包装
|
|
53
|
+
* (单测夹具就是那形状)。
|
|
54
|
+
*/
|
|
55
|
+
export function parseSvdStructure(xml, registers = FACTS_REGISTERS) {
|
|
56
|
+
const result = {};
|
|
57
|
+
const collectFrom = (chunk, filterByName) => {
|
|
58
|
+
for (const m of chunk.matchAll(/<register>([\s\S]*?)<\/register>/g)) {
|
|
59
|
+
const reg = m?.[1] ?? "";
|
|
60
|
+
const name = nodeText(reg, "name");
|
|
61
|
+
if (name === "")
|
|
62
|
+
continue;
|
|
63
|
+
if (filterByName && !registers.includes(name))
|
|
64
|
+
continue;
|
|
65
|
+
const fields = [];
|
|
66
|
+
for (const fm of reg.matchAll(/<field>([\s\S]*?)<\/field>/g)) {
|
|
67
|
+
const fl = fm?.[1] ?? "";
|
|
68
|
+
const enums = [];
|
|
69
|
+
for (const em of fl.matchAll(/<enumeratedValue>([\s\S]*?)<\/enumeratedValue>/g)) {
|
|
70
|
+
const ebody = em?.[1] ?? "";
|
|
71
|
+
enums.push({ value: nodeText(ebody, "value"), description: nodeText(ebody, "description") });
|
|
72
|
+
}
|
|
73
|
+
fields.push({
|
|
74
|
+
name: nodeText(fl, "name"),
|
|
75
|
+
bitOffset: Number(nodeText(fl, "bitOffset")),
|
|
76
|
+
bitWidth: Number(nodeText(fl, "bitWidth")),
|
|
77
|
+
description: nodeText(fl, "description"),
|
|
78
|
+
enums,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
fields.sort((a, b) => a.bitOffset - b.bitOffset);
|
|
82
|
+
result[name] = { description: nodeText(reg, "description"), fields };
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
for (const pm of xml.matchAll(/<peripheral>([\s\S]*?)<\/peripheral>/g)) {
|
|
86
|
+
const block = pm?.[1] ?? "";
|
|
87
|
+
if (nodeText(block, "name").toUpperCase() !== "FLASH")
|
|
88
|
+
continue;
|
|
89
|
+
collectFrom(block, false);
|
|
90
|
+
}
|
|
91
|
+
if (Object.keys(result).length === 0)
|
|
92
|
+
collectFrom(xml, true);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
function sha256(buf) {
|
|
96
|
+
return createHash("sha256").update(buf).digest("hex");
|
|
97
|
+
}
|
|
98
|
+
/** SVD 的候选下载 URL,顺序 = 优先级(同 cmsis-sources 的实测结论:jsDelivr 最稳,raw 兜底)。 */
|
|
99
|
+
export function svdSourceUrls(relFile, repo = SVD_REPO, ref = SVD_REF) {
|
|
100
|
+
return [
|
|
101
|
+
"https://fastly.jsdelivr.net/gh/" + repo + "@" + ref + "/" + relFile,
|
|
102
|
+
"https://cdn.jsdelivr.net/gh/" + repo + "@" + ref + "/" + relFile,
|
|
103
|
+
"https://raw.githubusercontent.com/" + repo + "/" + ref + "/" + relFile,
|
|
104
|
+
"https://ghproxy.net/https://raw.githubusercontent.com/" + repo + "/" + ref + "/" + relFile,
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
/** 取一份 SVD:显式路径 → 本地仓库根 → 下载缓存 →(允许时)网络下载。 */
|
|
108
|
+
export async function acquireSvd(part, o = {}) {
|
|
109
|
+
const attempts = [];
|
|
110
|
+
const fail = (error) => ({
|
|
111
|
+
ok: false, file: "", source: "", url: "", sha256: "", bytes: 0, attempts, error, structure: {},
|
|
112
|
+
});
|
|
113
|
+
const target = partToFamilyLine(part);
|
|
114
|
+
if (target === null)
|
|
115
|
+
return fail("认不出型号:" + part);
|
|
116
|
+
const rel = target.family + "/" + target.line + ".svd";
|
|
117
|
+
const env = o.env ?? process.env;
|
|
118
|
+
const cacheDir = o.cacheDir ?? path.join(factsRoot(env), "svd");
|
|
119
|
+
const allowNetwork = o.allowNetwork ?? (env.DSH_STM32_OFFLINE ?? "").trim() !== "1";
|
|
120
|
+
const candidates = [];
|
|
121
|
+
if (o.svdPath !== undefined && o.svdPath !== "")
|
|
122
|
+
candidates.push(path.resolve(o.svdPath));
|
|
123
|
+
for (const root of o.svdRoots ?? [])
|
|
124
|
+
candidates.push(path.join(root, rel));
|
|
125
|
+
candidates.push(path.join(cacheDir, rel));
|
|
126
|
+
for (const cand of candidates) {
|
|
127
|
+
if (!fs.existsSync(cand)) {
|
|
128
|
+
attempts.push(cand + " → 不存在");
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const buf = fs.readFileSync(cand);
|
|
132
|
+
const structure = parseSvdStructure(buf.toString("utf8"));
|
|
133
|
+
if (Object.keys(structure).length === 0) {
|
|
134
|
+
attempts.push(cand + " → 找到了但没解析出 " + FACTS_REGISTERS.join("/") + "(不是预期内容)");
|
|
135
|
+
return fail("本地 SVD 内容对不上:" + cand);
|
|
136
|
+
}
|
|
137
|
+
return { ok: true, file: cand, source: "local", url: "", sha256: sha256(buf), bytes: buf.byteLength, attempts, error: "", structure };
|
|
138
|
+
}
|
|
139
|
+
if (!allowNetwork)
|
|
140
|
+
return fail("本地没有该 SVD,且联网被禁(DSH_STM32_OFFLINE)");
|
|
141
|
+
const outcome = await fetchOne({
|
|
142
|
+
path: rel,
|
|
143
|
+
urls: svdSourceUrls(rel),
|
|
144
|
+
minBytes: o.minBytes ?? 50_000,
|
|
145
|
+
// markers 认「是不是 SVD」而不是「是不是某个版本」:上游会漂移,见文件头。
|
|
146
|
+
markers: ["<device", "<cpu>"],
|
|
147
|
+
}, cacheDir, { fetchImpl: o.fetchImpl, timeoutMs: o.timeoutMs ?? 240_000, signal: o.signal });
|
|
148
|
+
for (const a of outcome.attempts)
|
|
149
|
+
attempts.push(a);
|
|
150
|
+
if (!outcome.ok)
|
|
151
|
+
return fail(outcome.error);
|
|
152
|
+
const svdFile = path.join(cacheDir, outcome.path === "" ? rel : outcome.path);
|
|
153
|
+
const buf = fs.readFileSync(svdFile);
|
|
154
|
+
const structure = parseSvdStructure(buf.toString("utf8"));
|
|
155
|
+
if (Object.keys(structure).length === 0)
|
|
156
|
+
return fail("下载内容解析不出 " + FACTS_REGISTERS.join("/") + ":" + outcome.url);
|
|
157
|
+
return {
|
|
158
|
+
ok: true,
|
|
159
|
+
file: svdFile,
|
|
160
|
+
source: "remote",
|
|
161
|
+
url: outcome.url,
|
|
162
|
+
sha256: sha256(buf),
|
|
163
|
+
bytes: buf.byteLength,
|
|
164
|
+
attempts,
|
|
165
|
+
error: "",
|
|
166
|
+
structure,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** RM 载体:rm-facts(语义)+ 本机 PDF 或显式 URL(只登记哈希,不解析)。 */
|
|
170
|
+
export async function acquireRm(o = {}) {
|
|
171
|
+
const env = o.env ?? process.env;
|
|
172
|
+
const items = [];
|
|
173
|
+
const sources = [];
|
|
174
|
+
const notes = [];
|
|
175
|
+
let doc = null;
|
|
176
|
+
let error = "";
|
|
177
|
+
if (o.rmFactsPath !== undefined && o.rmFactsPath !== "") {
|
|
178
|
+
try {
|
|
179
|
+
const raw = fs.readFileSync(path.resolve(o.rmFactsPath), "utf8");
|
|
180
|
+
const parsed = JSON.parse(raw);
|
|
181
|
+
doc = parsed.doc ?? null;
|
|
182
|
+
if (Array.isArray(parsed.items))
|
|
183
|
+
items.push(...parsed.items);
|
|
184
|
+
sources.push({ kind: "rm-facts", file: path.basename(o.rmFactsPath), sha256: sha256(Buffer.from(raw, "utf8")), path: path.resolve(o.rmFactsPath) });
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
error = "rm-facts 读不了(" + o.rmFactsPath + "):" + (e instanceof Error ? e.message : String(e));
|
|
188
|
+
notes.push(error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (o.rmPdfPath !== undefined && o.rmPdfPath !== "") {
|
|
192
|
+
const p = path.resolve(o.rmPdfPath);
|
|
193
|
+
if (fs.existsSync(p)) {
|
|
194
|
+
const buf = fs.readFileSync(p);
|
|
195
|
+
if (!buf.subarray(0, 5).toString("latin1").startsWith("%PDF")) {
|
|
196
|
+
notes.push("rm-pdf 不是 PDF 头(" + p + "),只登记不采信");
|
|
197
|
+
}
|
|
198
|
+
sources.push({ kind: "rm-pdf", file: path.basename(p), sha256: sha256(buf), bytes: buf.byteLength, path: p });
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
notes.push("rm-pdf 不存在:" + p);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (o.rmUrl !== undefined && o.rmUrl !== "") {
|
|
205
|
+
const allowNetwork = o.allowNetwork ?? (env.DSH_STM32_OFFLINE ?? "").trim() !== "1";
|
|
206
|
+
if (!allowNetwork) {
|
|
207
|
+
notes.push("rm-url 被跳过(离线模式):" + o.rmUrl);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const name = path.basename(new URL(o.rmUrl).pathname);
|
|
211
|
+
const destRoot = o.cacheDir ?? path.join(factsRoot(env), "rm");
|
|
212
|
+
const outcome = await fetchOne({ path: name, urls: [o.rmUrl], minBytes: 200_000, markers: ["%PDF"] }, destRoot, { fetchImpl: o.fetchImpl, timeoutMs: o.timeoutMs ?? 240_000, signal: o.signal, stallMs: 20_000, minBytesPerSec: 4096 });
|
|
213
|
+
if (outcome.ok) {
|
|
214
|
+
const buf = fs.readFileSync(path.join(destRoot, outcome.path));
|
|
215
|
+
sources.push({ kind: "rm-pdf", file: name, sha256: sha256(buf), bytes: buf.byteLength, url: outcome.url });
|
|
216
|
+
notes.push("RM PDF 已缓存:" + path.join(destRoot, outcome.path) + "(" + String(buf.byteLength) + " B)");
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
error = error === "" ? "RM 下载失败:" + outcome.error : error + ";RM 下载失败:" + outcome.error;
|
|
220
|
+
notes.push("RM PDF 下载失败(语义不受影响):" + outcome.error);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return { items, doc, sources, notes, error };
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* 完整链路:SVD(本地→下载)+ RM(rm-facts/PDF/URL,可缺)→ facts.json。
|
|
228
|
+
* SVD 拿不到 = 失败(结构是 facts 的底线);RM 拿不到 = 降级(semantics=0 + needs 说明)。
|
|
229
|
+
*/
|
|
230
|
+
export async function buildFacts(o) {
|
|
231
|
+
const env = o.env ?? process.env;
|
|
232
|
+
const target = partToFamilyLine(o.part);
|
|
233
|
+
const upper = o.part.trim().toUpperCase();
|
|
234
|
+
if (target === null) {
|
|
235
|
+
return { ok: false, message: "认不出型号:" + o.part, file: "", part: upper, family: "", line: "", structure: {}, registers: "", semantics: 0, needs: ["型号解析失败"], notes: [], sources: [] };
|
|
236
|
+
}
|
|
237
|
+
const svd = await acquireSvd(upper, o);
|
|
238
|
+
if (!svd.ok) {
|
|
239
|
+
return { ok: false, message: svd.error, file: "", part: upper, family: target.family, line: target.line, structure: {}, registers: "", semantics: 0, needs: ["SVD 没拿到(本地与下载都失败)"], notes: svd.attempts, sources: [] };
|
|
240
|
+
}
|
|
241
|
+
const rm = await acquireRm(o);
|
|
242
|
+
const notes = [...rm.notes];
|
|
243
|
+
// 自动载体通道:rm-facts 只给语义,PDF 本体(含哈希/页码定位)由这里补齐。
|
|
244
|
+
const rmAuto = o.rmAuto ?? "auto";
|
|
245
|
+
const allowNet = o.allowNetwork ?? ((o.env ?? process.env).DSH_STM32_OFFLINE ?? "").trim() !== "1";
|
|
246
|
+
let acquired = null;
|
|
247
|
+
if (rmAuto !== "off" && allowNet) {
|
|
248
|
+
const hasPdfSource = rm.sources.some((s) => s.kind === "rm-pdf");
|
|
249
|
+
if (!hasPdfSource) {
|
|
250
|
+
const channelOpts = { fetchImpl: o.fetchImpl, cacheDir: o.rmCacheDir, env, timeoutMs: o.timeoutMs, signal: o.signal, minGapMs: o.minGapMs };
|
|
251
|
+
const order = rmAuto === "sitemap" ? ["sitemap"] : rmAuto === "stmcu" ? ["stmcu"] : ["sitemap", "stmcu"];
|
|
252
|
+
for (const ch of order) {
|
|
253
|
+
const attempt = ch === "sitemap"
|
|
254
|
+
? await acquireRmViaSitemap(target.family, target.line, channelOpts)
|
|
255
|
+
: await acquireRmViaStmcu(target.family, target.line, channelOpts);
|
|
256
|
+
if (attempt.ok) {
|
|
257
|
+
acquired = attempt;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
notes.push("RM 自动通道 " + ch + " 失败:" + attempt.error);
|
|
261
|
+
if (attempt.url !== "")
|
|
262
|
+
acquired = attempt; // 失败但也带着哈希/覆盖信息,留给 notes
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const pdfSource = acquired !== null && acquired.ok
|
|
267
|
+
? { kind: "rm-pdf", doc: acquired.docId, file: acquired.file.split(/[\\/]/).pop() ?? "", path: acquired.file, sha256: acquired.sha256, bytes: acquired.bytes, url: acquired.url, origin: "auto:" + acquired.channel, coveredLines: Object.keys(acquired.coverage) }
|
|
268
|
+
: null;
|
|
269
|
+
// spec 自动抽取:人没喂 rm-facts 而且这颗芯片有主题清单时,用可用的载体 PDF 自动抽取。
|
|
270
|
+
// 载体两种都要认:双通道自动下的,和显式 --rm-pdf/--rm-url 给的(用户本地手册场景)。
|
|
271
|
+
const explicitPdfSource = rm.sources.find((s) => s.kind === "rm-pdf" && typeof s.path === "string" && s.path !== "");
|
|
272
|
+
const explicitPdfPath = typeof explicitPdfSource?.path === "string" ? explicitPdfSource.path : "";
|
|
273
|
+
const specPdf = acquired !== null && acquired.ok ? acquired.file : explicitPdfPath;
|
|
274
|
+
if (rm.items.length === 0 && specPdf !== "" && fs.existsSync(specPdf) && o.autoSpec !== false) {
|
|
275
|
+
const loaded = loadRmTopics(target.family, target.line, factsRoot(env));
|
|
276
|
+
if (loaded.hasSpec) {
|
|
277
|
+
const pageSet = new Set();
|
|
278
|
+
for (const tp of loaded.topics)
|
|
279
|
+
for (let n = Math.max(1, tp.page - 3); n <= tp.page + 3; n += 1)
|
|
280
|
+
pageSet.add(n);
|
|
281
|
+
const bridge = o.pdfTextBridge ?? defaultPdfTextBridge;
|
|
282
|
+
const got = await bridge(specPdf, [...pageSet]);
|
|
283
|
+
if (got.ok) {
|
|
284
|
+
// SVD 交叉要 spec 里每条线的结构;目标线现成,其余线懒取(取不到记「该线 SVD 没取到」,不炸)
|
|
285
|
+
const svdByLine = { [target.line]: svd.structure };
|
|
286
|
+
for (const l of loaded.lines) {
|
|
287
|
+
if (svdByLine[l] !== undefined)
|
|
288
|
+
continue;
|
|
289
|
+
const one = await acquireSvd(l, o);
|
|
290
|
+
if (one.ok)
|
|
291
|
+
svdByLine[l] = one.structure;
|
|
292
|
+
}
|
|
293
|
+
const rep = extractRmFacts({ family: target.family, lines: loaded.lines, topics: loaded.topics }, got.pages, svdByLine);
|
|
294
|
+
const asm = assembleRmFacts({ id: loaded.docId !== "" ? loaded.docId : (acquired?.docId ?? "RM?"), source: "auto:rm-topics", origins: loaded.origins }, rep.items);
|
|
295
|
+
rm.items.push(...asm.items);
|
|
296
|
+
if (rm.doc === null)
|
|
297
|
+
rm.doc = asm.doc;
|
|
298
|
+
notes.push("spec 自动抽取:" + String(rep.items.length) + " 条采信 / " + String(rep.rejected.length) + " 条拒收(清单 " + loaded.origins.join(" + ") + ")");
|
|
299
|
+
for (const x of rep.rejected)
|
|
300
|
+
notes.push(" REJECT " + x.id + ":" + x.reason);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
notes.push("有 rm-topics 清单但 pymupdf 桥不可用:" + got.error + "(" + got.tried.join(";") + ")");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
// 语义按线分级:声明过的适用范围(--rm-lines 优先,其次 rm-facts 的 doc.lines)
|
|
308
|
+
// 不含目标线时一律拒收,宁可降级只读也不把别的线的 RM 结论套过来。
|
|
309
|
+
const declaredLines = (o.rmLines !== undefined && o.rmLines.length > 0 ? o.rmLines : [])
|
|
310
|
+
.concat(typeof rm.doc?.lines === "string" || Array.isArray(rm.doc?.lines)
|
|
311
|
+
? (Array.isArray(rm.doc?.lines) ? rm.doc?.lines : [String(rm.doc?.lines)]).map((x) => String(x).toUpperCase())
|
|
312
|
+
: []);
|
|
313
|
+
const uniqueLines = [...new Set(declaredLines)];
|
|
314
|
+
// 采信口径:条目级 appliesTo(自动抽取产出)优先,其次包级 doc.lines,都没有就整批按人声明走。
|
|
315
|
+
let items = rm.items.filter((it) => {
|
|
316
|
+
const ap = Array.isArray(it.appliesTo) ? it.appliesTo.map((x) => String(x).toUpperCase()) : [];
|
|
317
|
+
if (ap.length > 0)
|
|
318
|
+
return ap.includes(target.line);
|
|
319
|
+
// 老式条目(无 appliesTo):仍受包级 doc.lines/--rm-lines 约束——修一个刚引入的回归:
|
|
320
|
+
// 若这里直接放行,「doc.lines 不含目标线」的整批门禁对混合数据就失效了。
|
|
321
|
+
return uniqueLines.length === 0 || uniqueLines.includes(target.line);
|
|
322
|
+
});
|
|
323
|
+
const needs = [];
|
|
324
|
+
const wholeBatchRejected = rm.items.length > 0 && items.length === 0;
|
|
325
|
+
if (wholeBatchRejected && uniqueLines.length > 0 && !uniqueLines.includes(target.line)) {
|
|
326
|
+
needs.push("rm-facts 声明只覆盖 " + uniqueLines.join("/") + ",不含 " + target.line + ":语义 " + String(rm.items.length) + " 条已拒收,需要 " + target.line + " 自己的 RM 抽取");
|
|
327
|
+
items = [];
|
|
328
|
+
}
|
|
329
|
+
else if (wholeBatchRejected) {
|
|
330
|
+
needs.push("rm-facts 里没有任何条目声明适用于 " + target.line + "(appliesTo 逐条筛完为空):需要这条线的 RM 抽取");
|
|
331
|
+
items = [];
|
|
332
|
+
}
|
|
333
|
+
else if (rm.items.length > 0 && uniqueLines.length === 0 && !rm.items.some((it) => Array.isArray(it.appliesTo))) {
|
|
334
|
+
notes.push("提醒:rm-facts 没声明适用线(doc.lines/--rm-lines/条目 appliesTo),本次按目标线 " + target.line + " 原样采信;抽取语义的人要自己核对 RM 是否覆盖该线");
|
|
335
|
+
}
|
|
336
|
+
if (items.length === 0 && rm.items.length === 0) {
|
|
337
|
+
if (acquired !== null && acquired.ok) {
|
|
338
|
+
// 载体已经到位:要么这颗芯片没主题清单(不拦烧录,路径可反查),要么有清单但机检没过。
|
|
339
|
+
const hasSpecHere = loadRmTopics(target.family, target.line, factsRoot(env)).hasSpec;
|
|
340
|
+
needs.push(hasSpecHere
|
|
341
|
+
? target.line + " 有 rm-topics 清单但机检没过/桥不可用:逐条原因见 notes;修好清单或 PDF 改版前,本线按只读对待"
|
|
342
|
+
: "这颗芯片没有 rm-topics 主题清单:只有结构、无语义(不拦烧录);RM 已缓存在本地,调试遇怪行为可按 sources.rm-pdf 的路径反查原文");
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
needs.push("本线只有结构、语义未核实:需要 " + target.line + " 的 RM 手册与 rm-facts 抽取(SVD 只给结构,不给组合规则/流程/引脚)");
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const outDir = o.outDir !== undefined && o.outDir !== "" ? path.resolve(o.outDir) : path.join(factsRoot(env), target.family, target.line);
|
|
349
|
+
const outFile = path.join(outDir, "facts.json");
|
|
350
|
+
const facts = {
|
|
351
|
+
schema: 2,
|
|
352
|
+
part: upper,
|
|
353
|
+
family: "STM32" + target.family.slice(5).toUpperCase(),
|
|
354
|
+
line: target.line,
|
|
355
|
+
generatedAt: new Date().toISOString(),
|
|
356
|
+
sources: [
|
|
357
|
+
{ kind: "svd", file: path.basename(svd.file), sha256: svd.sha256, bytes: svd.bytes, path: svd.file, origin: svd.source === "remote" ? svd.url : "local" },
|
|
358
|
+
...rm.sources,
|
|
359
|
+
...(pdfSource === null ? [] : [pdfSource]),
|
|
360
|
+
],
|
|
361
|
+
registers: svd.structure,
|
|
362
|
+
semantics: items,
|
|
363
|
+
...(rm.doc === null ? {} : { rmDoc: rm.doc }),
|
|
364
|
+
...(uniqueLines.length === 0 ? {} : { rmAppliesTo: uniqueLines }),
|
|
365
|
+
needs,
|
|
366
|
+
};
|
|
367
|
+
if (o.write !== false) {
|
|
368
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
369
|
+
fs.writeFileSync(outFile, JSON.stringify(facts, null, 1), "utf8");
|
|
370
|
+
}
|
|
371
|
+
const regs = Object.entries(svd.structure).map(([k, v]) => k + "(" + String(v.fields.length) + ")").join(" ");
|
|
372
|
+
const svdHow = svd.source === "remote" ? "下载(" + svd.url + ")" : "本地(" + svd.file + ")";
|
|
373
|
+
return {
|
|
374
|
+
ok: true,
|
|
375
|
+
message: "facts -> " + outFile + "(SVD 来自" + svdHow + ";语义 " + String(items.length) + " 条)",
|
|
376
|
+
file: outFile,
|
|
377
|
+
part: upper,
|
|
378
|
+
family: facts.family,
|
|
379
|
+
line: target.line,
|
|
380
|
+
structure: svd.structure,
|
|
381
|
+
registers: regs,
|
|
382
|
+
semantics: items.length,
|
|
383
|
+
needs,
|
|
384
|
+
notes,
|
|
385
|
+
sources: facts.sources,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* 自愈读:查到就信,查不到/有清单但没核过就当场补一次书(SVD+RM 载体+结构,
|
|
390
|
+
* 有 rm-topics 还带语义抽取),再读一遍返回。用户决定(2026-11-07):
|
|
391
|
+
* 各族一律自动准备资产、无需人为干预;门禁严格与否由「这颗芯片有没有主题清单」决定。
|
|
392
|
+
*
|
|
393
|
+
* 何时重建:facts 不存在,或有清单但没 verified(常见于用户刚放了芯片级 spec)。
|
|
394
|
+
* 已经 verified 的直接返回,不反复下载。构建失败不炸——返回失败后的现状,
|
|
395
|
+
* 由调用方(flash 门禁/风险行)按三态处理。
|
|
396
|
+
*/
|
|
397
|
+
export async function ensureFacts(part, env = process.env, o = {}) {
|
|
398
|
+
const first = readFactsFor(part, env);
|
|
399
|
+
const needsBuild = !first.found || (first.hasSpec && !first.verified);
|
|
400
|
+
if (!needsBuild)
|
|
401
|
+
return { summary: first, built: { attempted: false, ok: true, message: "" } };
|
|
402
|
+
const rep = await buildFacts({
|
|
403
|
+
part,
|
|
404
|
+
env,
|
|
405
|
+
...(o.timeoutMs === undefined ? {} : { timeoutMs: o.timeoutMs }),
|
|
406
|
+
...(o.allowNetwork === undefined ? {} : { allowNetwork: o.allowNetwork }),
|
|
407
|
+
...(o.signal === undefined ? {} : { signal: o.signal }),
|
|
408
|
+
...(o.fetchImpl === undefined ? {} : { fetchImpl: o.fetchImpl }),
|
|
409
|
+
...(o.pdfTextBridge === undefined ? {} : { pdfTextBridge: o.pdfTextBridge }),
|
|
410
|
+
...(o.svdPath === undefined ? {} : { svdPath: o.svdPath }),
|
|
411
|
+
...(o.svdRoots === undefined ? {} : { svdRoots: o.svdRoots }),
|
|
412
|
+
...(o.rmPdfPath === undefined ? {} : { rmPdfPath: o.rmPdfPath }),
|
|
413
|
+
});
|
|
414
|
+
const after = readFactsFor(part, env);
|
|
415
|
+
return { summary: after, built: { attempted: true, ok: rep.ok, message: rep.message } };
|
|
416
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { loadRmTopics } from "../data/rm-topics.js";
|
|
5
|
+
/** facts 缓存根(可用 DSH_STM32_FACTS 覆盖)。 */
|
|
6
|
+
export function factsRoot(env = process.env) {
|
|
7
|
+
const custom = env.DSH_STM32_FACTS;
|
|
8
|
+
return custom !== undefined && custom !== "" ? custom : join(homedir(), ".dsh-stm32", "facts");
|
|
9
|
+
}
|
|
10
|
+
/** STM32C031F6P6 → { family: "c0", line: "STM32C031" }。 */
|
|
11
|
+
export function partToFamilyLine(part) {
|
|
12
|
+
const m = /^STM32([A-Z])(\d)(\d)(\d)/i.exec(part.trim().toUpperCase());
|
|
13
|
+
if (m === null)
|
|
14
|
+
return null;
|
|
15
|
+
const a = m[1] ?? "";
|
|
16
|
+
const b = m[2] ?? "";
|
|
17
|
+
const c = m[3] ?? "";
|
|
18
|
+
const d = m[4] ?? "";
|
|
19
|
+
if (a === "" || b === "" || c === "" || d === "")
|
|
20
|
+
return null;
|
|
21
|
+
return { family: "stm32" + (a + b).toLowerCase(), line: "STM32" + a + b + c + d };
|
|
22
|
+
}
|
|
23
|
+
/** 从工程 .ioc 里读 MCU 型号(Mcu.CPN 或 Mcu.UserName)。 */
|
|
24
|
+
export function mcuFromIoc(projectDir) {
|
|
25
|
+
try {
|
|
26
|
+
const files = readdirSync(projectDir).filter((name) => name.toLowerCase().endsWith(".ioc"));
|
|
27
|
+
for (const name of files) {
|
|
28
|
+
const text = readFileSync(join(projectDir, name), "utf8");
|
|
29
|
+
const cpn = /^Mcu\.CPN=(.+)$/mu.exec(text)?.[1]?.trim();
|
|
30
|
+
if (cpn !== undefined && cpn !== "")
|
|
31
|
+
return cpn;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
/** 给 summary 补 spec/载体两个维度(readFactsFor 的每条出口都过这里)。 */
|
|
40
|
+
function withSpecFields(s, part, env) {
|
|
41
|
+
const t = partToFamilyLine(part);
|
|
42
|
+
if (t === null)
|
|
43
|
+
return { ...s, hasSpec: false, rmPath: "" };
|
|
44
|
+
const loaded = loadRmTopics(t.family, t.line, factsRoot(env));
|
|
45
|
+
let rmPath = "";
|
|
46
|
+
try {
|
|
47
|
+
const raw = JSON.parse(readFileSync(join(factsRoot(env), t.family, t.line, "facts.json"), "utf8"));
|
|
48
|
+
const pdf = (raw.sources ?? []).find((x) => x.kind === "rm-pdf" && typeof x.path === "string" && existsSync(x.path));
|
|
49
|
+
rmPath = typeof pdf?.path === "string" ? pdf.path : "";
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// facts 还没生成或读不动:rmPath 保持空串,不影响门禁
|
|
53
|
+
}
|
|
54
|
+
return { ...s, hasSpec: loaded.hasSpec, rmPath };
|
|
55
|
+
}
|
|
56
|
+
export function readFactsFor(part, env = process.env) {
|
|
57
|
+
return withSpecFields(readFactsRaw(part, env), part, env);
|
|
58
|
+
}
|
|
59
|
+
function readFactsRaw(part, env) {
|
|
60
|
+
const target = partToFamilyLine(part);
|
|
61
|
+
if (target === null) {
|
|
62
|
+
return { found: false, path: "", part, line: "", semantics: 0, verified: false, lines: [], needs: ["无法从型号 " + part + " 解析出族/线"] };
|
|
63
|
+
}
|
|
64
|
+
const file = join(factsRoot(env), target.family, target.line, "facts.json");
|
|
65
|
+
if (!existsSync(file)) {
|
|
66
|
+
return {
|
|
67
|
+
found: false,
|
|
68
|
+
path: file,
|
|
69
|
+
part: part.toUpperCase(),
|
|
70
|
+
line: target.line,
|
|
71
|
+
semantics: 0,
|
|
72
|
+
verified: false,
|
|
73
|
+
lines: [],
|
|
74
|
+
needs: ["facts 未生成:node scripts/facts/build-facts.mjs --part " + part.toUpperCase() + "(SVD 会自动本地找→下载缓存→联网取;语义另需该线 rm-facts)"],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
let pkg;
|
|
78
|
+
try {
|
|
79
|
+
pkg = JSON.parse(readFileSync(file, "utf8"));
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
return { found: false, path: file, part, line: target.line, semantics: 0, verified: false, lines: [], needs: ["facts.json 解析失败:" + (error instanceof Error ? error.message : String(error))] };
|
|
83
|
+
}
|
|
84
|
+
const semantics = pkg.semantics ?? [];
|
|
85
|
+
const lines = [];
|
|
86
|
+
for (const item of semantics) {
|
|
87
|
+
const page = item.source?.page === undefined ? "" : "(RM p." + String(item.source.page) + ")";
|
|
88
|
+
lines.push(item.id + ":" + item.statement + page);
|
|
89
|
+
if (Array.isArray(item.table) && item.table.length > 0) {
|
|
90
|
+
lines.push(" " + item.table.map((row) => Object.values(row).join("/")).join(" | "));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const declared = Array.isArray(pkg.needs) ? pkg.needs.filter((n) => typeof n === "string" && n !== "") : [];
|
|
94
|
+
const needs = semantics.length === 0
|
|
95
|
+
? (declared.length > 0 ? declared : ["本线只有结构、语义未核实:需要该线对应的 RM 手册与 rm-facts 抽取"])
|
|
96
|
+
: declared;
|
|
97
|
+
return { found: true, path: file, part: pkg.part ?? part, line: pkg.line ?? target.line, semantics: semantics.length, verified: semantics.length > 0, lines, needs };
|
|
98
|
+
}
|
|
99
|
+
/** 给工具输出用的摘要行(带结论语气)。 */
|
|
100
|
+
export function describeFacts(summary) {
|
|
101
|
+
if (!summary.found)
|
|
102
|
+
return ["事实层:" + (summary.needs[0] ?? "未生成") + (summary.hasSpec ? "(这颗芯片有清单,烧录门禁会拦)" : "(这颗芯片没清单,不拦烧录)")];
|
|
103
|
+
const rm = summary.rmPath === "" ? "" : "|RM 载体本地路径:" + summary.rmPath;
|
|
104
|
+
if (!summary.verified) {
|
|
105
|
+
if (summary.hasSpec)
|
|
106
|
+
return ["事实层:" + summary.line + " 有清单但机检没过:" + summary.needs.join(";") + "(stm32_flash 拒烧,allowUnknownFacts=true 可强制)" + rm];
|
|
107
|
+
return ["事实层:" + summary.line + " 仅结构、无主题清单:不拦烧录" + rm];
|
|
108
|
+
}
|
|
109
|
+
const out = ["事实层:" + summary.line + "(" + String(summary.semantics) + " 条语义,已验证)" + rm];
|
|
110
|
+
for (const line of summary.lines)
|
|
111
|
+
out.push(" · " + line);
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 给 stm32_generate / stm32_project_create 用的「生成前风险」一行式摘要。
|
|
116
|
+
* 与 describeFacts(doctor/flash 的多行详单)不同口径:这里只回答一件事——
|
|
117
|
+
* 生成完这块板,烧录门禁会不会拦、缺什么。三态各一句,found=false 也给指路命令。
|
|
118
|
+
*/
|
|
119
|
+
export function generationRiskNotes(part, env = process.env) {
|
|
120
|
+
if (part === null || part.trim() === "")
|
|
121
|
+
return [];
|
|
122
|
+
const s = readFactsFor(part, env);
|
|
123
|
+
const rm = s.rmPath === "" ? "" : ";RM 载体:本地 " + s.rmPath + "(调试怪行为可 pdf_scan/pdf_read_page 反查原文)";
|
|
124
|
+
if (s.found && s.verified) {
|
|
125
|
+
return ["事实层 " + s.line + " 已核实 " + String(s.semantics) + " 条语义(自举/调试门控/选项字节)→ stm32_flash 不会因只读门禁拦;逐条摘要见 stm32_doctor" + rm];
|
|
126
|
+
}
|
|
127
|
+
if (s.hasSpec) {
|
|
128
|
+
return ["事实层 " + (s.found ? s.line : part) + " 有 rm-topics 主题清单但机检没过/书没建起来 → stm32_flash 会拒烧(allowUnknownFacts=true 可强制)" + (s.needs.length > 0 ? ":" + s.needs[0] : "") + rm];
|
|
129
|
+
}
|
|
130
|
+
if (!s.found) {
|
|
131
|
+
return ["事实层(RM/SVD)未生成(stm32_flash 对没清单的芯片只提示不拦;工具会自动补,补失败多半是离线,可跑 build-facts.mjs --part " + s.part + ")"];
|
|
132
|
+
}
|
|
133
|
+
return ["事实层 " + s.line + " 只有结构、没有主题清单:不拦烧录,直接烧即可" + rm];
|
|
134
|
+
}
|