crc8.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "crc8.h"
  2. /**
  3. * @funtion:crc8多项式冗余校验
  4. * @param 1:pData,计算数据源地址
  5. * @param 2:dataLen,计算数据源长度
  6. * @param 3:initialValue,crc结果初值
  7. * @param 4:polynomial,多项式
  8. *
  9. * @return :校验结果
  10. */
  11. unsigned char crc8( unsigned char *pData,
  12. unsigned int dataLen,
  13. unsigned char initialValue,
  14. unsigned char polynomial )
  15. {
  16. unsigned char i;
  17. unsigned char crc;
  18. crc = initialValue;
  19. while (dataLen --)
  20. {
  21. crc ^= *pData ++;
  22. for( i = 0; i < 8; i++ )
  23. {
  24. if(crc & 0x80)
  25. {
  26. crc <<= 1; // shift left once
  27. crc ^= polynomial; // XOR with polynomial
  28. }
  29. else
  30. {
  31. crc <<= 1; // shift left once
  32. }
  33. }
  34. }
  35. return crc;
  36. }
  37. /**
  38. * @funtion :针对温湿度传感器sht3X系列的crc8校验
  39. * @param 1:pData,计算数据源地址
  40. * @param 2:dataLen,计算数据源长度
  41. * @param 3:targetCRC,对比结果CRC
  42. *
  43. * @return :对比校验结果,=1校验成功,=0校验失败
  44. */
  45. unsigned char crc8_ger( unsigned char *pData,
  46. unsigned int dataLen)
  47. {
  48. return crc8(pData, dataLen, 0xff, 0x31);
  49. }
  50. /**
  51. * @funtion :针对温湿度传感器sht3X系列的crc8校验
  52. * @param 1:pData,计算数据源地址
  53. * @param 2:dataLen,计算数据源长度
  54. * @param 3:targetCRC,对比结果CRC
  55. *
  56. * @return :对比校验结果,=1校验成功,=0校验失败
  57. */
  58. int crc8_gernCheckT( unsigned char *pData,
  59. unsigned int dataLen,
  60. unsigned char targetCRC)
  61. {
  62. if (crc8(pData, dataLen, 0xff, 0x31) == targetCRC)
  63. {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @funtion :针对温湿度传感器sht3X系列的crc8校验
  70. * @param 1:pData,计算数据源地址
  71. * @param 2:dataLen,计算数据源长度
  72. * @param 3:targetCRC,对比结果CRC
  73. *
  74. * @return :对比校验结果,=1校验成功,=0校验失败
  75. */
  76. int crc8_sht3x( unsigned char *pData,
  77. unsigned int dataLen,
  78. unsigned char targetCRC)
  79. {
  80. if (crc8(pData, dataLen, 0xff, 0x31) == targetCRC)
  81. {
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. /**
  87. * @funtion :针对温湿度传感器sht2X系列的crc8校验
  88. * @param 1:pData,计算数据源地址
  89. * @param 2:dataLen,计算数据源长度
  90. * @param 3:targetCRC,对比结果CRC
  91. *
  92. * @return :对比校验结果,=1校验成功,=0校验失败
  93. */
  94. int crc8_sht2x( unsigned char *pData,
  95. unsigned int dataLen,
  96. unsigned char targetCRC)
  97. {
  98. if (crc8(pData, dataLen, 0x00, 0x31) == targetCRC)
  99. {
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * @funtion :\
  106. * @param 1:pData,计算数据源地址
  107. * @param 2:dataLen,计算数据源长度
  108. * @param 3:targetCRC,对比结果CRC
  109. *
  110. * @return :对比校验结果,=1校验成功,=0校验失败
  111. */
  112. int cmp_crc8( uint8_t *pData,
  113. uint16_t dataLen,
  114. uint8_t targetCRC)
  115. {
  116. if (crc8(pData, dataLen, 0x55, 0x07) == targetCRC)
  117. {
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * @funtion :\
  124. * @param 1:pData,计算数据源地址
  125. * @param 2:dataLen,计算数据源长度
  126. *
  127. * @return :返回CRC结果
  128. */
  129. int get_crc8( uint8_t *pData,
  130. uint16_t dataLen)
  131. {
  132. return crc8(pData, dataLen, 0x55, 0x07);
  133. }
  134. bool checkFramLegal(uint8_t *srcBuffer, uint8_t srcLen)
  135. {
  136. bool ret = false;
  137. uint8_t len;
  138. uint8_t cmd;
  139. uint8_t crc1;
  140. // uint8_t crc2;
  141. len = srcBuffer[0];
  142. cmd = srcBuffer[1];
  143. crc1 = srcBuffer[len];
  144. // crc2 = srcBuffer[len];
  145. if (len == (srcLen - 1))
  146. {
  147. if ((cmd & 0x80) == 0)
  148. {
  149. if (cmp_crc8(srcBuffer, srcLen - 1, crc1))
  150. {
  151. ret = true;
  152. // ret = cmp_crc8(srcBuffer, srcLen - 1, crc2);
  153. }
  154. }
  155. else
  156. {
  157. ret = true;
  158. }
  159. }
  160. return ret;
  161. }
  162. void completFramParams(uint8_t *srcBuffer, uint8_t srcLen)
  163. {
  164. uint8_t *head;
  165. uint8_t *len;
  166. uint8_t *crc1;
  167. // uint8_t *crc2;
  168. uint8_t *end;
  169. len = &srcBuffer[0];
  170. crc1 = &srcBuffer[srcLen - 1];
  171. // crc2 = &srcBuffer[srcLen - 1];
  172. *len = srcLen - 1;
  173. *crc1 = get_crc8(srcBuffer, srcLen - 1);
  174. // *crc2 = get_crc8(srcBuffer, srcLen - 1);
  175. }