crc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. This software is subject to the license described in the license.txt file included with this software distribution.
  3. You may not use this file except in compliance with this license.
  4. Copyright © Dynastream Innovations Inc. 2012
  5. All rights reserved.
  6. */
  7. #include "crc.h"
  8. #include "compiler_abstraction.h"
  9. /**@brief Function for updating the current CRC-16 value for a single byte input.
  10. *
  11. * @param[in] current_crc The current calculated CRC-16 value.
  12. * @param[in] byte The input data byte for the computation.
  13. *
  14. * @return The updated CRC-16 value, based on the input supplied.
  15. */
  16. static __INLINE uint16_t crc16_get(uint16_t current_crc, uint8_t byte)
  17. {
  18. static const uint16_t crc16_table[16] =
  19. {
  20. 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
  21. 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
  22. };
  23. uint16_t temp;
  24. // Compute checksum of lower four bits of a byte.
  25. temp = crc16_table[current_crc & 0xF];
  26. current_crc = (current_crc >> 4u) & 0x0FFFu;
  27. current_crc = current_crc ^ temp ^ crc16_table[byte & 0xF];
  28. // Now compute checksum of upper four bits of a byte.
  29. temp = crc16_table[current_crc & 0xF];
  30. current_crc = (current_crc >> 4u) & 0x0FFFu;
  31. current_crc = current_crc ^ temp ^ crc16_table[(byte >> 4u) & 0xF];
  32. return current_crc;
  33. }
  34. uint16_t crc_crc16_update(uint16_t current_crc, const volatile void * p_data, uint32_t size)
  35. {
  36. uint8_t * p_block = (uint8_t *)p_data;
  37. while (size != 0)
  38. {
  39. current_crc = crc16_get(current_crc, *p_block);
  40. p_block++;
  41. size--;
  42. }
  43. return current_crc;
  44. }