nrf_rng.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_RNG_H__
  41. #define NRF_RNG_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_rng_hal RNG HAL
  48. * @{
  49. * @ingroup nrf_rng
  50. * @brief Hardware access layer (HAL) for managing the Random Number Generator (RNG) peripheral.
  51. */
  52. /** @brief RNG tasks. */
  53. typedef enum
  54. {
  55. NRF_RNG_TASK_START = offsetof(NRF_RNG_Type, TASKS_START), /**< Start the random number generator. */
  56. NRF_RNG_TASK_STOP = offsetof(NRF_RNG_Type, TASKS_STOP) /**< Stop the random number generator. */
  57. } nrf_rng_task_t;
  58. /** @brief RNG events. */
  59. typedef enum
  60. {
  61. NRF_RNG_EVENT_VALRDY = offsetof(NRF_RNG_Type, EVENTS_VALRDY) /**< New random number generated event. */
  62. } nrf_rng_event_t;
  63. /** @brief RNG interrupts. */
  64. typedef enum
  65. {
  66. NRF_RNG_INT_VALRDY_MASK = RNG_INTENSET_VALRDY_Msk /**< Mask for enabling or disabling an interrupt on VALRDY event. */
  67. } nrf_rng_int_mask_t;
  68. /** @brief Types of RNG shortcuts. */
  69. typedef enum
  70. {
  71. NRF_RNG_SHORT_VALRDY_STOP_MASK = RNG_SHORTS_VALRDY_STOP_Msk /**< Mask for setting shortcut between EVENT_VALRDY and TASK_STOP. */
  72. } nrf_rng_short_mask_t;
  73. /**
  74. * @brief Function for enabling interrupts.
  75. *
  76. * @param[in] mask Mask of interrupts to be enabled.
  77. */
  78. __STATIC_INLINE void nrf_rng_int_enable(uint32_t mask);
  79. /**
  80. * @brief Function for disabling interrupts.
  81. *
  82. * @param[in] mask Mask of interrupts to be disabled.
  83. */
  84. __STATIC_INLINE void nrf_rng_int_disable(uint32_t mask);
  85. /**
  86. * @brief Function for getting the state of the specified interrupt.
  87. *
  88. * @param[in] mask Interrupts to be checked.
  89. *
  90. * @retval true The interrupt is not enabled.
  91. * @retval false The interrupt is enabled.
  92. */
  93. __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t mask);
  94. /**
  95. * @brief Function for getting the address of the specified task.
  96. *
  97. * This function can be used by the PPI module.
  98. *
  99. * @param[in] rng_task The specified task.
  100. *
  101. * @return Address of the specified task.
  102. */
  103. __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task);
  104. /**
  105. * @brief Function for triggering the specified task.
  106. *
  107. * @param[in] rng_task The specified Task.
  108. */
  109. __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task);
  110. /**
  111. * @brief Function for getting address of the specified event.
  112. *
  113. * This function can be used by the PPI module.
  114. *
  115. * @param[in] rng_event The specified event.
  116. *
  117. * @return Address of the specified event.
  118. */
  119. __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event);
  120. /**
  121. * @brief Function for clearing the specified event.
  122. *
  123. * @param[in] rng_event The specified event.
  124. */
  125. __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event);
  126. /**
  127. * @brief Function for getting the state of the specified event.
  128. *
  129. * @param[in] rng_event The specified event.
  130. *
  131. * @retval true The event is not set.
  132. * @retval false The event is set.
  133. */
  134. __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event);
  135. /**
  136. * @brief Function for setting shortcuts.
  137. *
  138. * @param[in] mask Mask of shortcuts.
  139. */
  140. __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t mask);
  141. /**
  142. * @brief Function for clearing shortcuts.
  143. *
  144. * @param[in] mask Mask of shortcuts.
  145. */
  146. __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t mask);
  147. /**
  148. * @brief Function for getting the previously generated random value.
  149. *
  150. * @return Previously generated random value.
  151. */
  152. __STATIC_INLINE uint8_t nrf_rng_random_value_get(void);
  153. /** @brief Function for enabling digital error correction. */
  154. __STATIC_INLINE void nrf_rng_error_correction_enable(void);
  155. /** @brief Function for disabling digital error correction. */
  156. __STATIC_INLINE void nrf_rng_error_correction_disable(void);
  157. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  158. __STATIC_INLINE void nrf_rng_int_enable(uint32_t mask)
  159. {
  160. NRF_RNG->INTENSET = mask;
  161. }
  162. __STATIC_INLINE void nrf_rng_int_disable(uint32_t mask)
  163. {
  164. NRF_RNG->INTENCLR = mask;
  165. }
  166. __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t mask)
  167. {
  168. return (bool)(NRF_RNG->INTENCLR & mask);
  169. }
  170. __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task)
  171. {
  172. return (uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_task);
  173. }
  174. __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task)
  175. {
  176. *((volatile uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_task)) = 0x1UL;
  177. }
  178. __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event)
  179. {
  180. return (uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_event);
  181. }
  182. __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event)
  183. {
  184. *((volatile uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_event)) = 0x0UL;
  185. #if __CORTEX_M == 0x04
  186. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_event));
  187. (void)dummy;
  188. #endif
  189. }
  190. __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event)
  191. {
  192. return (bool) * ((volatile uint32_t *)((uint8_t *)NRF_RNG + (uint32_t)rng_event));
  193. }
  194. __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t mask)
  195. {
  196. NRF_RNG->SHORTS |= mask;
  197. }
  198. __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t mask)
  199. {
  200. NRF_RNG->SHORTS &= ~mask;
  201. }
  202. __STATIC_INLINE uint8_t nrf_rng_random_value_get(void)
  203. {
  204. return (uint8_t)(NRF_RNG->VALUE & RNG_VALUE_VALUE_Msk);
  205. }
  206. __STATIC_INLINE void nrf_rng_error_correction_enable(void)
  207. {
  208. NRF_RNG->CONFIG |= RNG_CONFIG_DERCEN_Msk;
  209. }
  210. __STATIC_INLINE void nrf_rng_error_correction_disable(void)
  211. {
  212. NRF_RNG->CONFIG &= ~RNG_CONFIG_DERCEN_Msk;
  213. }
  214. #endif
  215. /** @} */
  216. #ifdef __cplusplus
  217. }
  218. #endif
  219. #endif /* NRF_RNG_H__ */