Skip to content

Commit 6ad3bb1

Browse files
committed
nrf: Remove custom "random" module and use extmod version instead.
Hardware RNG code is moved to drivers/rng.[ch].
1 parent 4f3e5ea commit 6ad3bb1

File tree

7 files changed

+87
-241
lines changed

7 files changed

+87
-241
lines changed

ports/nrf/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ INC += -I./../../lib/cmsis/inc
5656
INC += -I./modules/machine
5757
INC += -I./modules/ubluepy
5858
INC += -I./modules/music
59-
INC += -I./modules/random
6059
INC += -I./modules/ble
6160
INC += -I./modules/board
6261
INC += -I../../lib/mp-readline
@@ -230,6 +229,7 @@ SRC_C += \
230229
pin_named_pins.c \
231230
fatfs_port.c \
232231
drivers/flash.c \
232+
drivers/rng.c \
233233
drivers/softpwm.c \
234234
drivers/ticker.c \
235235
drivers/bluetooth/ble_drv.c \
@@ -292,7 +292,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
292292
music/modmusic.c \
293293
music/musictunes.c \
294294
ble/modble.c \
295-
random/modrandom.c \
296295
)
297296

298297
# Custom micropython startup file with smaller interrupt vector table

ports/nrf/drivers/rng.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

ports/nrf/modules/random/modrandom.h ports/nrf/drivers/rng.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
uint32_t machine_rng_generate_random_word(void);
27+
uint32_t rng_generate_random_word(void);

ports/nrf/modules/random/modrandom.c

-223
This file was deleted.

ports/nrf/modules/uos/microbitfs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include "microbitfs.h"
3333
#include "drivers/flash.h"
34-
#include "modrandom.h"
34+
#include "drivers/rng.h"
3535
#include "py/obj.h"
3636
#include "py/stream.h"
3737
#include "py/runtime.h"
@@ -175,7 +175,7 @@ STATIC void init_limits(void) {
175175
}
176176

177177
STATIC void randomise_start_index(void) {
178-
start_index = machine_rng_generate_random_word() % chunks_in_file_system + 1;
178+
start_index = rng_generate_random_word() % chunks_in_file_system + 1;
179179
}
180180

181181
void microbit_filesystem_init(void) {

ports/nrf/modules/uos/moduos.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//#include "portmodules.h"
4343

4444
#if MICROPY_HW_ENABLE_RNG
45-
#include "modrandom.h"
45+
#include "drivers/rng.h"
4646
#endif // MICROPY_HW_ENABLE_RNG
4747

4848
/// \module os - basic "operating system" services
@@ -105,7 +105,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
105105
vstr_t vstr;
106106
vstr_init_len(&vstr, n);
107107
for (int i = 0; i < n; i++) {
108-
vstr.buf[i] = (uint8_t)(machine_rng_generate_random_word() & 0xFF);
108+
vstr.buf[i] = (uint8_t)(rng_generate_random_word() & 0xFF);
109109
}
110110
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
111111
}

0 commit comments

Comments
 (0)