Update libopencm3 and add Random Number Generator (RNG) (#2192)

* Compilation fixes (CAN bus)

* Update libopencm3

* Compilation fixes

* Added Random Number Generator for stm32 autopilots

* Compilation fixes
This commit is contained in:
Michal Podhradsky
2017-11-28 15:01:36 -08:00
committed by GitHub
parent da7003b358
commit 640c5aa8c5
9 changed files with 148 additions and 9 deletions
@@ -56,6 +56,7 @@ Aggie Air ARK
<modules> <modules>
<module name="sys_mon"/> <module name="sys_mon"/>
<module name="rng"/>
<module name="copilot"/> <module name="copilot"/>
<module name="extra_dl"> <module name="extra_dl">
<!-- in order to use uart1 without chibios we need to remap the peripheral--> <!-- in order to use uart1 without chibios we need to remap the peripheral-->
@@ -53,6 +53,7 @@
<module name="gps" type="ubx_ucenter"/> <module name="gps" type="ubx_ucenter"/>
<module name="geo_mag"/> <module name="geo_mag"/>
<module name="air_data"/> <module name="air_data"/>
<module name="rng"/>
</modules> </modules>
<servos driver="Pwm"> <servos driver="Pwm">
+17
View File
@@ -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 target="ap|fbw">
<define name="USE_RNG"/>
<file_arch name="rng_arch.c" dir="mcu_periph"/>
</makefile>
</module>
+4 -4
View File
@@ -51,7 +51,7 @@ const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
.ppre1 = RCC_CFGR_PPRE_DIV_4, .ppre1 = RCC_CFGR_PPRE_DIV_4,
.ppre2 = RCC_CFGR_PPRE_DIV_2, .ppre2 = RCC_CFGR_PPRE_DIV_2,
.power_save = 1, .power_save = 1,
.flash_config = FLASH_ACR_ICE | FLASH_ACR_DCE | FLASH_ACR_LATENCY_3WS, .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
.ahb_frequency = 48000000, .ahb_frequency = 48000000,
.apb1_frequency = 12000000, .apb1_frequency = 12000000,
.apb2_frequency = 24000000, .apb2_frequency = 24000000,
@@ -65,7 +65,7 @@ const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
.ppre1 = RCC_CFGR_PPRE_DIV_2, .ppre1 = RCC_CFGR_PPRE_DIV_2,
.ppre2 = RCC_CFGR_PPRE_DIV_NONE, .ppre2 = RCC_CFGR_PPRE_DIV_NONE,
.power_save = 1, .power_save = 1,
.flash_config = FLASH_ACR_ICE | FLASH_ACR_DCE | FLASH_ACR_LATENCY_2WS, .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_2WS,
.ahb_frequency = 84000000, .ahb_frequency = 84000000,
.apb1_frequency = 42000000, .apb1_frequency = 42000000,
.apb2_frequency = 84000000, .apb2_frequency = 84000000,
@@ -79,7 +79,7 @@ const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
.ppre1 = RCC_CFGR_PPRE_DIV_4, .ppre1 = RCC_CFGR_PPRE_DIV_4,
.ppre2 = RCC_CFGR_PPRE_DIV_2, .ppre2 = RCC_CFGR_PPRE_DIV_2,
.power_save = 1, .power_save = 1,
.flash_config = FLASH_ACR_ICE | FLASH_ACR_DCE | FLASH_ACR_LATENCY_3WS, .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
.ahb_frequency = 120000000, .ahb_frequency = 120000000,
.apb1_frequency = 30000000, .apb1_frequency = 30000000,
.apb2_frequency = 60000000, .apb2_frequency = 60000000,
@@ -92,7 +92,7 @@ const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
.hpre = RCC_CFGR_HPRE_DIV_NONE, .hpre = RCC_CFGR_HPRE_DIV_NONE,
.ppre1 = RCC_CFGR_PPRE_DIV_4, .ppre1 = RCC_CFGR_PPRE_DIV_4,
.ppre2 = RCC_CFGR_PPRE_DIV_2, .ppre2 = RCC_CFGR_PPRE_DIV_2,
.flash_config = FLASH_ACR_ICE | FLASH_ACR_DCE | FLASH_ACR_LATENCY_5WS, .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS,
.ahb_frequency = 168000000, .ahb_frequency = 168000000,
.apb1_frequency = 42000000, .apb1_frequency = 42000000,
.apb2_frequency = 84000000, .apb2_frequency = 84000000,
+10 -4
View File
@@ -196,9 +196,11 @@ int can_hw_transmit(uint32_t id, const uint8_t *buf, uint8_t len)
#ifdef STM32F1 #ifdef STM32F1
void usb_lp_can_rx0_isr(void) void usb_lp_can_rx0_isr(void)
{ {
uint32_t id, fmi; uint32_t id;
uint8_t fmi;
bool ext, rtr; bool ext, rtr;
uint8_t length, data[8]; uint8_t length, data[8];
uint16_t timestamp;
can_receive(CAN1, can_receive(CAN1,
0, /* FIFO: 0 */ 0, /* FIFO: 0 */
@@ -208,7 +210,8 @@ void usb_lp_can_rx0_isr(void)
&rtr, &rtr,
&fmi, &fmi,
&length, &length,
data); data,
&timestamp);
_can_run_rx_callback(id, data, length); _can_run_rx_callback(id, data, length);
@@ -216,9 +219,11 @@ void usb_lp_can_rx0_isr(void)
} }
#elif STM32F4 #elif STM32F4
void can1_rx0_isr(void){ void can1_rx0_isr(void){
uint32_t id, fmi; uint32_t id;
uint8_t fmi;
bool ext, rtr; bool ext, rtr;
uint8_t length, data[8]; uint8_t length, data[8];
uint16_t timestamp;
can_receive(CAN1, can_receive(CAN1,
0, /* FIFO: 0 */ 0, /* FIFO: 0 */
@@ -228,7 +233,8 @@ void can1_rx0_isr(void){
&rtr, &rtr,
&fmi, &fmi,
&length, &length,
data); data,
&timestamp);
_can_run_rx_callback(id, data, length); _can_run_rx_callback(id, data, length);
@@ -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
View File
@@ -61,6 +61,9 @@
#ifdef USE_DAC #ifdef USE_DAC
#include "mcu_periph/dac.h" #include "mcu_periph/dac.h"
#endif #endif
#ifdef USE_RNG
#include "mcu_periph/rng.h"
#endif
#endif /* PERIPHERALS_AUTO_INIT */ #endif /* PERIPHERALS_AUTO_INIT */
void WEAK board_init(void) void WEAK board_init(void)
@@ -219,6 +222,10 @@ void mcu_init(void)
udp_arch_init(); udp_arch_init();
#endif #endif
#ifdef USE_RNG
rng_init();
#endif
#else #else
INFO("PERIPHERALS_AUTO_INIT not enabled! Peripherals (including sys_time) need explicit initialization.") INFO("PERIPHERALS_AUTO_INIT not enabled! Peripherals (including sys_time) need explicit initialization.")
#endif /* PERIPHERALS_AUTO_INIT */ #endif /* PERIPHERALS_AUTO_INIT */
+39
View File
@@ -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 */