From ee3ade6f00b453ab306081395eb3bc4feff2ca96 Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 13 Aug 2026 16:06:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(vd960DBN):=20PRINT=20=E5=AE=8F=E4=B8=B4?= =?UTF-8?q?=E7=95=8C=E5=8C=BA=E4=BF=9D=E6=8A=A4=20=E2=80=94=20printf=20?= =?UTF-8?q?=E9=87=8D=E5=85=A5=E4=BF=AE=E5=A4=8D=20(=E4=B9=B1=E7=A0=81+Hard?= =?UTF-8?q?Fault=20=E6=A0=B9=E5=9B=A0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现场: LUP 帧后乱码+复位。JSON: sensor 打印被二进制污染、 LUP Rx hex 残留重复 → printf 交叉输出的典型症状。 根因: BLE 协议栈回调(peripheral.c, BB 中断上下文)与主循环 都有 PRINT, 交叉调用 printf → 输出乱码 + newlib 堆/状态破坏 → HardFault (RST=0x10000000 为 HardFault_Handler 内 NVIC_SystemReset)。 修复: - debug.h PRINT 宏: 保存/恢复 MSTATUS 临界区 (__get_MSTATUS/ __disable_irq/printf/__set_MSTATUS), 中断里调用也能正确恢复, 不会嵌套误开中断 - loop_uart_proto: LUP Rx 逐字节打印改缓冲一次性输出 — 逐字节 PRINT 关中断 ~350us/字节屏蔽 UART2 ISR 丢帧 --- .../OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c | 14 +++++++++----- vd960DBN/SRC/Debug/debug.h | 11 ++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c index 0f0941b..3c999d2 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c @@ -686,12 +686,16 @@ void lup_process_frame(const uint8_t *pkg, uint16_t len) if (len < 6) { FAULT_MARKER(MK_LUP_FRAME_OUT); return; } - // Debug: print raw - PRINT("LUP Rx:"); - for (i = 0; i < len; i++) { - PRINT(" %02X", pkg[i]); + // Debug: print raw (2026-08-13: 缓冲一次性输出 — 逐字节 PRINT 关中断 ~350us/字节 + // 会屏蔽 UART2 ISR 丢帧, 且高频打印放大 printf 重入窗口) + { + char _hex[224]; + int _p = snprintf(_hex, sizeof(_hex), "LUP Rx:"); + for (i = 0; i < len && _p >= 0 && _p < (int)sizeof(_hex) - 5; i++) { + _p += snprintf(_hex + _p, sizeof(_hex) - (size_t)_p, " %02X", pkg[i]); + } + PRINT("%s\n", _hex); } - PRINT("\n"); // --- Checksum --- csr = lup_verify_checksum(pkg, len); diff --git a/vd960DBN/SRC/Debug/debug.h b/vd960DBN/SRC/Debug/debug.h index ebead36..ca8e7bd 100644 --- a/vd960DBN/SRC/Debug/debug.h +++ b/vd960DBN/SRC/Debug/debug.h @@ -46,7 +46,16 @@ void USART_Printf_Init(uint32_t baudrate); void SDI_Printf_Enable(void); #if(DEBUG) - #define PRINT(format, ...) printf(format, ##__VA_ARGS__) + /* 2026-08-13 printf 重入修复: BLE 协议栈回调(peripheral.c, BB 中断上下文) + 与主循环都有 PRINT, 交叉调用 printf → 输出乱码 + 堆/状态破坏 → HardFault。 + 临界区用保存/恢复 MSTATUS: 中断里调用也能正确恢复, 不会嵌套误开中断。 + 注意: 高频逐字节 PRINT (如 LUP Rx hex) 会长时间关中断, 应改缓冲一次性输出 */ + #define PRINT(format, ...) do { \ + uint32_t _print_ms = __get_MSTATUS(); \ + __disable_irq(); \ + printf(format, ##__VA_ARGS__); \ + __set_MSTATUS(_print_ms); \ + } while(0) #else #define PRINT(X...) #endif