Skip to content

Commit

Permalink
nrf: Remove custom "random" module and use extmod version instead.
Browse files Browse the repository at this point in the history
Hardware RNG code is moved to drivers/rng.[ch].
  • Loading branch information
dpgeorge committed Feb 18, 2020
1 parent 4f3e5ea commit 6ad3bb1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 241 deletions.
3 changes: 1 addition & 2 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions ports/nrf/drivers/rng.c
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
* THE SOFTWARE.
*/

uint32_t machine_rng_generate_random_word(void);
uint32_t rng_generate_random_word(void);
223 changes: 0 additions & 223 deletions ports/nrf/modules/random/modrandom.c

This file was deleted.

4 changes: 2 additions & 2 deletions ports/nrf/modules/uos/microbitfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/modules/uos/moduos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 6ad3bb1

Please sign in to comment.