crc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * This software is subject to the ANT+ Shared Source License
  3. * www.thisisant.com/swlicenses
  4. * Copyright (c) Garmin Canada Inc. 2012
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * 1) Redistributions of source code must retain the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer.
  14. *
  15. * 2) Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * 3) Neither the name of Garmin nor the names of its
  21. * contributors may be used to endorse or promote products
  22. * derived from this software without specific prior
  23. * written permission.
  24. *
  25. * The following actions are prohibited:
  26. *
  27. * 1) Redistribution of source code containing the ANT+ Network
  28. * Key. The ANT+ Network Key is available to ANT+ Adopters.
  29. * Please refer to http://thisisant.com to become an ANT+
  30. * Adopter and access the key.
  31. *
  32. * 2) Reverse engineering, decompilation, and/or disassembly of
  33. * software provided in binary form under this license.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE HEREBY
  39. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  42. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; DAMAGE TO ANY DEVICE, LOSS OF USE, DATA, OR
  44. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  45. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  47. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE. SOME STATES DO NOT ALLOW
  49. * THE EXCLUSION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THE
  50. * ABOVE LIMITATIONS MAY NOT APPLY TO YOU.
  51. *
  52. */
  53. #include "crc.h"
  54. #include "compiler_abstraction.h"
  55. /**@brief Function for updating the current CRC-16 value for a single byte input.
  56. *
  57. * @param[in] current_crc The current calculated CRC-16 value.
  58. * @param[in] byte The input data byte for the computation.
  59. *
  60. * @return The updated CRC-16 value, based on the input supplied.
  61. */
  62. static __INLINE uint16_t crc16_get(uint16_t current_crc, uint8_t byte)
  63. {
  64. static const uint16_t crc16_table[16] =
  65. {
  66. 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
  67. 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
  68. };
  69. uint16_t temp;
  70. // Compute checksum of lower four bits of a byte.
  71. temp = crc16_table[current_crc & 0xF];
  72. current_crc = (current_crc >> 4u) & 0x0FFFu;
  73. current_crc = current_crc ^ temp ^ crc16_table[byte & 0xF];
  74. // Now compute checksum of upper four bits of a byte.
  75. temp = crc16_table[current_crc & 0xF];
  76. current_crc = (current_crc >> 4u) & 0x0FFFu;
  77. current_crc = current_crc ^ temp ^ crc16_table[(byte >> 4u) & 0xF];
  78. return current_crc;
  79. }
  80. uint16_t crc_crc16_update(uint16_t current_crc, const volatile void * p_data, uint32_t size)
  81. {
  82. uint8_t * p_block = (uint8_t *)p_data;
  83. while (size != 0)
  84. {
  85. current_crc = crc16_get(current_crc, *p_block);
  86. p_block++;
  87. size--;
  88. }
  89. return current_crc;
  90. }