mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-31 17:31:18 +01:00
cpu/kinetis_common: adapted HWRNG implementation
This commit is contained in:
parent
1b58b187a9
commit
6f0179ce69
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
* Copyright (C) 2014-2016 Freie Universität Berlin
|
||||
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -13,7 +13,7 @@
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level random number generator driver implementation.
|
||||
* @brief HWRNG interface implementation
|
||||
*
|
||||
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
@ -22,21 +22,24 @@
|
||||
*/
|
||||
|
||||
#include "cpu.h"
|
||||
#include "periph/random.h"
|
||||
#include "periph/hwrng.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
#if RANDOM_NUMOF
|
||||
#ifdef KINETIS_RNGA
|
||||
|
||||
void random_init(void)
|
||||
void hwrng_init(void)
|
||||
{
|
||||
random_poweron();
|
||||
/* nothing to do here */
|
||||
}
|
||||
|
||||
int random_read(char *buf, unsigned int num)
|
||||
void hwrng_read(uint8_t *buf, unsigned int num)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
|
||||
/* power on and enable the device */
|
||||
HWRNG_CLKEN();
|
||||
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
|
||||
|
||||
/* self-seeding */
|
||||
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));
|
||||
|
||||
@ -50,31 +53,14 @@ int random_read(char *buf, unsigned int num)
|
||||
|
||||
/* 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)
|
||||
{
|
||||
RANDOM_CLKEN();
|
||||
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
|
||||
}
|
||||
|
||||
void random_poweroff(void)
|
||||
{
|
||||
/* power of the device */
|
||||
KINETIS_RNGA->CR = 0;
|
||||
RANDOM_CLKDIS();
|
||||
HWRNG_CLKDIS();
|
||||
}
|
||||
|
||||
/*
|
||||
void isr_rng(void)
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
#endif /* KINETIS_RNGA */
|
||||
#endif /* RANDOM_NUMOF */
|
||||
@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
* Copyright (C) 2014-2016 Freie Universität Berlin
|
||||
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
|
||||
* Copyright (C) 2015 Eistec AB
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -13,7 +13,7 @@
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Low-level random number generator driver implementation.
|
||||
* @brief HWRNG interface implementation
|
||||
*
|
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se> (adaption for Freescale's RNGB)
|
||||
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
|
||||
@ -23,22 +23,32 @@
|
||||
*/
|
||||
|
||||
#include "cpu.h"
|
||||
#include "periph/random.h"
|
||||
#include "periph/hwrng.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
#if RANDOM_NUMOF
|
||||
#ifdef KINETIS_RNGB
|
||||
|
||||
|
||||
void random_init(void)
|
||||
void hwrng_init(void)
|
||||
{
|
||||
random_poweron();
|
||||
/* nothing to be done here */
|
||||
}
|
||||
|
||||
int random_read(char *buf, unsigned int num)
|
||||
void hwrng_read(uint8_t *buf, unsigned int num)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
|
||||
HWRNG_CLKEN();
|
||||
|
||||
if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001) {
|
||||
/* Wrong type of RNG */
|
||||
/* TODO: Handle */
|
||||
}
|
||||
|
||||
/* Software reset, bit is self-clearing */
|
||||
BITBAND_REG32(KINETIS_RNGB->CMD, RNG_CMD_SR_SHIFT) = 1;
|
||||
/* Set up automatic reseed */
|
||||
KINETIS_RNGB->CR = RNG_CR_AR_MASK | RNG_CR_MASKERR_MASK | RNG_CR_MASKDONE_MASK;
|
||||
|
||||
while (count < num) {
|
||||
uint32_t tmp;
|
||||
|
||||
@ -49,40 +59,13 @@ int random_read(char *buf, unsigned int num)
|
||||
|
||||
/* 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)
|
||||
{
|
||||
RANDOM_CLKEN();
|
||||
|
||||
if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001) {
|
||||
/* Wrong type of RNG */
|
||||
/* TODO: Handle */
|
||||
}
|
||||
|
||||
/* Software reset, bit is self-clearing */
|
||||
BITBAND_REG32(KINETIS_RNGB->CMD, RNG_CMD_SR_SHIFT) = 1;
|
||||
/* Set up automatic reseed */
|
||||
KINETIS_RNGB->CR = RNG_CR_AR_MASK | RNG_CR_MASKERR_MASK | RNG_CR_MASKDONE_MASK;
|
||||
}
|
||||
|
||||
void random_poweroff(void)
|
||||
{
|
||||
KINETIS_RNGB->CR = 0;
|
||||
RANDOM_CLKDIS();
|
||||
HWRNG_CLKDIS();
|
||||
}
|
||||
|
||||
/*
|
||||
void isr_rng(void)
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
#endif /* KINETIS_RNGB */
|
||||
#endif /* RANDOM_NUMOF */
|
||||
Loading…
x
Reference in New Issue
Block a user