aboutsummaryrefslogtreecommitdiffstats
path: root/src/random.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/random.c')
-rw-r--r--src/random.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/random.c b/src/random.c
new file mode 100644
index 0000000..9c1e0d1
--- /dev/null
+++ b/src/random.c
@@ -0,0 +1,24 @@
+#include <random.h>
+
+#include <stm32l476xx.h>
+#include <clock.h>
+
+void random_init(void)
+{
+ // setup and enable RNG clock
+ RCC->CCIPR &= ~(RCC_CCIPR_CLK48SEL);
+ RCC->CCIPR |= RCC_CCIPR_CLK48SEL_1;
+ RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
+ RNG->CR |= RNG_CR_RNGEN;
+}
+
+uint32_t random_get(void)
+{
+ if (RNG->SR & (RNG_SR_SEIS | RNG_SR_CEIS))
+ return 0;
+
+ while (!(RNG->SR & RNG_SR_DRDY))
+ delay(1);
+
+ return RNG->DR;
+}