Mid_temp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. ******************************************************************************
  3. * @file Mid_temp.c
  4. * @brief 温度数据中间层实现 (Mid)
  5. *
  6. * @note 由 CPLD 任务周期轮询 GET_ALL 后调用 Mid_Temp_UpdateGetData()
  7. * 解析 0x42 GET_DATA 帧 (6400: 83B, 协议 v2.0 §18), 缓存写入本
  8. * 模块; 其他任务 (PID/预加热/上位机) 通过 getter 读取, 不直接
  9. * 接触串口。温度为单字节整数摄氏度, 0xFF=无效, 无任何换算。
  10. ******************************************************************************
  11. */
  12. #include <string.h>
  13. #include "Mid_temp.h"
  14. /* 腔体(风扇分区)温度源: 每腔体取 BMC 最中间 2 路求均温 —— 那两路位于腔体中心,
  15. * 温度最高, 比 CT7428 (不在腔体中心) 更能代表腔体实际温度。
  16. * 腔体 k(0~3) = BMC[6k+2], BMC[6k+3], 与 PID 分区 k = PTC 6k..6k+5 同组
  17. * (分区定义见 Mid_pid.c 的 g_fan_zones)。仅风扇分档使用。 */
  18. static const uint8_t s_cavity_bmc_ch[MID_TEMP_CAVITY_NUM][2] =
  19. {
  20. { 2, 3 },
  21. { 8, 9 },
  22. { 14, 15 },
  23. { 20, 21 },
  24. };
  25. /* 预加热用的 CT7428 组选择: 27 路按 9 槽一组 (v1.0: 3 芯片 x 9 寄存器; 组内
  26. * offset0 是芯片自身 Local, 不参与判定) ⇒ 每组只有 offset1~8 共 8 路 Remote。
  27. * 组内那 8 路用固定相对下标 1~8, 绝对下标 = BASE + 相对下标 ⇒ 切组只改 GROUP。
  28. */
  29. #define MID_TEMP_CT_GROUP 0 /* 0=ct[0..8] 1=ct[9..17] 2=ct[18..26] */
  30. #define MID_TEMP_CT_GROUP_STRIDE 9 /* 每组槽数 = 1 路 Local + 8 路 Remote */
  31. #define MID_TEMP_CT_GROUP_BASE (MID_TEMP_CT_GROUP * MID_TEMP_CT_GROUP_STRIDE)
  32. /* 预加热达标判定温度源: 本板自己的 CT7428, 与风扇分区故意不同源
  33. * (BMC 在预加热阶段查不到, 不适合判"腔体是否加热到位")。
  34. * 每腔体 2 路、腔体成对相邻: 组内 1/2=腔体1, 3/4=腔体2, 5/6=腔体3, 7/8=腔体4;
  35. * 同一对里前者进风、后者出风, 均温只见腔体、不区分进/出风。
  36. * 协议定长 27 路, 未接通道读到 0xFF。 */
  37. static const uint8_t s_preheat_ct_ch[MID_TEMP_PREHEAT_NUM][2] =
  38. {
  39. { MID_TEMP_CT_GROUP_BASE + 1, MID_TEMP_CT_GROUP_BASE + 2 },
  40. { MID_TEMP_CT_GROUP_BASE + 3, MID_TEMP_CT_GROUP_BASE + 4 },
  41. { MID_TEMP_CT_GROUP_BASE + 5, MID_TEMP_CT_GROUP_BASE + 6 },
  42. { MID_TEMP_CT_GROUP_BASE + 7, MID_TEMP_CT_GROUP_BASE + 8 },
  43. };
  44. static mid_temp_snapshot_t s_snapshot;
  45. static uint8_t s_has_data = 0;
  46. void Mid_Temp_Init(void)
  47. {
  48. memset(&s_snapshot, 0, sizeof(s_snapshot));
  49. s_has_data = 0;
  50. }
  51. uint8_t Mid_Temp_UpdateGetData(const uint8_t *p, uint16_t len)
  52. {
  53. if (p == NULL || len != MID_TEMP_PAYLOAD_GET_DATA)
  54. {
  55. return 0;
  56. }
  57. /* 当前生效输出回读 */
  58. memcpy(s_snapshot.ptc_applied, &p[MID_TEMP_OFF_PTC_APPLIED], 24);
  59. memcpy(s_snapshot.front_applied, &p[MID_TEMP_OFF_FRONT_APPLIED], 4);
  60. memcpy(s_snapshot.rear_applied, &p[MID_TEMP_OFF_REAR_APPLIED], 4);
  61. /* 温度直接按 1B 整数摄氏度缓存, 0xFF(无效)由 getter 解释 */
  62. memcpy(s_snapshot.bmc, &p[MID_TEMP_OFF_BMC], MID_TEMP_BMC_CH);
  63. memcpy(s_snapshot.ct, &p[MID_TEMP_OFF_CT], MID_TEMP_CT_CH);
  64. s_has_data = 1;
  65. return 1;
  66. }
  67. const mid_temp_snapshot_t *Mid_Temp_GetSnapshot(void)
  68. {
  69. return &s_snapshot;
  70. }
  71. uint8_t Mid_Temp_HasData(void)
  72. {
  73. return s_has_data;
  74. }
  75. uint8_t Mid_Temp_IsBmcReady(void)
  76. {
  77. uint8_t i;
  78. /* 未收到 GET_DATA 时快照是全 0x00 (Init 的 memset) 而非 0xFF:
  79. * 不先看 s_has_data, 上电瞬间会把这 24 个 0 全数成有效 */
  80. if (!s_has_data)
  81. {
  82. return 0;
  83. }
  84. for (i = 0; i < MID_TEMP_BMC_CH; i++)
  85. {
  86. if (s_snapshot.bmc[i] != 0xFF)
  87. {
  88. return 1; /* 至少一路有效即算就绪 */
  89. }
  90. }
  91. return 0; /* 24 路全无效: 服务器还没上报温度 */
  92. }
  93. int16_t Mid_Temp_GetBmcTemp(uint8_t ch)
  94. {
  95. if (!s_has_data || ch >= MID_TEMP_BMC_CH ||
  96. s_snapshot.bmc[ch] == 0xFF)
  97. {
  98. return MID_TEMP_INVALID;
  99. }
  100. return (int16_t)s_snapshot.bmc[ch];
  101. }
  102. int16_t Mid_Temp_GetCtTemp(uint8_t ch)
  103. {
  104. if (!s_has_data || ch >= MID_TEMP_CT_CH ||
  105. s_snapshot.ct[ch] == 0xFF)
  106. {
  107. return MID_TEMP_INVALID;
  108. }
  109. return (int16_t)s_snapshot.ct[ch];
  110. }
  111. int16_t Mid_Temp_GetPreheat(uint8_t idx)
  112. {
  113. int16_t a;
  114. int16_t b;
  115. if (idx >= MID_TEMP_PREHEAT_NUM)
  116. {
  117. return MID_TEMP_INVALID;
  118. }
  119. a = Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][0]);
  120. b = Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][1]);
  121. /* 一路无效用另一路兜底, 两路都无效才判无效 (不因单路故障卡住预热) */
  122. if (a < 0)
  123. {
  124. return (b < 0) ? MID_TEMP_INVALID : b;
  125. }
  126. if (b < 0)
  127. {
  128. return a;
  129. }
  130. /* 四舍五入均温 (与风扇腔体均温同算法) */
  131. return (int16_t)((a + b + 1) / 2);
  132. }
  133. int16_t Mid_Temp_GetPreheatRaw(uint8_t idx)
  134. {
  135. if (idx >= MID_TEMP_PREHEAT_RAW_NUM)
  136. {
  137. return MID_TEMP_INVALID;
  138. }
  139. /* 展开预加热通道表: idx/2 = 腔体号, idx%2 = 进风/出风; 组号由 MID_TEMP_CT_GROUP 选。
  140. * 返回单路原始读数, 不取均温 —— 供上位机 0x0D 查看本组 8 路 CT 各自的值 */
  141. return Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx / 2][idx % 2]);
  142. }
  143. int16_t Mid_Temp_GetCavityTemp(uint8_t idx)
  144. {
  145. int16_t a;
  146. int16_t b;
  147. if (idx >= MID_TEMP_CAVITY_NUM)
  148. {
  149. return MID_TEMP_INVALID;
  150. }
  151. a = Mid_Temp_GetBmcTemp(s_cavity_bmc_ch[idx][0]);
  152. b = Mid_Temp_GetBmcTemp(s_cavity_bmc_ch[idx][1]);
  153. /* 一路无效用另一路兜底, 两路都无效才判无效 (不因单路故障丢掉整个腔体) */
  154. if (a < 0)
  155. {
  156. return (b < 0) ? MID_TEMP_INVALID : b;
  157. }
  158. if (b < 0)
  159. {
  160. return a;
  161. }
  162. /* 四舍五入均温: 取整偏上, 风扇分档偏散热一侧 */
  163. return (int16_t)((a + b + 1) / 2);
  164. }