forked from Evsdd/EvohomeWirelessFW
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CCx.cpp
219 lines (160 loc) · 4.55 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
// EvohomeWirelessFW - RFBee firmware for evohome wireless communications
// Copyright (c) 2015 Hydrogenetic
//
// based on HoneyComm - Alternative RFBee firmware to communicate with
// HR80 radiator controllers.
//
// Copyright (C) 2011 Wladimir Komarow
//
// and work from CrazyDiamond and others at http://www.domoticaforum.eu/viewtopic.php?f=7&t=5806
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#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()
{
Spi.mode( ( 1<<SPR1 ) | ( 1<<SPR0 ) );//SPICLK=CPU/128
// 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);
// wait for MISO to go low
while(digitalRead(MISO_PIN));
Spi.slaveSelect(HIGH);
}
byte CCX::Read(byte addr,byte* data)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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()
{
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)
{
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();