xref: /nrf52832-nimble/nordic/nrfx/hal/nrf_rng.h (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1 /*
2  * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * 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
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_RNG_H__
33 #define NRF_RNG_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_rng_hal RNG HAL
43  * @{
44  * @ingroup nrf_rng
45  * @brief   Hardware access layer for managing the Random Number Generator (RNG) peripheral.
46  */
47 
48 #define NRF_RNG_TASK_SET    (1UL)
49 #define NRF_RNG_EVENT_CLEAR (0UL)
50 /**
51  * @enum nrf_rng_task_t
52  * @brief RNG tasks.
53  */
54 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
55 {
56     NRF_RNG_TASK_START = offsetof(NRF_RNG_Type, TASKS_START), /**< Start the random number generator. */
57     NRF_RNG_TASK_STOP  = offsetof(NRF_RNG_Type, TASKS_STOP)   /**< Stop the random number generator. */
58 } nrf_rng_task_t;                                             /*lint -restore */
59 
60 /**
61  * @enum nrf_rng_event_t
62  * @brief RNG events.
63  */
64 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
65 {
66     NRF_RNG_EVENT_VALRDY = offsetof(NRF_RNG_Type, EVENTS_VALRDY) /**< New random number generated event. */
67 } nrf_rng_event_t;                                               /*lint -restore */
68 
69 /**
70  * @enum nrf_rng_int_mask_t
71  * @brief RNG interrupts.
72  */
73 typedef enum
74 {
75     NRF_RNG_INT_VALRDY_MASK = RNG_INTENSET_VALRDY_Msk /**< Mask for enabling or disabling an interrupt on VALRDY event.  */
76 } nrf_rng_int_mask_t;
77 
78 /**
79  * @enum nrf_rng_short_mask_t
80  * @brief Types of RNG shortcuts.
81  */
82 typedef enum
83 {
84     NRF_RNG_SHORT_VALRDY_STOP_MASK = RNG_SHORTS_VALRDY_STOP_Msk /**<  Mask for setting shortcut between EVENT_VALRDY and TASK_STOP. */
85 } nrf_rng_short_mask_t;
86 
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 /**
95  * @brief Function for disabling interrupts.
96  *
97  * @param[in]  rng_int_mask              Mask of interrupts.
98  */
99 __STATIC_INLINE void nrf_rng_int_disable(uint32_t rng_int_mask);
100 
101 /**
102  * @brief Function for getting the state of a specific interrupt.
103  *
104  * @param[in]  rng_int_mask              Interrupt.
105  *
106  * @retval     true                   If the interrupt is not enabled.
107  * @retval     false                  If the interrupt is enabled.
108  */
109 __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask);
110 
111 /**
112  * @brief Function for getting the address of a specific task.
113  *
114  * This function can be used by the PPI module.
115  *
116  * @param[in]  rng_task              Task.
117  */
118 __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task);
119 
120 /**
121  * @brief Function for setting a specific task.
122  *
123  * @param[in]  rng_task              Task.
124  */
125 __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task);
126 
127 /**
128  * @brief Function for getting address of a specific event.
129  *
130  * This function can be used by the PPI module.
131  *
132  * @param[in]  rng_event              Event.
133  */
134 __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event);
135 
136 /**
137  * @brief Function for clearing a specific event.
138  *
139  * @param[in]  rng_event              Event.
140  */
141 __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event);
142 
143 /**
144  * @brief Function for getting the state of a specific event.
145  *
146  * @param[in]  rng_event              Event.
147  *
148  * @retval     true               If the event is not set.
149  * @retval     false              If the event is set.
150  */
151 __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event);
152 
153 /**
154  * @brief Function for setting shortcuts.
155  *
156  * @param[in]  rng_short_mask              Mask of shortcuts.
157  *
158  */
159 __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t rng_short_mask);
160 
161 /**
162  * @brief Function for clearing shortcuts.
163  *
164  * @param[in]  rng_short_mask              Mask of shortcuts.
165  *
166  */
167 __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t rng_short_mask);
168 
169 /**
170  * @brief Function for getting the previously generated random value.
171  *
172  * @return     Previously generated random value.
173  */
174 __STATIC_INLINE uint8_t nrf_rng_random_value_get(void);
175 
176 /**
177  * @brief Function for enabling digital error correction.
178  */
179 __STATIC_INLINE void nrf_rng_error_correction_enable(void);
180 
181 /**
182  * @brief Function for disabling digital error correction.
183  */
184 __STATIC_INLINE void nrf_rng_error_correction_disable(void);
185 
186 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
187 
nrf_rng_int_enable(uint32_t rng_int_mask)188 __STATIC_INLINE void nrf_rng_int_enable(uint32_t rng_int_mask)
189 {
190     NRF_RNG->INTENSET = rng_int_mask;
191 }
192 
nrf_rng_int_disable(uint32_t rng_int_mask)193 __STATIC_INLINE void nrf_rng_int_disable(uint32_t rng_int_mask)
194 {
195     NRF_RNG->INTENCLR = rng_int_mask;
196 }
197 
nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask)198 __STATIC_INLINE bool nrf_rng_int_get(nrf_rng_int_mask_t rng_int_mask)
199 {
200     return (bool)(NRF_RNG->INTENCLR & rng_int_mask);
201 }
202 
nrf_rng_task_address_get(nrf_rng_task_t rng_task)203 __STATIC_INLINE uint32_t * nrf_rng_task_address_get(nrf_rng_task_t rng_task)
204 {
205     return (uint32_t *)((uint8_t *)NRF_RNG + rng_task);
206 }
207 
nrf_rng_task_trigger(nrf_rng_task_t rng_task)208 __STATIC_INLINE void nrf_rng_task_trigger(nrf_rng_task_t rng_task)
209 {
210     *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_task)) = NRF_RNG_TASK_SET;
211 }
212 
nrf_rng_event_address_get(nrf_rng_event_t rng_event)213 __STATIC_INLINE uint32_t * nrf_rng_event_address_get(nrf_rng_event_t rng_event)
214 {
215     return (uint32_t *)((uint8_t *)NRF_RNG + rng_event);
216 }
217 
nrf_rng_event_clear(nrf_rng_event_t rng_event)218 __STATIC_INLINE void nrf_rng_event_clear(nrf_rng_event_t rng_event)
219 {
220     *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event)) = NRF_RNG_EVENT_CLEAR;
221 #if __CORTEX_M == 0x04
222     volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event));
223     (void)dummy;
224 #endif
225 }
226 
nrf_rng_event_get(nrf_rng_event_t rng_event)227 __STATIC_INLINE bool nrf_rng_event_get(nrf_rng_event_t rng_event)
228 {
229     return (bool) * ((volatile uint32_t *)((uint8_t *)NRF_RNG + rng_event));
230 }
231 
nrf_rng_shorts_enable(uint32_t rng_short_mask)232 __STATIC_INLINE void nrf_rng_shorts_enable(uint32_t rng_short_mask)
233 {
234      NRF_RNG->SHORTS |= rng_short_mask;
235 }
236 
nrf_rng_shorts_disable(uint32_t rng_short_mask)237 __STATIC_INLINE void nrf_rng_shorts_disable(uint32_t rng_short_mask)
238 {
239      NRF_RNG->SHORTS &= ~rng_short_mask;
240 }
241 
nrf_rng_random_value_get(void)242 __STATIC_INLINE uint8_t nrf_rng_random_value_get(void)
243 {
244     return (uint8_t)(NRF_RNG->VALUE & RNG_VALUE_VALUE_Msk);
245 }
246 
nrf_rng_error_correction_enable(void)247 __STATIC_INLINE void nrf_rng_error_correction_enable(void)
248 {
249     NRF_RNG->CONFIG |= RNG_CONFIG_DERCEN_Msk;
250 }
251 
nrf_rng_error_correction_disable(void)252 __STATIC_INLINE void nrf_rng_error_correction_disable(void)
253 {
254     NRF_RNG->CONFIG &= ~RNG_CONFIG_DERCEN_Msk;
255 }
256 
257 #endif
258 
259 /** @} */
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif /* NRF_RNG_H__ */
266