Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6478930
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16635
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: 嵌入式

2014-07-22 15:54:32

操作系统:Debian7.5

前言
    很多设备的通信命令中,都会用到校验,常用的有crc8,crc16等。现在封装一下crc16的实现。

一,源码
    1,《crc.h》

点击(此处)折叠或打开

  1. #ifndef CRC_H
  2. #define CRC_H


  3. #include "commontypes.h"        

  4. #define CRC_INIT 0xFFFF


  5. uint16 crc_polynomial(uint8 b, uint16 acc);                    
  6. uint16 crc_sum(uint8* message, uint16 length, uint16 crc);            
  7. uint32 crc_verify(uint8* message, uint16 length);                        
  8. void crc_append(uint8* message, uint16 length);

  9. #endif
    2,《crc.c》

点击(此处)折叠或打开

  1. //!-------------------------------------------------------------------
  2. //! Include Files
  3. //!-------------------------------------------------------------------     
  4. //#include "PR9200.h"
  5. #include "crc.h"


  6. //!-------------------------------------------------------------------
  7. //! Definitions
  8. //!-------------------------------------------------------------------



  9. //!-------------------------------------------------------------------
  10. //! Global Data Declaration
  11. //!-------------------------------------------------------------------


  12. //!-------------------------------------------------------------------
  13. //! Fuction Definitions
  14. //!-------------------------------------------------------------------


  15. //!---------------------------------------------------------------
  16. //!    @brief
  17. //!        Polynomial functions for CRC (X16 + X12 + X5 + 1)
  18. //!
  19. //! @param
  20. //!        b: a byte data
  21. //!        acc: accumulated value
  22. //!
  23. //! @return
  24. //!        accumulated value
  25. //!
  26. //!---------------------------------------------------------------
  27. uint16 crc_polynomial(uint8 b, uint16 acc)
  28. {
  29.     acc = (uint8)(acc >> 8) | (acc << 8);
  30.     acc ^= b;
  31.     acc ^= (uint8)(acc & 0xff) >> 4;
  32.     acc ^= (acc << 8) << 4;
  33.     acc ^= ((acc & 0xff) << 4) << 1;

  34.     return acc;
  35. }

  36. //!---------------------------------------------------------------
  37. //!    @brief
  38. //!        Caculate 16-bit CRC on all the messages
  39. //!
  40. //! @param
  41. //!        * msg:    data pointer
  42. //!        length: data length
  43. //!     crc: CRC initial value(0xFFFF)
  44. //!
  45. //! @return
  46. //!        crc value
  47. //!
  48. //!---------------------------------------------------------------
  49. uint16 crc_sum(uint8* msg, uint16 length, uint16 crc)
  50. {
  51.     uint16 i;

  52.     for(i = 0; i < length; i++) {
  53.         crc = crc_polynomial(msg[i],crc);
  54.     }
  55.     return crc;
  56. }

  57. //!---------------------------------------------------------------
  58. //!    @brief
  59. //!        Check the CRC validity     
  60. //!
  61. //! @param
  62. //!        * msg: data pointer
  63. //!        length: data length
  64. //!
  65. //! @return
  66. //!        valid(0) / invalid(Not 0)
  67. //!
  68. //!---------------------------------------------------------------    
  69. uint32 crc_verify(uint8* msg, uint16 length)
  70. {
  71.     uint16 expected;

  72.     expected = crc_sum(msg, length - 2, CRC_INIT);
  73.     return !( ( expected & 0xff) == msg[length - 1] &&
  74.               ((expected >> 8) & 0xff) == msg[length - 2] );
  75. }


  76. //!---------------------------------------------------------------
  77. //!    @brief
  78. //!        Append the CRC value to the end of data
  79. //!
  80. //! @param
  81. //!        * message: data pointer
  82. //!        length: data length
  83. //!
  84. //! @return
  85. //!        None
  86. //!
  87. //!---------------------------------------------------------------    
  88. void crc_append(uint8* message, uint16 length)
  89. {
  90. #ifdef    __FEATURE_ENABLE_RCP_CRC__

  91.     uint16 crc;

  92.     crc = crc_sum(message, length, CRC_INIT);
  93.     message[length+1] = (uint8)(crc & 0xff);
  94.     message[length] = (uint8)((crc >> 8) & 0xff);

  95. #endif
  96. }

二,测试结果



三,源码包
    crc16.rar





阅读(3317) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~