Contiki-Inga 3.x
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
cpu
cc2430
dev
random.c
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2010, Loughborough University - Computer Science
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 cc2430 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 "
cc2430_sfr.h
"
43
#include "
dev/cc2430_rf.h
"
44
/*---------------------------------------------------------------------------*/
45
/**
46
* \brief Generates a new random number using the cc2430 RNG.
47
* \return The random number.
48
*/
49
unsigned
short
50
random_rand
(
void
)
51
{
52
/* Clock the RNG LSFR once */
53
ADCCON1 |= ADRCTRL0;
54
55
return
(RNDL | (RNDH << 8));
56
}
57
/*---------------------------------------------------------------------------*/
58
/**
59
* \brief Seed the cc2430 random number generator.
60
* \param seed Seed value for the RNG.
61
*
62
* If the SEED argument is 0, seed the RNG with IF_ADC as
63
* discussed in the cc2430 datasheet (rev. 2.1), section 13.11.2.2,
64
* page 134. Seeding with this method should not be done during
65
* normal radio operation. Thus, use this function before
66
* initialising the network.
67
*
68
* If the SEED is provided, seed with this value instead. This will
69
* result in the same sequence of random numbers each time the node
70
* reboots. So, don't use it unless you have a reason (e.g. tests)
71
*/
72
void
73
random_init
(
unsigned
short
seed)
74
{
75
int
i;
76
77
/* Comment out this if() block to save a nice 16 bytes of code size */
78
if
(seed) {
79
/* If the caller provides a seed, write the high-byte first and then
80
* write the low byte */
81
RNDL = seed >> 8;
/* High byte first */
82
RNDL = seed & 0xFF;
83
return
;
84
}
85
86
/*
87
* cc2430 Datasheet:
88
* "When a true random value is required, the LFSR should be seeded by
89
* writing RNDL with random values from the IF_ADC in the RF receive path."
90
*
91
* "To use this seeding method, the radio must first be powered on by
92
* enabling the voltage regulator"
93
*/
94
RFPWR &= ~RREG_RADIO_PD;
/* Turn on the voltage regulator */
95
while
(!(RFIF & IRQ_RREG_ON));
/* Wait for power up*/
96
97
/* OK, it's powered. The respective interrupt flag has been set, clear it */
98
RFIF &= ~IRQ_RREG_ON;
99
100
/*
101
* "The radio should be placed in infinite TX state, to avoid possible sync
102
* detect in RX state."
103
*
104
* Judging by old chipcon cc2430 code examples as well as by the way cc2530
105
* works, this is very likely to be "RX state" (i.e. a typo in the datasheet)
106
*
107
* With infinite TX, ADCTSTx always read as 0 so we'll use infinite RX
108
*/
109
MDMCTRL1L = 0x02;
/* RX mode 10 - RX_INFINITE state */
110
111
/* "Enter RX State - Immediate" command strobe */
112
cc2430_rf_command
(ISRXON);
113
114
/* Make sure the RNG is on */
115
ADCCON1 &= ~(ADRCTRL1 | ADRCTRL0);
116
117
/* Wait for IF_ADC I-branch and Q-branch values */
118
while
(!(ADCTSTH & ADCTSTL));
119
120
/* 32 times as per the chipcon example. This seems to increase randomness */
121
for
(i = 0; i < 32; i++) {
122
/* Seed the RNG by writing into RNDL twice with values from ADCTSTx */
123
RNDL = ADCTSTH;
124
RNDL = ADCTSTL;
125
126
/* Clock the RNG LSFR once */
127
ADCCON1 |= ADRCTRL0;
128
}
129
130
/*
131
* Exit RX state. Just shut down, network initialisation will take care of
132
* properly starting the radio for us.
133
*/
134
RFPWR |= RREG_RADIO_PD;
135
}
Generated on Thu Apr 24 2014 16:26:11 for Contiki-Inga 3.x by
1.8.3.1