-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCC1101.cpp
More file actions
321 lines (256 loc) · 7.79 KB
/
CC1101.cpp
File metadata and controls
321 lines (256 loc) · 7.79 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
/*
* Author: Arne Mauer
* Parts of this code is based on the itho-library made by 'supersjimmie', 'Thinkpad', 'Klusjesman' and 'jodur'.
*/
#include "CC1101.h"
// default constructor
CC1101::CC1101()
{
SPI.begin();
#ifdef ESP8266
pinMode(SS, OUTPUT);
#endif
} //CC1101
// default destructor
CC1101::~CC1101()
{
} //~CC1101
/***********************/
// SPI helper functions select() and deselect()
inline void CC1101::select(void) {
digitalWrite(SS, LOW);
}
inline void CC1101::deselect(void) {
digitalWrite(SS, HIGH);
}
void CC1101::spi_waitMiso()
{
while(digitalRead(MISO) == HIGH) delay(0); // delay will call esp_yield()
}
void CC1101::init()
{
reset();
}
void CC1101::reset()
{
deselect();
delayMicroseconds(5);
select();
delayMicroseconds(10);
deselect();
delayMicroseconds(45);
select();
spi_waitMiso();
SPI.transfer(CC1101_SRES);
delay(10);
spi_waitMiso();
deselect();
}
uint8_t CC1101::writeCommand(uint8_t command)
{
uint8_t result;
select();
spi_waitMiso();
result = SPI.transfer(command);
deselect();
return result;
}
void CC1101::writeRegister(uint8_t address, uint8_t data)
{
select();
spi_waitMiso();
SPI.transfer(address);
SPI.transfer(data);
deselect();
}
uint8_t CC1101::readRegister(uint8_t address)
{
uint8_t val;
select();
spi_waitMiso();
SPI.transfer(address);
val = SPI.transfer(0);
deselect();
return val;
}
uint8_t CC1101::readRegisterMedian3(uint8_t address)
{
uint8_t val, val1, val2, val3;
select();
spi_waitMiso();
SPI.transfer(address);
val1 = SPI.transfer(0);
SPI.transfer(address);
val2 = SPI.transfer(0);
SPI.transfer(address);
val3 = SPI.transfer(0);
deselect();
// reverse sort (largest in val1) because this is te expected order for TX_BUFFER
if (val3 > val2) {val = val3; val3 = val2; val2 = val; } //Swap(val3,val2)
if (val2 > val1) {val = val2; val2 = val1, val1 = val; } //Swap(val2,val1)
if (val3 > val2) {val = val3; val3 = val2, val2 = val; } //Swap(val3,val2)
return val2;
}
/* Known SPI/26MHz synchronization bug (see CC1101 errata)
This issue affects the following registers: SPI status byte (fields STATE and FIFO_BYTES_AVAILABLE),
FREQEST or RSSI while the receiver is active, MARCSTATE at any time other than an IDLE radio state,
RXBYTES when receiving or TXBYTES when transmitting, and WORTIME1/WORTIME0 at any time.*/
//uint8_t CC1101::readRegisterWithSyncProblem(uint8_t address, uint8_t registerType)
uint8_t /* ICACHE_RAM_ATTR */ CC1101::readRegisterWithSyncProblem(uint8_t address, uint8_t registerType)
{
uint8_t value1, value2;
value1 = readRegister(address | registerType);
//if two consecutive reads gives us the same result then we know we are ok
do
{
value2 = value1;
value1 = readRegister(address | registerType);
}
while (value1 != value2);
return value1;
}
//registerType = CC1101_CONFIG_REGISTER or CC1101_STATUS_REGISTER
uint8_t CC1101::readRegister(uint8_t address, uint8_t registerType)
{
switch (address)
{
case CC1101_FREQEST:
case CC1101_MARCSTATE:
case CC1101_RXBYTES:
case CC1101_TXBYTES:
case CC1101_WORTIME1:
case CC1101_WORTIME0:
return readRegisterWithSyncProblem(address, registerType);
default:
return readRegister(address | registerType);
}
}
void CC1101::writeBurstRegister(uint8_t address, uint8_t* data, uint8_t length)
{
uint8_t i;
select();
spi_waitMiso();
SPI.transfer(address | CC1101_WRITE_BURST);
for (i = 0; i < length; i++) {
SPI.transfer(data[i]);
}
deselect();
}
void CC1101::readBurstRegister(uint8_t* buffer, uint8_t address, uint8_t length)
{
uint8_t i;
select();
spi_waitMiso();
SPI.transfer(address | CC1101_READ_BURST);
for (i = 0; i < length; i++) {
buffer[i] = SPI.transfer(0x00);
}
deselect();
}
uint8_t CC1101::getRxBytes(){
uint8_t rxBytes = readRegisterWithSyncProblem(CC1101_RXBYTES, CC1101_STATUS_REGISTER);
return (rxBytes & CC1101_BITS_RX_BYTES_IN_FIFO);
}
/* 6 (0x06) = Asserts when sync word has been sent / received, and de-asserts at the end of the packet.
// automatic CRC
=> In RX, the pin will also deassert when a packet is discarded due to address or maximum length filtering
*/
uint8_t CC1101::receiveData(CC1101Packet* packet){
uint8_t val;
uint8_t rxBytes = readRegisterWithSyncProblem(CC1101_RXBYTES, CC1101_STATUS_REGISTER);
// char bigLogBuf[20];
// snprintf(bigLogBuf, sizeof(bigLogBuf), "rxBYTES: %u;", (rxBytes & CC1101_BITS_RX_BYTES_IN_FIFO));
// Serial.println(bigLogBuf);
// Any byte waiting to be read
if ( (rxBytes & CC1101_BITS_RX_BYTES_IN_FIFO)){
// get first byte of packet => contains total bytes of message (excl. crc)
//readBurstRegister(packet_length, CC1101_RXFIFO, 1);
packet->length = readRegister(CC1101_RXFIFO | CC1101_READ_SINGLE);
// If packet is too long
if (packet->length > CCPACKET_DATA_LEN){
flushRXAndSwitchToRX();
packet->length = 0; // Discard packet
} else {
// Read data packet
readBurstRegister(packet->data, CC1101_RXFIFO, packet->length);
// Read RSSI
packet->rssi = readRegister(CC1101_RXFIFO | CC1101_READ_SINGLE);
// Read LQI and CRC_OK
val = readRegister(CC1101_RXFIFO | CC1101_READ_SINGLE);
packet->lqi = val & 0x7F;
packet->crc_ok = bitRead(val, 7);
}
}else{
packet->length = 0;
}
return packet->length;
}
bool CC1101::checkForRxFifoOverFlow(){
uint8_t MarcState = (readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & CC1101_BITS_MARCSTATE);
if (MarcState == CC1101_MARCSTATE_RXFIFO_OVERFLOW){
flushRXAndSwitchToRX();
return true;
}else{
return false;
}
}
void CC1101::flushRXAndSwitchToRX(){
readRegister(CC1101_RXFIFO | CC1101_READ_SINGLE);
delayMicroseconds(0);
writeCommand(CC1101_SIDLE); //idle
writeCommand(CC1101_SFRX); //flush RX buffer
writeCommand(CC1101_SRX); //switch to RX state
}
//This function is able to send packets bigger then the FIFO size.
void CC1101::sendData(CC1101Packet *packet)
{
uint8_t index = 0;
uint8_t txStatus, MarcState;
uint8_t length;
writeCommand(CC1101_SIDLE); //idle
txStatus = readRegisterWithSyncProblem(CC1101_TXBYTES, CC1101_STATUS_REGISTER);
//clear TX fifo if needed
if (txStatus & CC1101_BITS_TX_FIFO_UNDERFLOW)
{
writeCommand(CC1101_SIDLE); //idle
writeCommand(CC1101_SFTX); //flush TX buffer
}
writeCommand(CC1101_SIDLE); //idle
//determine how many bytes to send
length = (packet->length <= CC1101_DATA_LEN ? packet->length : CC1101_DATA_LEN);
packet->data[0] = (packet->length -1 );
writeBurstRegister(CC1101_TXFIFO, packet->data, length);
//start sending packet
writeCommand(CC1101_STX);
//continue sending when packet is bigger than 64 bytes
if (packet->length > CC1101_DATA_LEN)
{
index += length;
//loop until all bytes are transmitted
while (index < packet->length)
{
//check if there is free space in the fifo
while ((txStatus = (readRegisterMedian3(CC1101_TXBYTES | CC1101_STATUS_REGISTER) & CC1101_BITS_RX_BYTES_IN_FIFO)) > (CC1101_DATA_LEN - 2));
//calculate how many bytes we can send
length = (CC1101_DATA_LEN - txStatus);
length = ((packet->length - index) < length ? (packet->length - index) : length);
//send some more bytes
for (int i=0; i<length; i++)
writeRegister(CC1101_TXFIFO, packet->data[index+i]);
index += length;
}
}
//wait until transmission is finished (TXOFF_MODE is expected to be set to 3/RX)
//
// TODO: ADD some timeout!
//
do
{
//delay(1); // to prevent watchdog resets esp! We cant call yield here...
//esp_yield();
delay(0); // delay will call esp_yield()
MarcState = (readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & CC1101_BITS_MARCSTATE);
if (MarcState == CC1101_MARCSTATE_TXFIFO_UNDERFLOW) Serial.print(F("TXFIFO_UNDERFLOW occured in sendData() \n"));
}
while ((readRegisterWithSyncProblem(CC1101_MARCSTATE, CC1101_STATUS_REGISTER)) != CC1101_MARCSTATE_RX);
}