[ion] Ion::random()

Change-Id: Ib85055ab3b4bf3dfce991f9990659c744be20f39
This commit is contained in:
Romain Goyet
2017-02-16 11:36:29 +01:00
parent bd220b2cd7
commit 809fd8a17a
4 changed files with 26 additions and 0 deletions

View File

@@ -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

View File

@@ -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();
}

View File

@@ -0,0 +1,6 @@
#include <ion.h>
#include <stdlib.h>
uint32_t Ion::random() {
return rand();
}

View File

@@ -12,6 +12,7 @@ objs += $(addprefix ion/src/shared/, \
events_keyboard.o \
log_printf.o \
power.o \
random.o \
)
SFLAGS += `fltk-config --cflags`