|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017-2018 Glenn Ruben Bakke |
| 7 | + * Copyright (c) 2018 Ayke van Laethem |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/mpconfig.h" |
| 29 | + |
| 30 | +#if MICROPY_PY_RANDOM_HW_RNG |
| 31 | + |
| 32 | +#include "nrf_rng.h" |
| 33 | +#include "drivers/rng.h" |
| 34 | + |
| 35 | +#if BLUETOOTH_SD |
| 36 | +#include "nrf_soc.h" |
| 37 | +#include "ble_drv.h" |
| 38 | +#define BLUETOOTH_STACK_ENABLED() (ble_drv_stack_enabled()) |
| 39 | +#endif |
| 40 | + |
| 41 | +static inline uint32_t generate_hw_random(void) { |
| 42 | + uint32_t retval = 0; |
| 43 | + uint8_t * p_retval = (uint8_t *)&retval; |
| 44 | + |
| 45 | + nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); |
| 46 | + nrf_rng_task_trigger(NRF_RNG_TASK_START); |
| 47 | + |
| 48 | + for (uint16_t i = 0; i < 4; i++) { |
| 49 | + while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) { |
| 50 | + ; |
| 51 | + } |
| 52 | + |
| 53 | + nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); |
| 54 | + p_retval[i] = nrf_rng_random_value_get(); |
| 55 | + } |
| 56 | + |
| 57 | + nrf_rng_task_trigger(NRF_RNG_TASK_STOP); |
| 58 | + |
| 59 | + return retval; |
| 60 | +} |
| 61 | + |
| 62 | +uint32_t rng_generate_random_word(void) { |
| 63 | + |
| 64 | +#if BLUETOOTH_SD |
| 65 | + if (BLUETOOTH_STACK_ENABLED() == 1) { |
| 66 | + uint32_t retval = 0; |
| 67 | + uint32_t status; |
| 68 | + do { |
| 69 | + status = sd_rand_application_vector_get((uint8_t *)&retval, 4); // Extract 4 bytes |
| 70 | + } while (status != 0); |
| 71 | + |
| 72 | + return retval; |
| 73 | + } |
| 74 | +#endif |
| 75 | + |
| 76 | + return generate_hw_random(); |
| 77 | +} |
| 78 | + |
| 79 | +#endif // MICROPY_PY_RANDOM_HW_RNG |
0 commit comments