From 809fd8a17a0d19e1880d57e51d7a7d3092befccb Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Thu, 16 Feb 2017 11:36:29 +0100 Subject: [PATCH] [ion] Ion::random() Change-Id: Ib85055ab3b4bf3dfce991f9990659c744be20f39 --- ion/include/ion.h | 3 +++ ion/src/device/device.cpp | 16 ++++++++++++++++ ion/src/shared/random.cpp | 6 ++++++ ion/src/simulator/Makefile | 1 + 4 files changed, 26 insertions(+) create mode 100644 ion/src/shared/random.cpp diff --git a/ion/include/ion.h b/ion/include/ion.h index 3c7b281ad..5ae753fb6 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -33,6 +33,9 @@ void reset(); // Only accepts whole 32bit values uint32_t crc32(const uint32_t * data, size_t length); +// Provides a true random number +uint32_t random(); + } #endif diff --git a/ion/src/device/device.cpp b/ion/src/device/device.cpp index cd302e99c..c87a2c142 100644 --- a/ion/src/device/device.cpp +++ b/ion/src/device/device.cpp @@ -41,6 +41,22 @@ uint32_t Ion::crc32(const uint32_t * data, size_t length) { return result; } +uint32_t Ion::random() { + bool initialRNGEngineState = RCC.AHB2ENR()->getRNGEN(); + RCC.AHB2ENR()->setRNGEN(true); + + RNG.CR()->setRNGEN(true); + + while (RNG.SR()->getDRDY() == 0) { + } + uint32_t result = RNG.DR()->get(); + + RNG.CR()->setRNGEN(false); + RCC.AHB2ENR()->setRNGEN(initialRNGEngineState); + + return result; +} + void Ion::reset() { CM4.AIRCR()->requestReset(); } diff --git a/ion/src/shared/random.cpp b/ion/src/shared/random.cpp new file mode 100644 index 000000000..9ec8feb00 --- /dev/null +++ b/ion/src/shared/random.cpp @@ -0,0 +1,6 @@ +#include +#include + +uint32_t Ion::random() { + return rand(); +} diff --git a/ion/src/simulator/Makefile b/ion/src/simulator/Makefile index 19466fbe0..cfaf469ad 100644 --- a/ion/src/simulator/Makefile +++ b/ion/src/simulator/Makefile @@ -12,6 +12,7 @@ objs += $(addprefix ion/src/shared/, \ events_keyboard.o \ log_printf.o \ power.o \ + random.o \ ) SFLAGS += `fltk-config --cflags`