gd32f4xx_crc.c 3.8 KB

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