perf(vd960DBN): 二轮RAM瘦身 — RECE_BUF_LEN 1024 + ARP 16 + 中断路径static (2026-08-17)

.map 分析: BLE 栈固定占 RAM 低 16KB 不可裁; 用户 48KB 内 .bss 41.7KB。
- net_config.h: RECE_BUF_LEN MSS×2(1400)→1024 (MSS=700帧+头≈740B, 5数组省~2.6KB)
- net_config.h: WCHNET_NUM_ARP_TABLE 50→16 (停车场景IP少, 省~0.8KB)
- tcp_json_srv.c: tmp_buf[RECE_BUF_LEN]/frame[800] 局部→static (中断上下文栈数组防溢出)

栈 4.7KB→~6.3KB, 中断栈峰值 -1.8KB。devlog 记录 .map 分析结论。
This commit is contained in:
wangfq
2026-08-17 14:26:22 +08:00
parent 89ba602e4f
commit cb63c581be
3 changed files with 25 additions and 10 deletions
@@ -90,7 +90,10 @@ extern "C" {
/* If you want to achieve a higher transmission speed,
* try to increase RECE_BUF_LEN to (WCHNET_TCP_MSS*4)
* and increase WCHNET_NUM_TCP_SEG to (WCHNET_NUM_TCP*4)*/
#define RECE_BUF_LEN (WCHNET_TCP_MSS*2) /* socket receive buffer size */
/* 2026-08-17 RAM 瘦身: MSS×2(1400)→1024 — MSS=700 时完整 TCP 段+头≈740B,
1024 足够; 5 个 RECE_BUF_LEN 数组 (SocketRecvBuf/MyBuf/tmp/_iot_wchnet_buf/
g_json_wchnet_buf) 合计省 ~2.6KB .bss (栈空间 +2.6KB) */
#define RECE_BUF_LEN 1024 /* socket receive buffer size */
#define WCHNET_NUM_PBUF WCHNET_NUM_POOL_BUF /* Number of PBUF structures */
@@ -98,7 +101,7 @@ extern "C" {
#define WCHNET_MEM_HEAP_SIZE (((WCHNET_TCP_MSS+0x10+54+8)*WCHNET_NUM_TCP_SEG)+ETH_TX_BUF_SZE+64+2*0x18) /* memory heap size */
#define WCHNET_NUM_ARP_TABLE 50 /* Number of ARP lists */
#define WCHNET_NUM_ARP_TABLE 16 /* 2026-08-17: 50→16 (停车设备同时连接 IP 少, 省 ~0.8KB .bss) */
#define WCHNET_MEM_ALIGNMENT 4 /* 4 byte alignment */
@@ -1003,13 +1003,13 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) {
uint16_t space = TCP_JSON_RECV_BUF_LEN - g_json_recv_len;
if (recv_len > space) recv_len = space;
uint32_t rd_len = recv_len;
uint8_t tmp_buf[RECE_BUF_LEN];
static uint8_t tmp_buf[RECE_BUF_LEN]; /* 2026-08-17: static — 中断上下文 1KB 栈数组 (防溢出) */
WCHNET_SocketRecv(socketid, tmp_buf, &rd_len);
g_json_last_comm_ts = mstick(); // 有数据交互
memcpy(g_json_recv_buf + g_json_recv_len, tmp_buf, (uint16_t)rd_len);
g_json_recv_len += (uint16_t)rd_len;
char frame[TCP_JSON_MAX_FRAME];
static char frame[TCP_JSON_MAX_FRAME]; /* 2026-08-17: static — 中断上下文 800B 栈数组 (防溢出) */
while (json_extract_frame(g_json_recv_buf, &g_json_recv_len, frame, sizeof(frame))) {
PRINT("JSON recv: %s\n", frame);
json_process_frame(socketid, frame);
+18 -6
View File
@@ -65,13 +65,25 @@ BOOT_CNT:1 @0x2000f84c END:0x2000f834 SP:0x2000ffd0
⚠ ETH_MAX_PACKET_SIZE 768 是贴着 MSS=700 的最小值:TCP 帧 758B ✓;
若现场出现 >728B 数据的大 UDP 包会截断,保守可改 1024(省 2.48KB)。
### .map 分析 + 二轮瘦身(2026-08-17 晚,commit 后述)
用户提供编译后 .map 解析结论:
- **BLE 栈(libwchble.a)固定占用 RAM 低 16KB0x20000000~0x20004000,链接脚本外)**——WCH CH32V20x 工程约定,不可裁剪
- 用户 48KB.data 1.6K + .bss 41.7K + .noinit 28B → 栈 4.7KB(实测稳定)
- .bss 大块:RECE_BUF_LEN 系列 ~10KB、Mem_Heap 7.2KB、MACRx/TxBuf 3.8KB、eth DMA 5.9KB、SPI_FLASH_BUF 4KB、ARP 1.2KB、iot buf 2KB
二轮瘦身(省 ~3.4KB,栈 4.7KB → ~6.3KB + 中断栈峰值降 1.8KB):
| 文件 | 项目 | 改动 | 收益 |
|------|------|------|------|
| net_config.h | RECE_BUF_LEN | MSS×2(1400)→**1024**MSS=700 帧+头≈740B 够) | 5 数组省 ~2.6KB |
| net_config.h | WCHNET_NUM_ARP_TABLE | 50→**16**(停车设备连接 IP 少) | ~0.8KB |
| tcp_json_srv.c | tmp_buf[RECE_BUF_LEN] | 局部→**static**(中断上下文 1KB 栈数组) | 栈峰值 -1KB |
| tcp_json_srv.c | frame[TCP_JSON_MAX_FRAME] | 局部→**static**(中断上下文 800B | 栈峰值 -0.8KB |
### 遗留(根治)
- [ ] **确认 BLE 栈(libwchble.a)实际占用**:需用户提供编译后 .mapobj/*.map 或 MRS Release 目录)
BLE 栈若是 20+KB 固定成本,应用层已省 4.8KB 足够; 若可裁剪更好
- [ ] map 确认后可选)RECE_BUF_LEN 1400→1024SocketRecvBuf[3][1400]+MyBuf 省 1.5KB
ARP 表 50→16
- [ ] 局部大数组转 statictcp_json data_json[1024]×2、net_srv resp[600]):省 .bss 有余量后做,栈峰值 -2KB+
- [ ] output_cfg 恢复:减栈后重新启用调试打印
- [x] BLE 栈占用确认:16KB 固定(RAM 低区),不可裁剪
- [ ] (可选)g_report_cfg 1400B 结构体审查(ReportConfig 含大数组?
- [ ] 可选)Mem_Heap_Memory 7.2KB 是 WCHNET 库内部堆,需查库配置是否可减
- [ ] 快照区 3s 延后初始化保留(已确认非根因,作为启动早期 SPI 减压无害)
### 验证