Com_cpld_protocol.c 8.5 KB

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