gd32e23x_crc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!
  2. \file gd32e23x_crc.c
  3. \brief CRC driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  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 "gd32e23x_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. only one parameter can be selected which is shown as below:
  124. \arg CRC_INPUT_DATA_NOT: input data is not reversed
  125. \arg CRC_INPUT_DATA_BYTE: input data is reversed on 8 bits
  126. \arg CRC_INPUT_DATA_HALFWORD: input data is reversed on 16 bits
  127. \arg CRC_INPUT_DATA_WORD: input data is reversed on 32 bits
  128. \param[out] none
  129. \retval none
  130. */
  131. void crc_input_data_reverse_config(uint32_t data_reverse)
  132. {
  133. CRC_CTL &= (uint32_t)(~CRC_CTL_REV_I);
  134. CRC_CTL |= (uint32_t)data_reverse;
  135. }
  136. /*!
  137. \brief configure the CRC size of polynomial function
  138. \param[in] poly_size: size of polynomial
  139. only one parameter can be selected which is shown as below:
  140. \arg CRC_CTL_PS_32: 32-bit polynomial for CRC calculation
  141. \arg CRC_CTL_PS_16: 16-bit polynomial for CRC calculation
  142. \arg CRC_CTL_PS_8: 8-bit polynomial for CRC calculation
  143. \arg CRC_CTL_PS_7: 7-bit polynomial for CRC calculation
  144. \param[out] none
  145. \retval none
  146. */
  147. void crc_polynomial_size_set(uint32_t poly_size)
  148. {
  149. CRC_CTL &= (uint32_t)(~(CRC_CTL_PS));
  150. CRC_CTL |= (uint32_t)poly_size;
  151. }
  152. /*!
  153. \brief configure the CRC polynomial value function
  154. \param[in] poly: configurable polynomial value
  155. \param[out] none
  156. \retval none
  157. */
  158. void crc_polynomial_set(uint32_t poly)
  159. {
  160. CRC_POLY &= (uint32_t)(~CRC_POLY_POLY);
  161. CRC_POLY = poly;
  162. }
  163. /*!
  164. \brief CRC calculate single data
  165. \param[in] sdata: specify input data
  166. \param[in] data_format: input data format
  167. only one parameter can be selected which is shown as below:
  168. \arg INPUT_FORMAT_WORD: input data in word format
  169. \arg INPUT_FORMAT_HALFWORD: input data in half-word format
  170. \arg INPUT_FORMAT_BYTE: input data in byte format
  171. \param[out] none
  172. \retval CRC calculate value
  173. */
  174. uint32_t crc_single_data_calculate(uint32_t sdata, uint8_t data_format)
  175. {
  176. if(INPUT_FORMAT_WORD == data_format){
  177. REG32(CRC) = sdata;
  178. }else if(INPUT_FORMAT_HALFWORD == data_format){
  179. REG16(CRC) = (uint16_t)sdata;
  180. }else{
  181. REG8(CRC) = (uint8_t)sdata;
  182. }
  183. return(CRC_DATA);
  184. }
  185. /*!
  186. \brief CRC calculate a data array
  187. \param[in] array: pointer to the input data array
  188. \param[in] size: size of the array
  189. \param[in] data_format: input data format
  190. only one parameter can be selected which is shown as below:
  191. \arg INPUT_FORMAT_WORD: input data in word format
  192. \arg INPUT_FORMAT_HALFWORD: input data in half-word format
  193. \arg INPUT_FORMAT_BYTE: input data in byte format
  194. \param[out] none
  195. \retval CRC calculate value
  196. */
  197. uint32_t crc_block_data_calculate(void *array, uint32_t size, uint8_t data_format)
  198. {
  199. uint8_t *data8;
  200. uint16_t *data16;
  201. uint32_t *data32;
  202. uint32_t index;
  203. if(INPUT_FORMAT_WORD == data_format){
  204. data32 = (uint32_t *)array;
  205. for(index = 0U; index < size; index++){
  206. REG32(CRC) = data32[index];
  207. }
  208. }else if(INPUT_FORMAT_HALFWORD == data_format){
  209. data16 = (uint16_t *)array;
  210. for(index = 0U; index < size; index++){
  211. REG16(CRC) = data16[index];
  212. }
  213. }else{
  214. data8 = (uint8_t *)array;
  215. for(index = 0U; index < size; index++){
  216. REG8(CRC) = data8[index];
  217. }
  218. }
  219. return (CRC_DATA);
  220. }