Skip to content

Commit

Permalink
cpu/stm32f4: adapted HWRNG implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Feb 14, 2016
1 parent 79cf50d commit 0c375e2
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions cpu/stm32f4/periph/random.c → cpu/stm32f4/periph/hwrng.c
@@ -1,9 +1,9 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2016 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.
* 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.
*/

/**
Expand All @@ -19,48 +19,40 @@
*/

#include "cpu.h"
#include "periph/random.h"
#include "periph_conf.h"
#include "periph/hwrng.h"

/* ignore file in case no RNG device is defined */
#if RANDOM_NUMOF
/* only build if the CPU actually provides a RNG peripheral */
#ifdef RNG

void random_init(void)
void hwrng_init(void)
{
random_poweron();
/* no need for initialization */
}

int random_read(char *buf, unsigned int num)
void hwrng_read(uint8_t *buf, unsigned int num)
{
/* cppcheck-suppress variableScope */
uint32_t tmp;
unsigned int count = 0;

/* power on and enable the device */
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
RNG->CR = RNG_CR_RNGEN;

/* get random data */
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;
uint32_t tmp = RNG->DR;
/* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (char)tmp;
buf[count++] = (uint8_t)tmp;
tmp = tmp >> 8;
}
}

return (int)count;
}

void random_poweron(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
RNG->CR = RNG_CR_RNGEN;
}

void random_poweroff(void)
{
/* finally disable the device again */
RNG->CR = 0;
RCC->AHB2ENR &= ~RCC_AHB2ENR_RNGEN;
}

#endif /* RANDOM_NUMOF */
#endif /* CPUID_LEN */

0 comments on commit 0c375e2

Please sign in to comment.