Contiki 3.x
random.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, George Oikonomou - <oikonomou@users.sourceforge.net>
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  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * Random number generator routines exploiting the cc2530 hardware
35  * capabilities.
36  *
37  * This file overrides core/lib/random.c.
38  *
39  * \author
40  * George Oikonomou - <oikonomou@users.sourceforge.net>
41  */
42 #include "cc253x.h"
43 #include "sfr-bits.h"
44 #include "dev/cc2530-rf.h"
45 /*---------------------------------------------------------------------------*/
46 /**
47  * \brief Generates a new random number using the cc253x RNG.
48  * \return The random number.
49  */
50 unsigned short
52 {
53  /* Clock the RNG LSFR once */
54  ADCCON1 |= ADCCON1_RCTRL0;
55 
56  return (RNDL | (RNDH << 8));
57 }
58 /*---------------------------------------------------------------------------*/
59 /**
60  * \brief Seed the cc253x random number generator.
61  * \param seed Ignored. It's here because the function prototype is in core.
62  *
63  * We form a seed for the RNG by sampling IF_ADC as
64  * discussed in the user guide.
65  * Seeding with this method should not be done during
66  * normal radio operation. Thus, use this function before
67  * initialising the network.
68  */
69 void
70 random_init(unsigned short seed)
71 {
72  int i;
73 
74  /* Make sure the RNG is on */
75  ADCCON1 &= ~(ADCCON1_RCTRL1 | ADCCON1_RCTRL0);
76 
77  /* Infinite RX */
78  FRMCTRL0 = FRMCTRL0_RX_MODE1;
79 
80  /* Turn RF on */
81  CC2530_CSP_ISRXON();
82 
83  /* Wait until (user guide sec. 23.12, p 239) "the chip has been in RX long
84  * enough for the transients to have died out. A convenient way to do this is
85  * to wait for the RSSI-valid signal to go high." */
86  while(!(RSSISTAT & RSSISTAT_RSSI_VALID));
87 
88  /*
89  * Form the seed by concatenating bits from IF_ADC in the RF receive path.
90  * Keep sampling until we have read at least 16 bits AND the seed is valid
91  *
92  * Invalid seeds are 0x0000 and 0x8003 - User Guide (sec. 14.2.2 p. 146):
93  * "Note that a seed value of 0x0000 or 0x8003 always leads to an unchanged
94  * value in the LFSR after clocking, as no values are pushed in via in_bit
95  * (see Figure 14-1); hence, neither of these seed values should not be used
96  * for random-number generation."
97  */
98  i = 0;
99  while(i < 16 || (seed == 0x0000 || seed == 0x8003)) {
100  seed = (seed << 1) | (RFRND & RFRND_IRND);
101  seed <<= 1;
102  i++;
103  }
104 
105  /* High byte first */
106  RNDL = seed >> 8;
107  RNDL = seed & 0xFF;
108 
109  /* RF Off. NETSTACK_RADIO.init() will sort out normal RF operation */
110  CC2530_CSP_ISRFOFF();
111 }
Definitions for TI/Chipcon cc2530, cc2531 and cc2533 SFR registers.
Header file with definitions of bit masks for some cc2530 SFRs
void random_init(unsigned short seed)
Seed the cc2430 random number generator.
Definition: random.c:41
Implementation of the cc2530 RF driver
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47