Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Random Number Generator for stm32 autopilots
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE module SYSTEM "module.dtd"> | ||
|
||
<module name="rng" dir="mcu_periph" task="mcu"> | ||
<doc> | ||
<description> | ||
Random Number Generator | ||
</description> | ||
</doc> | ||
<header> | ||
<file name="rng.h" dir="mcu_periph"/> | ||
</header> | ||
<makefile> | ||
<define name="USE_RNG"/> | ||
<file_arch name="rng_arch.c" dir="mcu_periph"/> | ||
</makefile> | ||
</module> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2017 Michal Podhradsky <mpodhradsky@galois.com> | ||
* | ||
* This file is part of Paparazzi. | ||
* | ||
* Paparazzi is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* Paparazzi is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Paparazzi; see the file COPYING. If not, write to | ||
* the Free Software Foundation, 59 Temple Place - Suite 330, | ||
* Boston, MA 02111-1307, USA. | ||
* | ||
*/ | ||
|
||
/** \file rng_arch.c | ||
* \brief arch specific Random Number Generator API | ||
* | ||
*/ | ||
#include "mcu_periph/rng.h" | ||
#include <libopencm3/stm32/rcc.h> | ||
#include <libopencm3/stm32/rng.h> | ||
|
||
uint32_t last; | ||
|
||
void rng_init(void) { | ||
rcc_periph_clock_enable(RCC_RNG); | ||
rng_enable(); | ||
// dont forget to throw away the first generated number | ||
last = rng_wait_and_get(); | ||
} | ||
|
||
void rng_deinit(void) { | ||
rng_disable(); | ||
rcc_periph_clock_disable(RCC_RNG); | ||
} | ||
|
||
// Return true only if we got a new number | ||
// that is different from the previous one | ||
bool rng_get(uint32_t *rand_nr) { | ||
uint32_t tmp = 0; | ||
if (rng_get_random(&tmp) && (tmp != last)) { | ||
last = tmp; | ||
*rand_nr = tmp; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// Wait until we get a new number that is different | ||
// from the previous one. We can wait forever here if | ||
// the clocks are not setup properly. | ||
uint32_t rng_wait_and_get(void) { | ||
uint32_t tmp = last; | ||
while (tmp == last) { | ||
tmp = rng_get_random_blocking(); | ||
} | ||
last = tmp; | ||
return tmp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2017 Michal Podhradsky <mpodhradsky@galois.com> | ||
* | ||
* This file is part of Paparazzi. | ||
* | ||
* Paparazzi is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* Paparazzi is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Paparazzi; see the file COPYING. If not, write to | ||
* the Free Software Foundation, 59 Temple Place - Suite 330, | ||
* Boston, MA 02111-1307, USA. | ||
* | ||
*/ | ||
|
||
/** \file rng.h | ||
* \brief arch independent Random Number Generator API | ||
* | ||
*/ | ||
|
||
#ifndef MCU_PERIPH_RNG_H | ||
#define MCU_PERIPH_RNG_H | ||
|
||
#include <inttypes.h> | ||
#include "std.h" | ||
|
||
void rng_init(void); | ||
void rng_deinit(void); | ||
bool rng_get(uint32_t *rand_nr); | ||
uint32_t rng_wait_and_get(void); | ||
|
||
#endif /* MCU_PERIPH_RNG_H */ |