gd32f1x0_crc.c 5.2 KB

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