diff --git a/App/log/console_log.c b/App/log/console_log.c index fdd4986d..6283c837 100644 --- a/App/log/console_log.c +++ b/App/log/console_log.c @@ -69,6 +69,12 @@ * 再读 DR"清除 ⇒ 永不自愈);引脚悬空只影响发生率,不是必要条件。 * 缓解措施与结论不变,仅改注释;机制详见 * docs/子系统设计/控制台单写者日志设计.md §9。 + * 2026-11-16 修正 con_dwt_ensure() 无条件清零 CYCCNT(台架取证发现,见 + * App/util/task_prof.c 与 Drivers/BSP/IOBOARD/ioboard.c 的契约注释)。 + * 本模块的超时判据是 (CYCCNT - t0) 差值,t0 是调用时刻的快照,不依赖 + * 绝对零点;因此清零没有收益,却会让同内核其他三处"在途"差值变成 + * 接近 2^32 的回绕并被静默丢弃——台架表现为 [PROF2] 的 defi 最坏值 + * 不完整。改为只在使能 CYCCNTENA 时打开计数,不再清零。 */ #include "console_log.h" @@ -219,16 +225,22 @@ static uint16_t con_used(void) { * 入口参数:无 * 返回值:无 * 限定条件:任意上下文可调(幂等;纯寄存器写,不创建任何内核对象) - * 函数说明:CoreDebug->DEMCR.TRCENA=1 → DWT->CYCCNT=0 → DWT->CTRL.CYCCNTENA=1; + * 函数说明:CoreDebug->DEMCR.TRCENA=1 → DWT->CTRL.CYCCNTENA=1; * 使能成功置 s_con_dwt_up=1(供超时判据选择),失败保持 0(退回 HAL 超时)。 * 在 Cortex-M4 上 DWT 恒存在;失败仅可能出现在调试单元被硬件关闭时。 + * **刻意不清零 CYCCNT**:本模块只用 (CYCCNT - t0) 差值判超时,基准是调用 + * 时刻抓的快照,不是绝对零点,清零对判据毫无帮助;而 CYCCNT 是全内核共享 + * 的单一计数器,本仓另有三个使用者同样在取差值——App/util/task_prof.c + * (探针最坏值)、Drivers/BSP/IOBOARD/ioboard.c(微秒延时)、 + * Drivers/BSP/ScreenLink/sb_link.c(组帧耗时)。清零会让它们"在途"的 + * t0 瞬间失效,差值变成一次接近 2^32 的无符号回绕,被各自的守卫静默丢弃。 + * 历史上这里确实无条件清零,与上述三处的契约注释直接冲突,2026-11-16 修正。 */ static void con_dwt_ensure(void) { if (s_con_dwt_up != 0U) { return; } CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - DWT->CYCCNT = 0U; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; if ((DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U) { s_con_dwt_up = 1U; diff --git a/App/util/task_prof.c b/App/util/task_prof.c index b49809dd..67b46776 100644 --- a/App/util/task_prof.c +++ b/App/util/task_prof.c @@ -184,9 +184,13 @@ static void prof_emit(uint32_t raw) { * 入口参数:无 * 返回值:当前 CYCCNT 值(周期数) * 限定条件:任意上下文可调(幂等;纯寄存器读写) - * 函数说明:只置使能位、**不清零** CYCCNT——本模块只用差值,清零无意义, - * 且会与 console_log.c 的同名惰性使能互相干扰(该模块对 CYCCNT - * 清零后,在途的差值会变成一次无符号回绕,由 glitch 守卫丢弃)。 + * 函数说明:只置使能位、**不清零** CYCCNT——本模块只用差值,清零无意义。 + * 本仓另有三个使用者共享这同一个计数器且同样取差值:App/log/console_log.c + * (发送超时硬上界)、Drivers/BSP/IOBOARD/ioboard.c(微秒延时)、 + * Drivers/BSP/ScreenLink/sb_link.c(组帧耗时)。2026-11-16 之前 + * console_log.c 曾无条件清零,造成在途差值回绕、样本被 glitch 守卫吞掉; + * 现四处口径统一为"只开不清"。**任何人再往这里加清零,等于让另外三处 + * 同时丢样本**,改之前先看本段。 */ uint32_t task_prof_cyc(void) { if (s_dwt_ok == 0u) { diff --git a/App/util/task_prof.h b/App/util/task_prof.h index 6ddd017b..89fd161b 100644 --- a/App/util/task_prof.h +++ b/App/util/task_prof.h @@ -69,9 +69,11 @@ typedef enum { * 返回值:当前 CYCCNT 值(周期数) * 限定条件:任意上下文可调(幂等;纯寄存器读,不创建任何内核对象) * 函数说明:首次调用置 CoreDebug->DEMCR.TRCENA 与 DWT->CTRL.CYCCNTENA。 - * 刻意**不清零** CYCCNT:本模块只用差值,清零无意义,且会与 - * console_log.c 的同名惰性使能互相干扰。DWT 使能失败时恒返回 0, - * 采样侧把 0 判为"不可用"(见 task_prof_sample)。 + * 刻意**不清零** CYCCNT:本模块只用差值,清零无意义。CYCCNT 是全内核 + * 共享的单一计数器,本仓 console_log.c、IOBOARD 驱动与 ScreenLink 驱动 + * 也在用它取差值;任何一处清零都会让其余使用者"在途"的基准失效。 + * 该口径已于 2026-11-16 在四处统一(此前 console_log.c 会清零)。 + * DWT 使能失败时恒返回 0,采样侧把 0 判为"不可用"(见 task_prof_sample)。 */ uint32_t task_prof_cyc(void); diff --git a/docs/测试说明/专项验证/链路延迟测试说明.md b/docs/测试说明/专项验证/链路延迟测试说明.md index 1d324a30..df6ca943 100644 --- a/docs/测试说明/专项验证/链路延迟测试说明.md +++ b/docs/测试说明/专项验证/链路延迟测试说明.md @@ -154,7 +154,8 @@ ENABLE_PHASE14_TESTS [网] 生产路径时延(生产路径帧周期~uiTask ### 4.3 时基(DWT CYCCNT) - 工程内首个 DWT 用户(`sys_clock` 是纯 HAL tick,无 µs 能力);`lat_time_init()` 幂等使能 - `CoreDebug->DEMCR | TRCENA` + `DWT->CYCCNT=0` + `DWT->CTRL | CYCCNTENA`; + `CoreDebug->DEMCR | TRCENA` + `DWT->CTRL | CYCCNTENA`(**刻意不清零 CYCCNT**:本模块只用 + 前后两次读数差值,而 CYCCNT 为全内核共享,清零会打乱其余四个使用者在途的计时基准); - 分辨率 1/168MHz ≈ 5.95ns;换算用 `SystemCoreClock`(不写死频率); - **32 位回绕 = 25.17ms**:M1 各段(<5ms)与 M2 调用方侧测量(单次 <25ms)用单次读数差; 跨 25ms 的段(芯片→线上+PC/生产路径帧周期整帧与重传尖峰)由 PC 端 peer 计时,或 MCU 侧**累积采样** diff --git a/test/board/ch395f_reuse_bench.c b/test/board/ch395f_reuse_bench.c index 7b177556..e70330cb 100644 --- a/test/board/ch395f_reuse_bench.c +++ b/test/board/ch395f_reuse_bench.c @@ -401,7 +401,9 @@ static const int s_bench_legacy = 0; /* --- DWT 周期计数器:168 MHz,1 cycle ~= 5.95 ns(不依赖 HAL 时基/中断) --- */ static void bench_dwt_enable(void) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - DWT->CYCCNT = 0U; + /* 不清零 CYCCNT:本台架只用前后读数差值;CYCCNT 全内核共享,清零会打乱 + * console_log.c / task_prof.c / IOBOARD / ScreenLink 四处"在途"的计时。 + * 2026-11-16 全仓统一为"只开不清"。 */ DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; } diff --git a/test/board/net_latency_test.c b/test/board/net_latency_test.c index 9a76bd19..d8fb3778 100644 --- a/test/board/net_latency_test.c +++ b/test/board/net_latency_test.c @@ -50,7 +50,11 @@ void lat_time_init(void) return; } CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; /* 使能 DWT 的前置条件 */ - DWT->CYCCNT = 0U; + /* 不清零 CYCCNT:本模块只用前后两次读数的差值,基准是各自快照而非绝对零点。 + * 而 CYCCNT 是全内核共享的单一计数器,本仓另有 console_log.c(发送超时)、 + * App/util/task_prof.c(探针最坏值)、IOBOARD 驱动(微秒延时)、 + * ScreenLink 驱动(组帧耗时)四处同样取差值;清零会让它们"在途"的基准失效, + * 差值变成接近 2^32 的回绕并被各自的守卫静默丢弃。2026-11-16 统一为"只开不清"。 */ DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; s_dwt_ready = 1; } diff --git a/test/host/console_log/console_log_test.c b/test/host/console_log/console_log_test.c index 5ff4d043..fe35edce 100644 --- a/test/host/console_log/console_log_test.c +++ b/test/host/console_log/console_log_test.c @@ -953,8 +953,8 @@ static void tc_dwt(void) "TC-CONLOG-091 DEMCR.TRCENA really gets written (bit number matches core_cm4.h)"); CHECK((g_con_dwt.CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U, "TC-CONLOG-092 DWT.CTRL.CYCCNTENA really gets written"); - CHECK(tx_is(0U, "ok\r\n") && con_dwt_reads == 4U, - "TC-CONLOG-093 with the channel ready the send loop reads no further CYCCNT (3 from ensure + 1 for t0)"); + CHECK(tx_is(0U, "ok\r\n") && con_dwt_reads == 3U, + "TC-CONLOG-093 with the channel ready the send loop reads no further CYCCNT (2 from ensure + 1 for t0; the third write is gone because ensure no longer resets CYCCNT)"); printf("-- TC-CONLOG-094..097 DWT enable failure -> con_tx_counted instruction ceiling --\n"); reset_all(); @@ -965,7 +965,7 @@ static void tc_dwt(void) CHECK(con_uart_txe_reads == CON_TX_GUARD_MAX, "TC-CONLOG-095 each byte spins at most CON_TX_GUARD_MAX(20000) times then gives up (bounded, not a dead wait)"); CHECK(con_uart_tx_n == 0U, "TC-CONLOG-096 if the block is not out within the counted ceiling the whole block is abandoned"); - CHECK(con_dwt_reads == 3U, + CHECK(con_dwt_reads == 2U, "TC-CONLOG-097 the counted branch never reads CYCCNT (independent of DWT, hence of any time source)"); printf("-- TC-CONLOG-098..099 the timeout criterion does not use HAL_GetTick --\n"); @@ -992,7 +992,7 @@ static void tc_null_uart(void) dbg_console_write_raw("boom", 5U); CHECK(con_uart_tx_n == 0U && con_uart_txe_reads == 0U, "TC-CONLOG-100 with Instance==NULL not one register is touched (no dereference of address 0)"); - CHECK(con_dwt_reads == 3U, + CHECK(con_dwt_reads == 2U, "TC-CONLOG-101 DWT is still lazily enabled before the short circuit (ensure stays ahead of the NULL check)"); reset_all(); @@ -1084,7 +1084,7 @@ static void tc_counted_paths(void) dbg_console_write_raw("AB", 2U); CHECK(s_con_dwt_up == 0U && tx_is(0U, "AB"), "TC-CONLOG-130 the counted branch sends the whole block and returns success (fallback path did not regress)"); - CHECK(con_uart_tc_reads == 1U && con_dwt_reads == 3U && con_tick_reads == 0U, + CHECK(con_uart_tc_reads == 1U && con_dwt_reads == 2U && con_tick_reads == 0U, "TC-CONLOG-131 the counted branch waits no tick and reads no CYCCNT (zero external time source)"); reset_all();