PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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 2.9KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (c) 2002, 2003, 2004 Marek Michalkiewicz
  2. Copyright (c) 2005, 2007 Joerg Wunsch
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of the copyright holders nor the names of
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE. */
  26. // Port to Energia / MPS430 by Yannick DEVOS XV4Y - (c) 2013
  27. // http://xv4y.radioclub.asia/
  28. //
  29. /* $Id: crc16.h 2136 2010-06-08 12:03:38Z joerg_wunsch $ */
  30. #ifndef _UTIL_CRC16_H_
  31. #define _UTIL_CRC16_H_
  32. #include <stdint.h>
  33. #define lo8(x) ((x)&0xff)
  34. #define hi8(x) ((x)>>8)
  35. uint16_t crc16_update(uint16_t crc, uint8_t a)
  36. {
  37. int i;
  38. crc ^= a;
  39. for (i = 0; i < 8; ++i)
  40. {
  41. if (crc & 1)
  42. crc = (crc >> 1) ^ 0xA001;
  43. else
  44. crc = (crc >> 1);
  45. }
  46. return crc;
  47. }
  48. uint16_t crc_xmodem_update (uint16_t crc, uint8_t data)
  49. {
  50. int i;
  51. crc = crc ^ ((uint16_t)data << 8);
  52. for (i=0; i<8; i++)
  53. {
  54. if (crc & 0x8000)
  55. crc = (crc << 1) ^ 0x1021;
  56. else
  57. crc <<= 1;
  58. }
  59. return crc;
  60. }
  61. uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
  62. {
  63. data ^= lo8 (crc);
  64. data ^= data << 4;
  65. return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
  66. ^ ((uint16_t)data << 3));
  67. }
  68. uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data)
  69. {
  70. uint8_t i;
  71. crc = crc ^ data;
  72. for (i = 0; i < 8; i++)
  73. {
  74. if (crc & 0x01)
  75. crc = (crc >> 1) ^ 0x8C;
  76. else
  77. crc >>= 1;
  78. }
  79. return crc;
  80. }
  81. #endif /* _UTIL_CRC16_H_ */