crc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "../bsp/dp32g030/crc.h"
  17. #include "crc.h"
  18. void CRC_Init(void)
  19. {
  20. CRC_CR = 0
  21. | CRC_CR_CRC_EN_BITS_DISABLE
  22. | CRC_CR_INPUT_REV_BITS_NORMAL
  23. | CRC_CR_INPUT_INV_BITS_NORMAL
  24. | CRC_CR_OUTPUT_REV_BITS_NORMAL
  25. | CRC_CR_OUTPUT_INV_BITS_NORMAL
  26. | CRC_CR_DATA_WIDTH_BITS_8
  27. | CRC_CR_CRC_SEL_BITS_CRC_16_CCITT
  28. ;
  29. CRC_IV = 0;
  30. }
  31. uint16_t CRC_Calculate(const void *pBuffer, uint16_t Size)
  32. {
  33. const uint8_t *pData = (const uint8_t *)pBuffer;
  34. uint16_t i, Crc;
  35. CRC_CR = (CRC_CR & ~CRC_CR_CRC_EN_MASK) | CRC_CR_CRC_EN_BITS_ENABLE;
  36. for (i = 0; i < Size; i++) {
  37. CRC_DATAIN = pData[i];
  38. }
  39. Crc = (uint16_t)CRC_DATAOUT;
  40. CRC_CR = (CRC_CR & ~CRC_CR_CRC_EN_MASK) | CRC_CR_CRC_EN_BITS_DISABLE;
  41. return Crc;
  42. }