ch32v20x_crc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v20x_crc.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file provides all the CRC firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v20x_crc.h"
  13. /*********************************************************************
  14. * @fn CRC_ResetDR
  15. *
  16. * @brief Resets the CRC Data register (DR).
  17. *
  18. * @return none
  19. */
  20. void CRC_ResetDR(void)
  21. {
  22. CRC->CTLR = CRC_CTLR_RESET;
  23. }
  24. /*********************************************************************
  25. * @fn CRC_CalcCRC
  26. *
  27. * @brief Computes the 32-bit CRC of a given data word(32-bit).
  28. *
  29. * @param Data - data word(32-bit) to compute its CRC.
  30. *
  31. * @return 32-bit CRC.
  32. */
  33. uint32_t CRC_CalcCRC(uint32_t Data)
  34. {
  35. CRC->DATAR = Data;
  36. return (CRC->DATAR);
  37. }
  38. /*********************************************************************
  39. * @fn CRC_CalcBlockCRC
  40. *
  41. * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
  42. *
  43. * @param pBuffer - pointer to the buffer containing the data to be computed.
  44. * BufferLength - length of the buffer to be computed.
  45. *
  46. * @return 32-bit CRC.
  47. */
  48. uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
  49. {
  50. uint32_t index = 0;
  51. for(index = 0; index < BufferLength; index++){
  52. CRC->DATAR = pBuffer[index];
  53. }
  54. return (CRC->DATAR);
  55. }
  56. /*********************************************************************
  57. * @fn CRC_GetCRC
  58. *
  59. * @brief Returns the current CRC value.
  60. *
  61. * @return 32-bit CRC.
  62. */
  63. uint32_t CRC_GetCRC(void)
  64. {
  65. return (CRC->DATAR);
  66. }
  67. /*********************************************************************
  68. * @fn CRC_SetIDRegister
  69. *
  70. * @brief Stores a 8-bit data in the Independent Data(ID) register.
  71. *
  72. * @param IDValue - 8-bit value to be stored in the ID register.
  73. *
  74. * @return none
  75. */
  76. void CRC_SetIDRegister(uint8_t IDValue)
  77. {
  78. CRC->IDATAR = IDValue;
  79. }
  80. /*********************************************************************
  81. * @fn CRC_GetIDRegister
  82. *
  83. * @brief Returns the 8-bit data stored in the Independent Data(ID) register.
  84. *
  85. * @return 8-bit value of the ID register.
  86. */
  87. uint8_t CRC_GetIDRegister(void)
  88. {
  89. return (CRC->IDATAR);
  90. }