Contiki-Inga 3.x
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
cpu
cc2538
dev
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 core/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
*/
57
unsigned
short
58
random_rand
(
void
)
59
{
60
uint32_t rv;
61
62
/* Clock the RNG LSFR once */
63
REG(
SOC_ADC_ADCCON1
) |=
SOC_ADC_ADCCON1_RCTRL0
;
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
*/
83
void
84
random_init
(
unsigned
short
seed)
85
{
86
int
i;
87
unsigned
short
s = 0;
88
89
/* Make sure the RNG is on */
90
REG(
SOC_ADC_ADCCON1
) &= ~(
SOC_ADC_ADCCON1_RCTRL1
|
SOC_ADC_ADCCON1_RCTRL0
);
91
92
/* Enable clock for the RF Core */
93
REG(
SYS_CTRL_RCGCRFC
) = 1;
94
95
/* Infinite RX - FRMCTRL0[3:2] = 10
96
* This will mess with radio operation - see note above */
97
REG(
RFCORE_XREG_FRMCTRL0
) = 0x00000008;
98
99
/* Turn RF on */
100
CC2538_RF_CSP_ISRXON
();
101
102
/*
103
* Wait until "the chip has been in RX long enough for the transients to
104
* have died out. A convenient way to do this is to wait for the RSSI-valid
105
* signal to go high."
106
*/
107
while
(!(REG(
RFCORE_XREG_RSSISTAT
) &
RFCORE_XREG_RSSISTAT_RSSI_VALID
));
108
109
/*
110
* Form the seed by concatenating bits from IF_ADC in the RF receive path.
111
* Keep sampling until we have read at least 16 bits AND the seed is valid
112
*
113
* Invalid seeds are 0x0000 and 0x8003 and should not be used.
114
*/
115
while
(s == 0x0000 || s == 0x8003) {
116
for
(i = 0; i < 16; i++) {
117
s |= (REG(
RFCORE_XREG_RFRND
) &
RFCORE_XREG_RFRND_IRND
);
118
s <<= 1;
119
}
120
}
121
122
/* High byte first */
123
REG(
SOC_ADC_RNDL
) = (s >> 8) & 0x00FF;
124
REG(
SOC_ADC_RNDL
) = s & 0xFF;
125
126
/* RF Off. NETSTACK_RADIO.init() will sort out normal RF operation */
127
CC2538_RF_CSP_ISRFOFF
();
128
}
129
130
/**
131
* @}
132
* @}
133
*/
Generated on Thu Apr 24 2014 16:26:11 for Contiki-Inga 3.x by
1.8.3.1