2014-08-25 13:54:15 +02:00
|
|
|
/*
|
2016-02-05 16:48:10 +01:00
|
|
|
* Copyright (C) 2014-2016 Freie Universität Berlin
|
2014-08-25 13:54:15 +02:00
|
|
|
*
|
2016-02-05 16:48:10 +01:00
|
|
|
* 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.
|
2014-08-25 13:54:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_stm32f4
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level random number generator driver implementation
|
|
|
|
*
|
2016-02-09 16:09:40 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2014-08-25 13:54:15 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
2016-08-29 18:55:19 +02:00
|
|
|
#include "periph_conf.h"
|
2016-02-05 16:48:10 +01:00
|
|
|
#include "periph/hwrng.h"
|
2014-08-25 13:54:15 +02:00
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
/* only build if the CPU actually provides a RNG peripheral */
|
|
|
|
#ifdef RNG
|
2014-08-25 13:54:15 +02:00
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
void hwrng_init(void)
|
2014-08-25 13:54:15 +02:00
|
|
|
{
|
2016-02-05 16:48:10 +01:00
|
|
|
/* no need for initialization */
|
2014-08-25 13:54:15 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
void hwrng_read(uint8_t *buf, unsigned int num)
|
2014-08-25 13:54:15 +02:00
|
|
|
{
|
2015-05-27 21:46:47 +02:00
|
|
|
unsigned int count = 0;
|
2014-08-25 13:54:15 +02:00
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
/* power on and enable the device */
|
2016-08-29 18:55:19 +02:00
|
|
|
periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN);
|
2016-02-05 16:48:10 +01:00
|
|
|
RNG->CR = RNG_CR_RNGEN;
|
|
|
|
|
|
|
|
/* get random data */
|
2014-08-25 13:54:15 +02:00
|
|
|
while (count < num) {
|
|
|
|
/* wait for random data to be ready to read */
|
2016-01-17 12:20:17 +01:00
|
|
|
while (!(RNG->SR & RNG_SR_DRDY)) {}
|
2014-08-25 13:54:15 +02:00
|
|
|
/* read next 4 bytes */
|
2016-02-05 16:48:10 +01:00
|
|
|
uint32_t tmp = RNG->DR;
|
2014-08-25 13:54:15 +02:00
|
|
|
/* copy data into result vector */
|
|
|
|
for (int i = 0; i < 4 && count < num; i++) {
|
2016-02-05 16:48:10 +01:00
|
|
|
buf[count++] = (uint8_t)tmp;
|
2014-08-25 13:54:15 +02:00
|
|
|
tmp = tmp >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
/* finally disable the device again */
|
2014-08-25 13:54:15 +02:00
|
|
|
RNG->CR = 0;
|
2016-08-29 18:55:19 +02:00
|
|
|
periph_clk_dis(AHB2, RCC_AHB2ENR_RNGEN);
|
2014-08-25 13:54:15 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 16:48:10 +01:00
|
|
|
#endif /* CPUID_LEN */
|