Teensy 4.1 core updated for 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.

66 lines
1.4KB

  1. #ifndef _UTIL_CRC16_H_
  2. #define _UTIL_CRC16_H_
  3. #include <stdint.h>
  4. static inline uint16_t _crc16_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  5. static inline uint16_t _crc16_update(uint16_t crc, uint8_t data)
  6. {
  7. unsigned int i;
  8. crc ^= data;
  9. for (i = 0; i < 8; ++i) {
  10. if (crc & 1) {
  11. crc = (crc >> 1) ^ 0xA001;
  12. } else {
  13. crc = (crc >> 1);
  14. }
  15. }
  16. return crc;
  17. }
  18. static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  19. static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data)
  20. {
  21. unsigned int i;
  22. crc = crc ^ ((uint16_t)data << 8);
  23. for (i=0; i<8; i++) {
  24. if (crc & 0x8000) {
  25. crc = (crc << 1) ^ 0x1021;
  26. } else {
  27. crc <<= 1;
  28. }
  29. }
  30. return crc;
  31. }
  32. static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data) __attribute__((always_inline, unused));
  33. static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
  34. {
  35. data ^= (crc & 255);
  36. data ^= data << 4;
  37. return ((((uint16_t)data << 8) | (crc >> 8)) ^ (uint8_t)(data >> 4)
  38. ^ ((uint16_t)data << 3));
  39. }
  40. static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data) __attribute__((always_inline, unused));
  41. static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data)
  42. {
  43. unsigned int i;
  44. crc = crc ^ data;
  45. for (i = 0; i < 8; i++) {
  46. if (crc & 0x01) {
  47. crc = (crc >> 1) ^ 0x8C;
  48. } else {
  49. crc >>= 1;
  50. }
  51. }
  52. return crc;
  53. }
  54. #endif