fix(ioboard): repair three judgement bugs in the DI/DO board suite

The suite ran on the bench and reset the target every 56 seconds, right
after the last Phase 3 assertion. Root cause was the suite itself: the
worker thread entry function returned instead of calling vTaskDelete.
Returning from a FreeRTOS task function is undefined behaviour, and on the
Cortex-M4F port the saved LR points at prvTaskExitError, so the return
trips configASSERT. This project routes configASSERT to
vApplicationAssertFailed, which resets the MCU. The assert banner never
reached the console because the reset happened 0.27 s after the last
line, while its bytes were still queued.

Two assertions were also wrong in ways that hid real state:

- The "which relay is commanded" mask was computed as MASK_ALL & ~bit,
  i.e. the complement of what the comment claimed, so the suite reported
  the three idle relays as commanded and failed every Phase 3 row even
  though the shadow register was correct.
- The "other relays stay released" check was written as (a|b)==b, which
  is the same predicate as "at least bit b set" and therefore duplicated
  the check above it instead of testing the remaining channels. It now
  asserts (active & ~only)==0.

Phase counters are also reset on entry to each phase. The report macro
only prints, so without a reset each phase summary carried every earlier
phase's failures and Phase 2 read 44/62 with no failures of its own.

Hard-coded 0x0F masks are replaced by IOBOARD_TEST_DO_MASK_ALL.

Bench result after the fix: the suite completes in about 54 s with one
reset cause line (boot only), and prints Phase 4, the final release check
and "IOBOARD Tests: 17/17 PASSED". Phase 2 moves to 21/21 and Phase 3 to
16/16.

Still unverified, and not claimed as passing: the eight DI channel order
needs a person to short the terminals, and the relay polarity needs a
person to hear the contacts. Note that ioboard_do_read_level reads ODR,
so the Phase 3 level assertions prove write intent propagation, not
electrical actuation.

Keil build with the suite enabled: Code=264880, 0 errors 0 warnings.
Product build after the suite switch is restored: Code=257932, unchanged.
This commit is contained in:
2026-11-16 19:58:52 +08:00
parent cc7f17f231
commit 3e80c301e5
+67 -15
View File
@@ -104,6 +104,15 @@
#define IOBOARD_TEST_DI_MASK_ALL ((uint8_t)IOBOARD_DI_MASK_ALL)
#define IOBOARD_TEST_DO_MASK_ALL ((uint8_t)IOBOARD_DO_MASK_ALL)
/* 进入各 Phase 前把统计清零:TEST_REPORT_IMPL 只打印、不自清零,若不清,
* Phase N 的汇总行会带上 Phase 1..N-1 的累计失败数(台架 2026-11-16 实证:
* Phase 2 报 "44/62 PASSED (failed=18)",那 18 条全是 Phase 1 的)。
* 套件级约定见 docs/测试说明/板级测试/SD2506_Test_Guide.md,先例见
* test/board/sd2506_test_task.c 进入各 Phase 前的 memset。 */
#define IOBOARD_TEST_STATS_RESET() do { \
(void)memset(&g_ioboard_test_stats, 0, sizeof(g_ioboard_test_stats)); \
} while (0)
/* 全局测试统计(宏自动管理) */
ioboard_test_stats_t g_ioboard_test_stats;
@@ -152,13 +161,38 @@ static void ioboard_io_delay(uint32_t ms)
}
}
/*
* 函数功能:把「4 路电气电平」折回「4 路是否吸合」的位掩码
* 入口参数:level - ioboard_do_read_level() 回的电气电平(bit0..bit3 = 4 路)
* 返回值:每一位 1 = 该路吸合,0 = 该路释放
* 限定条件:纯函数,无副作用,任意上下文可调
* 函数说明:**逐位**折算——每一位 XOR 该位自己的 RELEASE 电平。这是唯一正确的形态:
* 吸合 ⇔ 电平 ≠ RELEASE。见 ioboard_release_all 上方注释里的反例。
*/
static uint8_t ioboard_level_to_attracted(uint8_t level)
{
#if (IOBOARD_DO_LEVEL_RELEASE != 0U)
/* RELEASE = 1 时逐位取反;否则电平本身就是"是否吸合" */
return (uint8_t)(~level & (uint8_t)IOBOARD_TEST_DO_MASK_ALL);
#else
return (uint8_t)(level & (uint8_t)IOBOARD_TEST_DO_MASK_ALL);
#endif
}
/*
* 全释放的**唯一出口**:任何失败路径、任何 Phase 收尾、线程退出前都必须过这里。
* 打印与断言分开——这样"释放"这个动作本身不会被套件的统计口径影响。
*
* 「与极性一致」的判法:ioboard_do_read_level() 给的是**电气电平**(1 = 引脚为高),
* 而 IOBOARD_DO_LEVEL_ACTIVE 定义了「吸合 = 高还是低」。两者按位异或 RELEASE 电平,
* 就把电气电平折回「是否吸合」,极性宏一旦被改,本判据自动跟着改。
* 而 IOBOARD_DO_LEVEL_ACTIVE 定义了「吸合 = 高还是低」。把电气电平**折回"是否吸合"**
* 是「每一位都换成该位自己的语义」,所以折算必须**逐位**做:每位 XOR 上 RELEASE 电平。
* ⚠ 不能写成 `level ^ IOBOARD_DO_LEVEL_RELEASE` —— 那是把一个标量电平异或进整个掩码,
* RELEASE=0 时恒等(等于没折算),RELEASE=1 时把 4 位全部取反、语义彻底反过来。
* 2026-11-16 台架实证:本文件曾写 `level ^ (IOBOARD_DO_LEVEL_RELEASE & 0x0Fu)`,
* 在当前 RELEASE=0 下它是恒等变换,等价于「把电气电平原样当成吸合位掩码」。
* 这一写法在 RELEASE=0 时碰巧给出正确结果(因为那时"电平"就等于"吸合"),
* 但一旦有人把 IOBOARD_DO_LEVEL_ACTIVE 改成 0(极性反接),它会把 4 位全部取反,
* 判据整体自相矛盾地翻面。折算已收敛到下面这个函数,全文件只此一处。
*
* 函数功能:ioboard_do_write(0) 全释放,并回读电气电平核对是否真回到 RELEASE。
* 入口参数:where - 调用点标识(纯 ASCII,进日志用于定位是哪个 Phase 释放的)
@@ -249,7 +283,7 @@ static int ioboard_phase_selftest(void)
"all 4 DO released at entry (shadow=0x%02X expect 0x00)",
(unsigned int)shadow);
IOBOARD_TEST_CHECK((uint8_t)(level & IOBOARD_TEST_DO_MASK_ALL) ==
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & 0x0Fu),
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & IOBOARD_TEST_DO_MASK_ALL),
"all 4 DO pins at RELEASE level (level=0x%02X)",
(unsigned int)level);
@@ -345,6 +379,7 @@ static void ioboard_phase_di_order(void)
char line[IOBOARD_TEST_DI_COUNT_EXPECT + 1U];
DBG_INFO("=== IOBOARD Phase 1: DI bit order (one channel at a time + bijection) ===");
IOBOARD_TEST_STATS_RESET();
/* 基线:全部断开时应为 0。先采一轮建立"静止基准",后面每一路都以它为参照。 */
if (ioboard_di_sample(IOBOARD_TEST_DI_SAMPLES, IOBOARD_TEST_DI_SAMPLE_GAP_MS,
@@ -490,6 +525,7 @@ static void ioboard_phase_di_stable(void)
char line[IOBOARD_TEST_DI_COUNT_EXPECT + 1U];
DBG_INFO("=== IOBOARD Phase 2: DI stability with all channels open ===");
IOBOARD_TEST_STATS_RESET();
DBG_INFO(" KEEP ALL DI TERMINALS OPEN for the next ~%u ms",
(unsigned int)(IOBOARD_TEST_DI_STABLE_ROUNDS *
(IOBOARD_TEST_DI_SAMPLES * IOBOARD_TEST_DI_SAMPLE_GAP_MS)));
@@ -552,11 +588,12 @@ static void ioboard_phase_do_actuation(void)
uint32_t i;
DBG_INFO("=== IOBOARD Phase 3: DO relay tap test (4 relays, NO LOAD) ===");
IOBOARD_TEST_STATS_RESET();
DBG_INFO(" listen for the relay click; release level = %u, active level = %u",
(unsigned int)IOBOARD_DO_LEVEL_RELEASE, (unsigned int)IOBOARD_DO_LEVEL_ACTIVE);
for (i = 0u; i < (uint32_t)IOBOARD_TEST_DO_COUNT_EXPECT; i++) {
uint8_t one = (uint8_t)(IOBOARD_TEST_DO_MASK_ALL & (uint8_t)(~k_do_bit[i]));
uint8_t only = (uint8_t)(k_do_bit[i] & (uint8_t)IOBOARD_TEST_DO_MASK_ALL);
uint8_t shadow = 0xFFu;
uint8_t level = 0xFFu;
uint32_t active_bits;
@@ -564,10 +601,10 @@ static void ioboard_phase_do_actuation(void)
DBG_INFO(" [%u/4] CUE: %s relay will CLOSE for %u ms .. listen for click",
(unsigned int)(i + 1u), k_do_name[i], (unsigned int)IOBOARD_TEST_DO_ARM_MS);
rc = ioboard_do_write(one);
rc = ioboard_do_write(only);
if (rc != BSP_OK) {
IOBOARD_TEST_CHECK(0, "%s: ioboard_do_write(0x%02X) ret=%d",
k_do_name[i], (unsigned int)one, (int)rc);
k_do_name[i], (unsigned int)only, (int)rc);
ioboard_emergency_release("ioboard_do_write failed");
ioboard_release_all("after emergency");
continue;
@@ -579,27 +616,31 @@ static void ioboard_phase_do_actuation(void)
(void)ioboard_do_read_level(&level);
/* "与极性一致":按 IOBOARD_DO_LEVEL_ACTIVE 把电气电平折回"是否吸合" */
active_bits = ((level ^ (uint8_t)(IOBOARD_DO_LEVEL_RELEASE & 0x0Fu)) & 0x0Fu);
active_bits = ioboard_level_to_attracted(level);
DBG_INFO(" shadow=0x%02X level=0x%02X -> attracted=0x%02X (expected 0x%02X)",
(unsigned int)shadow, (unsigned int)level,
(unsigned int)active_bits, (unsigned int)k_do_bit[i]);
IOBOARD_TEST_CHECK((uint8_t)(shadow & 0x0Fu) == one,
/* 三条断言分工不同,不要合并:
* ① 影子寄存器 == 只有这一路(写入意图是否正确落地)
* ② 该路确实处于吸合电平(极性折算是否正确)
* ③ 其余 3 路都没有被带起来(其余位全为 0,不是"至少有一位") */
IOBOARD_TEST_CHECK((uint8_t)(shadow & IOBOARD_TEST_DO_MASK_ALL) == only,
"%s: shadow=0x%02X (expect 0x%02X: only this bit set)",
k_do_name[i], (unsigned int)shadow, (unsigned int)one);
k_do_name[i], (unsigned int)shadow, (unsigned int)only);
IOBOARD_TEST_CHECK((active_bits & k_do_bit[i]) != 0u,
"%s: pin at ACTIVE level while commanded (level=0x%02X)",
k_do_name[i], (unsigned int)level);
IOBOARD_TEST_CHECK((active_bits | k_do_bit[i]) == k_do_bit[i],
IOBOARD_TEST_CHECK((uint8_t)(active_bits & ~only) == 0u,
"%s: other 3 relays stay released (attracted=0x%02X)",
k_do_name[i], (unsigned int)active_bits);
/* 立即释放,并核对全部回到不吸合 */
ioboard_release_all("after tap");
(void)ioboard_do_read_level(&level);
IOBOARD_TEST_CHECK((uint8_t)(level & 0x0Fu) ==
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & 0x0Fu),
IOBOARD_TEST_CHECK((uint8_t)(level & IOBOARD_TEST_DO_MASK_ALL) ==
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & IOBOARD_TEST_DO_MASK_ALL),
"%s: all 4 released after tap (level=0x%02X)",
k_do_name[i], (unsigned int)level);
ioboard_io_delay(IOBOARD_TEST_DO_RELEASE_MS);
@@ -630,7 +671,7 @@ static void ioboard_test_body(void)
DBG_INFO("#### Strings are English-only; order matches the log lines. ####");
DBG_INFO("");
(void)memset(&g_ioboard_test_stats, 0, sizeof(g_ioboard_test_stats));
IOBOARD_TEST_STATS_RESET();
/* 任何异常退出(Phase 之间 return、断言崩溃之外)都保证全释放 */
ioboard_release_all("suite entry");
@@ -688,13 +729,24 @@ static void ioboard_thread_entry(void *argument)
/* 序列结束(无论成败)必须全释放,并单独记一条断言:这是安全口径的最后一道 */
ioboard_release_all("suite end");
(void)ioboard_do_read_level(&level);
IOBOARD_TEST_CHECK((uint8_t)(level & 0x0Fu) ==
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & 0x0Fu),
IOBOARD_TEST_CHECK((uint8_t)(level & IOBOARD_TEST_DO_MASK_ALL) ==
(uint8_t)(IOBOARD_DO_LEVEL_RELEASE & IOBOARD_TEST_DO_MASK_ALL),
"final: all 4 DO at RELEASE level after suite (level=0x%02X)",
(unsigned int)level);
IOBOARD_TEST_REPORT(IOBOARD_TEST_SUMMARY_NAME);
DBG_INFO("IOBOARD test suite done (relays released, thread exits)");
/* 必须显式自杀:FreeRTOS 的任务函数**返回**是未定义行为(Cortex-M4F 端口里
* LR 被初始化成 prvTaskExitError,返回即触发 configASSERT)。本仓 configASSERT
* 接到 vApplicationAssertFailed → 打 ASSERT FAILED 后 NVIC_SystemReset(),
* 台架表现为"套件跑完就静默复位"。原先此处只 return、注释却写"线程退出即删除
* 自己",是错的:任务不会因为函数返回而被删除。 */
vTaskDelete(NULL);
/* 防御:vTaskDelete(NULL) 之后不返回。若将来有人把它注释掉,这条守住语义。 */
for (;;) {
osDelay(1000);
}
}
/* ===================== 套件入口 ===================== */