forked from LowPowerLab/RFM69
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFM69.h
98 lines (87 loc) · 4.2 KB
/
RFM69.h
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
// **********************************************************************************
// Driver definition for HopeRF RFM69W/RFM69HW, Semtech SX1231/1231H
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/3.0/
// 2013-06-14 (C) [email protected]
// **********************************************************************************
#ifndef RFM69_h
#define RFM69_h
#include <Arduino.h> //assumes Arduino IDE v1.0 or greater
#define MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead)
#define SPI_CS SS // SS is the SPI slave select pin, for instance D10 on atmega328
#define RF69_IRQ_PIN 2 // INT0 on AVRs should be connected to DIO0 (ex on Atmega328 it's D2)
#define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
#define RF69_MODE_SLEEP 0 // XTAL OFF
#define RF69_MODE_STANDBY 1 // XTAL ON
#define RF69_MODE_SYNTH 2 // PLL ON
#define RF69_MODE_RX 3 // RX MODE
#define RF69_MODE_TX 4 // TX MODE
//available frequency bands
#define RF69_315MHZ 31 // non trivial values to avoid misconfiguration
#define RF69_433MHZ 43
#define RF69_868MHZ 86
#define RF69_915MHZ 91
#define null 0
#define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
#define RF69_BROADCAST_ADDR 255
class RFM69 {
public:
static volatile byte DATA[MAX_DATA_LEN]; // recv/xmit buf, including hdr & crc bytes
static volatile byte DATALEN;
static volatile byte SENDERID;
static volatile byte TARGETID; //should match _address
static volatile byte PAYLOADLEN;
static volatile byte ACK_REQUESTED;
static volatile byte ACK_RECEIVED; /// Should be polled immediately after sending a packet with ACK request
static volatile int RSSI; //most accurate RSSI during reception (closest to the reception)
static volatile byte _mode; //should be protected?
RFM69(byte slaveSelectPin=SPI_CS, byte interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false) {
_slaveSelectPin = slaveSelectPin;
_interruptPin = interruptPin;
_mode = RF69_MODE_STANDBY;
_promiscuousMode = false;
_powerLevel = 31;
_isRFM69HW = isRFM69HW;
}
bool initialize(byte freqBand, byte ID, byte networkID=1);
void setAddress(byte addr);
bool canSend();
void send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK=false);
bool sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries=2, byte retryWaitTime=15);
bool receiveDone();
bool ACKReceived(byte fromNodeID);
void sendACK(const void* buffer = "", uint8_t bufferSize=0);
void setFrequency(uint32_t FRF);
void encrypt(const char* key);
void setCS(byte newSPISlaveSelect);
int readRSSI(bool forceTrigger=false);
void promiscuous(bool onOff=true);
void setHighPower(bool onOFF=true); //have to call it after initialize for RFM69HW
void setPowerLevel(byte level); //reduce/increase transmit power level
void sleep();
byte readTemperature(byte calFactor=0); //get CMOS temperature (8bit)
void rcCalibration(); //calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
// allow hacking registers by making these public
byte readReg(byte addr);
void writeReg(byte addr, byte val);
void readAllRegs();
protected:
static void isr0();
void virtual interruptHandler();
void sendFrame(byte toAddress, const void* buffer, byte size, bool requestACK=false, bool sendACK=false);
static RFM69* selfPointer;
byte _slaveSelectPin;
byte _interruptPin;
byte _address;
bool _promiscuousMode;
byte _powerLevel;
bool _isRFM69HW;
void receiveBegin();
void setMode(byte mode);
void setHighPowerRegs(bool onOff);
void select();
void unselect();
};
#endif