Contiki-NG
random.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 * \addtogroup cc2538
33 * @{
34 *
35 * \defgroup cc2538-random cc2538 Random Number Generator
36 *
37 * Driver for the cc2538 Hardware Random Number Generator
38 * @{
39 *
40 * \file
41 * Random number generator routines exploiting the cc2538 hardware
42 * capabilities.
43 *
44 * This file overrides os/lib/random.c.
45 */
46#include "contiki.h"
47#include "dev/rfcore.h"
48#include "dev/cc2538-rf.h"
49#include "dev/soc-adc.h"
50#include "dev/sys-ctrl.h"
51#include "reg.h"
52/*---------------------------------------------------------------------------*/
53/**
54 * \brief Generates a new random number using the cc2538 RNG.
55 * \return The random number.
56 */
57unsigned short
59{
60 uint32_t rv;
61
62 /* Clock the RNG LSFR once */
64
65 rv = REG(SOC_ADC_RNDL) | (REG(SOC_ADC_RNDH) << 8);
66 return ((unsigned short)rv);
67}
68/*---------------------------------------------------------------------------*/
69/**
70 * \brief Seed the cc2538 random number generator.
71 * \param seed Ignored. It's here because the function prototype is in core.
72 *
73 * We form a seed for the RNG by sampling IF_ADC as
74 * discussed in the user guide.
75 * Seeding with this method should not be done during
76 * normal radio operation. Thus, use this function before
77 * initialising the network.
78 *
79 * \note Must not be called after the RF driver has been initialised and is
80 * in normal operation. If it is absolutely necessary to do so, the
81 * radio will need re-initialised.
82 */
83void
84random_init(unsigned short seed)
85{
86 int i;
87 unsigned short s = 0;
88
89 /* Make sure the RNG is on */
91
92 /* Enable clock for the RF Core */
93 REG(SYS_CTRL_RCGCRFC) = 1;
94
95 /* Wait for the clock ungating to take effect */
96 while(REG(SYS_CTRL_RCGCRFC) != 1);
97
98 /* Infinite RX - FRMCTRL0[3:2] = 10
99 * This will mess with radio operation - see note above */
100 REG(RFCORE_XREG_FRMCTRL0) = 0x00000008;
101
102 /* Turn RF on */
104
105 /*
106 * Wait until "the chip has been in RX long enough for the transients to
107 * have died out. A convenient way to do this is to wait for the RSSI-valid
108 * signal to go high."
109 */
111
112 /*
113 * Form the seed by concatenating bits from IF_ADC in the RF receive path.
114 * Keep sampling until we have read at least 16 bits AND the seed is valid
115 *
116 * Invalid seeds are 0x0000 and 0x8003 and should not be used.
117 */
118 while(s == 0x0000 || s == 0x8003) {
119 for(i = 0; i < 16; i++) {
121 s <<= 1;
122 }
123 }
124
125 /* High byte first */
126 REG(SOC_ADC_RNDL) = (s >> 8) & 0x00FF;
127 REG(SOC_ADC_RNDL) = s & 0xFF;
128
129 /* RF Off. NETSTACK_RADIO.init() will sort out normal RF operation */
131}
132
133/**
134 * @}
135 * @}
136 */
Header file for the cc2538 RF driver.
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
#define CC2538_RF_CSP_ISRFOFF()
Send a RF OFF command strobe to the CSP.
Definition: cc2538-rf.h:107
#define CC2538_RF_CSP_ISRXON()
Send an RX ON command strobe to the CSP.
Definition: cc2538-rf.h:95
#define RFCORE_XREG_FRMCTRL0
Frame handling.
Definition: rfcore-xreg.h:53
#define RFCORE_XREG_RSSISTAT
RSSI valid status register.
Definition: rfcore-xreg.h:69
#define RFCORE_XREG_RFRND_IRND
Random bit from the I channel.
Definition: rfcore-xreg.h:413
#define RFCORE_XREG_RSSISTAT_RSSI_VALID
RSSI value is valid.
Definition: rfcore-xreg.h:327
#define RFCORE_XREG_RFRND
Random data.
Definition: rfcore-xreg.h:83
#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
#define SYS_CTRL_RCGCRFC
RF Core clocks - active mode.
Definition: sys-ctrl.h:93
Header file with register manipulation macro definitions.
Top-level header file for cc2538 RF Core registers.
Header file with register declarations for the cc2538 ADC and H/W RNG.
Header file for the cc2538 System Control driver.