diff --git a/boards/stm32f4discovery/include/periph_conf.h b/boards/stm32f4discovery/include/periph_conf.h index 708ba21a95..78dc280013 100644 --- a/boards/stm32f4discovery/include/periph_conf.h +++ b/boards/stm32f4discovery/include/periph_conf.h @@ -37,7 +37,6 @@ #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY_5WS /** @} */ - /** * @name Timer configuration * @{ @@ -66,7 +65,6 @@ #define TIMER_1_IRQ_CHAN TIM5_IRQn /** @} */ - /** * @name UART configuration * @{ @@ -106,7 +104,6 @@ #define UART_1_AF 7 /** @} */ - /** * @name ADC configuration * @{ @@ -145,7 +142,6 @@ #define ADC_1_CH1_PIN 2 /** @} */ - /** * @name PWM configuration * @{ @@ -186,6 +182,12 @@ #define PWM_1_PIN_AF 2 /** @} */ +/** + * @name Random Number Generator configuration + * @{ + */ +#define RANDOM_NUMOF (1U) +/** @} */ /** * @name SPI configuration @@ -260,7 +262,6 @@ #define I2C_1_SDA_AFCFG() /** @} */ - /** * @name GPIO configuration * @{ diff --git a/cpu/stm32f4/periph/random.c b/cpu/stm32f4/periph/random.c new file mode 100644 index 0000000000..8fe59636ed --- /dev/null +++ b/cpu/stm32f4/periph/random.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @ingroup cpu_stm32f4 + * @{ + * + * @file + * @brief Low-level random number generator driver implementation + * + * @author Hauke Petersen + * + * @} + */ + +#include "cpu.h" +#include "periph/random.h" +#include "periph_conf.h" + +/* ignore file in case no RNG device is defined */ +#if RANDOM_NUMOF + +void random_init(void) +{ + random_poweron(); +} + +int random_read(char *buf, unsigned int num) +{ + uint32_t tmp; + int count = 0; + + while (count < num) { + /* wait for random data to be ready to read */ + while (!(RNG->SR & RNG_SR_DRDY)); + /* read next 4 bytes */ + tmp = RNG->DR; + /* copy data into result vector */ + for (int i = 0; i < 4 && count < num; i++) { + buf[count++] = (char)tmp; + tmp = tmp >> 8; + } + } + + return count; +} + +void random_poweron(void) +{ + RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; + RNG->CR = RNG_CR_RNGEN; +} + +void random_poweroff(void) +{ + RNG->CR = 0; + RCC->AHB2ENR &= ~RCC_AHB2ENR_RNGEN; +} + +#endif /* RANDOM_NUMOF */