fix(dwt): stop resetting CYCCNT, a counter six modules share
Three of the six DWT CYCCNT users wrote the counter to zero on init: console_log, net_latency_test and ch395f_reuse_bench. Every one of them only ever takes the difference (CYCCNT - t0) against a local snapshot, so the reset bought nothing and instead caused a bogus ~2^32 delta for anyone who sampled across it. That delta trips the task_prof glitch guard (TASK_PROF_GLITCH_CYC = 10 s worth of cycles), which discards the sample, so a whole worst-case measurement could vanish silently. All three call sites now leave the counter alone; the shared contract is "enable only, never reset", documented at each of the six users. The documented behaviour in the link latency test guide (section 4.3) is updated to match. Net source change is three deleted lines, no new executable code. Verification: - grep for a zero assignment to CYCCNT across the tree: no hits left. - Keil build: Code=257932 RO-data=32108 RW-data=2104 ZI-data=152688, 0 errors 0 warnings. - Host unit tests: console_log 153/153, task_prof 27/27, ioboard 100/100, sb_link 164/164. - Four console_log assertions pinned the absolute number of DWT register touches; dropping the reset write lowers it by one, so their expected counts move from 3 to 2 (and 4 to 3 for TC-CONLOG-093). The test text now says why the third write disappeared. - check_doc_links: 6 known issues, unchanged from baseline.
This commit is contained in:
+14
-2
@@ -69,6 +69,12 @@
|
|||||||
* 再读 DR"清除 ⇒ 永不自愈);引脚悬空只影响发生率,不是必要条件。
|
* 再读 DR"清除 ⇒ 永不自愈);引脚悬空只影响发生率,不是必要条件。
|
||||||
* 缓解措施与结论不变,仅改注释;机制详见
|
* 缓解措施与结论不变,仅改注释;机制详见
|
||||||
* docs/子系统设计/控制台单写者日志设计.md §9。
|
* 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"
|
#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 超时)。
|
* 使能成功置 s_con_dwt_up=1(供超时判据选择),失败保持 0(退回 HAL 超时)。
|
||||||
* 在 Cortex-M4 上 DWT 恒存在;失败仅可能出现在调试单元被硬件关闭时。
|
* 在 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) {
|
static void con_dwt_ensure(void) {
|
||||||
if (s_con_dwt_up != 0U) {
|
if (s_con_dwt_up != 0U) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
||||||
DWT->CYCCNT = 0U;
|
|
||||||
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
||||||
if ((DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U) {
|
if ((DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U) {
|
||||||
s_con_dwt_up = 1U;
|
s_con_dwt_up = 1U;
|
||||||
|
|||||||
@@ -184,9 +184,13 @@ static void prof_emit(uint32_t raw) {
|
|||||||
* 入口参数:无
|
* 入口参数:无
|
||||||
* 返回值:当前 CYCCNT 值(周期数)
|
* 返回值:当前 CYCCNT 值(周期数)
|
||||||
* 限定条件:任意上下文可调(幂等;纯寄存器读写)
|
* 限定条件:任意上下文可调(幂等;纯寄存器读写)
|
||||||
* 函数说明:只置使能位、**不清零** CYCCNT——本模块只用差值,清零无意义,
|
* 函数说明:只置使能位、**不清零** CYCCNT——本模块只用差值,清零无意义。
|
||||||
* 且会与 console_log.c 的同名惰性使能互相干扰(该模块对 CYCCNT
|
* 本仓另有三个使用者共享这同一个计数器且同样取差值:App/log/console_log.c
|
||||||
* 清零后,在途的差值会变成一次无符号回绕,由 glitch 守卫丢弃)。
|
* (发送超时硬上界)、Drivers/BSP/IOBOARD/ioboard.c(微秒延时)、
|
||||||
|
* Drivers/BSP/ScreenLink/sb_link.c(组帧耗时)。2026-11-16 之前
|
||||||
|
* console_log.c 曾无条件清零,造成在途差值回绕、样本被 glitch 守卫吞掉;
|
||||||
|
* 现四处口径统一为"只开不清"。**任何人再往这里加清零,等于让另外三处
|
||||||
|
* 同时丢样本**,改之前先看本段。
|
||||||
*/
|
*/
|
||||||
uint32_t task_prof_cyc(void) {
|
uint32_t task_prof_cyc(void) {
|
||||||
if (s_dwt_ok == 0u) {
|
if (s_dwt_ok == 0u) {
|
||||||
|
|||||||
@@ -69,9 +69,11 @@ typedef enum {
|
|||||||
* 返回值:当前 CYCCNT 值(周期数)
|
* 返回值:当前 CYCCNT 值(周期数)
|
||||||
* 限定条件:任意上下文可调(幂等;纯寄存器读,不创建任何内核对象)
|
* 限定条件:任意上下文可调(幂等;纯寄存器读,不创建任何内核对象)
|
||||||
* 函数说明:首次调用置 CoreDebug->DEMCR.TRCENA 与 DWT->CTRL.CYCCNTENA。
|
* 函数说明:首次调用置 CoreDebug->DEMCR.TRCENA 与 DWT->CTRL.CYCCNTENA。
|
||||||
* 刻意**不清零** CYCCNT:本模块只用差值,清零无意义,且会与
|
* 刻意**不清零** CYCCNT:本模块只用差值,清零无意义。CYCCNT 是全内核
|
||||||
* console_log.c 的同名惰性使能互相干扰。DWT 使能失败时恒返回 0,
|
* 共享的单一计数器,本仓 console_log.c、IOBOARD 驱动与 ScreenLink 驱动
|
||||||
* 采样侧把 0 判为"不可用"(见 task_prof_sample)。
|
* 也在用它取差值;任何一处清零都会让其余使用者"在途"的基准失效。
|
||||||
|
* 该口径已于 2026-11-16 在四处统一(此前 console_log.c 会清零)。
|
||||||
|
* DWT 使能失败时恒返回 0,采样侧把 0 判为"不可用"(见 task_prof_sample)。
|
||||||
*/
|
*/
|
||||||
uint32_t task_prof_cyc(void);
|
uint32_t task_prof_cyc(void);
|
||||||
|
|
||||||
|
|||||||
@@ -154,7 +154,8 @@ ENABLE_PHASE14_TESTS [网] 生产路径时延(生产路径帧周期~uiTask
|
|||||||
### 4.3 时基(DWT CYCCNT)
|
### 4.3 时基(DWT CYCCNT)
|
||||||
|
|
||||||
- 工程内首个 DWT 用户(`sys_clock` 是纯 HAL tick,无 µs 能力);`lat_time_init()` 幂等使能
|
- 工程内首个 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`(不写死频率);
|
- 分辨率 1/168MHz ≈ 5.95ns;换算用 `SystemCoreClock`(不写死频率);
|
||||||
- **32 位回绕 = 25.17ms**:M1 各段(<5ms)与 M2 调用方侧测量(单次 <25ms)用单次读数差;
|
- **32 位回绕 = 25.17ms**:M1 各段(<5ms)与 M2 调用方侧测量(单次 <25ms)用单次读数差;
|
||||||
跨 25ms 的段(芯片→线上+PC/生产路径帧周期整帧与重传尖峰)由 PC 端 peer 计时,或 MCU 侧**累积采样**
|
跨 25ms 的段(芯片→线上+PC/生产路径帧周期整帧与重传尖峰)由 PC 端 peer 计时,或 MCU 侧**累积采样**
|
||||||
|
|||||||
@@ -401,7 +401,9 @@ static const int s_bench_legacy = 0;
|
|||||||
/* --- DWT 周期计数器:168 MHz,1 cycle ~= 5.95 ns(不依赖 HAL 时基/中断) --- */
|
/* --- DWT 周期计数器:168 MHz,1 cycle ~= 5.95 ns(不依赖 HAL 时基/中断) --- */
|
||||||
static void bench_dwt_enable(void) {
|
static void bench_dwt_enable(void) {
|
||||||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
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;
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,11 @@ void lat_time_init(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; /* 使能 DWT 的前置条件 */
|
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;
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
||||||
s_dwt_ready = 1;
|
s_dwt_ready = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -953,8 +953,8 @@ static void tc_dwt(void)
|
|||||||
"TC-CONLOG-091 DEMCR.TRCENA really gets written (bit number matches core_cm4.h)");
|
"TC-CONLOG-091 DEMCR.TRCENA really gets written (bit number matches core_cm4.h)");
|
||||||
CHECK((g_con_dwt.CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U,
|
CHECK((g_con_dwt.CTRL & DWT_CTRL_CYCCNTENA_Msk) != 0U,
|
||||||
"TC-CONLOG-092 DWT.CTRL.CYCCNTENA really gets written");
|
"TC-CONLOG-092 DWT.CTRL.CYCCNTENA really gets written");
|
||||||
CHECK(tx_is(0U, "ok\r\n") && con_dwt_reads == 4U,
|
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 (3 from ensure + 1 for t0)");
|
"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");
|
printf("-- TC-CONLOG-094..097 DWT enable failure -> con_tx_counted instruction ceiling --\n");
|
||||||
reset_all();
|
reset_all();
|
||||||
@@ -965,7 +965,7 @@ static void tc_dwt(void)
|
|||||||
CHECK(con_uart_txe_reads == CON_TX_GUARD_MAX,
|
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)");
|
"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_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)");
|
"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");
|
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);
|
dbg_console_write_raw("boom", 5U);
|
||||||
CHECK(con_uart_tx_n == 0U && con_uart_txe_reads == 0U,
|
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)");
|
"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)");
|
"TC-CONLOG-101 DWT is still lazily enabled before the short circuit (ensure stays ahead of the NULL check)");
|
||||||
|
|
||||||
reset_all();
|
reset_all();
|
||||||
@@ -1084,7 +1084,7 @@ static void tc_counted_paths(void)
|
|||||||
dbg_console_write_raw("AB", 2U);
|
dbg_console_write_raw("AB", 2U);
|
||||||
CHECK(s_con_dwt_up == 0U && tx_is(0U, "AB"),
|
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)");
|
"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)");
|
"TC-CONLOG-131 the counted branch waits no tick and reads no CYCCNT (zero external time source)");
|
||||||
|
|
||||||
reset_all();
|
reset_all();
|
||||||
|
|||||||
Reference in New Issue
Block a user