um482.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "um482.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define CRC32_POLYNOMIAL 0xEDB88320
  5. /* 消息类型 */
  6. enum
  7. {
  8. UM482_MSG_TYPE_BINARY = 0,
  9. UM482_MSG_TYPE_ASCII = 1,
  10. UM482_MSG_TYPE_ASCII_SIMPLE = 2
  11. };
  12. /* 消息解析阶段 */
  13. enum
  14. {
  15. UM482_PARSE_STATE_UNINIT = 0,
  16. UM482_PARSE_STATE_IDLE = 1,
  17. UM482_PARSE_STATE_GOT_HEADER = 2,
  18. UM482_PARSE_STATE_GOT_DATA = 3,
  19. UM482_PARSE_STATE_GOT_CRC = 4
  20. };
  21. /* msg id 枚举定义 */
  22. enum
  23. {
  24. UM482_MSG_BESTPOS_ID = 42,
  25. UM482_MSG_BESTVEL_ID = 99,
  26. UM482_MSG_HEADING_ID = 971,
  27. };
  28. unsigned long _CRC32Value(int i)
  29. {
  30. int j;
  31. unsigned long ulCRC;
  32. ulCRC = i;
  33. for (j = 8; j > 0; j--)
  34. {
  35. if (ulCRC & 1)
  36. ulCRC = (ulCRC >> 1) ^ CRC32_POLYNOMIAL;
  37. else
  38. ulCRC >>= 1;
  39. }
  40. return ulCRC;
  41. }
  42. unsigned long _CalculateBlockCRC32(unsigned char *ucBuffer,
  43. unsigned long ulCount, uint32_t initValue)
  44. {
  45. unsigned long ulTemp1;
  46. unsigned long ulTemp2;
  47. unsigned long ulCRC = initValue;
  48. while (ulCount-- != 0)
  49. {
  50. ulTemp1 = (ulCRC >> 8) & 0x00FFFFFF;
  51. ulTemp2 = _CRC32Value(((int)ulCRC ^ *ucBuffer++) & 0xff);
  52. ulCRC = ulTemp1 ^ ulTemp2;
  53. }
  54. return (ulCRC);
  55. }
  56. void um482_rxmsg_init(um482_msg_t *msg)
  57. {
  58. msg->_parse_state = UM482_PARSE_STATE_IDLE;
  59. msg->_head_rx_index = 0;
  60. msg->_data_rx_index = 0;
  61. msg->_check_rx_index = 0;
  62. }
  63. void um482_rxmsg_deinit(um482_msg_t *msg)
  64. {
  65. msg->_parse_state = UM482_PARSE_STATE_UNINIT;
  66. }
  67. int8_t um482_rxmsg_parse_char(uint8_t c, um482_msg_t *msg)
  68. {
  69. int8_t ret_val = 0;
  70. if (msg->_parse_state == UM482_PARSE_STATE_UNINIT)
  71. {
  72. um482_rxmsg_init(msg);
  73. }
  74. switch (msg->_parse_state)
  75. {
  76. case UM482_PARSE_STATE_IDLE:
  77. switch (msg->_head_rx_index)
  78. {
  79. case 0:
  80. if (c == 0xAA)
  81. {
  82. msg->header.sync[0] = c;
  83. msg->_head_rx_index++;
  84. }
  85. else
  86. {
  87. um482_rxmsg_init(msg);
  88. }
  89. break;
  90. case 1:
  91. if (c == 0x44)
  92. {
  93. msg->header.sync[1] = c;
  94. msg->_head_rx_index++;
  95. }
  96. else
  97. {
  98. um482_rxmsg_init(msg);
  99. }
  100. break;
  101. case 2:
  102. if (c == 0x12)
  103. {
  104. msg->header.sync[2] = c;
  105. msg->_head_rx_index++;
  106. }
  107. else
  108. {
  109. um482_rxmsg_init(msg);
  110. }
  111. break;
  112. case 3:
  113. if (c == sizeof(msg->header))
  114. {
  115. msg->header.header_len = c;
  116. msg->_head_rx_index++;
  117. }
  118. else
  119. {
  120. um482_rxmsg_init(msg);
  121. }
  122. break;
  123. default:
  124. if (msg->_head_rx_index < sizeof(msg->header))
  125. {
  126. uint8_t *p = (uint8_t *)&msg->header;
  127. *(p + msg->_head_rx_index) = c;
  128. msg->_head_rx_index++;
  129. if (msg->_head_rx_index >= sizeof(msg->header))
  130. {
  131. if (msg->header.msg_len <= sizeof(msg->data))
  132. {
  133. msg->_parse_state = UM482_PARSE_STATE_GOT_HEADER;
  134. msg->_head_rx_index = 0;
  135. msg->_data_rx_index = 0;
  136. }
  137. else
  138. {
  139. um482_rxmsg_init(msg);
  140. }
  141. }
  142. }
  143. else
  144. {
  145. um482_rxmsg_init(msg);
  146. }
  147. break;
  148. }
  149. break;
  150. case UM482_PARSE_STATE_GOT_HEADER:
  151. if (msg->_data_rx_index < msg->header.msg_len)
  152. {
  153. msg->data[msg->_data_rx_index] = c;
  154. msg->_data_rx_index++;
  155. if (msg->_data_rx_index >= msg->header.msg_len)
  156. {
  157. msg->_parse_state = UM482_PARSE_STATE_GOT_DATA;
  158. msg->_check_rx_index = 0;
  159. msg->crc32_check = 0;
  160. }
  161. }
  162. else
  163. {
  164. um482_rxmsg_init(msg);
  165. }
  166. break;
  167. case UM482_PARSE_STATE_GOT_DATA:
  168. if (msg->_check_rx_index < sizeof(msg->crc32_check))
  169. {
  170. msg->crc32_check += c << (msg->_check_rx_index * 8);
  171. msg->_check_rx_index++;
  172. if (msg->_check_rx_index >= sizeof(msg->crc32_check))
  173. {
  174. uint32_t check = 0;
  175. /* 计算校验 */
  176. check = _CalculateBlockCRC32((uint8_t *)&msg->header,
  177. sizeof(msg->header), 0);
  178. check =
  179. _CalculateBlockCRC32(msg->data, msg->header.msg_len, check);
  180. if (check == msg->crc32_check)
  181. {
  182. ret_val = 1;
  183. }
  184. else
  185. {
  186. ret_val = -1;
  187. }
  188. um482_rxmsg_init(msg);
  189. }
  190. }
  191. else
  192. {
  193. um482_rxmsg_init(msg);
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. return ret_val;
  200. }
  201. void um482_rxmsg_decode(um482_msg_t *msg, um482_rx_data_t *pdata)
  202. {
  203. switch (msg->header.msg_id)
  204. {
  205. case UM482_MSG_BESTPOS_ID:
  206. pdata->bestpos_rx_count++;
  207. if (pdata->bestpos_rx_count == 0)
  208. pdata->bestpos_rx_count = 1;
  209. if (pdata->bestpos_data_link_status != COMP_CLOSED)
  210. pdata->bestpos_data_link_status = COMP_NORMAL;
  211. memcpy(&pdata->bestpos, msg->data, sizeof(pdata->bestpos));
  212. if (pdata->bestpos_rx_callback)
  213. pdata->bestpos_rx_callback(pdata);
  214. break;
  215. case UM482_MSG_BESTVEL_ID:
  216. pdata->bestvel_rx_count++;
  217. if (pdata->bestvel_rx_count == 0)
  218. pdata->bestvel_rx_count = 1;
  219. if (pdata->bestvel_data_link_status != COMP_CLOSED)
  220. pdata->bestvel_data_link_status = COMP_NORMAL;
  221. memcpy(&pdata->bestvel, msg->data, sizeof(pdata->bestvel));
  222. if (pdata->bestvel_rx_callback)
  223. pdata->bestvel_rx_callback(pdata);
  224. break;
  225. case UM482_MSG_HEADING_ID:
  226. pdata->heading_rx_count++;
  227. if (pdata->heading_rx_count == 0)
  228. pdata->heading_rx_count = 1;
  229. if (pdata->heading_data_link_status != COMP_CLOSED)
  230. pdata->heading_data_link_status = COMP_NORMAL;
  231. memcpy(&pdata->heading, msg->data, sizeof(pdata->heading));
  232. if (pdata->heading_rx_callback)
  233. pdata->heading_rx_callback(pdata);
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. void um482_rxmsg_link_check(um482_rx_data_t *pdata)
  240. {
  241. /* printf("%d %d %d\r\n", pdata->bestvel_rx_count, pdata->bestpos_rx_count,
  242. pdata->heading_rx_count); */
  243. pdata->rx_count = pdata->bestvel_rx_count + pdata->bestpos_rx_count +
  244. pdata->heading_rx_count;
  245. if (pdata->bestvel_data_link_status == COMP_NORMAL)
  246. {
  247. if (pdata->bestvel_rx_count == 0)
  248. {
  249. pdata->bestvel_data_link_status = COMP_LOST;
  250. }
  251. pdata->bestvel_rx_count = 0;
  252. }
  253. if (pdata->bestpos_data_link_status == COMP_NORMAL)
  254. {
  255. if (pdata->bestpos_rx_count == 0)
  256. {
  257. pdata->bestpos_data_link_status = COMP_LOST;
  258. }
  259. pdata->bestpos_rx_count = 0;
  260. }
  261. if (pdata->heading_data_link_status == COMP_NORMAL)
  262. {
  263. if (pdata->heading_rx_count == 0)
  264. {
  265. pdata->heading_data_link_status = COMP_LOST;
  266. }
  267. pdata->heading_rx_count = 0;
  268. }
  269. }