crc16.c 886 B

12345678910111213141516171819202122232425262728293031
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "crc16.h"
  13. #include <stdlib.h>
  14. uint16_t crc16_compute(uint8_t const * p_data, uint32_t size, uint16_t const * p_crc)
  15. {
  16. uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc;
  17. for (uint32_t i = 0; i < size; i++)
  18. {
  19. crc = (uint8_t)(crc >> 8) | (crc << 8);
  20. crc ^= p_data[i];
  21. crc ^= (uint8_t)(crc & 0xFF) >> 4;
  22. crc ^= (crc << 8) << 4;
  23. crc ^= ((crc & 0xFF) << 4) << 1;
  24. }
  25. return crc;
  26. }