gd32f3x0_crc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*!
  2. \file gd32f3x0_crc.c
  3. \brief CRC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_crc.h"
  10. /*!
  11. \brief deinit CRC calculation unit
  12. \param[in] none
  13. \param[out] none
  14. \retval none
  15. */
  16. void crc_deinit(void)
  17. {
  18. CRC_IDATA = (uint32_t)0xFFFFFFFFU;
  19. CRC_DATA = (uint32_t)0xFFFFFFFFU;
  20. CRC_FDATA = (uint32_t)0x00000000U;
  21. CRC_POLY = (uint32_t)0x04C11DB7U;
  22. CRC_CTL = CRC_CTL_RST;
  23. }
  24. /*!
  25. \brief enable the reverse operation of output data
  26. \param[in] none
  27. \param[out] none
  28. \retval none
  29. */
  30. void crc_reverse_output_data_enable(void)
  31. {
  32. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  33. CRC_CTL |= (uint32_t)CRC_CTL_REV_O;
  34. }
  35. /*!
  36. \brief disable the reverse operation of output data
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void crc_reverse_output_data_disable(void)
  42. {
  43. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  44. }
  45. /*!
  46. \brief reset data register to the value of initializaiton data register
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. void crc_data_register_reset(void)
  52. {
  53. CRC_CTL |= (uint32_t)CRC_CTL_RST;
  54. }
  55. /*!
  56. \brief read the data register
  57. \param[in] none
  58. \param[out] none
  59. \retval 32-bit value of the data register
  60. */
  61. uint32_t crc_data_register_read(void)
  62. {
  63. uint32_t data;
  64. data = CRC_DATA;
  65. return (data);
  66. }
  67. /*!
  68. \brief read the free data register
  69. \param[in] none
  70. \param[out] none
  71. \retval 8-bit value of the free data register
  72. */
  73. uint8_t crc_free_data_register_read(void)
  74. {
  75. uint8_t fdata;
  76. fdata = (uint8_t)CRC_FDATA;
  77. return (fdata);
  78. }
  79. /*!
  80. \brief write the free data register
  81. \param[in] free_data: specify 8-bit data
  82. \param[out] none
  83. \retval none
  84. */
  85. void crc_free_data_register_write(uint8_t free_data)
  86. {
  87. CRC_FDATA = (uint32_t)free_data;
  88. }
  89. /*!
  90. \brief write the initializaiton data register
  91. \param[in] init_data:specify 32-bit data
  92. \param[out] none
  93. \retval none
  94. */
  95. void crc_init_data_register_write(uint32_t init_data)
  96. {
  97. CRC_IDATA = (uint32_t)init_data;
  98. }
  99. /*!
  100. \brief configure the CRC input data function
  101. \param[in] data_reverse: specify input data reverse function
  102. \arg CRC_INPUT_DATA_NOT: input data is not reversed
  103. \arg CRC_INPUT_DATA_BYTE: input data is reversed on 8 bits
  104. \arg CRC_INPUT_DATA_HALFWORD: input data is reversed on 16 bits
  105. \arg CRC_INPUT_DATA_WORD: input data is reversed on 32 bits
  106. \param[out] none
  107. \retval none
  108. */
  109. void crc_input_data_reverse_config(uint32_t data_reverse)
  110. {
  111. CRC_CTL &= (uint32_t)(~CRC_CTL_REV_I);
  112. CRC_CTL |= (uint32_t)data_reverse;
  113. }
  114. /*!
  115. \brief configure the CRC size of polynomial function
  116. \param[in] poly_size: size of polynomial
  117. \arg CRC_CTL_PS_32: 32-bit polynomial for CRC calculation
  118. \arg CRC_CTL_PS_16: 16-bit polynomial for CRC calculation
  119. \arg CRC_CTL_PS_8: 8-bit polynomial for CRC calculation
  120. \arg CRC_CTL_PS_7: 7-bit polynomial for CRC calculation
  121. \param[out] none
  122. \retval none
  123. */
  124. void crc_polynomial_size_set(uint32_t poly_size)
  125. {
  126. CRC_CTL &= (uint32_t)(~(CRC_CTL_PS));
  127. CRC_CTL |= (uint32_t)poly_size;
  128. }
  129. /*!
  130. \brief configure the CRC polynomial value function
  131. \param[in] poly: configurable polynomial value
  132. \param[out] none
  133. \retval none
  134. */
  135. void crc_polynomial_set(uint32_t poly)
  136. {
  137. CRC_POLY &= (uint32_t)(~CRC_POLY_POLY);
  138. CRC_POLY = poly;
  139. }
  140. /*!
  141. \brief CRC calculate a 32-bit data
  142. \param[in] sdata: specify 32-bit data
  143. \param[out] none
  144. \retval 32-bit CRC calculate value
  145. */
  146. uint32_t crc_single_data_calculate(uint32_t sdata)
  147. {
  148. CRC_DATA = sdata;
  149. return(CRC_DATA);
  150. }
  151. /*!
  152. \brief CRC calculate a 32-bit data array
  153. \param[in] array: pointer to an array of 32 bit data words
  154. \param[in] size: size of the array
  155. \param[out] none
  156. \retval 32-bit CRC calculate value
  157. */
  158. uint32_t crc_block_data_calculate(uint32_t array[], uint32_t size)
  159. {
  160. uint32_t index;
  161. for(index = 0U; index < size; index++){
  162. CRC_DATA = array[index];
  163. }
  164. return (CRC_DATA);
  165. }