Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
rng_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
26 #include "mcu_periph/rng.h"
27 #include <hal.h>
28 
29 /***********
30  * RNG API *
31  ***********/
32 
33 // FIXME remove and use API from ChibiOS after updating to newer version
34 
35 static void TRNGStart(void)
36 {
37  rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0);
38  RNG->CR |= RNG_CR_RNGEN;
39 }
40 
41 static void TRNGStop(void)
42 {
43  RNG->CR &= ~RNG_CR_RNGEN;
44  rccDisableAHB2(RCC_AHB2ENR_RNGEN);
45 }
46 
47 static uint32_t TRNGGet(void)
48 {
49  // waiting for data ready
50  while ((RNG->SR & RNG_SR_DRDY) == 0) {};
51  return RNG->DR;
52 }
53 
54 static bool TRNGGetErrors(void)
55 {
56  return (RNG->SR & RNG_SR_CECS) || (RNG->SR & RNG_SR_SECS);
57 }
58 
59 static void TRNGClearErrors(void)
60 {
61  RNG->SR &= ~(RNG_SR_SEIS | RNG_SR_CEIS);
62 }
63 
64 static bool TRNGGenerate(size_t size, uint32_t *out)
65 {
66  while (size) {
67  out[--size] = TRNGGet();
68  }
69  return TRNGGetErrors();
70 }
71 
72 
73 /************
74  * PPRZ API *
75  ************/
76 
77 void rng_init(void)
78 {
79  TRNGStart();
80 }
81 
82 void rng_deinit(void)
83 {
84  TRNGStop();
85 }
86 
87 // Return true only if we got a new number
88 // that is different from the previous one
89 bool rng_get(uint32_t *rand_nr)
90 {
91  bool err = TRNGGenerate(1, rand_nr);
92  if (err) {
94  return false;
95  }
96  else {
97  return true;
98  }
99 }
100 
101 // Wait until we get a new number that is different
102 // from the previous one. We can wait forever here if
103 // the clocks are not setup properly.
105 {
106  uint32_t tmp = 0;
107  bool err = true;
108  do {
109  err = TRNGGenerate(1, &tmp);
110  } while (err);
111  return tmp;
112 }
113 
static void TRNGStop(void)
Definition: rng_arch.c:41
void rng_deinit(void)
Definition: rng_arch.c:82
bool rng_get(uint32_t *rand_nr)
Definition: rng_arch.c:89
static bool TRNGGenerate(size_t size, uint32_t *out)
Definition: rng_arch.c:64
static uint32_t TRNGGet(void)
Definition: rng_arch.c:47
static bool TRNGGetErrors(void)
Definition: rng_arch.c:54
uint32_t rng_wait_and_get(void)
Definition: rng_arch.c:104
static void TRNGClearErrors(void)
Definition: rng_arch.c:59
static void TRNGStart(void)
Definition: rng_arch.c:35
void rng_init(void)
Definition: rng_arch.c:77
arch independent Random Number Generator API
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78