/** ****************************************************************************** * @file Mid_temp.c * @brief 温度数据中间层实现 (Mid) * * @note 由 CPLD 任务周期轮询 GET_ALL 后调用 Mid_Temp_UpdateGetData() * 解析 0x42 GET_DATA 帧 (6400: 83B, 协议 v2.0 §18), 缓存写入本 * 模块; 其他任务 (PID/预加热/上位机) 通过 getter 读取, 不直接 * 接触串口。温度为单字节整数摄氏度, 0xFF=无效, 无任何换算。 ****************************************************************************** */ #include #include "Mid_temp.h" /* 腔体(风扇分区)温度源: 每腔体取 BMC 最中间 2 路求均温 —— 那两路位于腔体中心, * 温度最高, 比 CT7428 (不在腔体中心) 更能代表腔体实际温度。 * 腔体 k(0~3) = BMC[6k+2], BMC[6k+3], 与 PID 分区 k = PTC 6k..6k+5 同组 * (分区定义见 Mid_pid.c 的 g_fan_zones)。仅风扇分档使用。 */ static const uint8_t s_cavity_bmc_ch[MID_TEMP_CAVITY_NUM][2] = { { 2, 3 }, { 8, 9 }, { 14, 15 }, { 20, 21 }, }; /* 预加热用的 CT7428 组选择: 27 路按 9 槽一组 (v1.0: 3 芯片 x 9 寄存器; 组内 * offset0 是芯片自身 Local, 不参与判定) ⇒ 每组只有 offset1~8 共 8 路 Remote。 * 组内那 8 路用固定相对下标 1~8, 绝对下标 = BASE + 相对下标 ⇒ 切组只改 GROUP。 */ #define MID_TEMP_CT_GROUP 0 /* 0=ct[0..8] 1=ct[9..17] 2=ct[18..26] */ #define MID_TEMP_CT_GROUP_STRIDE 9 /* 每组槽数 = 1 路 Local + 8 路 Remote */ #define MID_TEMP_CT_GROUP_BASE (MID_TEMP_CT_GROUP * MID_TEMP_CT_GROUP_STRIDE) /* 预加热达标判定温度源: 本板自己的 CT7428, 与风扇分区故意不同源 * (BMC 在预加热阶段查不到, 不适合判"腔体是否加热到位")。 * 每腔体 2 路、腔体成对相邻: 组内 1/2=腔体1, 3/4=腔体2, 5/6=腔体3, 7/8=腔体4; * 同一对里前者进风、后者出风, 均温只见腔体、不区分进/出风。 * 协议定长 27 路, 未接通道读到 0xFF。 */ static const uint8_t s_preheat_ct_ch[MID_TEMP_PREHEAT_NUM][2] = { { MID_TEMP_CT_GROUP_BASE + 1, MID_TEMP_CT_GROUP_BASE + 2 }, { MID_TEMP_CT_GROUP_BASE + 3, MID_TEMP_CT_GROUP_BASE + 4 }, { MID_TEMP_CT_GROUP_BASE + 5, MID_TEMP_CT_GROUP_BASE + 6 }, { MID_TEMP_CT_GROUP_BASE + 7, MID_TEMP_CT_GROUP_BASE + 8 }, }; static mid_temp_snapshot_t s_snapshot; static uint8_t s_has_data = 0; void Mid_Temp_Init(void) { memset(&s_snapshot, 0, sizeof(s_snapshot)); s_has_data = 0; } uint8_t Mid_Temp_UpdateGetData(const uint8_t *p, uint16_t len) { if (p == NULL || len != MID_TEMP_PAYLOAD_GET_DATA) { return 0; } /* 当前生效输出回读 */ memcpy(s_snapshot.ptc_applied, &p[MID_TEMP_OFF_PTC_APPLIED], 24); memcpy(s_snapshot.front_applied, &p[MID_TEMP_OFF_FRONT_APPLIED], 4); memcpy(s_snapshot.rear_applied, &p[MID_TEMP_OFF_REAR_APPLIED], 4); /* 温度直接按 1B 整数摄氏度缓存, 0xFF(无效)由 getter 解释 */ memcpy(s_snapshot.bmc, &p[MID_TEMP_OFF_BMC], MID_TEMP_BMC_CH); memcpy(s_snapshot.ct, &p[MID_TEMP_OFF_CT], MID_TEMP_CT_CH); s_has_data = 1; return 1; } const mid_temp_snapshot_t *Mid_Temp_GetSnapshot(void) { return &s_snapshot; } uint8_t Mid_Temp_HasData(void) { return s_has_data; } uint8_t Mid_Temp_IsBmcReady(void) { uint8_t i; /* 未收到 GET_DATA 时快照是全 0x00 (Init 的 memset) 而非 0xFF: * 不先看 s_has_data, 上电瞬间会把这 24 个 0 全数成有效 */ if (!s_has_data) { return 0; } for (i = 0; i < MID_TEMP_BMC_CH; i++) { if (s_snapshot.bmc[i] != 0xFF) { return 1; /* 至少一路有效即算就绪 */ } } return 0; /* 24 路全无效: 服务器还没上报温度 */ } int16_t Mid_Temp_GetBmcTemp(uint8_t ch) { if (!s_has_data || ch >= MID_TEMP_BMC_CH || s_snapshot.bmc[ch] == 0xFF) { return MID_TEMP_INVALID; } return (int16_t)s_snapshot.bmc[ch]; } int16_t Mid_Temp_GetCtTemp(uint8_t ch) { if (!s_has_data || ch >= MID_TEMP_CT_CH || s_snapshot.ct[ch] == 0xFF) { return MID_TEMP_INVALID; } return (int16_t)s_snapshot.ct[ch]; } int16_t Mid_Temp_GetPreheat(uint8_t idx) { int16_t a; int16_t b; if (idx >= MID_TEMP_PREHEAT_NUM) { return MID_TEMP_INVALID; } a = Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][0]); b = Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][1]); /* 一路无效用另一路兜底, 两路都无效才判无效 (不因单路故障卡住预热) */ if (a < 0) { return (b < 0) ? MID_TEMP_INVALID : b; } if (b < 0) { return a; } /* 四舍五入均温 (与风扇腔体均温同算法) */ return (int16_t)((a + b + 1) / 2); } int16_t Mid_Temp_GetPreheatRaw(uint8_t idx) { if (idx >= MID_TEMP_PREHEAT_RAW_NUM) { return MID_TEMP_INVALID; } /* 展开预加热通道表: idx/2 = 腔体号, idx%2 = 进风/出风; 组号由 MID_TEMP_CT_GROUP 选。 * 返回单路原始读数, 不取均温 —— 供上位机 0x0D 查看本组 8 路 CT 各自的值 */ return Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx / 2][idx % 2]); } int16_t Mid_Temp_GetCavityTemp(uint8_t idx) { int16_t a; int16_t b; if (idx >= MID_TEMP_CAVITY_NUM) { return MID_TEMP_INVALID; } a = Mid_Temp_GetBmcTemp(s_cavity_bmc_ch[idx][0]); b = Mid_Temp_GetBmcTemp(s_cavity_bmc_ch[idx][1]); /* 一路无效用另一路兜底, 两路都无效才判无效 (不因单路故障丢掉整个腔体) */ if (a < 0) { return (b < 0) ? MID_TEMP_INVALID : b; } if (b < 0) { return a; } /* 四舍五入均温: 取整偏上, 风扇分档偏散热一侧 */ return (int16_t)((a + b + 1) / 2); }