Contiki-NG
Loading...
Searching...
No Matches
cc2538-prng.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/**
33 * \addtogroup random
34 * @{
35 *
36 * \file
37 * Driver for the cc2538 Hardware Random Number Generator
38 */
39
40#include "lib/random.h"
41#include "dev/soc-adc.h"
42#include "reg.h"
43
44/*---------------------------------------------------------------------------*/
45static uint_fast16_t
46rand(void)
47{
48 /* Clock the RNG LSFR once */
50 return REG(SOC_ADC_RNDL) | (REG(SOC_ADC_RNDH) << 8);
51}
52/*---------------------------------------------------------------------------*/
53static void
54seed(uint64_t seed)
55{
56 /* Make sure the RNG is on */
58
59 /* Invalid seeds are 0x0000 and 0x8003 and should not be used. */
60 while(((seed & 0xFFFF) == 0x8003) || !(seed & 0xFFFF)) {
61 seed >>= 1;
62 /* Prepend a one in order to eventually find a seed */
63 seed |= 1ULL << 63;
64 }
65
66 /* High byte first */
67 REG(SOC_ADC_RNDL) = (seed >> 8) & 0xFF;
68 REG(SOC_ADC_RNDL) = seed & 0xFF;
69}
70/*---------------------------------------------------------------------------*/
71const struct random_prng cc2538_prng = {
72 seed,
73 rand
74};
75/*---------------------------------------------------------------------------*/
76
77/** @} */
#define SOC_ADC_RNDL
RNG low byte.
Definition soc-adc.h:54
#define SOC_ADC_ADCCON1_RCTRL1
RCTRL high bit.
Definition soc-adc.h:66
#define SOC_ADC_RNDH
RNG high byte.
Definition soc-adc.h:55
#define SOC_ADC_ADCCON1_RCTRL0
RCTRL low bit.
Definition soc-adc.h:67
#define SOC_ADC_ADCCON1
ADC Control 1.
Definition soc-adc.h:49
Header file for generating non-cryptographic random numbers.
Header file with register manipulation macro definitions.
Header file with register declarations for the cc2538 ADC and H/W RNG.
Structure of PRNG drivers.
Definition random.h:61
uint_fast16_t(* rand)(void)
Generates a 16-bit pseudo-random number.
Definition random.h:73
void(* seed)(uint64_t seed)
Seeds the PRNG with a seed.
Definition random.h:67