gd32e23x_crc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!
  2. \file gd32e23x_crc.c
  3. \brief CRC driver
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2024, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32e23x_crc.h"
  30. /*!
  31. \brief deinit CRC calculation unit
  32. \param[in] none
  33. \param[out] none
  34. \retval none
  35. */
  36. void crc_deinit(void)
  37. {
  38. CRC_IDATA = (uint32_t)0xFFFFFFFFU;
  39. CRC_DATA = (uint32_t)0xFFFFFFFFU;
  40. CRC_FDATA = (uint32_t)0x00000000U;
  41. CRC_POLY = (uint32_t)0x04C11DB7U;
  42. CRC_CTL = CRC_CTL_RST;
  43. }
  44. /*!
  45. \brief enable the reverse operation of output data
  46. \param[in] none
  47. \param[out] none
  48. \retval none
  49. */
  50. void crc_reverse_output_data_enable(void)
  51. {
  52. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  53. CRC_CTL |= (uint32_t)CRC_CTL_REV_O;
  54. }
  55. /*!
  56. \brief disable the reverse operation of output data
  57. \param[in] none
  58. \param[out] none
  59. \retval none
  60. */
  61. void crc_reverse_output_data_disable(void)
  62. {
  63. CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
  64. }
  65. /*!
  66. \brief reset data register to the value of initializaiton data register
  67. \param[in] none
  68. \param[out] none
  69. \retval none
  70. */
  71. void crc_data_register_reset(void)
  72. {
  73. CRC_CTL |= (uint32_t)CRC_CTL_RST;
  74. }
  75. /*!
  76. \brief read the data register
  77. \param[in] none
  78. \param[out] none
  79. \retval 32-bit value of the data register
  80. */
  81. uint32_t crc_data_register_read(void)
  82. {
  83. uint32_t data;
  84. data = CRC_DATA;
  85. return (data);
  86. }
  87. /*!
  88. \brief read the free data register
  89. \param[in] none
  90. \param[out] none
  91. \retval 8-bit value of the free data register
  92. */
  93. uint8_t crc_free_data_register_read(void)
  94. {
  95. uint8_t fdata;
  96. fdata = (uint8_t)CRC_FDATA;
  97. return (fdata);
  98. }
  99. /*!
  100. \brief write the free data register
  101. \param[in] free_data: specify 8-bit data
  102. \param[out] none
  103. \retval none
  104. */
  105. void crc_free_data_register_write(uint8_t free_data)
  106. {
  107. CRC_FDATA = (uint32_t)free_data;
  108. }
  109. /*!
  110. \brief write the initializaiton data register
  111. \param[in] init_data:specify 32-bit data
  112. \param[out] none
  113. \retval none
  114. */
  115. void crc_init_data_register_write(uint32_t init_data)
  116. {
  117. CRC_IDATA = (uint32_t)init_data;
  118. }
  119. /*!
  120. \brief configure the CRC input data function
  121. \param[in] data_reverse: specify input data reverse function
  122. only one parameter can be selected which is shown as below:
  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. only one parameter can be selected which is shown as below:
  139. \arg CRC_CTL_PS_32: 32-bit polynomial for CRC calculation
  140. \arg CRC_CTL_PS_16: 16-bit polynomial for CRC calculation
  141. \arg CRC_CTL_PS_8: 8-bit polynomial for CRC calculation
  142. \arg CRC_CTL_PS_7: 7-bit polynomial for CRC calculation
  143. \param[out] none
  144. \retval none
  145. */
  146. void crc_polynomial_size_set(uint32_t poly_size)
  147. {
  148. CRC_CTL &= (uint32_t)(~(CRC_CTL_PS));
  149. CRC_CTL |= (uint32_t)poly_size;
  150. }
  151. /*!
  152. \brief configure the CRC polynomial value function
  153. \param[in] poly: configurable polynomial value
  154. \param[out] none
  155. \retval none
  156. */
  157. void crc_polynomial_set(uint32_t poly)
  158. {
  159. CRC_POLY &= (uint32_t)(~CRC_POLY_POLY);
  160. CRC_POLY = poly;
  161. }
  162. /*!
  163. \brief CRC calculate single data
  164. \param[in] sdata: specify input data
  165. \param[in] data_format: input data format
  166. only one parameter can be selected which is shown as below:
  167. \arg INPUT_FORMAT_WORD: input data in word format
  168. \arg INPUT_FORMAT_HALFWORD: input data in half-word format
  169. \arg INPUT_FORMAT_BYTE: input data in byte format
  170. \param[out] none
  171. \retval CRC calculate value
  172. */
  173. uint32_t crc_single_data_calculate(uint32_t sdata, uint8_t data_format)
  174. {
  175. if(INPUT_FORMAT_WORD == data_format){
  176. REG32(CRC) = sdata;
  177. }else if(INPUT_FORMAT_HALFWORD == data_format){
  178. REG16(CRC) = (uint16_t)sdata;
  179. }else{
  180. REG8(CRC) = (uint8_t)sdata;
  181. }
  182. return(CRC_DATA);
  183. }
  184. /*!
  185. \brief CRC calculate a data array
  186. \param[in] array: pointer to the input data array
  187. \param[in] size: size of the array
  188. \param[in] data_format: input data format
  189. only one parameter can be selected which is shown as below:
  190. \arg INPUT_FORMAT_WORD: input data in word format
  191. \arg INPUT_FORMAT_HALFWORD: input data in half-word format
  192. \arg INPUT_FORMAT_BYTE: input data in byte format
  193. \param[out] none
  194. \retval CRC calculate value
  195. */
  196. uint32_t crc_block_data_calculate(void *array, uint32_t size, uint8_t data_format)
  197. {
  198. uint8_t *data8;
  199. uint16_t *data16;
  200. uint32_t *data32;
  201. uint32_t index;
  202. if(INPUT_FORMAT_WORD == data_format){
  203. data32 = (uint32_t *)array;
  204. for(index = 0U; index < size; index++){
  205. REG32(CRC) = data32[index];
  206. }
  207. }else if(INPUT_FORMAT_HALFWORD == data_format){
  208. data16 = (uint16_t *)array;
  209. for(index = 0U; index < size; index++){
  210. REG16(CRC) = data16[index];
  211. }
  212. }else{
  213. data8 = (uint8_t *)array;
  214. for(index = 0U; index < size; index++){
  215. REG8(CRC) = data8[index];
  216. }
  217. }
  218. return (CRC_DATA);
  219. }