crc8.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Library: libcrc
  3. * File: src/crc8.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 1999-2016 Lammert Bies
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. *
  28. * Description
  29. * -----------
  30. * The source file src/crc8.c contains routines for the calculation of 8 bit
  31. * CRC values according to the calculation rules used in the SHT1x and SHT7x
  32. * series of temperature and humidity sensors.
  33. */
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. /*
  37. * static uint8_t sht75_crc_table[];
  38. *
  39. * The SHT75 humidity sensor is capable of calculating an 8 bit CRC checksum to
  40. * ensure data integrity. The lookup table crc_table[] is used to recalculate
  41. * the CRC.
  42. */
  43. static uint8_t sht75_crc_table[] = {
  44. 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, 46,
  45. 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, 92, 109,
  46. 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, 202, 153, 168,
  47. 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, 184, 137, 218, 235,
  48. 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, 215, 64, 113, 34, 19,
  49. 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, 165, 148, 3, 50, 97, 80,
  50. 187, 138, 217, 232, 127, 78, 29, 44, 2, 51, 96, 81, 198, 247, 164, 149,
  51. 248, 201, 154, 171, 60, 13, 94, 111, 65, 112, 35, 18, 133, 180, 231, 214,
  52. 122, 75, 24, 41, 190, 143, 220, 237, 195, 242, 161, 144, 7, 54, 101, 84,
  53. 57, 8, 91, 106, 253, 204, 159, 174, 128, 177, 226, 211, 68, 117, 38, 23,
  54. 252, 205, 158, 175, 56, 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210,
  55. 191, 142, 221, 236, 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145,
  56. 71, 118, 37, 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105,
  57. 4, 53, 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42,
  58. 193, 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239,
  59. 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, 172
  60. };
  61. /*
  62. * uint8_t crc_8( const unsigned char *input_str, size_t num_bytes );
  63. *
  64. * The function crc_8() calculates the 8 bit wide CRC of an input string of a
  65. * given length.
  66. */
  67. uint8_t crc_8( const unsigned char *input_str, size_t num_bytes ) {
  68. size_t a;
  69. uint8_t crc;
  70. const unsigned char *ptr;
  71. crc = CRC_START_8;
  72. ptr = input_str;
  73. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  74. crc = sht75_crc_table[(*ptr) ^ crc];
  75. ptr++;
  76. }
  77. return crc;
  78. } /* crc_8 */
  79. /*
  80. * uint8_t update_crc_8( unsigned char crc, unsigned char val );
  81. *
  82. * Given a databyte and the previous value of the CRC value, the function
  83. * update_crc_8() calculates and returns the new actual CRC value of the data
  84. * comming in.
  85. */
  86. uint8_t update_crc_8( unsigned char crc, unsigned char val ) {
  87. return sht75_crc_table[val ^ crc];
  88. } /* update_crc_8 */