2015-01-06 00:19:24 +01:00
|
|
|
/*
|
2016-02-05 16:45:43 +01:00
|
|
|
* Copyright (C) 2014-2016 Freie Universität Berlin
|
2015-01-06 00:19:24 +01:00
|
|
|
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
|
|
|
|
*
|
2016-02-05 16:45:43 +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.
|
2015-01-06 00:19:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-10-27 01:26:26 +02:00
|
|
|
* @ingroup cpu_kinetis
|
2018-06-01 12:25:00 +02:00
|
|
|
* @ingroup drivers_periph_hwrng
|
2015-01-06 00:19:24 +01:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2016-02-05 16:45:43 +01:00
|
|
|
* @brief HWRNG interface implementation
|
2015-01-06 00:19:24 +01:00
|
|
|
*
|
|
|
|
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
|
2016-02-09 16:09:40 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-01-06 00:19:24 +01:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
2016-02-05 16:45:43 +01:00
|
|
|
#include "periph/hwrng.h"
|
2015-01-06 00:19:24 +01:00
|
|
|
#include "periph_conf.h"
|
2017-11-09 08:01:43 +01:00
|
|
|
#include "bit.h"
|
2015-01-06 00:19:24 +01:00
|
|
|
|
|
|
|
#ifdef KINETIS_RNGA
|
|
|
|
|
2016-02-05 16:45:43 +01:00
|
|
|
void hwrng_init(void)
|
2015-01-06 00:19:24 +01:00
|
|
|
{
|
2016-02-05 16:45:43 +01:00
|
|
|
/* nothing to do here */
|
2015-01-06 00:19:24 +01:00
|
|
|
}
|
|
|
|
|
2017-02-07 13:20:39 +01:00
|
|
|
void hwrng_read(void *buf, unsigned int num)
|
2015-01-06 00:19:24 +01:00
|
|
|
{
|
2015-05-27 17:10:51 +02:00
|
|
|
unsigned int count = 0;
|
2017-02-07 13:20:39 +01:00
|
|
|
uint8_t *b = (uint8_t *)buf;
|
2015-01-06 00:19:24 +01:00
|
|
|
|
2016-02-05 16:45:43 +01:00
|
|
|
/* power on and enable the device */
|
|
|
|
HWRNG_CLKEN();
|
|
|
|
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
|
|
|
|
|
2015-01-06 00:19:24 +01:00
|
|
|
/* self-seeding */
|
|
|
|
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));
|
|
|
|
|
|
|
|
KINETIS_RNGA->ER = KINETIS_RNGA->OR ^ (uint32_t)buf;
|
|
|
|
|
|
|
|
while (count < num) {
|
|
|
|
/* wait for random data to be ready to read */
|
|
|
|
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));
|
|
|
|
|
2015-03-24 08:47:51 +01:00
|
|
|
uint32_t tmp = KINETIS_RNGA->OR;
|
2015-01-06 00:19:24 +01:00
|
|
|
|
|
|
|
/* copy data into result vector */
|
|
|
|
for (int i = 0; i < 4 && count < num; i++) {
|
2017-02-07 13:20:39 +01:00
|
|
|
b[count++] = (uint8_t)tmp;
|
2015-01-06 00:19:24 +01:00
|
|
|
tmp = tmp >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:01:43 +01:00
|
|
|
/* power off the device */
|
2015-01-06 00:19:24 +01:00
|
|
|
KINETIS_RNGA->CR = 0;
|
2016-02-05 16:45:43 +01:00
|
|
|
HWRNG_CLKDIS();
|
2015-01-06 00:19:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* KINETIS_RNGA */
|