Com_cpld_protocol.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * CPLD (USART3) 通信模块——与下位机 CPLD 进行串口通信的协议封装
  3. * 协议版本: v2.0
  4. */
  5. #include "Com_cpld_protocol.h"
  6. #include "Com_log.h" /* SELFTEST_PRINTF: 自检打印, 不受日志宏约束 */
  7. #include <stdio.h>
  8. #include <string.h>
  9. /**
  10. * @brief CRC-16/CCITT-FALSE 校验计算
  11. * @note 初值 0xFFFF, 多项式 0x1021, 输入/输出不反转
  12. * @param data : 参与 CRC 计算的数据指针
  13. * @param len : 数据长度 (字节数)
  14. * @return 16 位 CRC 校验值
  15. */
  16. uint16_t Com_cpld_crc16(const uint8_t *data, uint16_t len)
  17. {
  18. uint16_t crc = 0xFFFF;
  19. uint16_t i;
  20. if (data == NULL)
  21. {
  22. return crc;
  23. }
  24. for (i = 0; i < len; i++)
  25. {
  26. uint8_t bit;
  27. crc ^= (uint16_t)data[i] << 8;
  28. for (bit = 0; bit < 8; bit++)
  29. {
  30. if (crc & 0x8000)
  31. {
  32. crc = (uint16_t)((crc << 1) ^ 0x1021);
  33. }
  34. else
  35. {
  36. crc = (uint16_t)(crc << 1);
  37. }
  38. }
  39. }
  40. return crc;
  41. }
  42. /**
  43. * @brief 通用帧封装: |A5 5A|Type|Length|Payload|CRC_L|CRC_H
  44. * @note CRC 计算范围 = Type + Length + Payload
  45. * @param type : 命令类型 (如 0x01 SET_ALL)
  46. * @param payload : 载荷数据指针
  47. * @param len : 载荷长度 (0..255)
  48. * @param frame : 输出帧缓冲区指针 (需 >= COM_CPLD_FRAME_MAX_LEN)
  49. * @return 帧总长度; 输入非法返回 0
  50. */
  51. uint16_t Com_cpld_encode(uint8_t type, const uint8_t *payload, uint8_t len, uint8_t *frame)
  52. {
  53. uint16_t crc;
  54. uint16_t idx;
  55. uint8_t i;
  56. if (frame == NULL || (len > 0 && payload == NULL))
  57. {
  58. return 0;
  59. }
  60. frame[0] = COM_CPLD_HEADER_H;
  61. frame[1] = COM_CPLD_HEADER_L;
  62. frame[2] = type;
  63. frame[3] = len;
  64. for (i = 0; i < len; i++)
  65. {
  66. frame[4 + i] = payload[i];
  67. }
  68. /* CRC 覆盖 Type+Length+Payload */
  69. crc = Com_cpld_crc16(&frame[2], (uint16_t)(2 + len));
  70. idx = (uint16_t)(4 + len);
  71. frame[idx] = (uint8_t)(crc & 0xFF); /* CRC 低字节 */
  72. frame[idx + 1] = (uint8_t)(crc >> 8); /* CRC 高字节 */
  73. return idx + 2;
  74. }
  75. /**
  76. * @brief 封装 SET_ALL (0x01) 帧: 32 字节载荷
  77. * @note duty[0..23]=PTC, duty[24..27]=进风, duty[28..31]=出风
  78. * @param duty : 32 字节输出占空比数组指针 (每路 0..100)
  79. * @param frame : 输出帧缓冲区指针
  80. * @return 帧总长度; 输入非法返回 0
  81. */
  82. uint16_t Com_cpld_set_all(const uint8_t *duty, uint8_t *frame)
  83. {
  84. if (duty == NULL || frame == NULL)
  85. {
  86. return 0;
  87. }
  88. return Com_cpld_encode(COM_CPLD_TYPE_SET_ALL, duty, COM_CPLD_PAYLOAD_SET_ALL, frame);
  89. }
  90. /**
  91. * @brief 封装 GET_ALL (0x02) 查询帧: 0 字节载荷
  92. * @param frame : 输出帧缓冲区指针
  93. * @return 帧总长度; 输入非法返回 0
  94. */
  95. uint16_t Com_cpld_get_all(uint8_t *frame)
  96. {
  97. if (frame == NULL)
  98. {
  99. return 0;
  100. }
  101. return Com_cpld_encode(COM_CPLD_TYPE_GET_ALL, NULL, 0, frame);
  102. }
  103. /**
  104. * @brief 解析 NACK (0xC0) 载荷: [被拒TYPE, reason]
  105. * @param payload : 载荷数据指针
  106. * @param len : 载荷长度 (须为 2)
  107. * @param nack : [out] 解析结果
  108. * @return 1=解析成功, 0=参数非法
  109. */
  110. uint8_t Com_cpld_nack_parse(const uint8_t *payload, uint8_t len, com_cpld_nack_t *nack)
  111. {
  112. if (payload == NULL || nack == NULL || len != COM_CPLD_PAYLOAD_NACK)
  113. {
  114. return 0;
  115. }
  116. nack->request_type = payload[0];
  117. nack->reason = payload[1];
  118. return 1;
  119. }
  120. /**
  121. * @brief 帧校验: 检查帧头与 CRC
  122. * @note 通过后回传 type/payload/payload_len
  123. * @param frame : 待校验的帧数据指针
  124. * @param len : 帧数据长度 (字节数)
  125. * @param type : [out] 校验通过时的命令类型
  126. * @param payload : [out] 校验通过时的载荷数据指针
  127. * @param payload_len : [out] 校验通过时的载荷长度
  128. * @return 1=校验通过, 0=校验失败或参数非法
  129. */
  130. uint8_t Com_cpld_check_frame(const uint8_t *frame, uint16_t len, uint8_t *type, const uint8_t **payload, uint8_t *payload_len)
  131. {
  132. uint16_t expect_len;
  133. uint16_t exp_crc, got_crc;
  134. if (frame == NULL || len < 6)
  135. {
  136. return 0;
  137. }
  138. /* 帧头 */
  139. if (frame[0] != COM_CPLD_HEADER_H || frame[1] != COM_CPLD_HEADER_L)
  140. {
  141. return 0;
  142. }
  143. expect_len = (uint16_t)(2 + 1 + 1 + frame[3] + 2);
  144. if (len != expect_len)
  145. {
  146. return 0;
  147. }
  148. /* CRC */
  149. exp_crc = (uint16_t)(frame[len - 2] | ((uint16_t)frame[len - 1] << 8));
  150. got_crc = Com_cpld_crc16(&frame[2], (uint16_t)(frame[3] + 2));
  151. if (exp_crc != got_crc)
  152. {
  153. return 0;
  154. }
  155. if (type != NULL)
  156. {
  157. *type = frame[2];
  158. }
  159. if (payload != NULL)
  160. {
  161. *payload = (const uint8_t *)&frame[4];
  162. }
  163. if (payload_len != NULL)
  164. {
  165. *payload_len = frame[3];
  166. }
  167. return 1;
  168. }
  169. /* ------------------------- 开机自检 ------------------------- */
  170. /* 注意!自检结果必须始终可见 ⇒ 走 SELFTEST_PRINTF(不受任何日志宏约束)。
  171. * 别用 LOG_PRINTF(关日志就没了), 也别用 printf(绕开总开关的临时后门)。 */
  172. #define SELF_TEST_FAIL(name) \
  173. do { SELFTEST_PRINTF("[SELFTEST] FAIL: %s\r\n", (name)); ret = 1; } while (0)
  174. /**
  175. * @brief 协议层开机自检: CRC golden 向量 + 帧封装/解析向量
  176. * @note 向量来源: v2.0 §5 ("123456789" -> 0x29B1) 与 §24 完整 HEX 示例。
  177. * 全部通过返回 0; 任一失败返回 1。
  178. */
  179. uint8_t Com_cpld_self_test(void)
  180. {
  181. uint8_t ret = 0;
  182. uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
  183. uint16_t flen;
  184. uint8_t type;
  185. const uint8_t *payload;
  186. uint8_t plen;
  187. /* 1. CRC 标准自测字符串 "123456789" -> 0x29B1 (v2.0 §5) */
  188. {
  189. static const uint8_t vec[] = { '1','2','3','4','5','6','7','8','9' };
  190. if (Com_cpld_crc16(vec, sizeof(vec)) != 0x29B1u)
  191. {
  192. SELF_TEST_FAIL("crc 123456789");
  193. }
  194. }
  195. /* 2. GET_ALL golden: A5 5A 02 00 6D 7B (v2.0 §24.1) */
  196. flen = Com_cpld_get_all(frame);
  197. {
  198. static const uint8_t vec[] = { 0xA5,0x5A,0x02,0x00,0x6D,0x7B };
  199. if (flen != sizeof(vec) ||
  200. memcmp(frame, vec, sizeof(vec)) != 0)
  201. {
  202. SELF_TEST_FAIL("GET_ALL golden");
  203. }
  204. }
  205. /* 3. SET_ACK / GET_ACK golden (v2.0 §24.1/§24.2) */
  206. flen = Com_cpld_encode(COM_CPLD_TYPE_SET_ACK, NULL, 0, frame);
  207. {
  208. static const uint8_t vec[] = { 0xA5,0x5A,0x81,0x00,0xA6,0x35 };
  209. if (flen != sizeof(vec) ||
  210. memcmp(frame, vec, sizeof(vec)) != 0)
  211. {
  212. SELF_TEST_FAIL("SET_ACK golden");
  213. }
  214. }
  215. flen = Com_cpld_encode(COM_CPLD_TYPE_GET_ACK, NULL, 0, frame);
  216. {
  217. static const uint8_t vec[] = { 0xA5,0x5A,0x82,0x00,0xF5,0x60 };
  218. if (flen != sizeof(vec) ||
  219. memcmp(frame, vec, sizeof(vec)) != 0)
  220. {
  221. SELF_TEST_FAIL("GET_ACK golden");
  222. }
  223. }
  224. /* 4. NACK BAD_VALUE golden: A5 5A C0 02 01 03 56 5A (v2.0 §24.3) */
  225. {
  226. static const uint8_t nack_pl[2] = { 0x01, 0x03 };
  227. static const uint8_t vec[] = { 0xA5,0x5A,0xC0,0x02,0x01,0x03,0x56,0x5A };
  228. flen = Com_cpld_encode(COM_CPLD_TYPE_NACK, nack_pl, sizeof(nack_pl), frame);
  229. if (flen != sizeof(vec) ||
  230. memcmp(frame, vec, sizeof(vec)) != 0)
  231. {
  232. SELF_TEST_FAIL("NACK golden");
  233. }
  234. }
  235. /* 5. 6400 SET_ALL golden: PTC 全 0 + 8 路风扇 20% (v2.0 §24.5, 38B) */
  236. {
  237. uint8_t duty[COM_CPLD_PAYLOAD_SET_ALL];
  238. uint8_t i;
  239. static const uint8_t vec[] = {
  240. 0xA5,0x5A,0x01,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  241. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  242. 0x00,0x00,0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,
  243. 0xE7,0x0A };
  244. for (i = 0; i < sizeof(duty); i++)
  245. {
  246. duty[i] = 0;
  247. }
  248. for (i = COM_CPLD_SET_ALL_FRONT_OFF; i < COM_CPLD_PAYLOAD_SET_ALL; i++)
  249. {
  250. duty[i] = 20;
  251. }
  252. flen = Com_cpld_set_all(duty, frame);
  253. if (flen != sizeof(vec) ||
  254. memcmp(frame, vec, sizeof(vec)) != 0)
  255. {
  256. SELF_TEST_FAIL("6400 SET_ALL golden");
  257. }
  258. }
  259. /* 6. check_frame 回读: 用 GET_ACK 帧验证 type/payload/len 输出 */
  260. flen = Com_cpld_encode(COM_CPLD_TYPE_GET_ACK, NULL, 0, frame);
  261. if (!Com_cpld_check_frame(frame, flen, &type, &payload, &plen) ||
  262. type != COM_CPLD_TYPE_GET_ACK || plen != 0)
  263. {
  264. SELF_TEST_FAIL("check_frame GET_ACK");
  265. }
  266. /* 7. check_frame 拒绝坏 CRC */
  267. flen = Com_cpld_encode(COM_CPLD_TYPE_GET_ACK, NULL, 0, frame);
  268. frame[flen - 1] ^= 0xFF;
  269. if (Com_cpld_check_frame(frame, flen, &type, &payload, &plen))
  270. {
  271. SELF_TEST_FAIL("check_frame bad CRC");
  272. }
  273. if (ret == 0)
  274. {
  275. SELFTEST_PRINTF("[SELFTEST] CPLD protocol: all pass\r\n");
  276. }
  277. return ret;
  278. }