You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crc16.h 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // CRC compatibility, adapted from the C-only comments here:
  2. // http://svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/util/crc16.h?revision=933&root=avr-libc&view=markup
  3. /* Copyright (c) 2002, 2003, 2004 Marek Michalkiewicz
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of the copyright holders nor the names of
  14. contributors may be used to endorse or promote products derived
  15. from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #ifndef _UTIL_CRC16_H_
  28. #define _UTIL_CRC16_H_
  29. #include <stdint.h>
  30. static inline uint16_t _crc16_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  31. static inline uint16_t _crc16_update(uint16_t crc, uint8_t data)
  32. {
  33. unsigned int i;
  34. crc ^= data;
  35. for (i = 0; i < 8; ++i) {
  36. if (crc & 1) {
  37. crc = (crc >> 1) ^ 0xA001;
  38. } else {
  39. crc = (crc >> 1);
  40. }
  41. }
  42. return crc;
  43. }
  44. static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  45. static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data)
  46. {
  47. unsigned int i;
  48. crc = crc ^ ((uint16_t)data << 8);
  49. for (i=0; i<8; i++) {
  50. if (crc & 0x8000) {
  51. crc = (crc << 1) ^ 0x1021;
  52. } else {
  53. crc <<= 1;
  54. }
  55. }
  56. return crc;
  57. }
  58. static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  59. static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
  60. {
  61. data ^= (crc & 255);
  62. data ^= data << 4;
  63. return ((((uint16_t)data << 8) | (crc >> 8)) ^ (uint8_t)(data >> 4)
  64. ^ ((uint16_t)data << 3));
  65. }
  66. static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data) __attribute__((always_inline, unused));
  67. static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data)
  68. {
  69. unsigned int i;
  70. crc = crc ^ data;
  71. for (i = 0; i < 8; i++) {
  72. if (crc & 0x01) {
  73. crc = (crc >> 1) ^ 0x8C;
  74. } else {
  75. crc >>= 1;
  76. }
  77. }
  78. return crc;
  79. }
  80. #endif