gd32f10x_crc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. \file gd32f10x_crc.c
  3. \brief CRC driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_crc.h"
  33. #define CRC_DATA_RESET_VALUE ((uint32_t)0xFFFFFFFFU)
  34. #define CRC_FDATA_RESET_VALUE ((uint32_t)0x00000000U)
  35. /*!
  36. \brief deinit CRC calculation unit
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void crc_deinit(void)
  42. {
  43. CRC_DATA = CRC_DATA_RESET_VALUE;
  44. CRC_FDATA = CRC_FDATA_RESET_VALUE;
  45. CRC_CTL = (uint32_t)CRC_CTL_RST;
  46. }
  47. /*!
  48. \brief reset data register to the value of initializaiton data register
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. void crc_data_register_reset(void)
  54. {
  55. CRC_CTL |= (uint32_t)CRC_CTL_RST;
  56. }
  57. /*!
  58. \brief read the value of the data register
  59. \param[in] none
  60. \param[out] none
  61. \retval 32-bit value of the data register
  62. */
  63. uint32_t crc_data_register_read(void)
  64. {
  65. uint32_t data;
  66. data = CRC_DATA;
  67. return (data);
  68. }
  69. /*!
  70. \brief read the value of the free data register
  71. \param[in] none
  72. \param[out] none
  73. \retval 8-bit value of the free data register
  74. */
  75. uint8_t crc_free_data_register_read(void)
  76. {
  77. uint8_t fdata;
  78. fdata = (uint8_t)CRC_FDATA;
  79. return (fdata);
  80. }
  81. /*!
  82. \brief write data to the free data register
  83. \param[in] free_data: specify 8-bit data
  84. \param[out] none
  85. \retval none
  86. */
  87. void crc_free_data_register_write(uint8_t free_data)
  88. {
  89. CRC_FDATA = (uint32_t)free_data;
  90. }
  91. /*!
  92. \brief calculate the CRC value of a 32-bit data
  93. \param[in] sdata: specified 32-bit data
  94. \param[out] none
  95. \retval 32-bit value calculated by CRC
  96. */
  97. uint32_t crc_single_data_calculate(uint32_t sdata)
  98. {
  99. CRC_DATA = sdata;
  100. return (CRC_DATA);
  101. }
  102. /*!
  103. \brief calculate the CRC value of an array of 32-bit values
  104. \param[in] array: pointer to an array of 32-bit values
  105. \param[in] size: size of the array
  106. \param[out] none
  107. \retval 32-bit value calculated by CRC
  108. */
  109. uint32_t crc_block_data_calculate(uint32_t array[], uint32_t size)
  110. {
  111. uint32_t index;
  112. for(index = 0U; index < size; index++){
  113. CRC_DATA = array[index];
  114. }
  115. return (CRC_DATA);
  116. }