Skip to content

Commit

Permalink
Added Random Number Generator for stm32 autopilots
Browse files Browse the repository at this point in the history
  • Loading branch information
podhrmic committed Nov 28, 2017
1 parent f97cfff commit 23515a0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/airframes/AGGIEAIR/aggieair_ark_hexa_1-8.xml
Expand Up @@ -56,6 +56,7 @@ Aggie Air ARK

<modules>
<module name="sys_mon"/>
<module name="rng"/>
<module name="copilot"/>
<module name="extra_dl">
<!-- in order to use uart1 without chibios we need to remap the peripheral-->
Expand Down
1 change: 1 addition & 0 deletions conf/airframes/examples/quadrotor_lisa_mx.xml
Expand Up @@ -53,6 +53,7 @@
<module name="gps" type="ubx_ucenter"/>
<module name="geo_mag"/>
<module name="air_data"/>
<module name="rng"/>
</modules>

<servos driver="Pwm">
Expand Down
17 changes: 17 additions & 0 deletions conf/modules/rng.xml
@@ -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>

68 changes: 68 additions & 0 deletions sw/airborne/arch/stm32/mcu_periph/rng_arch.c
@@ -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;
}
7 changes: 7 additions & 0 deletions sw/airborne/mcu.c
Expand Up @@ -61,6 +61,9 @@
#ifdef USE_DAC
#include "mcu_periph/dac.h"
#endif
#ifdef USE_RNG
#include "mcu_periph/rng.h"
#endif
#endif /* PERIPHERALS_AUTO_INIT */

void WEAK board_init(void)
Expand Down Expand Up @@ -219,6 +222,10 @@ void mcu_init(void)
udp_arch_init();
#endif

#ifdef USE_RNG
rng_init();
#endif

#else
INFO("PERIPHERALS_AUTO_INIT not enabled! Peripherals (including sys_time) need explicit initialization.")
#endif /* PERIPHERALS_AUTO_INIT */
Expand Down
39 changes: 39 additions & 0 deletions sw/airborne/mcu_periph/rng.h
@@ -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 */

0 comments on commit 23515a0

Please sign in to comment.