forked from LowPowerLab/RFM69
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFM69.cpp
565 lines (484 loc) · 20.1 KB
/
RFM69.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// **********************************************************************************
// Arduino library for HopeRF RFM69(W/HW/CW/HCW), Semtech SX1231/1231H
// **********************************************************************************
// forked from http://lowpowerlab.com, Felix Rusu (2014), [email protected]
//
// **********************************************************************************
// License
// **********************************************************************************
// 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 2 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, write
// to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Licence can be viewed at
// http://www.fsf.org/licenses/gpl.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
#include <RFM69.h>
#include <RFM69registers.h>
#include <SPI.h>
#include <EEPROM.h>
volatile byte RFM69::DATA[RF69_MAX_DATA_LEN];
volatile byte RFM69::_mode; // current transceiver state
volatile boolean RFM69::dataReceived;
volatile byte RFM69::DATALEN;
volatile byte RFM69::SENDERID;
volatile byte RFM69::TARGETID; //should match _address
volatile byte RFM69::PAYLOADLEN;
volatile byte RFM69::ACK_REQUESTED;
volatile byte RFM69::ACK_RECEIVED; /// Should be polled immediately after sending a packet with ACK request
volatile int RFM69::RSSI; //most accurate RSSI during reception (closest to the reception)
RFM69* RFM69::selfPointer;
bool RFM69::initialize(byte nodeId, byte networkId, char* encryptKey){
if(_debug){
Serial.print(F("RFM69 init : nodeId=")); Serial.print(nodeId); Serial.print(F(" networdId=")); Serial.println(networkId);
}
const byte CONFIG[][2] =
{
/* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY },
/* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, //no shaping
/* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, //default:4.8 KBPS
/* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_55555},
/* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, //default:5khz, (FDEV + BitRate/2 <= 500Khz)
/* 0x06 */ { REG_FDEVLSB, RF_FDEVLSB_50000},
/* 0x07 */ { REG_FRFMSB, (_frequencyBand==RF69_315MHZ ? RF_FRFMSB_315 : (_frequencyBand==RF69_433MHZ ? RF_FRFMSB_433 : (_frequencyBand==RF69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915))) },
/* 0x08 */ { REG_FRFMID, (_frequencyBand==RF69_315MHZ ? RF_FRFMID_315 : (_frequencyBand==RF69_433MHZ ? RF_FRFMID_433 : (_frequencyBand==RF69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915))) },
/* 0x09 */ { REG_FRFLSB, (_frequencyBand==RF69_315MHZ ? RF_FRFLSB_315 : (_frequencyBand==RF69_433MHZ ? RF_FRFLSB_433 : (_frequencyBand==RF69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915))) },
// looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm
// +17dBm and +20dBm are possible on RFM69HW
// +13dBm formula: Pout=-18+OutputPower (with PA0 or PA1**)
// +17dBm formula: Pout=-14+OutputPower (with PA1 and PA2)**
// +20dBm formula: Pout=-11+OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet)
///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111},
///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, //over current protection (default is 95mA)
// RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4khz)
/* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, //(BitRate < 2 * RxBw)
//for BR-19200: //* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
/* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, //DIO0 is the only IRQ we're using
/* 0x29 */ { REG_RSSITHRESH, 220 }, //must be set to dBm = (-Sensitivity / 2) - default is 0xE4=228 so -114dBm
///* 0x2d */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA
/* 0x2e */ { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 },
/* 0x2f */ { REG_SYNCVALUE1, 0x2D }, //attempt to make this compatible with sync1 byte of RFM12B lib
/* 0x30 */ { REG_SYNCVALUE2, networkId }, //NETWORK ID
/* 0x37 */ { REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF },
/* 0x38 */ { REG_PAYLOADLENGTH, 66 }, //in variable length mode: the max frame size, not used in TX
//* 0x39 */ { REG_NODEADRS, nodeId }, //turned off because we're not using address filtering
/* 0x3C */ { REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE }, //TX on FIFO not empty
/* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, //RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
//for BR-19200: //* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, //RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
//* 0x6F */ { REG_TESTDAGC, RF_DAGC_CONTINUOUS }, // run DAGC continuously in RX mode
/* 0x6F */ { REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0 }, // run DAGC continuously in RX mode, recommended default for AfcLowBetaOn=0
{255, 0}
};
pinMode(_slaveSelectPin, OUTPUT);
SPI.begin();
do writeReg(REG_SYNCVALUE1, 0xaa); while (readReg(REG_SYNCVALUE1) != 0xaa);
do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55);
for (byte i = 0; CONFIG[i][0] != 255; i++)
writeReg(CONFIG[i][0], CONFIG[i][1]);
// Encryption is persistent between resets and can trip you up during debugging.
setEncrypt(encryptKey);
setHighPower(_isRFM69HW); //called regardless if it's a RFM69W or RFM69HW
setMode(RF69_MODE_STANDBY);
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
attachInterrupt(_interruptNum, RFM69::isr0, RISING);
selfPointer = this;
_address = nodeId;
if(_debug){
Serial.print(F("Transmitting at "));
Serial.print(_frequencyBand==RF69_315MHZ ? "315" : (_frequencyBand==RF69_433MHZ ? "433" : (_frequencyBand==RF69_868MHZ ? "868" : "915")));
Serial.println("MHz");
}
return true;
}
void RFM69::setDebug(boolean d){
_debug = d;
}
/**
* Enrollement du noeud vis-à-vis d'une la passerelle qui lui affecte un numéro
* 5 est le numéro de noeud réservé aux communications des noeuds non enrollés
* @param networkId
* @param gatewayId : nodeId de la gateway référente
* @param nodeId_eeprom_addr : Adresse de conservation du nodeId dans l'EEPROM
* @return nodeId affecté. 0 ou 255 si échec
*/
byte RFM69::enrollNode(byte networkId, byte gatewayId, unsigned int nodeId_eeprom_addr, char* encryptKey, byte retries, byte retryWaitTime){
if(_debug) Serial.println(F("enrollNode()"));
byte nodeId = EEPROM.read(nodeId_eeprom_addr);
if(_debug){ Serial.print(F("EEPROM nodeId:")); Serial.println(nodeId); }
if(nodeId >=10 && nodeId<255) return nodeId; //Déjà enrollé
Serial.println(F("Enrollement du noeud"));
nodeId = ENROLL_NODE_ID;
//Initialisation RFM
initialize(nodeId,networkId,encryptKey);
sleep();
delay(10);
//Envoi de la trame
char* payload = "E";
if(sendWithRetry(gatewayId, payload, strlen(payload), retries, retryWaitTime)){
//Attente de la réponse
if(_debug) Serial.println(F("Attente de la reponse"));
byte retry = 0;
boolean rd = false;
while(!rd && ++retry<250){
rd = receiveDone();
delay(5);
}
if(rd){
if(_debug) Serial.println(F("reponse recue"));
char buff[80]; //Buffer
buff[0] = '\0';
byte len = DATALEN;
if(len>=80) len = 79;
strncat(buff,(char*)DATA,len);
//Acquittement
if(ACK_REQUESTED){
sendACK();
if(_debug) Serial.println(F("ACK sent"));
}
if(_debug){ Serial.print(F("received: "));Serial.println(buff); }
char* nodeIdStr = &buff[5];
nodeId = atol(nodeIdStr);
EEPROM.write(nodeId_eeprom_addr,nodeId);
return nodeId;
}
else{
if(_debug) Serial.println(F("Echec: Aucune reponse recue"));
return 0;
}
}
else{
//Echec de l'enrollement
if(_debug) Serial.println(F("Echec: erreur de transmission"));
return 0;
}
}
void RFM69::setFrequency(uint32_t FRF){
writeReg(REG_FRFMSB, FRF >> 16);
writeReg(REG_FRFMID, FRF >> 8);
writeReg(REG_FRFLSB, FRF);
}
void RFM69::setMode(byte newMode){
if (newMode == _mode) return; //TODO: can remove this?
switch (newMode) {
case RF69_MODE_TX:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
if (_isRFM69HW) setHighPowerRegs(true);
break;
case RF69_MODE_RX:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
if (_isRFM69HW) setHighPowerRegs(false);
break;
case RF69_MODE_SYNTH:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
break;
case RF69_MODE_STANDBY:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_STANDBY);
break;
case RF69_MODE_SLEEP:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SLEEP);
break;
default: return;
}
// we are using packet mode, so this check is not really needed
// but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode
while (_mode == RF69_MODE_SLEEP && (readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
_mode = newMode;
}
void RFM69::sleep() {
setMode(RF69_MODE_SLEEP);
}
void RFM69::setAddress(byte addr){
_address = addr;
writeReg(REG_NODEADRS, _address);
}
// set output power: 0=min, 31=max
// this results in a "weaker" transmitted signal, and directly results in a lower RSSI at the receiver
void RFM69::setPowerLevel(byte powerLevel){
_powerLevel = powerLevel;
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0xE0) | (_powerLevel > 31 ? 31 : _powerLevel));
}
bool RFM69::canSend(){
if (_mode == RF69_MODE_RX && PAYLOADLEN == 0 && readRSSI() < CSMA_LIMIT) // if signal stronger than -100dBm is detected assume channel activity
{
setMode(RF69_MODE_STANDBY);
return true;
}
return false;
}
void RFM69::send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK){
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
long now = millis();
while (!canSend() && millis()-now < RF69_CSMA_LIMIT_MS){
receiveDone();
}
sendFrame(toAddress, buffer, bufferSize, requestACK, false);
}
// to increase the chance of getting a packet across, call this function instead of send
// and it handles all the ACK requesting/retrying for you :)
// The only twist is that you have to manually listen to ACK requests on the other side and send back the ACKs
// The reason for the semi-automaton is that the lib is ingterrupt driven and
// requires user action to read the received data and decide what to do with it
// replies usually take only 5-8ms at 50kbps@915Mhz
bool RFM69::sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries, byte retryWaitTime) {
long sentTime;
for (byte i=0; i<=retries; i++)
{
send(toAddress, buffer, bufferSize, true);
sentTime = millis();
while (millis()-sentTime<retryWaitTime)
{
if (ACKReceived(toAddress))
{
//if(_debug) Serial.print(" ~ms:");Serial.print(millis()-sentTime);
return true;
}
}
if(_debug){ Serial.print(" No ack received in "); Serial.print(retryWaitTime); Serial.print("ms RETRY ");Serial.println(i+1); }
}
return false;
}
/// Should be polled immediately after sending a packet with ACK request
bool RFM69::ACKReceived(byte fromNodeID) {
if (receiveDone())
return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
return false;
}
//check whether an ACK was requested in the last received packet (non-broadcasted packet)
bool RFM69::ACKRequested() {
return ACK_REQUESTED && (TARGETID != RF69_BROADCAST_ADDR);
}
// Should be called immediately after reception in case sender wants ACK
void RFM69::sendACK(const void* buffer, byte bufferSize) {
byte sender = SENDERID;
while (!canSend()) receiveDone();
sendFrame(sender, buffer, bufferSize, false, true);
}
void RFM69::sendFrame(byte toAddress, const void* buffer, byte bufferSize, bool requestACK, bool sendACK){
if(_debug){
Serial.print(F("sendFrame to ")); Serial.print(toAddress); Serial.print(":"); Serial.write((char*)buffer,bufferSize);
if(requestACK) Serial.print(F(" ack request"));
if(sendACK) Serial.print(F(" ack"));
Serial.println();
}
setMode(RF69_MODE_STANDBY); //turn off receiver to prevent reception while filling fifo
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
byte ackByte = 0x00;
if (sendACK) ackByte = ackByte | 0x80;
else if (requestACK) ackByte = ackByte | 0x40;
//write to FIFO
select();
SPI.transfer(REG_FIFO | 0x80);
SPI.transfer(bufferSize + 3); //Payload length = toAddress byte + ack byte + buffer length
SPI.transfer(toAddress); //Destinataire address
SPI.transfer(_address); //Source address
SPI.transfer(ackByte); //Ack byte
for (byte i = 0; i < bufferSize; i++){
SPI.transfer(((uint8_t*)buffer)[i]);
}
unselect();
/* no need to wait for transmit mode to be ready since its handled by the radio */
setMode(RF69_MODE_TX);
uint32_t txStart = millis();
while (digitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); //wait for DIO0 to turn HIGH signalling transmission finish
//Si le RFM n'est pas présent, on sort sur timeout
//while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // Wait for ModeReady
setMode(RF69_MODE_STANDBY);
}
void RFM69::interruptHandler() {
//pinMode(4, OUTPUT);
//digitalWrite(4, 1);
dataReceived = true;
//digitalWrite(4, 0);
}
void RFM69::isr0() { selfPointer->interruptHandler(); }
void RFM69::receiveBegin() {
DATALEN = 0;
SENDERID = 0;
TARGETID = 0;
PAYLOADLEN = 0;
ACK_REQUESTED = 0;
ACK_RECEIVED = 0;
RSSI = 0;
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); //set DIO0 to "PAYLOADREADY" in receive mode
setMode(RF69_MODE_RX);
}
void RFM69::readReceivedData(){
if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY))
{
setMode(RF69_MODE_STANDBY);
select();
SPI.transfer(REG_FIFO & 0x7f);
PAYLOADLEN = SPI.transfer(0);
PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; //precaution
TARGETID = SPI.transfer(0);
if(!(_promiscuousMode || TARGETID==_address || TARGETID==RF69_BROADCAST_ADDR)) //match this node's address, or broadcast address or anything in promiscuous mode
{
PAYLOADLEN = 0;
unselect();
return;
}
DATALEN = PAYLOADLEN - 3;
SENDERID = SPI.transfer(0);
byte CTLbyte = SPI.transfer(0);
ACK_RECEIVED = CTLbyte & 0x80; //extract ACK-requested flag
ACK_REQUESTED = CTLbyte & 0x40; //extract ACK-received flag
for (byte i= 0; i < DATALEN; i++)
{
DATA[i] = SPI.transfer(0);
}
if (DATALEN<RF69_MAX_DATA_LEN) DATA[DATALEN]=0; //add null at end of string
unselect();
setMode(RF69_MODE_RX);
}
RSSI = readRSSI();
dataReceived = false;
}
bool RFM69::receiveDone() {
// ATOMIC_BLOCK(ATOMIC_FORCEON)
// {
//noInterrupts(); //re-enabled in unselect() via setMode() or via receiveBegin()
if(dataReceived)
readReceivedData();
if (_mode == RF69_MODE_RX && PAYLOADLEN>0)
{
setMode(RF69_MODE_STANDBY); //enables interrupts
return true;
}
else if (_mode == RF69_MODE_RX) //already in RX no payload yet
{
interrupts(); //explicitly re-enable interrupts
return false;
}
receiveBegin();
return false;
//}
}
// To enable encryption: radio.encrypt("ABCDEFGHIJKLMNOP");
// To disable encryption: radio.encrypt(null) or radio.encrypt(0)
// KEY HAS TO BE 16 bytes !!!
void RFM69::setEncrypt(const char* key) {
setMode(RF69_MODE_STANDBY);
if (key!=0)
{
select();
SPI.transfer(REG_AESKEY1 | 0x80);
for (byte i = 0; i<16; i++)
SPI.transfer(key[i]);
unselect();
}
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
}
int RFM69::readRSSI(bool forceTrigger) {
int rssi = 0;
if (forceTrigger)
{
//RSSI trigger not needed if DAGC is in continuous mode
writeReg(REG_RSSICONFIG, RF_RSSI_START);
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // Wait for RSSI_Ready
}
rssi = -readReg(REG_RSSIVALUE);
rssi >>= 1;
return rssi;
}
byte RFM69::readReg(byte addr){
select();
SPI.transfer(addr & 0x7F);
byte regval = SPI.transfer(0);
unselect();
return regval;
}
void RFM69::writeReg(byte addr, byte value){
select();
SPI.transfer(addr | 0x80);
SPI.transfer(value);
unselect();
}
/// Select the transceiver
void RFM69::select() {
noInterrupts();
//save current SPI settings
_SPCR = SPCR;
_SPSR = SPSR;
//set RFM69 SPI settings
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV4); //decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
digitalWrite(_slaveSelectPin, LOW);
}
/// UNselect the transceiver chip
void RFM69::unselect() {
digitalWrite(_slaveSelectPin, HIGH);
//restore SPI settings to what they were before talking to RFM69
SPCR = _SPCR;
SPSR = _SPSR;
interrupts();
}
// ON = disable filtering to capture all frames on network
// OFF = enable node+broadcast filtering to capture only frames sent to this/broadcast address
void RFM69::promiscuous(bool onOff) {
_promiscuousMode=onOff;
//writeReg(REG_PACKETCONFIG1, (readReg(REG_PACKETCONFIG1) & 0xF9) | (onOff ? RF_PACKET1_ADRSFILTERING_OFF : RF_PACKET1_ADRSFILTERING_NODEBROADCAST));
}
void RFM69::setHighPower(bool onOff) {
_isRFM69HW = onOff;
writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
if (_isRFM69HW) //turning ON
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); //enable P1 & P2 amplifier stages
else
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); //enable P0 only
}
void RFM69::setHighPowerRegs(bool onOff) {
writeReg(REG_TESTPA1, onOff ? 0x5D : 0x55);
writeReg(REG_TESTPA2, onOff ? 0x7C : 0x70);
}
void RFM69::setCS(byte newSPISlaveSelect) {
_slaveSelectPin = newSPISlaveSelect;
pinMode(_slaveSelectPin, OUTPUT);
}
//for debugging
void RFM69::readAllRegs(){
byte regVal;
for (byte regAddr = 1; regAddr <= 0x4F; regAddr++)
{
select();
SPI.transfer(regAddr & 0x7f); // send address + r/w bit
regVal = SPI.transfer(0);
unselect();
Serial.print(regAddr, HEX);
Serial.print(" - ");
Serial.print(regVal,HEX);
Serial.print(" - ");
Serial.println(regVal,BIN);
}
unselect();
}
byte RFM69::readTemperature(byte calFactor){ //returns centigrade
setMode(RF69_MODE_STANDBY);
writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)) Serial.print('*');
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; //'complement'corrects the slope, rising temp = rising val
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
void RFM69::rcCalibration(){
writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00);
}