Mid_temp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. ******************************************************************************
  3. * @file Mid_temp.c
  4. * @brief 温度数据中间层实现 (Mid)
  5. *
  6. * @note 由 CPLD 任务周期轮询 GET_ALL 后调用 Mid_Temp_UpdateGetData()
  7. * 解析 0x42 GET_DATA 帧 (6400 V2.0: 86B, 协议 v2.1 §30.1), 缓存写入本
  8. * 模块; 其他任务 (PID/预加热/上位机) 通过 getter 读取, 不直接
  9. * 接触串口。温度为单字节整数摄氏度, 0xFF=无效, 无任何换算。
  10. ******************************************************************************
  11. */
  12. #include <string.h>
  13. #include "Mid_temp.h"
  14. #define MID_TEMP_BMC_PER_CAVITY (MID_TEMP_BMC_CH / MID_TEMP_CAVITY_NUM)
  15. /* 腔体(风扇分区)温度源: 取本腔 6 路 BMC 里"最高的 2 路"求均温。
  16. * 注意!不能固定取中间 2 路 —— 好条与坏条混插时, 好条工作时自发热明显更热、
  17. * 坏条几乎不发热, 固定的中间路可能正好落在坏条上, 控温就会一路过冲。
  18. * 按最热的两条算, 才能保证"最热的条不超过目标"。
  19. * 每腔 6 路表见 s_cavity_bmc_all, 索引与 PTC 分组 6k..6k+5 同组。 */
  20. static const uint8_t s_cavity_bmc_all[MID_TEMP_CAVITY_NUM][MID_TEMP_BMC_PER_CAVITY] =
  21. {
  22. { 0, 1, 2, 3, 4, 5 },
  23. { 6, 7, 8, 9, 10, 11 },
  24. { 12, 13, 14, 15, 16, 17 },
  25. { 18, 19, 20, 21, 22, 23 },
  26. };
  27. /* 预加热用的 CT7428 组选择: 27 路按 9 槽一组 (v1.0: 3 芯片 x 9 寄存器; 组内
  28. * offset0 是芯片自身 Local, 不参与判定) ⇒ 每组只有 offset1~8 共 8 路 Remote。
  29. * 组内那 8 路用固定相对下标 1~8, 绝对下标 = BASE + 相对下标 ⇒ 切组只改 GROUP。
  30. */
  31. #define MID_TEMP_CT_GROUP 0 /* 0=ct[0..8] 1=ct[9..17] 2=ct[18..26] */
  32. #define MID_TEMP_CT_GROUP_STRIDE 9 /* 每组槽数 = 1 路 Local + 8 路 Remote */
  33. #define MID_TEMP_CT_GROUP_BASE (MID_TEMP_CT_GROUP * MID_TEMP_CT_GROUP_STRIDE)
  34. /* 预加热达标判定温度源: 本板自己的 CT7428, 与风扇分区故意不同源
  35. * (BMC 在预加热阶段查不到, 不适合判"腔体是否加热到位")。
  36. * 每腔体 2 路、腔体成对相邻: 组内 1/2=腔体1, 3/4=腔体2, 5/6=腔体3, 7/8=腔体4;
  37. * 同一对里前者进风(前)、后者出风(后)。出风侧风扇已断开, 正常只用前路,
  38. * 后路仅在前路失效时顶上。协议定长 27 路, 未接通道读到 0xFF。 */
  39. static const uint8_t s_preheat_ct_ch[MID_TEMP_PREHEAT_NUM][2] =
  40. {
  41. { MID_TEMP_CT_GROUP_BASE + 1, MID_TEMP_CT_GROUP_BASE + 2 },
  42. { MID_TEMP_CT_GROUP_BASE + 3, MID_TEMP_CT_GROUP_BASE + 4 },
  43. { MID_TEMP_CT_GROUP_BASE + 5, MID_TEMP_CT_GROUP_BASE + 6 },
  44. { MID_TEMP_CT_GROUP_BASE + 7, MID_TEMP_CT_GROUP_BASE + 8 },
  45. };
  46. static mid_temp_snapshot_t s_snapshot;
  47. static uint8_t s_has_data = 0;
  48. void Mid_Temp_Init(void)
  49. {
  50. memset(&s_snapshot, 0, sizeof(s_snapshot));
  51. s_has_data = 0;
  52. }
  53. uint8_t Mid_Temp_UpdateGetData(const uint8_t *p, uint16_t len)
  54. {
  55. if (p == NULL || len < MID_TEMP_PAYLOAD_GET_DATA)
  56. {
  57. return 0;
  58. }
  59. /* 当前生效输出回读 */
  60. memcpy(s_snapshot.ptc_applied, &p[MID_TEMP_OFF_PTC_APPLIED], 24);
  61. memcpy(s_snapshot.front_applied, &p[MID_TEMP_OFF_FRONT_APPLIED], 4);
  62. memcpy(s_snapshot.rear_applied, &p[MID_TEMP_OFF_REAR_APPLIED], 4);
  63. memcpy(s_snapshot.ssr_applied, &p[MID_TEMP_OFF_SSR], MID_TEMP_SSR_NUM);
  64. /* 温度直接按 1B 整数摄氏度缓存, 0xFF(无效)由 getter 解释 */
  65. memcpy(s_snapshot.bmc, &p[MID_TEMP_OFF_BMC], MID_TEMP_BMC_CH);
  66. memcpy(s_snapshot.ct, &p[MID_TEMP_OFF_CT], MID_TEMP_CT_CH);
  67. s_has_data = 1;
  68. return 1;
  69. }
  70. const mid_temp_snapshot_t *Mid_Temp_GetSnapshot(void)
  71. {
  72. return &s_snapshot;
  73. }
  74. uint8_t Mid_Temp_HasData(void)
  75. {
  76. return s_has_data;
  77. }
  78. uint8_t Mid_Temp_IsBmcReady(void)
  79. {
  80. uint8_t i;
  81. /* 未收到 GET_DATA 时快照是全 0x00 (Init 的 memset) 而非 0xFF:
  82. * 不先看 s_has_data, 上电瞬间会把这 24 个 0 全数成有效 */
  83. if (!s_has_data)
  84. {
  85. return 0;
  86. }
  87. for (i = 0; i < MID_TEMP_BMC_CH; i++)
  88. {
  89. if (s_snapshot.bmc[i] != 0xFF)
  90. {
  91. return 1; /* 至少一路有效即算就绪 */
  92. }
  93. }
  94. return 0; /* 24 路全无效: 服务器还没上报温度 */
  95. }
  96. int16_t Mid_Temp_GetBmcTemp(uint8_t ch)
  97. {
  98. if (!s_has_data || ch >= MID_TEMP_BMC_CH ||
  99. s_snapshot.bmc[ch] == 0xFF)
  100. {
  101. return MID_TEMP_INVALID;
  102. }
  103. return (int16_t)s_snapshot.bmc[ch];
  104. }
  105. int16_t Mid_Temp_GetCtTemp(uint8_t ch)
  106. {
  107. if (!s_has_data || ch >= MID_TEMP_CT_CH ||
  108. s_snapshot.ct[ch] == 0xFF)
  109. {
  110. return MID_TEMP_INVALID;
  111. }
  112. return (int16_t)s_snapshot.ct[ch];
  113. }
  114. /**
  115. * @brief 剔除未接探头的 CT 读数
  116. * @note CPLD 对未接探头返回 0℃ 而不是 0xFF, 只判 0xFF 挡不住:
  117. * 23 与 0 会均成 12, 该腔永远判不出达标
  118. * @param v : CT 原始读数
  119. * @return 有效值原样返回; 开路返回 MID_TEMP_INVALID
  120. */
  121. static int16_t ct_drop_open(int16_t v)
  122. {
  123. if ((v >= 0) && (v < MID_TEMP_CT_OPEN_MIN))
  124. {
  125. return MID_TEMP_INVALID;
  126. }
  127. return v;
  128. }
  129. int16_t Mid_Temp_GetPreheat(uint8_t idx)
  130. {
  131. int16_t front;
  132. int16_t rear;
  133. if (idx >= MID_TEMP_PREHEAT_NUM)
  134. {
  135. return MID_TEMP_INVALID;
  136. }
  137. front = ct_drop_open(Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][0]));
  138. rear = ct_drop_open(Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][1]));
  139. /* 前(进风)传感器优先, 而且是唯一判据: 出风侧风扇已断开, 那里读数
  140. * 不反映腔体, 不参与达标/超温, 只在前路失效时顶上。
  141. * 两路都失效就返回 INVALID —— 该腔判不出达标, 由预热超时兜底 */
  142. if (front >= 0)
  143. {
  144. return front;
  145. }
  146. return rear;
  147. }
  148. int16_t Mid_Temp_GetPreheatMax(uint8_t idx)
  149. {
  150. int16_t front;
  151. int16_t rear;
  152. if (idx >= MID_TEMP_PREHEAT_NUM)
  153. {
  154. return MID_TEMP_INVALID;
  155. }
  156. front = ct_drop_open(Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][0]));
  157. rear = ct_drop_open(Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx][1]));
  158. /* 取两路中较高的那一路
  159. * 两路都失效返回 INVALID, 由调用方按"无温度源"处理 */
  160. if (front < 0)
  161. {
  162. return rear;
  163. }
  164. if (rear < 0)
  165. {
  166. return front;
  167. }
  168. return (front > rear) ? front : rear;
  169. }
  170. int16_t Mid_Temp_GetPreheatRaw(uint8_t idx)
  171. {
  172. if (idx >= MID_TEMP_PREHEAT_RAW_NUM)
  173. {
  174. return MID_TEMP_INVALID;
  175. }
  176. /* 展开预加热通道表: idx/2 = 腔体号, idx%2 = 进风/出风; 组号由 MID_TEMP_CT_GROUP 选。
  177. * 返回单路原始读数, 不做任何合成 —— 供上位机 0x0D 查看本组 8 路 CT 各自的值 */
  178. return Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx / 2][idx % 2]);
  179. }
  180. /**
  181. * @brief 取某腔的腔体温度: 本腔 6 路 BMC 里最热的 2 路取均温
  182. * @note 0 与 0xFF 都当"这条没有": 未插的槽上报 0, 若把 0 当成 0℃ 有效温度,
  183. * 该腔会被算成 0℃, 控温会一直满功率烧(实测把腔体烤到 109℃)
  184. * @param idx : 腔体序号 (0~MID_TEMP_CAVITY_NUM-1)
  185. * @return 温度 (℃); 本腔一条有效都没有返回 MID_TEMP_INVALID
  186. */
  187. int16_t Mid_Temp_GetCavityTemp(uint8_t idx)
  188. {
  189. uint8_t k;
  190. int16_t top1 = MID_TEMP_INVALID;
  191. int16_t top2 = MID_TEMP_INVALID;
  192. if (idx >= MID_TEMP_CAVITY_NUM)
  193. {
  194. return MID_TEMP_INVALID;
  195. }
  196. for (k = 0; k < MID_TEMP_BMC_PER_CAVITY; k++)
  197. {
  198. int16_t v = Mid_Temp_GetBmcTemp(s_cavity_bmc_all[idx][k]);
  199. if (v <= 0)
  200. {
  201. continue; /* 未插 / 无效, 不参与 */
  202. }
  203. if (v > top1)
  204. {
  205. top2 = top1;
  206. top1 = v;
  207. }
  208. else if (v > top2)
  209. {
  210. top2 = v;
  211. }
  212. }
  213. if (top1 < 0)
  214. {
  215. return MID_TEMP_INVALID; /* 本腔一条有效都没有 */
  216. }
  217. if (top2 < 0)
  218. {
  219. return top1; /* 只有一条, 就用它 */
  220. }
  221. /* 四舍五入均温: 取整偏上, 风扇分档偏散热一侧 */
  222. return (int16_t)((top1 + top2 + 1) / 2);
  223. }
  224. /**
  225. * @brief 取某腔的低温侧腔温: 本腔有效路里最低的 2 路取均温
  226. * @note 与 Mid_Temp_GetCavityTemp() 对称相反: 那个取最高 2 路给风扇
  227. * (必须看到最热的条才吹得动), 这个取最低 2 路给主块 PID
  228. * (按最凉的几条控, 保证它们不欠温)。
  229. * 取 2 路而不是只取 1 路: 单条读数偏低 (探头漂、风道死角) 会独自把
  230. * 整腔拖在满功率, 与次低一起平均能削掉这种单点异常。
  231. * 过温侧不在这条路径上管, 交给风扇与 CT 上限。
  232. * @param idx : 腔体序号 (0~MID_TEMP_CAVITY_NUM-1)
  233. * @return 温度 (℃); 本腔一条有效都没有返回 MID_TEMP_INVALID
  234. */
  235. int16_t Mid_Temp_GetCavityTempLo(uint8_t idx)
  236. {
  237. uint8_t k;
  238. int16_t low1 = MID_TEMP_INVALID;
  239. int16_t low2 = MID_TEMP_INVALID;
  240. if (idx >= MID_TEMP_CAVITY_NUM)
  241. {
  242. return MID_TEMP_INVALID;
  243. }
  244. for (k = 0; k < MID_TEMP_BMC_PER_CAVITY; k++)
  245. {
  246. int16_t v = Mid_Temp_GetBmcTemp(s_cavity_bmc_all[idx][k]);
  247. if (v <= 0)
  248. {
  249. continue; /* 未插 / 无效, 不参与 */
  250. }
  251. if ((low1 < 0) || (v < low1))
  252. {
  253. low2 = low1;
  254. low1 = v;
  255. }
  256. else if ((low2 < 0) || (v < low2))
  257. {
  258. low2 = v;
  259. }
  260. }
  261. if (low1 < 0)
  262. {
  263. return MID_TEMP_INVALID; /* 本腔一条有效都没有 */
  264. }
  265. if (low2 < 0)
  266. {
  267. return low1; /* 只有一条, 就用它 */
  268. }
  269. /* 与高温侧同一条规矩: 四舍五入偏上, 少给一点功率 */
  270. return (int16_t)((low1 + low2 + 1) / 2);
  271. }