gd32f4xx_trng.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*!
  2. \file gd32f4xx_trng.c
  3. \brief TRNG 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_trng.h"
  32. /*!
  33. \brief deinitialize the TRNG
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void trng_deinit(void)
  39. {
  40. rcu_periph_reset_enable(RCU_TRNGRST);
  41. rcu_periph_reset_disable(RCU_TRNGRST);
  42. }
  43. /*!
  44. \brief enable the TRNG interface
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. void trng_enable(void)
  50. {
  51. TRNG_CTL |= TRNG_CTL_TRNGEN;
  52. }
  53. /*!
  54. \brief disable the TRNG interface
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. void trng_disable(void)
  60. {
  61. TRNG_CTL &= ~TRNG_CTL_TRNGEN;
  62. }
  63. /*!
  64. \brief get the true random data
  65. \param[in] none
  66. \param[out] none
  67. \retval the generated random data
  68. */
  69. uint32_t trng_get_true_random_data(void)
  70. {
  71. return (TRNG_DATA);
  72. }
  73. /*!
  74. \brief enable the TRNG interrupt
  75. \param[in] none
  76. \param[out] none
  77. \retval none
  78. */
  79. void trng_interrupt_enable(void)
  80. {
  81. TRNG_CTL |= TRNG_CTL_IE;
  82. }
  83. /*!
  84. \brief disable the TRNG interrupt
  85. \param[in] none
  86. \param[out] none
  87. \retval none
  88. */
  89. void trng_interrupt_disable(void)
  90. {
  91. TRNG_CTL &= ~TRNG_CTL_IE;
  92. }
  93. /*!
  94. \brief get the trng status flags
  95. \param[in] flag: trng status flag, refer to trng_flag_enum
  96. only one parameter can be selected which is shown as below:
  97. \arg TRNG_FLAG_DRDY: Random Data ready status
  98. \arg TRNG_FLAG_CECS: Clock error current status
  99. \arg TRNG_FLAG_SECS: Seed error current status
  100. \param[out] none
  101. \retval FlagStatus: SET or RESET
  102. */
  103. FlagStatus trng_flag_get(trng_flag_enum flag)
  104. {
  105. if(RESET != (TRNG_STAT & flag)){
  106. return SET;
  107. }else{
  108. return RESET;
  109. }
  110. }
  111. /*!
  112. \brief get the trng interrupt flags
  113. \param[in] int_flag: trng interrupt flag, refer to trng_int_flag_enum
  114. only one parameter can be selected which is shown as below:
  115. \arg TRNG_INT_FLAG_CEIF: clock error interrupt flag
  116. \arg TRNG_INT_FLAG_SEIF: Seed error interrupt flag
  117. \param[out] none
  118. \retval FlagStatus: SET or RESET
  119. */
  120. FlagStatus trng_interrupt_flag_get(trng_int_flag_enum int_flag)
  121. {
  122. if(RESET != (TRNG_STAT & int_flag)){
  123. return SET;
  124. }else{
  125. return RESET;
  126. }
  127. }
  128. /*!
  129. \brief clear the trng interrupt flags
  130. \param[in] int_flag: trng interrupt flag, refer to trng_int_flag_enum
  131. only one parameter can be selected which is shown as below:
  132. \arg TRNG_INT_FLAG_CEIF: clock error interrupt flag
  133. \arg TRNG_INT_FLAG_SEIF: Seed error interrupt flag
  134. \param[out] none
  135. \retval none
  136. */
  137. void trng_interrupt_flag_clear(trng_int_flag_enum int_flag)
  138. {
  139. TRNG_STAT &= ~(uint32_t)int_flag;
  140. }