Repository navigation
Expand file tree
/
Copy pathCC1101_ESP_Arduino.cpp
More file actions
325 lines (276 loc) · 7.84 KB
/
Copy pathCC1101_ESP_Arduino.cpp
File metadata and controls
325 lines (276 loc) · 7.84 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* CC1101.cpp - CC1101 module library
* Copyright (c) 2010 Michael.
* Author: Michael, <www.elechouse.com>
* Version: November 12, 2010
*
* This library is designed to use CC1101/CC1100 module on Arduino platform.
* CC1101/CC1100 module is an useful wireless module.Using the functions of the
* library, you can easily send and receive data by the CC1101/CC1100 module.
* Just have fun!
* For the details, please refer to the datasheet of CC1100/CC1101.
* --------------------------------------------------------------------------------
*
* CC1101 ESP32/ESP8266/Arduino Driver. Mod by wladimir-computin.
*
*/
#include "CC1101_ESP_Arduino.h"
/****************************************************************/
CC1101::CC1101(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t gdo0, int8_t gdo2, SPIClass *spiInstance){
SCK_PIN = sck;
MISO_PIN = miso;
MOSI_PIN = mosi;
SS_PIN = cs;
GDO0 = gdo0;
GDO2 = gdo2;
_spi = spiInstance;
}
void CC1101::spiStart(){
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SS_PIN, OUTPUT);
if(GDO2 != -1){
pinMode(GDO0, OUTPUT);
pinMode(GDO2, INPUT);
} else {
pinMode(GDO0, INPUT);
}
#if defined ESP32
_spi->begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
#else
_spi->begin();
#endif
}
void CC1101::spiEnd(){
_spi->end();
}
void CC1101::spiStartTransaction(){
_spi->beginTransaction(spiSettings);
digitalWrite(SS_PIN, LOW);
while(digitalRead(MISO_PIN)){
yield();
};
}
void CC1101::spiEndTransaction(){
delay(1);
digitalWrite(SS_PIN, HIGH);
_spi->endTransaction();
}
void CC1101::spiStrobe(uint8_t strobe){
spiStartTransaction();
_spi->transfer(strobe);
spiEndTransaction();
}
uint8_t CC1101::spiReadReg(uint8_t addr){
uint8_t temp, value;
temp = addr| READ_SINGLE;
spiStartTransaction();
_spi->transfer(temp);
value=_spi->transfer(0);
spiEndTransaction();
return value;
}
uint8_t CC1101::spiReadStatus(uint8_t addr){
uint8_t value,temp;
temp = addr | READ_BURST;
spiStartTransaction();
_spi->transfer(temp);
value=_spi->transfer(0);
spiEndTransaction();
return value;
}
void CC1101::spiReadRegBurst(uint8_t addr, uint8_t *buffer, uint8_t buffer_len){
uint8_t temp;
temp = addr | READ_BURST;
spiStartTransaction();
_spi->transfer(temp);
for(uint8_t i = 0; i < buffer_len; i++){
buffer[i]=_spi->transfer(0);
}
spiEndTransaction();
}
void CC1101::spiWriteReg(uint8_t addr, uint8_t value){
spiStartTransaction();
_spi->transfer(addr);
_spi->transfer(value);
spiEndTransaction();
}
void CC1101::spiWriteRegBurst(uint8_t addr, const uint8_t *buffer, uint8_t buffer_len){
uint8_t temp;
temp = addr | WRITE_BURST;
spiStartTransaction();
_spi->transfer(temp);
for (uint8_t i = 0; i < buffer_len; i++) {
_spi->transfer(buffer[i]);
}
spiEndTransaction();
}
void CC1101::init(){
spiStart();
hardReset();
regConfigSettings();
setIdle();
}
void CC1101::regConfigSettings() {
//spiWriteReg(CC1101_FSCTRL1, 0x06);
//spiWriteReg(CC1101_FSCTRL0, 0x00);
spiWriteReg(CC1101_MDMCFG2, 0b00110000);
//spiWriteReg(CC1101_DEVIATN, 0x15);
//spiWriteReg(CC1101_FREND1, 0x56);
spiWriteReg(CC1101_FREND0, 0b00010001);
spiWriteReg(CC1101_MCSM0 , 0b00011000);
spiWriteReg(CC1101_FOCCFG, 0b00010100);
//spiWriteReg(CC1101_BSCFG, 0x1C);
//spiWriteReg(CC1101_AGCCTRL2, 0xC7);
//spiWriteReg(CC1101_AGCCTRL1, 0x00);
//spiWriteReg(CC1101_AGCCTRL0, 0xB2);
//spiWriteReg(CC1101_FSCAL3, 0xE9);
//spiWriteReg(CC1101_FSCAL2, 0x2A);
//spiWriteReg(CC1101_FSCAL1, 0x00);
//spiWriteReg(CC1101_FSCAL0, 0x1F);
//spiWriteReg(CC1101_FSTEST, 0x59);
//spiWriteReg(CC1101_TEST2, 0x81);
//spiWriteReg(CC1101_TEST1, 0x35);
//spiWriteReg(CC1101_TEST0, 0x09);
if(GDO2 != -1){
spiWriteReg(CC1101_IOCFG2, 0x0D); //Serial Data Output. Used for asynchronous serial mode.
} else {
spiWriteReg(CC1101_IOCFG0, 0x0D); //Serial Data Output. Used for asynchronous serial mode.
}
spiWriteReg(CC1101_PKTCTRL0, 0b00110010);
//spiWriteReg(CC1101_PKTCTRL1, 0x04);
//spiWriteReg(CC1101_PKTCTRL0, 0x32);
//spiWriteReg(CC1101_ADDR, 0x00);
//spiWriteReg(CC1101_PKTLEN, 0x3D);
}
void CC1101::hardReset(){
digitalWrite(SS_PIN, LOW);
delay(1);
digitalWrite(SS_PIN, HIGH);
delay(1);
spiStartTransaction();
_spi->transfer(CC1101_SRES);
spiEndTransaction();
spiStrobe(CC1101_SCAL);
}
void CC1101::softReset(){
spiStrobe(CC1101_SRES);
}
void CC1101::setMHZ(double target_freq_mhz){
target_freq_mhz -= 0.015;
unsigned long target_freq = target_freq_mhz * pow(10,6);
unsigned long freq_raw = (target_freq * pow(2,16)) / crystal;
uint8_t freq[3];
freq[0] = (freq_raw & 0x00FF0000) >> 16;
freq[1] = (freq_raw & 0x0000FF00) >> 8;
freq[2] = freq_raw & 0x000000FF;
spiWriteRegBurst(CC1101_FREQ2, freq, sizeof(freq) / sizeof(uint8_t));
}
void CC1101::setTXPwr(TX_DBM value){
uint8_t patable[] = {0x00, PA_433[value], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
spiWriteRegBurst(CC1101_PATABLE, patable, 8);
}
void CC1101::setChannelSpacing(int spacing_hz){
unsigned long spacing = spacing_hz * 1000;
uint8_t mdmcfg1 = spiReadReg(CC1101_MDMCFG1) & 0b11111100;
uint8_t chsp_e = log2((spacing * pow(2,10)) / crystal);
uint8_t chsp_m = ((spacing * pow(2,18)) / (crystal * pow(2,chsp_e))) - 256;
mdmcfg1 = mdmcfg1 | chsp_e;
spiWriteReg(CC1101_MDMCFG1, mdmcfg1);
spiWriteReg(CC1101_MDMCFG0, chsp_m);
}
void CC1101::setRxBW(RX_BW_KHZ RxBW){
if(RxBW < 16){
uint8_t drate_e = spiReadReg(CC1101_MDMCFG4) & 0b00001111;
uint8_t rbw = (RxBW << 4) | drate_e;
spiWriteReg(CC1101_MDMCFG4, rbw);
}
}
void CC1101::setDataRate(unsigned long baud){
if (baud >= 600 && baud <= 500000){
uint8_t rbw = spiReadReg(CC1101_MDMCFG4) & 0b11110000;
uint8_t drate_e = log2((baud * pow(2,20)) / crystal);
uint8_t drate_m = ((baud * pow(2,28)) / (crystal * pow(2,drate_e))) - 256;
uint8_t mdmcfg4 = rbw | (drate_e & 0b00001111);
spiWriteReg(CC1101_MDMCFG4, mdmcfg4);
spiWriteReg(CC1101_MDMCFG3, drate_m);
}
}
void CC1101::setModulation(MOD_FORMAT mod){
uint8_t data_old = spiReadReg(CC1101_MDMCFG2) & 0b10001111;
uint8_t data_new = ((uint8_t)mod << 4) | data_old;
spiWriteReg(CC1101_MDMCFG2, data_new);
}
void CC1101::setDeviationHZ(int deviation_hz){
uint8_t deviation_m = 0;
uint8_t deviation_e = 0;
int closest = INT_MAX;
for(uint8_t test_e = 0; test_e < 8; test_e++){
for(uint8_t test_m = 0; test_m < 8; test_m++){
int test_deviation = (crystal/pow(2,17))*(8+test_m)*pow(2, test_e);
int diff = abs(deviation_hz - test_deviation);
if (diff < closest){
closest = diff;
deviation_m = test_m;
deviation_e = test_e;
}
}
}
uint8_t data_new = (deviation_e << 4) | deviation_m;
Serial.println(data_new);
spiWriteReg(CC1101_DEVIATN, data_new);
}
void CC1101::setChannel(uint8_t channel){
spiWriteReg(CC1101_CHANNR, channel);
}
void CC1101::setIdle(){
spiStrobe(CC1101_SIDLE);
}
void CC1101::setTx(){
setIdle();
spiStrobe(CC1101_STX);
if(GDO2 == -1){
pinMode(GDO0, OUTPUT);
}
}
void CC1101::setRx(){;
setIdle();
spiStrobe(CC1101_SRX);
if(GDO2 == -1){
pinMode(GDO0, INPUT);
}
}
uint8_t CC1101::receiveData(uint8_t *rxBuffer, uint8_t len){
uint8_t size;
uint8_t status[2];
if(spiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO) {
size=spiReadReg(CC1101_RXFIFO);
spiReadRegBurst(CC1101_RXFIFO,rxBuffer,len);
spiReadRegBurst(CC1101_RXFIFO,status,2);
spiStrobe(CC1101_SFRX);
return size;
} else {
spiStrobe(CC1101_SFRX);
return 0;
}
}
uint8_t CC1101::getPartnum(){
return spiReadStatus(CC1101_PARTNUM);
}
uint8_t CC1101::getVersion(){
return spiReadStatus(CC1101_VERSION);
}
uint8_t CC1101::log2(double value){
return log10(value) / log10(2);
}
int8_t CC1101::getRSSI(){
uint8_t rssi_dec = spiReadStatus(CC1101_RSSI);
int8_t rssi_dbm;
if (rssi_dec >= 128) {
rssi_dbm = ((int16_t)rssi_dec - 256) / 2 - 74;
} else {
rssi_dbm = (int16_t)rssi_dec / 2 - 74;
}
return rssi_dbm;
}