From 6ad3bb1e121128b3ad3a5acacda97459f3a75514 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 Feb 2020 13:06:38 +1100 Subject: [PATCH] nrf: Remove custom "random" module and use extmod version instead. Hardware RNG code is moved to drivers/rng.[ch]. --- ports/nrf/Makefile | 3 +- ports/nrf/drivers/rng.c | 79 +++++++ .../random/modrandom.h => drivers/rng.h} | 2 +- ports/nrf/modules/random/modrandom.c | 223 ------------------ ports/nrf/modules/uos/microbitfs.c | 4 +- ports/nrf/modules/uos/moduos.c | 4 +- ports/nrf/mpconfigport.h | 13 +- 7 files changed, 87 insertions(+), 241 deletions(-) create mode 100644 ports/nrf/drivers/rng.c rename ports/nrf/{modules/random/modrandom.h => drivers/rng.h} (96%) delete mode 100644 ports/nrf/modules/random/modrandom.c diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 836a89c202..04e3b7fee0 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -56,7 +56,6 @@ INC += -I./../../lib/cmsis/inc INC += -I./modules/machine INC += -I./modules/ubluepy INC += -I./modules/music -INC += -I./modules/random INC += -I./modules/ble INC += -I./modules/board INC += -I../../lib/mp-readline @@ -230,6 +229,7 @@ SRC_C += \ pin_named_pins.c \ fatfs_port.c \ drivers/flash.c \ + drivers/rng.c \ drivers/softpwm.c \ drivers/ticker.c \ drivers/bluetooth/ble_drv.c \ @@ -292,7 +292,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\ music/modmusic.c \ music/musictunes.c \ ble/modble.c \ - random/modrandom.c \ ) # Custom micropython startup file with smaller interrupt vector table diff --git a/ports/nrf/drivers/rng.c b/ports/nrf/drivers/rng.c new file mode 100644 index 0000000000..05e9f951f2 --- /dev/null +++ b/ports/nrf/drivers/rng.c @@ -0,0 +1,79 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Glenn Ruben Bakke + * Copyright (c) 2018 Ayke van Laethem + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpconfig.h" + +#if MICROPY_PY_RANDOM_HW_RNG + +#include "nrf_rng.h" +#include "drivers/rng.h" + +#if BLUETOOTH_SD +#include "nrf_soc.h" +#include "ble_drv.h" +#define BLUETOOTH_STACK_ENABLED() (ble_drv_stack_enabled()) +#endif + +static inline uint32_t generate_hw_random(void) { + uint32_t retval = 0; + uint8_t * p_retval = (uint8_t *)&retval; + + nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); + nrf_rng_task_trigger(NRF_RNG_TASK_START); + + for (uint16_t i = 0; i < 4; i++) { + while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) { + ; + } + + nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); + p_retval[i] = nrf_rng_random_value_get(); + } + + nrf_rng_task_trigger(NRF_RNG_TASK_STOP); + + return retval; +} + +uint32_t rng_generate_random_word(void) { + +#if BLUETOOTH_SD + if (BLUETOOTH_STACK_ENABLED() == 1) { + uint32_t retval = 0; + uint32_t status; + do { + status = sd_rand_application_vector_get((uint8_t *)&retval, 4); // Extract 4 bytes + } while (status != 0); + + return retval; + } +#endif + + return generate_hw_random(); +} + +#endif // MICROPY_PY_RANDOM_HW_RNG diff --git a/ports/nrf/modules/random/modrandom.h b/ports/nrf/drivers/rng.h similarity index 96% rename from ports/nrf/modules/random/modrandom.h rename to ports/nrf/drivers/rng.h index 6a6b605c78..2fe49773f8 100644 --- a/ports/nrf/modules/random/modrandom.h +++ b/ports/nrf/drivers/rng.h @@ -24,4 +24,4 @@ * THE SOFTWARE. */ -uint32_t machine_rng_generate_random_word(void); +uint32_t rng_generate_random_word(void); diff --git a/ports/nrf/modules/random/modrandom.c b/ports/nrf/modules/random/modrandom.c deleted file mode 100644 index 702148c5de..0000000000 --- a/ports/nrf/modules/random/modrandom.c +++ /dev/null @@ -1,223 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Paul Sokolovsky - * Copyright (c) 2016 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -#include "py/runtime.h" - -#if MICROPY_PY_RANDOM_HW_RNG - -#include "nrf_rng.h" -#include "modrandom.h" - -#if BLUETOOTH_SD -#include "nrf_soc.h" -#include "ble_drv.h" -#define BLUETOOTH_STACK_ENABLED() (ble_drv_stack_enabled()) -#endif - -static inline uint32_t generate_hw_random(void) { - uint32_t retval = 0; - uint8_t * p_retval = (uint8_t *)&retval; - - nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); - nrf_rng_task_trigger(NRF_RNG_TASK_START); - - for (uint16_t i = 0; i < 4; i++) { - while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) { - ; - } - - nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); - p_retval[i] = nrf_rng_random_value_get(); - } - - nrf_rng_task_trigger(NRF_RNG_TASK_STOP); - - return retval; -} - -uint32_t machine_rng_generate_random_word(void) { - -#if BLUETOOTH_SD - if (BLUETOOTH_STACK_ENABLED() == 1) { - uint32_t retval = 0; - uint32_t status; - do { - status = sd_rand_application_vector_get((uint8_t *)&retval, 4); // Extract 4 bytes - } while (status != 0); - - return retval; - } -#endif - - return generate_hw_random(); -} - -static inline int rand30(void) { - uint32_t val = machine_rng_generate_random_word(); - return (val & 0x3fffffff); // binary mask b00111111111111111111111111111111 -} - -static inline int randbelow(int n) { - return rand30() % n; -} - -STATIC mp_obj_t mod_random_getrandbits(mp_obj_t num_in) { - int n = mp_obj_get_int(num_in); - if (n > 30 || n == 0) { - mp_raise_ValueError(NULL); - } - uint32_t mask = ~0; - // Beware of C undefined behavior when shifting by >= than bit size - mask >>= (32 - n); - return mp_obj_new_int_from_uint(rand30() & mask); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_getrandbits_obj, mod_random_getrandbits); - -STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) { - mp_int_t start = mp_obj_get_int(args[0]); - if (n_args == 1) { - // range(stop) - if (start > 0) { - return mp_obj_new_int(randbelow(start)); - } else { - mp_raise_ValueError(NULL); - } - } else { - mp_int_t stop = mp_obj_get_int(args[1]); - if (n_args == 2) { - // range(start, stop) - if (start < stop) { - return mp_obj_new_int(start + randbelow(stop - start)); - } else { - mp_raise_ValueError(NULL); - } - } else { - // range(start, stop, step) - mp_int_t step = mp_obj_get_int(args[2]); - mp_int_t n; - if (step > 0) { - n = (stop - start + step - 1) / step; - } else if (step < 0) { - n = (stop - start + step + 1) / step; - } else { - mp_raise_ValueError(NULL); - } - if (n > 0) { - return mp_obj_new_int(start + step * randbelow(n)); - } else { - mp_raise_ValueError(NULL); - } - } - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_random_randrange_obj, 1, 3, mod_random_randrange); - -STATIC mp_obj_t mod_random_randint(mp_obj_t a_in, mp_obj_t b_in) { - mp_int_t a = mp_obj_get_int(a_in); - mp_int_t b = mp_obj_get_int(b_in); - if (a <= b) { - return mp_obj_new_int(a + randbelow(b - a + 1)); - } else { - mp_raise_ValueError(NULL); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_randint_obj, mod_random_randint); - -STATIC mp_obj_t mod_random_choice(mp_obj_t seq) { - mp_int_t len = mp_obj_get_int(mp_obj_len(seq)); - if (len > 0) { - return mp_obj_subscr(seq, mp_obj_new_int(randbelow(len)), MP_OBJ_SENTINEL); - } else { - mp_raise_type(&mp_type_IndexError); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_choice_obj, mod_random_choice); - -#if MICROPY_PY_BUILTINS_FLOAT - -// returns a number in the range [0..1) using RNG to fill in the fraction bits -STATIC mp_float_t randfloat(void) { - #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE - typedef uint64_t mp_float_int_t; - #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT - typedef uint32_t mp_float_int_t; - #endif - union { - mp_float_t f; - #if MP_ENDIANNESS_LITTLE - struct { mp_float_int_t frc:MP_FLOAT_FRAC_BITS, exp:MP_FLOAT_EXP_BITS, sgn:1; } p; - #else - struct { mp_float_int_t sgn:1, exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p; - #endif - } u; - u.p.sgn = 0; - u.p.exp = (1 << (MP_FLOAT_EXP_BITS - 1)) - 1; - if (MP_FLOAT_FRAC_BITS <= 30) { - u.p.frc = rand30(); - } else { - u.p.frc = ((uint64_t)rand30() << 30) | (uint64_t)rand30(); - } - return u.f - 1; -} - -STATIC mp_obj_t mod_random_random(void) { - return mp_obj_new_float(randfloat()); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_random_random_obj, mod_random_random); - -STATIC mp_obj_t mod_random_uniform(mp_obj_t a_in, mp_obj_t b_in) { - mp_float_t a = mp_obj_get_float(a_in); - mp_float_t b = mp_obj_get_float(b_in); - return mp_obj_new_float(a + (b - a) * randfloat()); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_uniform_obj, mod_random_uniform); - -#endif - -STATIC const mp_rom_map_elem_t mp_module_random_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) }, - { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_random_getrandbits_obj) }, - { MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&mod_random_randrange_obj) }, - { MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&mod_random_randint_obj) }, - { MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&mod_random_choice_obj) }, -#if MICROPY_PY_BUILTINS_FLOAT - { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_random_random_obj) }, - { MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_random_uniform_obj) }, -#endif -}; - -STATIC MP_DEFINE_CONST_DICT(mp_module_random_globals, mp_module_random_globals_table); - -const mp_obj_module_t random_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&mp_module_random_globals, -}; - -#endif // MICROPY_PY_RANDOM_HW_RNG diff --git a/ports/nrf/modules/uos/microbitfs.c b/ports/nrf/modules/uos/microbitfs.c index 2ddf653cd7..776358ea35 100644 --- a/ports/nrf/modules/uos/microbitfs.c +++ b/ports/nrf/modules/uos/microbitfs.c @@ -31,7 +31,7 @@ #include "microbitfs.h" #include "drivers/flash.h" -#include "modrandom.h" +#include "drivers/rng.h" #include "py/obj.h" #include "py/stream.h" #include "py/runtime.h" @@ -175,7 +175,7 @@ STATIC void init_limits(void) { } STATIC void randomise_start_index(void) { - start_index = machine_rng_generate_random_word() % chunks_in_file_system + 1; + start_index = rng_generate_random_word() % chunks_in_file_system + 1; } void microbit_filesystem_init(void) { diff --git a/ports/nrf/modules/uos/moduos.c b/ports/nrf/modules/uos/moduos.c index 03a8dba069..64119b371b 100644 --- a/ports/nrf/modules/uos/moduos.c +++ b/ports/nrf/modules/uos/moduos.c @@ -42,7 +42,7 @@ //#include "portmodules.h" #if MICROPY_HW_ENABLE_RNG -#include "modrandom.h" +#include "drivers/rng.h" #endif // MICROPY_HW_ENABLE_RNG /// \module os - basic "operating system" services @@ -105,7 +105,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { vstr_t vstr; vstr_init_len(&vstr, n); for (int i = 0; i < n; i++) { - vstr.buf[i] = (uint8_t)(machine_rng_generate_random_word() & 0xFF); + vstr.buf[i] = (uint8_t)(rng_generate_random_word() & 0xFF); } return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 281d8111cf..b8467f5071 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -118,8 +118,8 @@ #define MICROPY_PY_IO_FILEIO (0) #define MICROPY_PY_UERRNO (0) #define MICROPY_PY_UBINASCII (0) -#define MICROPY_PY_URANDOM (0) -#define MICROPY_PY_URANDOM_EXTRA_FUNCS (0) +#define MICROPY_PY_URANDOM (1) +#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1) #define MICROPY_PY_UCTYPES (0) #define MICROPY_PY_UZLIB (0) #define MICROPY_PY_UJSON (0) @@ -218,7 +218,6 @@ extern const struct _mp_obj_module_t mp_module_utime; extern const struct _mp_obj_module_t mp_module_uos; extern const struct _mp_obj_module_t mp_module_ubluepy; extern const struct _mp_obj_module_t music_module; -extern const struct _mp_obj_module_t random_module; #if MICROPY_PY_UBLUEPY #define UBLUEPY_MODULE { MP_ROM_QSTR(MP_QSTR_ubluepy), MP_ROM_PTR(&mp_module_ubluepy) }, @@ -232,12 +231,6 @@ extern const struct _mp_obj_module_t random_module; #define MUSIC_MODULE #endif -#if MICROPY_PY_RANDOM_HW_RNG -#define RANDOM_MODULE { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&random_module) }, -#else -#define RANDOM_MODULE -#endif - #if BOARD_SPECIFIC_MODULES #include "boardmodules.h" #define MICROPY_BOARD_BUILTINS BOARD_MODULES @@ -263,7 +256,6 @@ extern const struct _mp_obj_module_t ble_module; BLE_MODULE \ MUSIC_MODULE \ UBLUEPY_MODULE \ - RANDOM_MODULE \ MICROPY_BOARD_BUILTINS \ @@ -275,7 +267,6 @@ extern const struct _mp_obj_module_t ble_module; { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \ { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos) }, \ MUSIC_MODULE \ - RANDOM_MODULE \ MICROPY_BOARD_BUILTINS \