dsh-stm32 0.1.7 → 0.1.8
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 +256 -363
- package/README_EN.md +257 -357
- package/client/client.js +4340 -1
- package/docs/reading-guide.md +62 -0
- package/lib/client.js +4340 -1
- package/lib/engine/artifacts.js +123 -0
- package/lib/engine/breakpoint-hits.js +73 -0
- package/lib/engine/build.js +73 -3
- package/lib/engine/channel-tap.js +227 -0
- package/lib/engine/channels.js +48 -0
- package/lib/engine/clocks.js +192 -0
- package/lib/engine/debug-session.js +46 -1
- package/lib/engine/event-bus.js +158 -0
- package/lib/engine/event-log.js +335 -0
- package/lib/engine/fault-hints.js +146 -0
- package/lib/engine/interrupts.js +247 -0
- package/lib/engine/ioc-diff.js +369 -0
- package/lib/engine/ioc-sync.js +58 -0
- package/lib/engine/msys2-install.js +31 -6
- package/lib/engine/panel-actions.js +103 -0
- package/lib/engine/pinout.js +140 -0
- package/lib/engine/probe-info.js +95 -0
- package/lib/engine/progress.js +99 -0
- package/lib/engine/project-conf.js +199 -0
- package/lib/engine/session-context.js +19 -0
- package/lib/engine/source-slice.js +59 -0
- package/lib/engine/tool-events.js +119 -0
- package/lib/engine/watch-admin.js +101 -0
- package/lib/engine/watch-record.js +39 -0
- package/lib/engine/watch-recorder.js +116 -0
- package/lib/engine/web-api.js +886 -4
- package/lib/engine/web-routes.js +18 -1
- package/lib/engine/workspace-memo.js +41 -0
- package/lib/index.js +67 -24
- package/lib/tools/ioc-apply.js +9 -0
- package/lib/tools/project-create.js +28 -0
- package/lib/tools/project-dir.js +7 -1
- package/lib/tools/rtt.js +8 -0
- package/lib/tools/serial.js +22 -0
- package/lib/tools/watch.js +24 -2
- package/package.json +1 -1
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中断表:从 .ioc 的 NVIC.* 键读出「哪些中断开着、抢占优先级与子优先级多少」。
|
|
3
|
+
*
|
|
4
|
+
* CubeMX 写下的值形如 `true\:0\:0\:false\:false\:true\:false\:false\:false`:
|
|
5
|
+
* 第一段是 NVIC 使能,第二段抢占优先级,第三段子优先级,其余段与中断表无关,不解释。
|
|
6
|
+
* 只读,不改 .ioc;排序按「抢占优先级 → 子优先级 → 名字」,与 CubeMX 的列表习惯一致。
|
|
7
|
+
*/
|
|
8
|
+
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { findIocPath } from "./pinout.js";
|
|
11
|
+
import { iocBackupDir, parseIocEntries } from "./ioc-diff.js";
|
|
12
|
+
/** `NVIC.USART1_IRQn` → `USART1`;不是这一形状返回空串。 */
|
|
13
|
+
export function interruptName(key) {
|
|
14
|
+
if (!key.startsWith("NVIC."))
|
|
15
|
+
return "";
|
|
16
|
+
const body = key.slice("NVIC.".length);
|
|
17
|
+
if (!body.endsWith("_IRQn"))
|
|
18
|
+
return "";
|
|
19
|
+
return body.slice(0, -"_IRQn".length);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 解析一条 NVIC 值。
|
|
23
|
+
*
|
|
24
|
+
* `.ioc` 里冒号被转义成 `\:`,先还原再切段;段数不足时保留能读出的部分,
|
|
25
|
+
* 优先级给 -1 而不是 0——0 是合法的高优先级,不能用来表示「没写」。
|
|
26
|
+
*/
|
|
27
|
+
export function parseNvicValue(value) {
|
|
28
|
+
const parts = value.replace(/\\:/gu, ":").split(":");
|
|
29
|
+
const enabled = (parts[0] ?? "").trim().toLowerCase() === "true";
|
|
30
|
+
const toNumber = (text) => {
|
|
31
|
+
const trimmed = (text ?? "").trim();
|
|
32
|
+
if (!/^\d+$/u.test(trimmed))
|
|
33
|
+
return -1;
|
|
34
|
+
const parsed = Number.parseInt(trimmed, 10);
|
|
35
|
+
return Number.isFinite(parsed) ? parsed : -1;
|
|
36
|
+
};
|
|
37
|
+
return { enabled, preempt: toNumber(parts[1]), sub: toNumber(parts[2]) };
|
|
38
|
+
}
|
|
39
|
+
/** 只认带三段的 NVIC 值:`NVIC.ForceEnableDMAVector=true` 这类开关不是中断。 */
|
|
40
|
+
export function collectInterrupts(entries) {
|
|
41
|
+
const out = [];
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
const name = interruptName(entry.key);
|
|
44
|
+
if (name === "")
|
|
45
|
+
continue;
|
|
46
|
+
const parts = entry.value.replace(/\\:/gu, ":").split(":");
|
|
47
|
+
if (parts.length < 3)
|
|
48
|
+
continue;
|
|
49
|
+
const parsed = parseNvicValue(entry.value);
|
|
50
|
+
out.push({ name, enabled: parsed.enabled, preempt: parsed.preempt, sub: parsed.sub });
|
|
51
|
+
}
|
|
52
|
+
out.sort((left, right) => {
|
|
53
|
+
if (left.preempt !== right.preempt)
|
|
54
|
+
return left.preempt - right.preempt;
|
|
55
|
+
if (left.sub !== right.sub)
|
|
56
|
+
return left.sub - right.sub;
|
|
57
|
+
return left.name.localeCompare(right.name);
|
|
58
|
+
});
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/** 读工程 .ioc 的中断表。没有 .ioc 或读不出来时返回错误文本,不抛。 */
|
|
62
|
+
export function readInterrupts(projectDir) {
|
|
63
|
+
const dir = typeof projectDir === "string" ? projectDir.trim() : "";
|
|
64
|
+
if (dir === "")
|
|
65
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
66
|
+
const iocPath = findIocPath(dir);
|
|
67
|
+
if (iocPath === "")
|
|
68
|
+
return { ok: false, error: "工程里没有 .ioc:先建工程,或确认 CubeMX 配置已保存" };
|
|
69
|
+
let text = "";
|
|
70
|
+
try {
|
|
71
|
+
text = readFileSync(iocPath, "utf8");
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
return { ok: false, error: "读取 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
75
|
+
}
|
|
76
|
+
const entries = collectInterrupts(parseIocEntries(text));
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
report: {
|
|
80
|
+
iocPath,
|
|
81
|
+
iocName: iocPath.slice(iocPath.lastIndexOf("\\") + 1),
|
|
82
|
+
entries,
|
|
83
|
+
enabledCount: entries.filter((entry) => entry.enabled).length,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 批量把若干路中断设成同一组抢占/子优先级(S74)。
|
|
89
|
+
*
|
|
90
|
+
* 只写 .ioc 的对应行,写前统一备份一次;某一路改了失败不影响其它路,
|
|
91
|
+
* 失败原因逐条回给面板。
|
|
92
|
+
*/
|
|
93
|
+
export function setInterruptPriorities(projectDir, names, preempt, sub) {
|
|
94
|
+
const dir = projectDir.trim();
|
|
95
|
+
if (dir === "")
|
|
96
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
97
|
+
const range = (value) => Number.isInteger(value) && value >= 0 && value <= 15;
|
|
98
|
+
if (!range(preempt))
|
|
99
|
+
return { ok: false, error: "抢占优先级必须是 0–15 的整数(实际 " + String(preempt) + ")" };
|
|
100
|
+
if (!range(sub))
|
|
101
|
+
return { ok: false, error: "子优先级必须是 0–15 的整数(实际 " + String(sub) + ")" };
|
|
102
|
+
const wanted = [];
|
|
103
|
+
for (const raw of names) {
|
|
104
|
+
const name = String(raw).trim();
|
|
105
|
+
if (name === "")
|
|
106
|
+
continue;
|
|
107
|
+
if (!/^[A-Za-z0-9_]+$/u.test(name))
|
|
108
|
+
return { ok: false, error: "中断名只允许字母、数字与下划线:" + name };
|
|
109
|
+
if (!wanted.includes(name))
|
|
110
|
+
wanted.push(name);
|
|
111
|
+
}
|
|
112
|
+
if (wanted.length === 0)
|
|
113
|
+
return { ok: false, error: "names 不能为空" };
|
|
114
|
+
const iocPath = findIocPath(dir);
|
|
115
|
+
if (iocPath === "")
|
|
116
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
117
|
+
let text;
|
|
118
|
+
try {
|
|
119
|
+
text = readFileSync(iocPath, "utf8");
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
return { ok: false, error: "读取 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
123
|
+
}
|
|
124
|
+
const lines = text.split(/\r?\n/u);
|
|
125
|
+
const applied = [];
|
|
126
|
+
const errors = [];
|
|
127
|
+
for (const name of wanted) {
|
|
128
|
+
const key = "NVIC." + name + "_IRQn";
|
|
129
|
+
let done = false;
|
|
130
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
131
|
+
const line = lines[index] ?? "";
|
|
132
|
+
const trimmed = line.trim();
|
|
133
|
+
if (trimmed === "" || trimmed.startsWith("#"))
|
|
134
|
+
continue;
|
|
135
|
+
const eq = trimmed.indexOf("=");
|
|
136
|
+
if (eq <= 0)
|
|
137
|
+
continue;
|
|
138
|
+
if (trimmed.slice(0, eq).trim() !== key)
|
|
139
|
+
continue;
|
|
140
|
+
const raw = trimmed.slice(eq + 1).trim();
|
|
141
|
+
const parts = raw.split("\\:");
|
|
142
|
+
if (parts.length < 3) {
|
|
143
|
+
errors.push({ name: name, error: "这一条 NVIC 值段数不足,无法安全改写:" + key });
|
|
144
|
+
done = true;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const before = parseNvicValue(raw);
|
|
148
|
+
parts[1] = String(preempt);
|
|
149
|
+
parts[2] = String(sub);
|
|
150
|
+
const indent = line.slice(0, line.indexOf(trimmed));
|
|
151
|
+
lines[index] = indent + key + "=" + parts.join("\\:");
|
|
152
|
+
applied.push({ name: name, before: before, after: { preempt: preempt, sub: sub } });
|
|
153
|
+
done = true;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (!done)
|
|
157
|
+
errors.push({ name: name, error: "工程 .ioc 里没有这一路中断:" + key });
|
|
158
|
+
}
|
|
159
|
+
if (applied.length === 0) {
|
|
160
|
+
return { ok: true, report: { iocPath: iocPath, backupPath: "", applied: [], errors: errors, changed: false } };
|
|
161
|
+
}
|
|
162
|
+
const nextText = lines.join("\n");
|
|
163
|
+
const backupPath = join(iocBackupDir(dir), "ioc-" + new Date().toISOString().replace(/[:.]/gu, "-") + ".ioc");
|
|
164
|
+
try {
|
|
165
|
+
mkdirSync(iocBackupDir(dir), { recursive: true });
|
|
166
|
+
copyFileSync(iocPath, backupPath);
|
|
167
|
+
writeFileSync(iocPath, nextText, "utf8");
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
return { ok: false, error: "写回 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
171
|
+
}
|
|
172
|
+
return { ok: true, report: { iocPath: iocPath, backupPath: backupPath, applied: applied, errors: errors, changed: true } };
|
|
173
|
+
}
|
|
174
|
+
export function setInterruptPriority(projectDir, name, preempt, sub) {
|
|
175
|
+
const dir = projectDir.trim();
|
|
176
|
+
if (dir === "")
|
|
177
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
178
|
+
const wanted = name.trim();
|
|
179
|
+
if (wanted === "" || !/^[A-Za-z0-9_]+$/u.test(wanted))
|
|
180
|
+
return { ok: false, error: "中断名只允许字母、数字与下划线:" + wanted };
|
|
181
|
+
const range = (value) => Number.isInteger(value) && value >= 0 && value <= 15;
|
|
182
|
+
if (!range(preempt))
|
|
183
|
+
return { ok: false, error: "抢占优先级必须是 0–15 的整数(实际 " + String(preempt) + ")" };
|
|
184
|
+
if (!range(sub))
|
|
185
|
+
return { ok: false, error: "子优先级必须是 0–15 的整数(实际 " + String(sub) + ")" };
|
|
186
|
+
const iocPath = findIocPath(dir);
|
|
187
|
+
if (iocPath === "")
|
|
188
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
189
|
+
let text;
|
|
190
|
+
try {
|
|
191
|
+
text = readFileSync(iocPath, "utf8");
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
return { ok: false, error: "读取 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
195
|
+
}
|
|
196
|
+
const key = "NVIC." + wanted + "_IRQn";
|
|
197
|
+
const lines = text.split(/\r?\n/u);
|
|
198
|
+
let before = { preempt: -1, sub: -1 };
|
|
199
|
+
let hit = false;
|
|
200
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
201
|
+
const line = lines[index] ?? "";
|
|
202
|
+
const trimmed = line.trim();
|
|
203
|
+
if (trimmed === "" || trimmed.startsWith("#"))
|
|
204
|
+
continue;
|
|
205
|
+
const eq = trimmed.indexOf("=");
|
|
206
|
+
if (eq <= 0)
|
|
207
|
+
continue;
|
|
208
|
+
if (trimmed.slice(0, eq).trim() !== key)
|
|
209
|
+
continue;
|
|
210
|
+
const raw = trimmed.slice(eq + 1).trim();
|
|
211
|
+
// `.ioc` 里冒号被转义成 `\:`,按转义分隔符切段再拼回去。
|
|
212
|
+
const parts = raw.split("\\:");
|
|
213
|
+
if (parts.length < 3)
|
|
214
|
+
return { ok: false, error: "这一条 NVIC 值段数不足,无法安全改写:" + key };
|
|
215
|
+
before = parseNvicValue(raw);
|
|
216
|
+
parts[1] = String(preempt);
|
|
217
|
+
parts[2] = String(sub);
|
|
218
|
+
const indent = line.slice(0, line.indexOf(trimmed));
|
|
219
|
+
lines[index] = indent + key + "=" + parts.join("\\:");
|
|
220
|
+
hit = true;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
if (!hit)
|
|
224
|
+
return { ok: false, error: "工程 .ioc 里没有这一路中断:" + key };
|
|
225
|
+
const nextText = lines.join("\n");
|
|
226
|
+
const backupPath = join(iocBackupDir(dir), "ioc-" + new Date().toISOString().replace(/[:.]/gu, "-") + ".ioc");
|
|
227
|
+
try {
|
|
228
|
+
mkdirSync(iocBackupDir(dir), { recursive: true });
|
|
229
|
+
copyFileSync(iocPath, backupPath);
|
|
230
|
+
writeFileSync(iocPath, nextText, "utf8");
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
return { ok: false, error: "写回 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
ok: true,
|
|
237
|
+
report: {
|
|
238
|
+
iocPath: iocPath,
|
|
239
|
+
name: wanted,
|
|
240
|
+
key: key,
|
|
241
|
+
before: before,
|
|
242
|
+
after: { preempt: preempt, sub: sub },
|
|
243
|
+
backupPath: backupPath,
|
|
244
|
+
changed: before.preempt !== preempt || before.sub !== sub,
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .ioc 基线对比:把当前 CubeMX 配置与上一次手动记下的基线逐键比较。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不看 git:工程可以不在版本库里,也可能是别人的机器;基线要能随手记、
|
|
5
|
+
* 随手比,所以单独存一份到用户目录,按工程路径分文件。
|
|
6
|
+
*
|
|
7
|
+
* 只读工程文件、只写自己的基线目录;不跑 CubeMX,也不改 .ioc。
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { basename, join, resolve } from "node:path";
|
|
13
|
+
import { findIocPath } from "./pinout.js";
|
|
14
|
+
/** 基线目录:默认 `~/.dsh-stm32/ioc-snapshots`,可用 DSH_STM32_SNAPSHOTS 覆盖。 */
|
|
15
|
+
export function snapshotRoot(env = process.env) {
|
|
16
|
+
const custom = env.DSH_STM32_SNAPSHOTS;
|
|
17
|
+
if (typeof custom === "string" && custom.trim() !== "")
|
|
18
|
+
return resolve(custom.trim());
|
|
19
|
+
return join(homedir(), ".dsh-stm32", "ioc-snapshots");
|
|
20
|
+
}
|
|
21
|
+
/** 一个工程一份基线,文件名取工程绝对路径的短哈希,换机器不冲突。 */
|
|
22
|
+
export function snapshotPath(projectDir, env = process.env) {
|
|
23
|
+
const digest = createHash("sha256").update(resolve(projectDir)).digest("hex").slice(0, 16);
|
|
24
|
+
return join(snapshotRoot(env), digest + ".ioc");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 解析 .ioc:只收第一个 `=` 前的键与之后的值。
|
|
28
|
+
* 注释行、空行、没有 `=` 的行都跳过——CubeMX 版本之间会插入说明行。
|
|
29
|
+
*/
|
|
30
|
+
export function parseIocEntries(text) {
|
|
31
|
+
const out = [];
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
34
|
+
const trimmed = line.trim();
|
|
35
|
+
if (trimmed === "" || trimmed.startsWith("#"))
|
|
36
|
+
continue;
|
|
37
|
+
const index = trimmed.indexOf("=");
|
|
38
|
+
if (index <= 0)
|
|
39
|
+
continue;
|
|
40
|
+
const key = trimmed.slice(0, index).trim();
|
|
41
|
+
const value = trimmed.slice(index + 1).trim();
|
|
42
|
+
if (key === "" || seen.has(key))
|
|
43
|
+
continue;
|
|
44
|
+
seen.add(key);
|
|
45
|
+
out.push({ key, value });
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
/** 逐键比较:新增、删除、值变化各列一张表,按 key 排序,输出稳定。 */
|
|
50
|
+
export function diffIocEntries(before, after) {
|
|
51
|
+
const beforeMap = new Map(before.map((entry) => [entry.key, entry.value]));
|
|
52
|
+
const afterMap = new Map(after.map((entry) => [entry.key, entry.value]));
|
|
53
|
+
const added = [];
|
|
54
|
+
const removed = [];
|
|
55
|
+
const changed = [];
|
|
56
|
+
for (const [key, value] of afterMap) {
|
|
57
|
+
if (!beforeMap.has(key))
|
|
58
|
+
added.push({ key, value });
|
|
59
|
+
else if (beforeMap.get(key) !== value)
|
|
60
|
+
changed.push({ key, before: beforeMap.get(key) ?? "", after: value });
|
|
61
|
+
}
|
|
62
|
+
for (const [key, value] of beforeMap) {
|
|
63
|
+
if (!afterMap.has(key))
|
|
64
|
+
removed.push({ key, value });
|
|
65
|
+
}
|
|
66
|
+
const byKey = (left, right) => left.key.localeCompare(right.key);
|
|
67
|
+
added.sort(byKey);
|
|
68
|
+
removed.sort(byKey);
|
|
69
|
+
changed.sort(byKey);
|
|
70
|
+
return { added, removed, changed };
|
|
71
|
+
}
|
|
72
|
+
/** 把当前 .ioc 记为基线。已有基线直接覆盖,不留历史版本。 */
|
|
73
|
+
export function saveIocSnapshot(projectDir, env = process.env) {
|
|
74
|
+
const dir = projectDir.trim();
|
|
75
|
+
if (dir === "")
|
|
76
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
77
|
+
const iocPath = findIocPath(dir);
|
|
78
|
+
if (iocPath === "")
|
|
79
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
80
|
+
const target = snapshotPath(dir, env);
|
|
81
|
+
try {
|
|
82
|
+
mkdirSync(snapshotRoot(env), { recursive: true });
|
|
83
|
+
copyFileSync(iocPath, target);
|
|
84
|
+
const text = readFileSync(iocPath, "utf8");
|
|
85
|
+
return {
|
|
86
|
+
ok: true,
|
|
87
|
+
report: {
|
|
88
|
+
iocPath: iocPath,
|
|
89
|
+
snapshotPath: target,
|
|
90
|
+
savedAt: new Date().toISOString(),
|
|
91
|
+
entries: parseIocEntries(text).length,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
return { ok: false, error: "记录基线失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 读某个引脚在 .ioc 里占的键(S50)。
|
|
101
|
+
*
|
|
102
|
+
* 引脚名在 .ioc 里是键前缀:`PA13.Signal`、`PA13.GPIOParameters`;
|
|
103
|
+
* 带功能的引脚(`PC13-TAMPER-RTC`)取第一段。只读,不改文件。
|
|
104
|
+
*/
|
|
105
|
+
export function readIocKeysForPin(projectDir, pin, env = process.env) {
|
|
106
|
+
const dir = projectDir.trim();
|
|
107
|
+
if (dir === "")
|
|
108
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
109
|
+
const wanted = pin.trim();
|
|
110
|
+
if (wanted === "")
|
|
111
|
+
return { ok: false, error: "pin 不能为空" };
|
|
112
|
+
const iocPath = findIocPath(dir);
|
|
113
|
+
if (iocPath === "")
|
|
114
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
115
|
+
let text;
|
|
116
|
+
try {
|
|
117
|
+
text = readFileSync(iocPath, "utf8");
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return { ok: false, error: "读取 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
121
|
+
}
|
|
122
|
+
const all = parseIocEntries(text);
|
|
123
|
+
// 键前缀就是 .ioc 里的引脚名(`PC13-TAMPER-RTC.Signal`);个别 .ioc 只写端口段,退一步再试。
|
|
124
|
+
const matches = (prefix) => all.filter((entry) => entry.key === prefix || entry.key.startsWith(prefix + "."));
|
|
125
|
+
let base = wanted;
|
|
126
|
+
let entries = matches(wanted);
|
|
127
|
+
if (entries.length === 0) {
|
|
128
|
+
const shortened = wanted.split("-")[0]?.trim() ?? "";
|
|
129
|
+
if (shortened !== "" && shortened !== wanted) {
|
|
130
|
+
base = shortened;
|
|
131
|
+
entries = matches(shortened);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { ok: true, report: { iocPath: iocPath, pin: wanted, base: base, entries: entries } };
|
|
135
|
+
}
|
|
136
|
+
/** 写 .ioc 前的备份目录:`<工程>/.dsh-stm32/backups`。 */
|
|
137
|
+
export function iocBackupDir(projectDir) {
|
|
138
|
+
return join(projectDir, ".dsh-stm32", "backups");
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 把一个键写回基线里的值(S42)。
|
|
142
|
+
*
|
|
143
|
+
* 只改这一行,其余字节原样保留;写之前先备份到 `.dsh-stm32/backups`。
|
|
144
|
+
* 基线里没有的键直接拒绝——不猜该填什么。
|
|
145
|
+
*/
|
|
146
|
+
export function restoreIocKey(projectDir, key, env = process.env) {
|
|
147
|
+
const dir = projectDir.trim();
|
|
148
|
+
if (dir === "")
|
|
149
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
150
|
+
const wanted = key.trim();
|
|
151
|
+
if (wanted === "")
|
|
152
|
+
return { ok: false, error: "key 不能为空" };
|
|
153
|
+
if (!/^[A-Za-z0-9_.]+$/u.test(wanted))
|
|
154
|
+
return { ok: false, error: "key 只允许字母、数字、下划线与点:" + wanted };
|
|
155
|
+
const iocPath = findIocPath(dir);
|
|
156
|
+
if (iocPath === "")
|
|
157
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
158
|
+
const target = snapshotPath(dir, env);
|
|
159
|
+
if (!existsSync(target))
|
|
160
|
+
return { ok: false, error: "还没有基线:先点「记下当前配置」" };
|
|
161
|
+
let baselineText;
|
|
162
|
+
let currentText;
|
|
163
|
+
try {
|
|
164
|
+
baselineText = readFileSync(target, "utf8");
|
|
165
|
+
currentText = readFileSync(iocPath, "utf8");
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
return { ok: false, error: "读取文件失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
169
|
+
}
|
|
170
|
+
const baseline = parseIocEntries(baselineText).find((entry) => entry.key === wanted);
|
|
171
|
+
if (baseline === undefined)
|
|
172
|
+
return { ok: false, error: "基线里没有这个键:" + wanted };
|
|
173
|
+
const lines = currentText.split(/\r?\n/u);
|
|
174
|
+
let before = "";
|
|
175
|
+
let hit = false;
|
|
176
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
177
|
+
const line = lines[index] ?? "";
|
|
178
|
+
const trimmed = line.trim();
|
|
179
|
+
if (trimmed === "" || trimmed.startsWith("#"))
|
|
180
|
+
continue;
|
|
181
|
+
const eq = trimmed.indexOf("=");
|
|
182
|
+
if (eq <= 0)
|
|
183
|
+
continue;
|
|
184
|
+
if (trimmed.slice(0, eq).trim() !== wanted)
|
|
185
|
+
continue;
|
|
186
|
+
before = trimmed.slice(eq + 1).trim();
|
|
187
|
+
// 保留行尾的空白与 CRLF 由最终 join 决定;这里只换掉「键=值」本身。
|
|
188
|
+
const indent = line.slice(0, line.indexOf(trimmed));
|
|
189
|
+
lines[index] = indent + wanted + "=" + baseline.value;
|
|
190
|
+
hit = true;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
if (!hit) {
|
|
194
|
+
// 当前文件里没有这个键(基线新增项之外被删掉的键):追加到末尾。
|
|
195
|
+
if (lines.length > 0 && lines[lines.length - 1] === "")
|
|
196
|
+
lines.pop();
|
|
197
|
+
lines.push(wanted + "=" + baseline.value);
|
|
198
|
+
}
|
|
199
|
+
const nextText = lines.join("\n");
|
|
200
|
+
const backupPath = join(iocBackupDir(dir), "ioc-" + new Date().toISOString().replace(/[:.]/gu, "-") + ".ioc");
|
|
201
|
+
try {
|
|
202
|
+
mkdirSync(iocBackupDir(dir), { recursive: true });
|
|
203
|
+
copyFileSync(iocPath, backupPath);
|
|
204
|
+
writeFileSync(iocPath, nextText, "utf8");
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
return { ok: false, error: "写回 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
ok: true,
|
|
211
|
+
report: {
|
|
212
|
+
iocPath: iocPath,
|
|
213
|
+
key: wanted,
|
|
214
|
+
before: before,
|
|
215
|
+
after: baseline.value,
|
|
216
|
+
backupPath: backupPath,
|
|
217
|
+
changed: before !== baseline.value || !hit,
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 把所有「改动」「删除」的键一次写回基线值(S52)。
|
|
223
|
+
*
|
|
224
|
+
* 只动这几行,其余字节原样保留;整批只做一次备份,便于整体回退。
|
|
225
|
+
* 「新增」的键不处理——基线里没有它们的值,删除与否交给用户判断。
|
|
226
|
+
*/
|
|
227
|
+
export function restoreAllIocKeys(projectDir, env = process.env) {
|
|
228
|
+
const dir = projectDir.trim();
|
|
229
|
+
if (dir === "")
|
|
230
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
231
|
+
const diff = readIocDiff(dir, env);
|
|
232
|
+
if (!diff.ok)
|
|
233
|
+
return { ok: false, error: diff.error };
|
|
234
|
+
if (!diff.report.hasSnapshot)
|
|
235
|
+
return { ok: false, error: "还没有基线:先点「记下当前配置」" };
|
|
236
|
+
const wanted = [...diff.report.changed.map((item) => item.key), ...diff.report.removed.map((item) => item.key)];
|
|
237
|
+
if (wanted.length === 0) {
|
|
238
|
+
return { ok: true, report: { iocPath: diff.report.iocPath, backupPath: "", restored: [], skipped: [], changed: false } };
|
|
239
|
+
}
|
|
240
|
+
const target = snapshotPath(dir, env);
|
|
241
|
+
let baselineText;
|
|
242
|
+
let currentText;
|
|
243
|
+
try {
|
|
244
|
+
baselineText = readFileSync(target, "utf8");
|
|
245
|
+
currentText = readFileSync(diff.report.iocPath, "utf8");
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
return { ok: false, error: "读取文件失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
249
|
+
}
|
|
250
|
+
const baseline = new Map(parseIocEntries(baselineText).map((entry) => [entry.key, entry.value]));
|
|
251
|
+
const lines = currentText.split(/\r?\n/u);
|
|
252
|
+
const restored = [];
|
|
253
|
+
const skipped = [];
|
|
254
|
+
for (const key of wanted) {
|
|
255
|
+
const value = baseline.get(key);
|
|
256
|
+
if (value === undefined) {
|
|
257
|
+
skipped.push(key);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
let hit = false;
|
|
261
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
262
|
+
const line = lines[index] ?? "";
|
|
263
|
+
const trimmed = line.trim();
|
|
264
|
+
if (trimmed === "" || trimmed.startsWith("#"))
|
|
265
|
+
continue;
|
|
266
|
+
const eq = trimmed.indexOf("=");
|
|
267
|
+
if (eq <= 0)
|
|
268
|
+
continue;
|
|
269
|
+
if (trimmed.slice(0, eq).trim() !== key)
|
|
270
|
+
continue;
|
|
271
|
+
const indent = line.slice(0, line.indexOf(trimmed));
|
|
272
|
+
lines[index] = indent + key + "=" + value;
|
|
273
|
+
hit = true;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
if (!hit) {
|
|
277
|
+
if (lines.length > 0 && lines[lines.length - 1] === "")
|
|
278
|
+
lines.pop();
|
|
279
|
+
lines.push(key + "=" + value);
|
|
280
|
+
}
|
|
281
|
+
restored.push(key);
|
|
282
|
+
}
|
|
283
|
+
if (restored.length === 0) {
|
|
284
|
+
return { ok: true, report: { iocPath: diff.report.iocPath, backupPath: "", restored: [], skipped: skipped, changed: false } };
|
|
285
|
+
}
|
|
286
|
+
const backupPath = join(iocBackupDir(dir), "ioc-" + new Date().toISOString().replace(/[:.]/gu, "-") + ".ioc");
|
|
287
|
+
try {
|
|
288
|
+
mkdirSync(iocBackupDir(dir), { recursive: true });
|
|
289
|
+
copyFileSync(diff.report.iocPath, backupPath);
|
|
290
|
+
writeFileSync(diff.report.iocPath, lines.join("\n"), "utf8");
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
return { ok: false, error: "写回 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
ok: true,
|
|
297
|
+
report: { iocPath: diff.report.iocPath, backupPath: backupPath, restored: restored, skipped: skipped, changed: true },
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/** 读当前 .ioc 与基线的差异;没有基线时 hasSnapshot=false,其余为空。 */
|
|
301
|
+
export function readIocDiff(projectDir, env = process.env) {
|
|
302
|
+
const dir = projectDir.trim();
|
|
303
|
+
if (dir === "")
|
|
304
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
305
|
+
const iocPath = findIocPath(dir);
|
|
306
|
+
if (iocPath === "")
|
|
307
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
308
|
+
let currentText;
|
|
309
|
+
try {
|
|
310
|
+
currentText = readFileSync(iocPath, "utf8");
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
return { ok: false, error: "读取 .ioc 失败:" + (error instanceof Error ? error.message : String(error)) };
|
|
314
|
+
}
|
|
315
|
+
const target = snapshotPath(dir, env);
|
|
316
|
+
const empty = {
|
|
317
|
+
iocPath: iocPath,
|
|
318
|
+
iocName: basename(iocPath),
|
|
319
|
+
snapshotPath: target,
|
|
320
|
+
hasSnapshot: false,
|
|
321
|
+
savedAt: "",
|
|
322
|
+
added: [],
|
|
323
|
+
removed: [],
|
|
324
|
+
changed: [],
|
|
325
|
+
counts: { added: 0, removed: 0, changed: 0, total: 0 },
|
|
326
|
+
notes: [],
|
|
327
|
+
};
|
|
328
|
+
if (!existsSync(target)) {
|
|
329
|
+
return { ok: true, report: { ...empty, notes: ["还没有基线:点「记下当前配置」后才有可比较的版本。"] } };
|
|
330
|
+
}
|
|
331
|
+
let beforeText;
|
|
332
|
+
try {
|
|
333
|
+
beforeText = readFileSync(target, "utf8");
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
return {
|
|
337
|
+
ok: true,
|
|
338
|
+
report: { ...empty, notes: ["基线文件读不出来:" + (error instanceof Error ? error.message : String(error))] },
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
const diff = diffIocEntries(parseIocEntries(beforeText), parseIocEntries(currentText));
|
|
342
|
+
let savedAt = "";
|
|
343
|
+
try {
|
|
344
|
+
savedAt = new Date(statSync(target).mtimeMs).toISOString();
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
savedAt = "";
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
ok: true,
|
|
351
|
+
report: {
|
|
352
|
+
iocPath: iocPath,
|
|
353
|
+
iocName: basename(iocPath),
|
|
354
|
+
snapshotPath: target,
|
|
355
|
+
hasSnapshot: true,
|
|
356
|
+
savedAt: savedAt,
|
|
357
|
+
added: diff.added,
|
|
358
|
+
removed: diff.removed,
|
|
359
|
+
changed: diff.changed,
|
|
360
|
+
counts: {
|
|
361
|
+
added: diff.added.length,
|
|
362
|
+
removed: diff.removed.length,
|
|
363
|
+
changed: diff.changed.length,
|
|
364
|
+
total: diff.added.length + diff.removed.length + diff.changed.length,
|
|
365
|
+
},
|
|
366
|
+
notes: [],
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .ioc 与生成代码是否同步。
|
|
3
|
+
*
|
|
4
|
+
* 场景:用户在 CubeMX 里改了配置、存了 .ioc,却忘了 stm32_generate;此后编译、烧录跑的都是旧代码,
|
|
5
|
+
* 现象是「配置明明改了怎么不生效」。这里只比文件时间,不读内容:.ioc 比生成物新就是「待生成」。
|
|
6
|
+
*
|
|
7
|
+
* 判定用 Core/Src/main.c 的修改时间当「最近一次生成」——generate 一定会重写它;
|
|
8
|
+
* 工程里没有这个文件时视为没生成过(或不是本插件的工程布局),给出提示而不下结论。
|
|
9
|
+
*/
|
|
10
|
+
import { statSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { findIocPath } from "./pinout.js";
|
|
13
|
+
function mtimeOf(file) {
|
|
14
|
+
try {
|
|
15
|
+
return statSync(file).mtimeMs;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function readIocSync(projectDir) {
|
|
22
|
+
const dir = projectDir.trim();
|
|
23
|
+
if (dir === "")
|
|
24
|
+
return { ok: false, error: "projectDir 不能为空" };
|
|
25
|
+
const iocPath = findIocPath(dir);
|
|
26
|
+
if (iocPath === "")
|
|
27
|
+
return { ok: false, error: "工程目录里没有 .ioc:" + dir };
|
|
28
|
+
const codePath = join(dir, "Core", "Src", "main.c");
|
|
29
|
+
const iocMtimeMs = mtimeOf(iocPath);
|
|
30
|
+
const codeMtimeMs = mtimeOf(codePath);
|
|
31
|
+
if (codeMtimeMs === 0) {
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
report: {
|
|
35
|
+
iocPath,
|
|
36
|
+
iocMtimeMs,
|
|
37
|
+
codePath,
|
|
38
|
+
codeMtimeMs: 0,
|
|
39
|
+
stale: false,
|
|
40
|
+
message: "还没有生成过代码(找不到 Core/Src/main.c):先跑一次 stm32_generate。",
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const stale = iocMtimeMs > codeMtimeMs + 1000;
|
|
45
|
+
return {
|
|
46
|
+
ok: true,
|
|
47
|
+
report: {
|
|
48
|
+
iocPath,
|
|
49
|
+
iocMtimeMs,
|
|
50
|
+
codePath,
|
|
51
|
+
codeMtimeMs,
|
|
52
|
+
stale,
|
|
53
|
+
message: stale
|
|
54
|
+
? ".ioc 比生成的代码新:配置改了但还没重新生成,编译烧录仍是旧代码。"
|
|
55
|
+
: ".ioc 与生成的代码一致。",
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|