gd32e230_crc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. \file gd32e230_crc.c
  3. \brief CRC driver
  4. \version 2018-06-19, V1.0.0, firmware for GD32E230
  5. */
  6. /*
  7. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e230_crc.h"
  31. /*!
  32. \brief deinit CRC calculation unit
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void crc_deinit(void)
  38. {
  39. CRC_IDATA = (uint32_t)0xFFFFFFFFU;
  40. CRC_DATA = (uint32_t)0xFFFFFFFFU;
  41. CRC_FDATA = (uint32_t)0x00000000U;
  42. CRC_POLY = (uint32_t)0x04C11DB7U;
  43. CRC_CTL = CRC_CTL_RST;
  44. }
  45. /*!
  46. \brief enable the reverse operation of output data
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. void crc_reverse_output_data_enable(void)
  52. {
  53. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  54. CRC_CTL |= (uint32_t)CRC_CTL_REV_O;
  55. }
  56. /*!
  57. \brief disable the reverse operation of output data
  58. \param[in] none
  59. \param[out] none
  60. \retval none
  61. */
  62. void crc_reverse_output_data_disable(void)
  63. {
  64. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  65. }
  66. /*!
  67. \brief reset data register to the value of initializaiton data register
  68. \param[in] none
  69. \param[out] none
  70. \retval none
  71. */
  72. void crc_data_register_reset(void)
  73. {
  74. CRC_CTL |= (uint32_t)CRC_CTL_RST;
  75. }
  76. /*!
  77. \brief read the data register
  78. \param[in] none
  79. \param[out] none
  80. \retval 32-bit value of the data register
  81. */
  82. uint32_t crc_data_register_read(void)
  83. {
  84. uint32_t data;
  85. data = CRC_DATA;
  86. return (data);
  87. }
  88. /*!
  89. \brief read the free data register
  90. \param[in] none
  91. \param[out] none
  92. \retval 8-bit value of the free data register
  93. */
  94. uint8_t crc_free_data_register_read(void)
  95. {
  96. uint8_t fdata;
  97. fdata = (uint8_t)CRC_FDATA;
  98. return (fdata);
  99. }
  100. /*!
  101. \brief write the free data register
  102. \param[in] free_data: specify 8-bit data
  103. \param[out] none
  104. \retval none
  105. */
  106. void crc_free_data_register_write(uint8_t free_data)
  107. {
  108. CRC_FDATA = (uint32_t)free_data;
  109. }
  110. /*!
  111. \brief write the initializaiton data register
  112. \param[in] init_data:specify 32-bit data
  113. \param[out] none
  114. \retval none
  115. */
  116. void crc_init_data_register_write(uint32_t init_data)
  117. {
  118. CRC_IDATA = (uint32_t)init_data;
  119. }
  120. /*!
  121. \brief configure the CRC input data function
  122. \param[in] data_reverse: specify input data reverse function
  123. \arg CRC_INPUT_DATA_NOT: input data is not reversed
  124. \arg CRC_INPUT_DATA_BYTE: input data is reversed on 8 bits
  125. \arg CRC_INPUT_DATA_HALFWORD: input data is reversed on 16 bits
  126. \arg CRC_INPUT_DATA_WORD: input data is reversed on 32 bits
  127. \param[out] none
  128. \retval none
  129. */
  130. void crc_input_data_reverse_config(uint32_t data_reverse)
  131. {
  132. CRC_CTL &= (uint32_t)(~CRC_CTL_REV_I);
  133. CRC_CTL |= (uint32_t)data_reverse;
  134. }
  135. /*!
  136. \brief configure the CRC size of polynomial function
  137. \param[in] poly_size: size of polynomial
  138. \arg CRC_CTL_PS_32: 32-bit polynomial for CRC calculation
  139. \arg CRC_CTL_PS_16: 16-bit polynomial for CRC calculation
  140. \arg CRC_CTL_PS_8: 8-bit polynomial for CRC calculation
  141. \arg CRC_CTL_PS_7: 7-bit polynomial for CRC calculation
  142. \param[out] none
  143. \retval none
  144. */
  145. void crc_polynomial_size_set(uint32_t poly_size)
  146. {
  147. CRC_CTL &= (uint32_t)(~(CRC_CTL_PS));
  148. CRC_CTL |= (uint32_t)poly_size;
  149. }
  150. /*!
  151. \brief configure the CRC polynomial value function
  152. \param[in] poly: configurable polynomial value
  153. \param[out] none
  154. \retval none
  155. */
  156. void crc_polynomial_set(uint32_t poly)
  157. {
  158. CRC_POLY &= (uint32_t)(~CRC_POLY_POLY);
  159. CRC_POLY = poly;
  160. }
  161. /*!
  162. \brief CRC calculate a 32-bit data
  163. \param[in] sdata: specify 32-bit data
  164. \param[out] none
  165. \retval 32-bit CRC calculate value
  166. */
  167. uint32_t crc_single_data_calculate(uint32_t sdata)
  168. {
  169. CRC_DATA = sdata;
  170. return(CRC_DATA);
  171. }
  172. /*!
  173. \brief CRC calculate a 32-bit data array
  174. \param[in] array: pointer to an array of 32 bit data words
  175. \param[in] size: size of the array
  176. \param[out] none
  177. \retval 32-bit CRC calculate value
  178. */
  179. uint32_t crc_block_data_calculate(uint32_t array[], uint32_t size)
  180. {
  181. uint32_t index;
  182. for(index = 0U; index < size; index++){
  183. CRC_DATA = array[index];
  184. }
  185. return (CRC_DATA);
  186. }