ald_trng.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_trng.c
  5. * @brief TRNG module driver.
  6. *
  7. * @version V1.0
  8. * @date 04 Dec 2017
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #include "ald_trng.h"
  17. /** @addtogroup ES32FXXX_ALD
  18. * @{
  19. */
  20. /** @defgroup TRNG TRNG
  21. * @brief TRNG module driver
  22. * @{
  23. */
  24. #ifdef ALD_TRNG
  25. /** @defgroup TRNG_Public_Functions TRNG Public Functions
  26. * @{
  27. */
  28. /** @addtogroup TRNG_Public_Functions_Group1 Initialization functions
  29. * @brief Initialization functions
  30. *
  31. * @verbatim
  32. ==============================================================================
  33. ##### Initialization functions #####
  34. ==============================================================================
  35. [..] This section provides functions allowing to initialize the TRNG:
  36. (+) This parameters can be configured:
  37. (++) Word Width
  38. (++) Seed Type
  39. (++) Seed
  40. (++) Start Time
  41. (++) Adjust parameter
  42. @endverbatim
  43. * @{
  44. */
  45. /**
  46. * @brief Initializes the TRNG according to the specified
  47. * parameters in the trng_init_t.
  48. * @param init: Pointer to a trng_init_t structure that contains
  49. * the configuration information.
  50. * @retval None
  51. */
  52. void trng_init(trng_init_t *init)
  53. {
  54. assert_param(IS_TRNG_DATA_WIDTH(init->data_width));
  55. assert_param(IS_TRNG_SEED_TYPE(init->seed_type));
  56. assert_param(IS_TRNG_ADJC(init->adjc));
  57. SET_BIT(TRNG->CR, TRNG_CR_TRNGSEL_MSK);
  58. MODIFY_REG(TRNG->CR, TRNG_CR_DSEL_MSK, (init->data_width) << TRNG_CR_DSEL_POSS);
  59. MODIFY_REG(TRNG->CR, TRNG_CR_SDSEL_MSK, (init->seed_type) << TRNG_CR_SDSEL_POSS);
  60. MODIFY_REG(TRNG->CR, TRNG_CR_ADJC_MSK, (init->adjc) << TRNG_CR_ADJC_POSS);
  61. if (init->adjc == 0) {
  62. MODIFY_REG(TRNG->CR, TRNG_CR_ADJC_MSK, (0) << TRNG_CR_ADJC_POSS);
  63. }
  64. else {
  65. MODIFY_REG(TRNG->CR, TRNG_CR_ADJC_MSK, (1) << TRNG_CR_ADJC_POSS);
  66. }
  67. WRITE_REG(TRNG->SEED, init->seed);
  68. MODIFY_REG(TRNG->CFGR, TRNG_CFGR_TSTART_MSK, (init->t_start) << TRNG_CFGR_TSTART_POSS);
  69. MODIFY_REG(TRNG->CR, TRNG_CR_POSTEN_MSK, (init->posten) << TRNG_CR_POSTEN_MSK);
  70. return;
  71. }
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup TRNG_Public_Functions_Group2 Peripheral Control functions
  76. * @brief Peripheral Control functions
  77. *
  78. * @verbatim
  79. ==============================================================================
  80. ##### Peripheral Control functions #####
  81. ==============================================================================
  82. [..] This section provides functions allowing to:
  83. (+) trng_get_result() API can Get the result.
  84. (+) trng_interrupt_config() API can be helpful to configure TRNG interrupt source.
  85. (+) trng_get_it_status() API can get the status of interrupt source.
  86. (+) trng_get_status() API can get the status of SR register.
  87. (+) trng_get_flag_status() API can get the status of interrupt flag.
  88. (+) trng_clear_flag_status() API can clear interrupt flag.
  89. @endverbatim
  90. * @{
  91. */
  92. /**
  93. * @brief Get the result.
  94. * @retval The resultl
  95. */
  96. uint32_t trng_get_result(void)
  97. {
  98. return (uint32_t)TRNG->DR;
  99. }
  100. /**
  101. * @brief Enable/disable the specified interrupts.
  102. * @param it: Specifies the interrupt sources to be enabled or disabled.
  103. * This parameter can be one of the @ref trng_it_t.
  104. * @param state: New state of the specified interrupts.
  105. * This parameter can be:
  106. * @arg ENABLE
  107. * @arg DISABLE
  108. * @retval None
  109. */
  110. void trng_interrupt_config(trng_it_t it, type_func_t state)
  111. {
  112. assert_param(IS_TRNG_IT(it));
  113. assert_param(IS_FUNC_STATE(state));
  114. if (state)
  115. SET_BIT(TRNG->IER, it);
  116. else
  117. CLEAR_BIT(TRNG->IER, it);
  118. return;
  119. }
  120. /**
  121. * @brief Get the status of SR register.
  122. * @param status: Specifies the TRNG status type.
  123. * This parameter can be one of the @ref trng_status_t.
  124. * @retval Status:
  125. * - 0: RESET
  126. * - 1: SET
  127. */
  128. flag_status_t trng_get_status(trng_status_t status)
  129. {
  130. assert_param(IS_TRNG_STATUS(status));
  131. if (READ_BIT(TRNG->SR, status))
  132. return SET;
  133. return RESET;
  134. }
  135. /**
  136. * @brief Get the status of interrupt source.
  137. * @param it: Specifies the interrupt source.
  138. * This parameter can be one of the @ref trng_it_t.
  139. * @retval Status:
  140. * - 0: RESET
  141. * - 1: SET
  142. */
  143. it_status_t trng_get_it_status(trng_it_t it)
  144. {
  145. assert_param(IS_TRNG_IT(it));
  146. if (READ_BIT(TRNG->IER, it))
  147. return SET;
  148. return RESET;
  149. }
  150. /**
  151. * @brief Get the status of interrupt flag.
  152. * @param flag: Specifies the interrupt flag.
  153. * This parameter can be one of the @ref trng_flag_t.
  154. * @retval Status:
  155. * - 0: RESET
  156. * - 1: SET
  157. */
  158. flag_status_t trng_get_flag_status(trng_flag_t flag)
  159. {
  160. assert_param(IS_TRNG_FLAG(flag));
  161. if (READ_BIT(TRNG->IFR, flag))
  162. return SET;
  163. return RESET;
  164. }
  165. /**
  166. * @brief Clear the interrupt flag.
  167. * @param flag: Specifies the interrupt flag.
  168. * This parameter can be one of the @ref trng_flag_t.
  169. * @retval None
  170. */
  171. void trng_clear_flag_status(trng_flag_t flag)
  172. {
  173. assert_param(IS_TRNG_FLAG(flag));
  174. WRITE_REG(TRNG->IFCR, flag);
  175. return;
  176. }
  177. /**
  178. * @}
  179. */
  180. /**
  181. * @}
  182. */
  183. #endif /* ALD_TRNG */
  184. /**
  185. * @}
  186. */
  187. /**
  188. * @}
  189. */