Skip to content

Commit ae82a93

Browse files
authored
Merge pull request adafruit#967 from arturo182/nrf_os
nrf: Rewrite the os common-hal using nrfx
2 parents a0425d9 + 6e6a500 commit ae82a93

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

ports/nrf/common-hal/os/__init__.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@
2828
#include "py/mpconfig.h"
2929
#include "py/objstr.h"
3030
#include "py/objtuple.h"
31-
#include "py/qstr.h"
3231

3332
#ifdef BLUETOOTH_SD
3433
#include "nrf_sdm.h"
3534
#endif
3635

37-
#include "nrf.h"
36+
#include "nrf_rng.h"
3837

3938
STATIC const qstr os_uname_info_fields[] = {
4039
MP_QSTR_sysname, MP_QSTR_nodename,
@@ -47,7 +46,6 @@ STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING
4746
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
4847
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
4948

50-
5149
STATIC MP_DEFINE_ATTRTUPLE(
5250
os_uname_info_obj,
5351
os_uname_info_fields,
@@ -63,28 +61,26 @@ mp_obj_t common_hal_os_uname(void) {
6361
return (mp_obj_t)&os_uname_info_obj;
6462
}
6563

66-
bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) {
64+
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
6765
#ifdef BLUETOOTH_SD
6866
uint8_t sd_en = 0;
6967
(void) sd_softdevice_is_enabled(&sd_en);
7068

71-
if (sd_en) {
72-
return NRF_SUCCESS == sd_rand_application_vector_get(buffer,length);
73-
}
69+
if (sd_en)
70+
return NRF_SUCCESS == sd_rand_application_vector_get(buffer, length);
7471
#endif
7572

76-
NRF_RNG->EVENTS_VALRDY = 0;
77-
NRF_RNG->TASKS_START = 1;
73+
nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
74+
nrf_rng_task_trigger(NRF_RNG_TASK_START);
7875

7976
for (uint32_t i = 0; i < length; i++) {
80-
while (NRF_RNG->EVENTS_VALRDY == 0) {
81-
;
82-
}
83-
NRF_RNG->EVENTS_VALRDY = 0;
84-
buffer[i] = (uint8_t) NRF_RNG->VALUE;
77+
while (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY) == 0);
78+
nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
79+
80+
buffer[i] = nrf_rng_random_value_get();
8581
}
8682

87-
NRF_RNG->TASKS_STOP = 1;
83+
nrf_rng_task_trigger(NRF_RNG_TASK_STOP);
8884

8985
return true;
9086
}

0 commit comments

Comments
 (0)