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