-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCCx.cpp
223 lines (168 loc) · 4.79 KB
/
CCx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// CCx.cpp Class to control the chipcon CCxxxx series transceivers
// see http://focus.ti.com/lit/ds/symlink/cc1101.pdf for details on the CC1101
// Copyright (c) 2010 Hans Klunder <hans.klunder (at) bigfoot.com>
// Author: Hans Klunder, based on the original Rfbee v1.0 firmware by Seeedstudio
// Version: May 22, 2010
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "WProgram.h"
#include "CCx.h"
#include "CCxCfg.h"
#include "Spi.h"
//---------- constructor ----------------------------------------------------
CCX::CCX(){}
// Power On Reset as described in 19.1.2 of cc1100 datasheet, tried APOR as described in 19.1.1 but that did not work :-(
void CCX::PowerOnStartUp()
{
DEBUGPRINT()
Spi.mode((1 << SPR1) | (1 << SPR0));//SPICLK=CPU/64
// start manual Power On Reset
Spi.slaveSelect(HIGH);
delayMicroseconds(1);
Spi.slaveSelect(LOW);
delayMicroseconds(10);
Spi.slaveSelect(HIGH);
delayMicroseconds(41);
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
Spi.transfer(CCx_SRES);
DEBUGPRINT("Waiting for CCx to complete POR")
// wait for MISO to go low
while(digitalRead(MISO_PIN));
Spi.slaveSelect(HIGH);
DEBUGPRINT("CCx POR complete")
}
byte CCX::Read(byte addr,byte* data)
{
DEBUGPRINT()
byte result;
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
result=Spi.transfer(addr | 0x80);
*data=Spi.transfer(0);
Spi.slaveSelect(HIGH);
return result;
}
byte CCX::ReadBurst(byte addr, byte* dataPtr, byte size)
{
DEBUGPRINT()
byte result;
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
result=Spi.transfer(addr | 0xc0);
while(size)
{
*dataPtr++ = Spi.transfer(0);
size--;
}
Spi.slaveSelect(HIGH);
return result;
}
byte CCX::Write(byte addr, byte dat)
{
DEBUGPRINT()
byte result;
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
result=Spi.transfer(addr);
result=Spi.transfer(dat);
Spi.slaveSelect(HIGH);
return result;
}
byte CCX::WriteBurst(byte addr, const byte* dataPtr, byte size)
{
DEBUGPRINT()
byte result;
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
result=Spi.transfer(addr | 0x40);
while(size)
{
result = Spi.transfer(*dataPtr++);
size--;
}
Spi.slaveSelect(HIGH);
return result;
}
byte CCX::Strobe(byte addr)
{
DEBUGPRINT()
byte result;
Spi.slaveSelect(LOW);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
result=Spi.transfer(addr);
Spi.slaveSelect(HIGH);
return result;
}
//configure registers of cc1100 making it work in specific mode
void CCX::Setup(byte configId)
{
DEBUGPRINT()
byte reg;
byte val;
if (configId < CCX_NR_OF_CONFIGS)
for(byte i = 0; i< CCX_NR_OF_REGISTERS; i++){
reg=pgm_read_byte(&CCx_registers[i]);
val=pgm_read_byte(&CCx_registerSettings[configId][i]);//read flash data no problem
byte temp = Write(reg,val);
//Serial.print(temp,HEX);
//Serial.print(" ");
}
}
// to aid debugging
//#ifdef DEBUG
void CCX::ReadSetup()
{
DEBUGPRINT()
byte reg;
byte value;
for(byte i = 0; i< CCX_NR_OF_REGISTERS; i++){
reg=pgm_read_byte(&CCx_registers[i]);
Read(reg,&value);
Serial.print(reg,HEX);
Serial.print(':');
Serial.println(value,HEX);
}
}
//#endif
void CCX::setPA(byte configId,byte paIndex)
{
DEBUGPRINT()
byte PAval=pgm_read_byte(&CCx_paTable[configId][paIndex]);
CCx.Write(CCx_PATABLE,PAval);
}
void CCX::Mode(byte md){
}
byte CCX::NrOfConfigs(){
return CCX_NR_OF_CONFIGS;
}
byte CCX::RSSIdecode(byte rssiEnc){
byte rssi;
byte rssiOffset=74; // is actually dataRate dependant, but for simplicity assumed to be fixed.
// RSSI is coded as 2's complement see section 17.3 RSSI of the cc1100 datasheet
if (rssiEnc >= 128)
rssi = (( rssiEnc - 256) >> 1) - rssiOffset;
else
rssi = (rssiEnc >> 1) - rssiOffset;
return rssi;
}
//---------- preinstantiate CCx object --------------------------------------
CCX CCx = CCX();