Skip to content

Commit

Permalink
Merge pull request #6 from gebart/pr/kinetis-rngb
Browse files Browse the repository at this point in the history
Kinetis RNGB implementation
  • Loading branch information
Johann Fischer committed Jan 12, 2015
2 parents bdf1abe + 67e3306 commit 6cddfe0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cpu/kinetis_common/random_rnga.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "periph_conf.h"

#if RANDOM_NUMOF
#ifdef KINETIS_RNGA


typedef struct RNG_MemMap {
uint32_t CR;
Expand Down Expand Up @@ -66,15 +68,15 @@ int random_read(char *buf, unsigned int num)
int count = 0;

/* self-seeding */
while (!(RNGA->SR & RNGA_SR_OREG_LVL_MASK));
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));

RNGA->ER = RNGA->OR ^ (uint32_t)buf;
KINETIS_RNGA->ER = KINETIS_RNGA->OR ^ (uint32_t)buf;

while (count < num) {
/* wait for random data to be ready to read */
while (!(RNGA->SR & RNGA_SR_OREG_LVL_MASK));
while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK));

tmp = RNGA->OR;
tmp = KINETIS_RNGA->OR;

/* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) {
Expand All @@ -89,12 +91,12 @@ int random_read(char *buf, unsigned int num)
void random_poweron(void)
{
RANDOM_CLKEN();
RNGA->CR = RNGA_CR_INTM_MASK | RNGA_CR_HA_MASK | RNGA_CR_GO_MASK;
KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
}

void random_poweroff(void)
{
RNGA->CR = 0;
KINETIS_RNGA->CR = 0;
RANDOM_CLKDIS();
}

Expand All @@ -104,4 +106,5 @@ void isr_rng(void)
}
*/

#endif /* KINETIS_RNGA */
#endif /* RANDOM_NUMOF */
90 changes: 90 additions & 0 deletions cpu/kinetis_common/random_rngb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2014 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.
*/

/**
* @ingroup cpu_kinetis_common
* @{
*
* @file
* @brief Low-level random number generator driver implementation.
* Driver for Freescale's RNGB module. RNGB generates data that
* looks random. Reference Manual recommends to use the RNGB as entropy
* source.
*
* @author Joakim Gebart <joakim.gebart@eistec.se> (adaption for Freescale's RNGB)
* @author Johann Fischer <j.fischer@phytec.de> (adaption for Freescale's RNGA)
* @author Hauke Petersen <mail@haukepetersen.de>
*
* @}
*/

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

#if RANDOM_NUMOF
#ifdef KINETIS_RNGB


void random_init(void)
{
random_poweron();
}

int random_read(char *buf, unsigned int num)
{
int count = 0;

while (count < num) {
uint32_t tmp;
/* wait for random data to be ready to read */
while (!(KINETIS_RNGB->SR & RNG_SR_FIFO_LVL_MASK));

tmp = KINETIS_RNGB->OUT;

/* 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)
{
RANDOM_CLKEN();
if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001)
{
/* Wrong type of RNG */
/* TODO: Handle */
}

/* Software reset, bit is self-clearing */
BITBAND_REG(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();
}

/*
void isr_rng(void)
{
}
*/

#endif /* KINETIS_RNGB */
#endif /* RANDOM_NUMOF */

0 comments on commit 6cddfe0

Please sign in to comment.