gd32f30x_crc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. \file gd32f30x_crc.c
  3. \brief CRC driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, 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 "gd32f30x_crc.h"
  30. #define CRC_DATA_RESET_VALUE ((uint32_t)0xFFFFFFFFU)
  31. #define CRC_FDATA_RESET_VALUE ((uint32_t)0x00000000U)
  32. /*!
  33. \brief deinit CRC calculation unit
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void crc_deinit(void)
  39. {
  40. CRC_DATA = CRC_DATA_RESET_VALUE;
  41. CRC_FDATA = CRC_FDATA_RESET_VALUE;
  42. CRC_CTL = (uint32_t)CRC_CTL_RST;
  43. }
  44. /*!
  45. \brief reset data register(CRC_DATA) to the value of 0xFFFFFFFF
  46. \param[in] none
  47. \param[out] none
  48. \retval none
  49. */
  50. void crc_data_register_reset(void)
  51. {
  52. CRC_CTL |= (uint32_t)CRC_CTL_RST;
  53. }
  54. /*!
  55. \brief read the value of the data register
  56. \param[in] none
  57. \param[out] none
  58. \retval 32-bit value of the data register
  59. */
  60. uint32_t crc_data_register_read(void)
  61. {
  62. uint32_t data;
  63. data = CRC_DATA;
  64. return (data);
  65. }
  66. /*!
  67. \brief read the value of the free data register
  68. \param[in] none
  69. \param[out] none
  70. \retval 8-bit value of the free data register
  71. */
  72. uint8_t crc_free_data_register_read(void)
  73. {
  74. uint8_t fdata;
  75. fdata = (uint8_t)CRC_FDATA;
  76. return (fdata);
  77. }
  78. /*!
  79. \brief write data to the free data register
  80. \param[in] free_data: specified 8-bit data
  81. \param[out] none
  82. \retval none
  83. */
  84. void crc_free_data_register_write(uint8_t free_data)
  85. {
  86. CRC_FDATA = (uint32_t)free_data;
  87. }
  88. /*!
  89. \brief calculate the CRC value of a 32-bit data
  90. \param[in] sdata: specified 32-bit data
  91. \param[out] none
  92. \retval 32-bit value calculated by CRC
  93. */
  94. uint32_t crc_single_data_calculate(uint32_t sdata)
  95. {
  96. CRC_DATA = sdata;
  97. return (CRC_DATA);
  98. }
  99. /*!
  100. \brief calculate the CRC value of an array of 32-bit values
  101. \param[in] array: pointer to an array of 32-bit values
  102. \param[in] size: size of the array
  103. \param[out] none
  104. \retval 32-bit value calculated by CRC
  105. */
  106. uint32_t crc_block_data_calculate(const uint32_t *array, uint32_t size)
  107. {
  108. uint32_t index;
  109. for(index = 0U; index < size; index++){
  110. CRC_DATA = *(array+index);
  111. }
  112. return (CRC_DATA);
  113. }