rfal_crc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_crc.c
  27. *
  28. * \author Oliver Regenfelder
  29. *
  30. * \brief CRC calculation implementation
  31. *
  32. */
  33. /*
  34. ******************************************************************************
  35. * INCLUDES
  36. ******************************************************************************
  37. */
  38. #include "rfal_crc.h"
  39. /*
  40. ******************************************************************************
  41. * LOCAL FUNCTION PROTOTYPES
  42. ******************************************************************************
  43. */
  44. static uint16_t rfalCrcUpdateCcitt(uint16_t crcSeed, uint8_t dataByte);
  45. /*
  46. ******************************************************************************
  47. * GLOBAL FUNCTIONS
  48. ******************************************************************************
  49. */
  50. uint16_t rfalCrcCalculateCcitt(uint16_t preloadValue, const uint8_t* buf, uint16_t length)
  51. {
  52. uint16_t crc = preloadValue;
  53. uint16_t index;
  54. for (index = 0; index < length; index++)
  55. {
  56. crc = rfalCrcUpdateCcitt(crc, buf[index]);
  57. }
  58. return crc;
  59. }
  60. /*
  61. ******************************************************************************
  62. * LOCAL FUNCTIONS
  63. ******************************************************************************
  64. */
  65. static uint16_t rfalCrcUpdateCcitt(uint16_t crcSeed, uint8_t dataByte)
  66. {
  67. uint16_t crc = crcSeed;
  68. uint8_t dat = dataByte;
  69. dat ^= (uint8_t)(crc & 0xFFU);
  70. dat ^= (dat << 4);
  71. crc = (crc >> 8)^(((uint16_t) dat) << 8)^(((uint16_t) dat) << 3)^(((uint16_t) dat) >> 4);
  72. return crc;
  73. }