crc16-ccitt.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Calculation of CRC 16 CCITT polynomial.
  3. *
  4. * @file crc16-ccitt.h
  5. * @ingroup CO_crc16_ccitt
  6. * @author Lammert Bies
  7. * @author Janez Paternoster
  8. * @copyright 2012 - 2020 Janez Paternoster
  9. *
  10. * This file is part of CANopenNode, an opensource CANopen Stack.
  11. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  12. * For more information on CANopen see <http://www.can-cia.org/>.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef CRC16_CCITT_H
  27. #define CRC16_CCITT_H
  28. #include "301/CO_driver.h"
  29. /* default configuration, see CO_config.h */
  30. #ifndef CO_CONFIG_CRC16
  31. #define CO_CONFIG_CRC16 (0)
  32. #endif
  33. #if ((CO_CONFIG_CRC16) & CO_CONFIG_CRC16_ENABLE) || defined CO_DOXYGEN
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * @defgroup CO_crc16_ccitt CRC 16 CCITT
  39. * @ingroup CO_CANopen_301
  40. * @{
  41. *
  42. * Calculation of CRC 16 CCITT polynomial.
  43. *
  44. * Equation:
  45. *
  46. * `x^16 + x^12 + x^5 + 1`
  47. */
  48. /**
  49. * Update crc16_ccitt variable with one data byte
  50. *
  51. * This function updates crc variable for one data byte using crc16 ccitt
  52. * algorithm.
  53. *
  54. * @param [in,out] crc Externally defined variable for CRC checksum. Before
  55. * start of new CRC calculation, variable must be initialized (zero for xmodem).
  56. * @param chr One byte of data
  57. */
  58. void crc16_ccitt_single(uint16_t *crc, const uint8_t chr);
  59. /**
  60. * Calculate CRC sum on block of data.
  61. *
  62. * @param block Pointer to block of data.
  63. * @param blockLength Length of data in bytes;
  64. * @param crc Initial value (zero for xmodem). If block is split into
  65. * multiple segments, previous CRC is used as initial.
  66. *
  67. * @return Calculated CRC.
  68. */
  69. uint16_t crc16_ccitt(const uint8_t block[],
  70. size_t blockLength,
  71. uint16_t crc);
  72. /** @} */ /* CO_crc16_ccitt */
  73. #ifdef __cplusplus
  74. }
  75. #endif /*__cplusplus*/
  76. #endif /* (CO_CONFIG_CRC16) & CO_CONFIG_CRC16_ENABLE */
  77. #endif /* CRC16_CCITT_H */