diff --git a/App/iec60870/104/iec104_core.c b/App/iec60870/104/iec104_core.c index ba4ce430..e9805cf1 100644 --- a/App/iec60870/104/iec104_core.c +++ b/App/iec60870/104/iec104_core.c @@ -18,12 +18,15 @@ #define CORE_FEEDBACK_CAPACITY 8U #define CORE_SCAN_CAPACITY 30U #define CORE_MONITOR_CAPACITY 105U - -typedef struct { - uint32_t value; - uint8_t quality; - bool initialized; -} core_previous_t; +/* 监视点索引段:0..58 单点、59..74 双点、75..104 测量值,与 iec104_source_point() + * 的点表顺序绑定(SP59 + DP16 + ME30 = 105)。段边界改点表后必须同步。 */ +#define CORE_SINGLE_FIRST 0U +#define CORE_DOUBLE_FIRST IEC104_DTU_SP_COUNT +#define CORE_MEASURE_FIRST (CORE_DOUBLE_FIRST + IEC104_DTU_DP_COUNT) +_Static_assert(IEC104_DTU_SP_COUNT + IEC104_DTU_DP_COUNT + IEC104_DTU_ME_COUNT == CORE_MONITOR_CAPACITY, + "Monitor capacity must equal the SP+DP+ME point-count sum"); +_Static_assert(CORE_SINGLE_FIRST == 0U && CORE_DOUBLE_FIRST == 59U && CORE_MEASURE_FIRST == 75U, + "Previous-value segment boundaries must stay 0/59/75"); typedef struct { IEC104_Interrogation_t *module; @@ -53,7 +56,16 @@ typedef struct { IEC104_Spontaneous_t *spontaneous; IEC104_SpontaneousChange_t change; IEC104_SpontaneousPublishInfo_t publication; - core_previous_t previous[CORE_MONITOR_CAPACITY]; + /* 变化检测基线按三角色分表存放:单点/双点 1 字节、测量值 4 字节位模式, + * 比统一 8 字节结构省 528B(sizeof(core_t) 2928 -> 2400)。分解:基线块 + * 840 -> 316(314 载荷 + sample_cursor 的 4 字节对齐填充)省 524,且 + * sample_cursor 偏移由 4(mod 8) 变 0(mod 8)、尾段少 4 字节填充,合计 528。 + * 位图代替每个元素的 bool。 */ + uint8_t prev_single[IEC104_DTU_SP_COUNT]; + uint8_t prev_double[IEC104_DTU_DP_COUNT]; + uint32_t prev_measure[IEC104_DTU_ME_COUNT]; + uint8_t prev_quality[CORE_MONITOR_CAPACITY]; + uint8_t prev_initialized[(CORE_MONITOR_CAPACITY + 7U) / 8U]; size_t sample_cursor; iec104_source_stats_t source_stats; IEC60870_CP56Time2a_t sample_time; @@ -416,6 +428,28 @@ static bool startup_step(void) return IEC104_Startup_Process(&s_core.startup, s_core.slave, s_core.system, &s_core.system_status); } +/* 函数功能:读取变化检测基线的值(按三段索引,测量值保留 32 位位模式)。 */ +static uint32_t previous_value(size_t cursor) +{ + if (cursor < CORE_DOUBLE_FIRST) { return s_core.prev_single[cursor]; } + if (cursor < CORE_MEASURE_FIRST) { return s_core.prev_double[cursor - CORE_DOUBLE_FIRST]; } + return s_core.prev_measure[cursor - CORE_MEASURE_FIRST]; +} + +/* 函数功能:写回变化检测基线的值;单点/双点只有 1 字节语义。 */ +static void previous_store(size_t cursor, uint32_t value) +{ + if (cursor < CORE_DOUBLE_FIRST) { s_core.prev_single[cursor] = (uint8_t)value; } + else if (cursor < CORE_MEASURE_FIRST) { s_core.prev_double[cursor - CORE_DOUBLE_FIRST] = (uint8_t)value; } + else { s_core.prev_measure[cursor - CORE_MEASURE_FIRST] = value; } +} + +/* 函数功能:查询某点是否已建立基线(位图,未初始化时不发首次变位)。 */ +static bool previous_initialized(size_t cursor) +{ + return (s_core.prev_initialized[cursor >> 3] & (uint8_t)(1U << (cursor & 7U))) != 0U; +} + /* Re-read current snapshots after backpressure; never consume source events. */ /* 函数功能:有界读取当前快照并将变化交给成熟突发引擎。 */ static bool collect_changes(void) @@ -426,10 +460,10 @@ static bool collect_changes(void) if (!IEC104_Spontaneous_Set_Time_Base(s_core.spontaneous, iec104_source_clock_ms(now), now, s_core.source_stats.synchronized)) { return false; } for (row = 0U; row < CORE_DATA_BUDGET; ++row) { + const size_t cursor = s_core.sample_cursor; uint32_t value = 0U; uint8_t quality; - core_previous_t *previous = &s_core.previous[s_core.sample_cursor]; - if (!iec104_source_point(s_core.sample_cursor, &s_core.object)) { return false; } + if (!iec104_source_point(cursor, &s_core.object)) { return false; } memset(&s_core.change, 0, sizeof(s_core.change)); s_core.change.common_address = s_core.ca; s_core.change.ioa = s_core.object.object.ioa; @@ -449,6 +483,7 @@ static bool collect_changes(void) s_core.change.value.double_point = (uint8_t)value; break; case 13U: + /* 位模式比较:不是浮点比较,NaN/-0 的判等语义必须原样保留。 */ memcpy(&value, &s_core.object.measurement.value, sizeof(value)); quality = s_core.object.measurement.quality; s_core.change.kind = IEC104_SPONTANEOUS_FLOAT; @@ -457,15 +492,16 @@ static bool collect_changes(void) default: return false; } s_core.change.quality = quality; - if (previous->initialized && (previous->value != value || previous->quality != quality)) { + if (previous_initialized(cursor) && + (previous_value(cursor) != value || s_core.prev_quality[cursor] != quality)) { IEC104_SpontaneousPublishResult_t result = IEC104_Spontaneous_Publish(s_core.spontaneous, &s_core.change, 1U, &s_core.publication); if (result == IEC104_SPONTANEOUS_PUBLISH_WAIT) { return true; } if (result != IEC104_SPONTANEOUS_PUBLISH_OK) { return false; } } - previous->value = value; - previous->quality = quality; - previous->initialized = true; + previous_store(cursor, value); + s_core.prev_quality[cursor] = quality; + s_core.prev_initialized[cursor >> 3] |= (uint8_t)(1U << (cursor & 7U)); ++s_core.sample_cursor; if (s_core.sample_cursor == CORE_MONITOR_CAPACITY) { s_core.sample_cursor = 0U; } } @@ -571,6 +607,19 @@ static int initialize_core(const iec104_core_config_t *config) memset(&s_core, 0, sizeof(s_core)); s_core.ca = config->common_address; iec104_source_init(); + /* 点表顺序被改动后这里必须同步,宁可启动失败也不能静默发错变位: + * 变化检测基线按 0..58 单点 / 59..74 双点 / 75..104 测量值分三张表存放, + * 段边界一旦和点表顺序错位,就会把一种类型的值当成另一种比较。 + * 时机:每次 iec104_core_start 成功都会跑一次(网络代次变化 / poll 服务级 + * 失败都会 stop→start,不是开机一次),代价是每次重连多 105 次 + * iec104_source_point() 调用。刻意不缓存成静态标志:重连后点表若已被改坏, + * 这一次校验就是唯一防线。 */ + for (i = 0U; i < CORE_MONITOR_CAPACITY; ++i) { + uint8_t expected = i < CORE_DOUBLE_FIRST ? 1U : (i < CORE_MEASURE_FIRST ? 3U : 13U); + if (!iec104_source_point(i, &s_core.object) || s_core.object.object.type_id != expected) { + return fail(); + } + } s_core.slave = IEC104_Slave_Create(); if (s_core.slave == NULL) { return fail(); } s_core.status.owns_resources = 1U; diff --git a/App/iec60870/104/iec104_source.c b/App/iec60870/104/iec104_source.c index 257bd7a4..6d9ca1ef 100644 --- a/App/iec60870/104/iec104_source.c +++ b/App/iec60870/104/iec104_source.c @@ -27,12 +27,12 @@ static IEC104_RemoteParameterRequest_t owner; static bool stage_active, stage_complete; static iec104_source_bindings_t bindings; static iec104_source_stats_t stats; -static uint8_t singles[IEC104_DTU_SP_COUNT], single_set[IEC104_DTU_SP_COUNT]; -static uint8_t doubles[IEC104_DTU_DP_COUNT]; +/* 模拟注入通道已删除:单点/双点/测量值三张置位表与值表既无写者也无导出写 API, + * 读出恒为“零值 + 无效品质”,直接由常量取代,不再占 RAM。 + * 仅保留仍被 iec104_source_find() 读取的 steps/bits(IOA 0x1201..0x1208 与 + * 0x1301..0x1308 的返回信息),删掉会把“返回 0 + 品质”变成“未知地址”。 */ static int8_t steps[8]; static uint32_t bits[8]; -static float measurements[IEC104_DTU_ME_COUNT]; -static uint8_t measurement_set[IEC104_DTU_ME_COUNT]; /* 标准参数点表按 (CA,IOA) 升序借用给 IEC104_Parameter 模块。 * 只有 common_address 是运行期值(同实例同一CA),故只在这里保留20行实测可知: * 整表仍是 RAM,但用 4 字节索引函数代替 120 行逐字段赋值,代码与 Flash 均更省。 */ @@ -184,10 +184,7 @@ void iec104_source_init(void) { size_t i, a, g, m; iec104_dtu_parameter_t p; uint32_t raw; memset(&bindings, 0, sizeof(bindings)); memset(&stats, 0, sizeof(stats)); - memset(singles, 0, sizeof(singles)); - memset(single_set, 0, sizeof(single_set)); memset(doubles, 0, sizeof(doubles)); memset(steps, 0, sizeof(steps)); memset(bits, 0, sizeof(bits)); - memset(measurements, 0, sizeof(measurements)); memset(measurement_set, 0, sizeof(measurement_set)); clear_stage(); clock_utc = UINT64_C(946684800000); clock_mono = 0U; #ifndef IEC104_SOURCE_NO_VALUE_STORE memset(values, 0, sizeof(values)); @@ -228,18 +225,15 @@ bool iec104_source_point(size_t index, iec104_source_object_t *out) if (bindings.signal != NULL && !bindings.signal(bindings.context, p.source_addr, &v, &q)) { v = 0U; q = NO_SOURCE_QUALITY; } - if (single_set[index]) { v = singles[index]; q = SIM_QUALITY; } return IEC60870_SinglePointInformation_Create(&out->single, p.ioa, v, q) == IEC60870_ASDU_OK; } if (p.type == 3U) { - n = index - 59U; v = doubles[n]; q = v == 0U ? NO_SOURCE_QUALITY : SIM_QUALITY; + v = 0U; q = NO_SOURCE_QUALITY; return IEC60870_DoublePointInformation_Create(&out->double_point, p.ioa, v, q) == IEC60870_ASDU_OK; } - n = index - 75U; if (bindings.measurement != NULL && !bindings.measurement(bindings.context, p.source_addr, &f, &q)) { f = 0.0f; q = NO_SOURCE_QUALITY; } - if (measurement_set[n]) { f = measurements[n]; q = SIM_QUALITY; } return IEC60870_MeasuredValueShort_Create(&out->measurement, p.ioa, f, q) == IEC60870_ASDU_OK; } n = index - 105U; @@ -370,9 +364,13 @@ const IEC104_CommandPoint_t *iec104_source_commands(uint16_t ca, size_t *count) * 回 IEC104_COMMAND_ACTION_NOT_STARTED:vendor 据此回否定确认(COT 7 + P/N=1) * 并结束事务,既不产生返回信息也不排 ACT_TERM,与割接前"除 YK11/YK12 外一律否定 * 确认"的对外行为对齐。 - * 恢复真实出口时:把有出口的分支改成调用对应驱动并返回 STARTED 即可,模拟状态数组 - * (singles/doubles/steps/measurements/bits)保留未删可直接复用;当前无人置位, - * 它们的读出恒为"零值 + IV/SB"。 */ + * 恢复真实出口时:把有出口的分支改成调用对应驱动并返回 STARTED 即可;模拟注入通道 + * 已随"无写者"核实删除(iec104_source.o 的 ZI 5912 -> 5624,省 288B;比数组 + * 名义总和 284B 多 4B,因为两处 2 字节对齐空隙一并消失:doubles 之后到 bits、 + * measurement_set 之后到 standard_points 各 2B),故恢复出口须重新引入所需状态表。 + * 仅保留仍被 iec104_source_find() 读取的 steps/bits(IOA 0x1201..0x1208 与 + * 0x1301..0x1308 的返回信息),其余注入通道已删除;当前无人置位, + * steps/bits 的读出恒为"零值 + IV/SB"。 */ IEC104_CommandActionResult_t iec104_source_command(void *context, const IEC104_CommandRequest_t *r) { (void)context; (void)r; diff --git a/App/task/iec104_serv.c b/App/task/iec104_serv.c index e32dab44..269f520f 100644 --- a/App/task/iec104_serv.c +++ b/App/task/iec104_serv.c @@ -137,9 +137,10 @@ static bool s_measure(void *ctx, uint16_t source_addr, float *value, uint8_t *qu * 入口参数:ctx / source_addr / v / q(同 s_measure) * 返回值:恒 false * 限定条件:无 - * 函数说明:docs/硬件平台/开入开出实施说明.md:28 明确"104 遥信点表未接开入、 - * 继电器动作语义未定义"。板端 8 路开入是 bit0=A..bit7=H 的无语义电平, - * 点表 59 个遥信 source_addr 是 0x9146/... 具体业务点。硬接 = 编造。 + * 函数说明:docs/硬件平台/开入开出实施说明.md 的「1.2 现在能不能用」一节明确 + * "104 遥信点表未接开入、继电器动作语义未定义"。板端 8 路开入是 + * bit0=A..bit7=H 的无语义电平,点表 59 个遥信 source_addr 是 0x9146/... + * 具体业务点。硬接 = 编造。 * 将来 Lead/用户给出对照表后,只在本函数查一张 static const 映射表 * (读 ioboard_di_snap_lookup() + ioboard_di_snap_read()),函数签名与 * bind 流程均不用改;详见 ci-artifacts/iec104-cutover/source-bind-plan.md §3.3。 */ @@ -155,7 +156,7 @@ static bool s_signal(void *ctx, uint16_t source_addr, uint8_t *value, uint8_t *q * 限定条件:仅 netTask,在 iec104_core_start() 成功分支、首次 poll 之前 * 函数说明:**为什么不能在系统初始化时绑一次就完事**—— * iec104_source_init() 的首条语句就是 memset(&bindings, 0, ...),而它唯一的 - * 产品侧调用点在 iec104_core.c:573 initialize_core(),由 iec104_core_start() + * 产品侧调用点是 iec104_core.c 的 initialize_core(),由 iec104_core_start() * 每次 start 触发(网络代次变化 / poll 服务级失败都会 stop→start)。 * 若只在开机时绑一次,第一次重连后 bindings 被清零且再无人重绑, * 主站又退回"0 + IV"且无任何日志——这是下一个接手人最容易踩的坑。 diff --git a/docs/内存管理/RAM使用分析与优化方案.md b/docs/内存管理/RAM使用分析与优化方案.md index 91669bd6..7b9cd196 100644 --- a/docs/内存管理/RAM使用分析与优化方案.md +++ b/docs/内存管理/RAM使用分析与优化方案.md @@ -47,10 +47,10 @@ dhara 失败类 +84;RO 另 +208,全是该失败类名表)→ `ER_IROM1` | 区域 | 用量 | 容量 | 占比 | |---|---|---|---| -| ER_IROM1 | 413,416 B (403.73 KB) | 1,048,576 B (1024 KB) | 39.4% | -| RW_IRAM1 | 118,120 B (115.35 KB) | 131,072 B (128 KB) | 90.1% | +| ER_IROM1 | 413,392 B (403.70 KB) | 1,048,576 B (1024 KB) | 39.4% | +| RW_IRAM1 | 117,304 B (114.55 KB) | 131,072 B (128 KB) | 89.5% | | RW_CCM | 57,376 B (56.03 KB) | 65,280 B (64 KB) | 87.9% | -| **Flash (ROM 合计)** | **416,152 B (406.4 KB)** | 1,048,576 B (1024 KB) | **39.7%** | +| **Flash (ROM 合计)** | **416,128 B (406.4 KB)** | 1,048,576 B (1024 KB) | **39.7%** | 各区的容量与常量来源: diff --git a/docs/内存管理/RAM预算台账.md b/docs/内存管理/RAM预算台账.md index 21385ca6..989acf5f 100644 --- a/docs/内存管理/RAM预算台账.md +++ b/docs/内存管理/RAM预算台账.md @@ -15,10 +15,10 @@ | 区域 | 用量 | 容量 | 占比 | 门禁(≤92%) | |---|---|---|---|---| -| ER_IROM1 (Flash) | 413,416 B | 1,048,576 B | 39.4% | 参考 | +| ER_IROM1 (Flash) | 413,392 B | 1,048,576 B | 39.4% | 参考 | | RW_CCM | 57,376 B | 65,280 B | 87.9% | PASS | | UNINIT_CCM | 48 B | 256 B | 18.8% | 参考 | -| RW_IRAM1 | 118,120 B | 131,072 B | 90.1% | PASS | +| RW_IRAM1 | 117,304 B | 131,072 B | 89.5% | PASS | | 子系统(静态 RW+ZI;除 RTOS 堆在 CCM 外均在 SRAM1) | 现值 | 配额 | 状态 | |---|---|---|---| @@ -26,7 +26,7 @@ | 存储 | 9,144 B | 12 KB | OK | | UI | 36,791 B | 16 KB | ⚠ 超 19.9 KB | | RTOS 堆 | 57,376 B | 56 KB | 堆=配额 | -| 应用 | 47,403 B | 8 KB | ⚠ 超 38.3 KB | +| 应用 | 46,587 B | 8 KB | ⚠ 超 37.5 KB | | 其它 | 10,797 B | — | 参考 | diff --git a/scripts/ram_baseline.tsv b/scripts/ram_baseline.tsv index be4c0ee0..07c22ab3 100644 --- a/scripts/ram_baseline.tsv +++ b/scripts/ram_baseline.tsv @@ -24,4 +24,4 @@ date code ro rw zi er_irom1 er_pct iram1 iram1_pct ccm ccm_pct top_rwzi 2026-11-13 256592 32036 2088 152584 288628 27.5 97248 74.2 57376 87.9 heap_4.o=57376;lv_mem_core_builtin.o=16384;nand_ftl.o=8288;lftpd.o=7884;ui_net_sink.o=6802;ui_port.o=6432;wave_ring.o=5332;sb_link.o=4376;ch395f.o=4162;console_log.o=4132 2026-11-15 256592 32036 2088 152584 288628 27.5 97248 74.2 57376 87.9 heap_4.o=57376;lv_mem_core_builtin.o=16384;nand_ftl.o=8288;lftpd.o=7884;ui_net_sink.o=6802;ui_port.o=6432;wave_ring.o=5332;sb_link.o=4376;ch395f.o=4162;console_log.o=4132 2026-11-18 365896 48336 2736 173104 414232 39.5 118416 90.3 57376 87.9 heap_4.o=57376;lv_mem_core_builtin.o=16384;nand_ftl.o=8288;lftpd.o=7884;iec104_tcpserver.o=7656;ui_net_sink.o=6802;ui_port.o=6432;iec104_source.o=5944;wave_ring.o=5332;sb_link.o=4376 -2026-11-19 365080 48336 2736 172808 413416 39.4 118120 90.1 57376 87.9 heap_4.o=57376;lv_mem_core_builtin.o=16384;nand_ftl.o=8288;lftpd.o=7884;iec104_tcpserver.o=7360;ui_net_sink.o=6802;ui_port.o=6432;iec104_source.o=5944;wave_ring.o=5332;sb_link.o=4376 +2026-11-19 365056 48336 2736 171992 413392 39.4 117304 89.5 57376 87.9 heap_4.o=57376;lv_mem_core_builtin.o=16384;nand_ftl.o=8288;lftpd.o=7884;iec104_tcpserver.o=7360;ui_net_sink.o=6802;ui_port.o=6432;iec104_source.o=5656;wave_ring.o=5332;sb_link.o=4376 diff --git a/scripts/size_attestation.json b/scripts/size_attestation.json index c3ae39e5..c61002cd 100644 --- a/scripts/size_attestation.json +++ b/scripts/size_attestation.json @@ -1,25 +1,25 @@ { - "built_at": "2026-11-18T22:22:18+08:00", - "fingerprint": "256d83d9b538cf07128939875d5be8c19b63937dbb183d69d3228d8dbcf2e5c9", - "fingerprint_files": 1564, + "built_at": "2026-11-19T12:39:05+08:00", + "fingerprint": "3cf7eb47ff97d4e8bca135c8bd81d0b47f6824b8bf42541108b0b0d3c9ade85a", + "fingerprint_files": 1563, "fingerprint_source": "git-index", "gated_regions": [ "RW_IRAM1", "RW_CCM" ], - "git_head": "be18b81e82ad300c9132ec48781eac40e7b42402", + "git_head": "f23d30111d3201cbccdef2b42866015e5fb10033", "map": "MDK-ARM/STM32F407-Demo/STM32F407-Demo.map", - "map_mtime": 1795011694, + "map_mtime": 1795062995, "pct": { - "ER_IROM1": 39.5, + "ER_IROM1": 39.42, "RW_CCM": 87.89, - "RW_IRAM1": 90.34, + "RW_IRAM1": 89.5, "UNINIT_CCM": 18.75 }, "regions": { "ER_IROM1": { "max": 1048576, - "size": 414232 + "size": 413392 }, "RW_CCM": { "max": 65280, @@ -27,7 +27,7 @@ }, "RW_IRAM1": { "max": 131072, - "size": 118416 + "size": 117304 }, "UNINIT_CCM": { "max": 256, @@ -37,10 +37,10 @@ "schema": 2, "scope_note": "\u540c\u7801\u51ed\u8bc1\uff1a\u6307\u7eb9\u8986\u76d6\u56fa\u4ef6\u4f53\u79ef\u76f8\u5173\u6e90\u7801\uff0c\u53e3\u5f84\u89c1\u672c\u811a\u672c docstring", "sizes": { - "code": 365896, + "code": 365056, "ro_data": 48336, "rw_data": 2736, - "zi_data": 173104 + "zi_data": 171992 }, "threshold_pct": 92.0, "worktree_dirt_at_stamp": 0,