nrf_rng.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @file
  14. * @brief RNG HAL API.
  15. */
  16. #ifndef NRF_RNG_H__
  17. #define NRF_RNG_H__
  18. /**
  19. * @defgroup nrf_rng_hal RNG HAL
  20. * @{
  21. * @ingroup nrf_rng
  22. * @brief Hardware access layer for managing the random number generator (RNG).
  23. */
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. #include <stdbool.h>
  27. #include "nrf.h"
  28. #define NRF_RNG_TASK_SET (1UL)
  29. #define NRF_RNG_EVENT_CLEAR (0UL)
  30. /**
  31. * @enum nrf_rng_task_t
  32. * @brief RNG tasks.
  33. */
  34. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  35. {
  36. NRF_RNG_TASK_START = offsetof(NRF_RNG_Type, TASKS_START), /**< Start the random number generator. */
  37. NRF_RNG_TASK_STOP = offsetof(NRF_RNG_Type, TASKS_STOP) /**< Stop the random number generator. */
  38. } nrf_rng_task_t; /*lint -restore */
  39. /**
  40. * @enum nrf_rng_event_t
  41. * @brief RNG events.
  42. */
  43. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  44. {
  45. NRF_RNG_EVENT_VALRDY = offsetof(NRF_RNG_Type, EVENTS_VALRDY) /**< New random number generated event. */
  46. } nrf_rng_event_t; /*lint -restore */
  47. /**
  48. * @enum nrf_rng_int_mask_t
  49. * @brief RNG interrupts.
  50. */
  51. typedef enum
  52. {
  53. NRF_RNG_INT_VALRDY_MASK = RNG_INTENSET_VALRDY_Msk /**< Mask for enabling or disabling an interrupt on VALRDY event. */
  54. } nrf_rng_int_mask_t;
  55. /**
  56. * @enum nrf_rng_short_mask_t
  57. * @brief Types of RNG shortcuts.
  58. */
  59. typedef enum
  60. {
  61. NRF_RNG_SHORT_VALRDY_STOP_MASK = RNG_SHORTS_VALRDY_STOP_Msk /**< Mask for setting shortcut between EVENT_VALRDY and TASK_STOP. */
  62. } nrf_rng_short_mask_t;
  63. /**
  64. * @brief Function for enabling interrupts.
  65. *
  66. * @param[in] rng_int_mask Mask of interrupts.
  67. */
  68. __STATIC_INLINE void nrf_rng_int_enable(uint32_t rng_int_mask)
  69. {
  70. NRF_RNG->INTENSET = rng_int_mask;
  71. }
  72. /**
  73. * @brief Function for disabling interrupts.
  74. *
  75. * @param[in] rng_int_mask Mask of interrupts.
  76. */
  77. __STATIC_INLINE void nrf_rng_int_disable(uint32_t rng_int_mask)
  78. {
  79. NRF_RNG->INTENCLR = rng_int_mask;
  80. }
  81. /**
  82. * @brief Function for getting the state of a specific interrupt.
  83. *
  84. * @param[in] rng_int_mask Interrupt.
  85. *
  86. * @retval true If the interrupt is not enabled.
  87. * @retval false If the interrupt is enabled.
  88. */
  89. __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask)
  90. {
  91. return (bool)(NRF_RNG->INTENCLR & rng_int_mask);
  92. }
  93. /**
  94. * @brief Function for getting the address of a specific task.
  95. *
  96. * This function can be used by the PPI module.
  97. *
  98. * @param[in] rng_task Task.
  99. */
  100. __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task)
  101. {
  102. return (uint32_t *)((uint8_t *)NRF_RNG + rng_task);
  103. }
  104. /**
  105. * @brief Function for setting a specific task.
  106. *
  107. * @param[in] rng_task Task.
  108. */
  109. __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task)
  110. {
  111. *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_task)) = NRF_RNG_TASK_SET;
  112. }
  113. /**
  114. * @brief Function for getting address of a specific event.
  115. *
  116. * This function can be used by the PPI module.
  117. *
  118. * @param[in] rng_event Event.
  119. */
  120. __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event)
  121. {
  122. return (uint32_t *)((uint8_t *)NRF_RNG + rng_event);
  123. }
  124. /**
  125. * @brief Function for clearing a specific event.
  126. *
  127. * @param[in] rng_event Event.
  128. */
  129. __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event)
  130. {
  131. *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event)) = NRF_RNG_EVENT_CLEAR;
  132. }
  133. /**
  134. * @brief Function for getting the state of a specific event.
  135. *
  136. * @param[in] rng_event Event.
  137. *
  138. * @retval true If the event is not set.
  139. * @retval false If the event is set.
  140. */
  141. __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event)
  142. {
  143. return (bool)*((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event));
  144. }
  145. /**
  146. * @brief Function for setting shortcuts.
  147. *
  148. * @param[in] rng_short_mask Mask of shortcuts.
  149. *
  150. */
  151. __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t rng_short_mask)
  152. {
  153. NRF_RNG->SHORTS |= rng_short_mask;
  154. }
  155. /**
  156. * @brief Function for clearing shortcuts.
  157. *
  158. * @param[in] rng_short_mask Mask of shortcuts.
  159. *
  160. */
  161. __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t rng_short_mask)
  162. {
  163. NRF_RNG->SHORTS &= ~rng_short_mask;
  164. }
  165. /**
  166. * @brief Function for getting the previously generated random value.
  167. *
  168. * @return Previously generated random value.
  169. */
  170. __STATIC_INLINE uint8_t nrf_rng_random_value_get(void)
  171. {
  172. return (uint8_t)(NRF_RNG->VALUE & RNG_VALUE_VALUE_Msk);
  173. }
  174. /**
  175. * @brief Function for enabling digital error correction.
  176. */
  177. __STATIC_INLINE void nrf_rng_error_correction_enable(void)
  178. {
  179. NRF_RNG->CONFIG |= RNG_CONFIG_DERCEN_Msk;
  180. }
  181. /**
  182. * @brief Function for disabling digital error correction.
  183. */
  184. __STATIC_INLINE void nrf_rng_error_correction_disable(void)
  185. {
  186. NRF_RNG->CONFIG &= ~RNG_CONFIG_DERCEN_Msk;
  187. }
  188. /**
  189. *@}
  190. **/
  191. #endif /* NRF_RNG_H__ */