nrf_rng.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 for managing the Random Number Generator (RNG) peripheral.
  51. */
  52. #define NRF_RNG_TASK_SET (1UL)
  53. #define NRF_RNG_EVENT_CLEAR (0UL)
  54. /**
  55. * @enum nrf_rng_task_t
  56. * @brief RNG tasks.
  57. */
  58. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  59. {
  60. NRF_RNG_TASK_START = offsetof(NRF_RNG_Type, TASKS_START), /**< Start the random number generator. */
  61. NRF_RNG_TASK_STOP = offsetof(NRF_RNG_Type, TASKS_STOP) /**< Stop the random number generator. */
  62. } nrf_rng_task_t; /*lint -restore */
  63. /**
  64. * @enum nrf_rng_event_t
  65. * @brief RNG events.
  66. */
  67. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  68. {
  69. NRF_RNG_EVENT_VALRDY = offsetof(NRF_RNG_Type, EVENTS_VALRDY) /**< New random number generated event. */
  70. } nrf_rng_event_t; /*lint -restore */
  71. /**
  72. * @enum nrf_rng_int_mask_t
  73. * @brief RNG interrupts.
  74. */
  75. typedef enum
  76. {
  77. NRF_RNG_INT_VALRDY_MASK = RNG_INTENSET_VALRDY_Msk /**< Mask for enabling or disabling an interrupt on VALRDY event. */
  78. } nrf_rng_int_mask_t;
  79. /**
  80. * @enum nrf_rng_short_mask_t
  81. * @brief Types of RNG shortcuts.
  82. */
  83. typedef enum
  84. {
  85. NRF_RNG_SHORT_VALRDY_STOP_MASK = RNG_SHORTS_VALRDY_STOP_Msk /**< Mask for setting shortcut between EVENT_VALRDY and TASK_STOP. */
  86. } nrf_rng_short_mask_t;
  87. /**
  88. * @brief Function for enabling interrupts.
  89. *
  90. * @param[in] rng_int_mask Mask of interrupts.
  91. */
  92. __STATIC_INLINE void nrf_rng_int_enable(uint32_t rng_int_mask);
  93. /**
  94. * @brief Function for disabling interrupts.
  95. *
  96. * @param[in] rng_int_mask Mask of interrupts.
  97. */
  98. __STATIC_INLINE void nrf_rng_int_disable(uint32_t rng_int_mask);
  99. /**
  100. * @brief Function for getting the state of a specific interrupt.
  101. *
  102. * @param[in] rng_int_mask Interrupt.
  103. *
  104. * @retval true If the interrupt is not enabled.
  105. * @retval false If the interrupt is enabled.
  106. */
  107. __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask);
  108. /**
  109. * @brief Function for getting the address of a specific task.
  110. *
  111. * This function can be used by the PPI module.
  112. *
  113. * @param[in] rng_task Task.
  114. */
  115. __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task);
  116. /**
  117. * @brief Function for setting a specific task.
  118. *
  119. * @param[in] rng_task Task.
  120. */
  121. __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task);
  122. /**
  123. * @brief Function for getting address of a specific event.
  124. *
  125. * This function can be used by the PPI module.
  126. *
  127. * @param[in] rng_event Event.
  128. */
  129. __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event);
  130. /**
  131. * @brief Function for clearing a specific event.
  132. *
  133. * @param[in] rng_event Event.
  134. */
  135. __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event);
  136. /**
  137. * @brief Function for getting the state of a specific event.
  138. *
  139. * @param[in] rng_event Event.
  140. *
  141. * @retval true If the event is not set.
  142. * @retval false If the event is set.
  143. */
  144. __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event);
  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. * @brief Function for clearing shortcuts.
  154. *
  155. * @param[in] rng_short_mask Mask of shortcuts.
  156. *
  157. */
  158. __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t rng_short_mask);
  159. /**
  160. * @brief Function for getting the previously generated random value.
  161. *
  162. * @return Previously generated random value.
  163. */
  164. __STATIC_INLINE uint8_t nrf_rng_random_value_get(void);
  165. /**
  166. * @brief Function for enabling digital error correction.
  167. */
  168. __STATIC_INLINE void nrf_rng_error_correction_enable(void);
  169. /**
  170. * @brief Function for disabling digital error correction.
  171. */
  172. __STATIC_INLINE void nrf_rng_error_correction_disable(void);
  173. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  174. __STATIC_INLINE void nrf_rng_int_enable(uint32_t rng_int_mask)
  175. {
  176. NRF_RNG->INTENSET = rng_int_mask;
  177. }
  178. __STATIC_INLINE void nrf_rng_int_disable(uint32_t rng_int_mask)
  179. {
  180. NRF_RNG->INTENCLR = rng_int_mask;
  181. }
  182. __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask)
  183. {
  184. return (bool)(NRF_RNG->INTENCLR & rng_int_mask);
  185. }
  186. __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task)
  187. {
  188. return (uint32_t *)((uint8_t *)NRF_RNG + rng_task);
  189. }
  190. __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task)
  191. {
  192. *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_task)) = NRF_RNG_TASK_SET;
  193. }
  194. __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event)
  195. {
  196. return (uint32_t *)((uint8_t *)NRF_RNG + rng_event);
  197. }
  198. __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event)
  199. {
  200. *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event)) = NRF_RNG_EVENT_CLEAR;
  201. #if __CORTEX_M == 0x04
  202. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event));
  203. (void)dummy;
  204. #endif
  205. }
  206. __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event)
  207. {
  208. return (bool) * ((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event));
  209. }
  210. __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t rng_short_mask)
  211. {
  212. NRF_RNG->SHORTS |= rng_short_mask;
  213. }
  214. __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t rng_short_mask)
  215. {
  216. NRF_RNG->SHORTS &= ~rng_short_mask;
  217. }
  218. __STATIC_INLINE uint8_t nrf_rng_random_value_get(void)
  219. {
  220. return (uint8_t)(NRF_RNG->VALUE & RNG_VALUE_VALUE_Msk);
  221. }
  222. __STATIC_INLINE void nrf_rng_error_correction_enable(void)
  223. {
  224. NRF_RNG->CONFIG |= RNG_CONFIG_DERCEN_Msk;
  225. }
  226. __STATIC_INLINE void nrf_rng_error_correction_disable(void)
  227. {
  228. NRF_RNG->CONFIG &= ~RNG_CONFIG_DERCEN_Msk;
  229. }
  230. #endif
  231. /** @} */
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif /* NRF_RNG_H__ */