MySensors Library & Examples  2.3.2-62-ge298769
RFM69_old.h
1 // **********************************************************************************
2 // Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
3 // **********************************************************************************
4 // Copyright Felix Rusu (2014), [email protected]
5 // http://lowpowerlab.com/
6 // **********************************************************************************
7 // License
8 // **********************************************************************************
9 // This program is free software; you can redistribute it
10 // and/or modify it under the terms of the GNU General
11 // Public License as published by the Free Software
12 // Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will
16 // be useful, but WITHOUT ANY WARRANTY; without even the
17 // implied warranty of MERCHANTABILITY or FITNESS FOR A
18 // PARTICULAR PURPOSE. See the GNU General Public
19 // License for more details.
20 //
21 // You should have received a copy of the GNU General
22 // Public License along with this program.
23 // If not, see <http://www.gnu.org/licenses/>.
24 //
25 // Licence can be viewed at
26 // http://www.gnu.org/licenses/gpl-3.0.txt
27 //
28 // Please maintain this license information along with authorship
29 // and copyright notices in any redistribution of this code
30 // **********************************************************************************
31 #ifndef RFM69_h
32 #define RFM69_h
33 
34 #if !defined(RFM69_SPI)
35 #define RFM69_SPI hwSPI
36 #endif
37 
38 #define RFM69_MAX_DATA_LEN (61u) // 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 - 2 bytes crc)
39 
40 #if defined(ARDUINO_ARCH_AVR)
41 #if defined(__AVR_ATmega32U4__)
42 #define DEFAULT_RFM69_IRQ_PIN (3)
43 #else
44 #define DEFAULT_RFM69_IRQ_PIN (2)
45 #endif
46 #define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN)
47 #elif defined(ARDUINO_ARCH_ESP8266)
48 #define DEFAULT_RFM69_IRQ_PIN (5)
49 #define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN)
50 #elif defined(ARDUINO_ARCH_ESP32)
51 #define DEFAULT_RFM69_IRQ_PIN (16)
52 #define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(DEFAULT_RFM69_IRQ_PIN)
53 #elif defined(ARDUINO_ARCH_SAMD)
54 #define DEFAULT_RFM69_IRQ_PIN (2)
55 #define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN)
56 #elif defined(LINUX_ARCH_RASPBERRYPI)
57 #define DEFAULT_RFM69_IRQ_PIN (22)
58 #define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN
59 #elif defined(ARDUINO_ARCH_STM32F1)
60 #define DEFAULT_RFM69_IRQ_PIN (PA3)
61 #define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN
62 #elif defined(TEENSYDUINO)
63 #define DEFAULT_RFM69_IRQ_PIN (8)
64 #define DEFAULT_RFM69_IRQ_NUM digitalPinToInterrupt(MY_RFM69_IRQ_PIN)
65 #else
66 #define DEFAULT_RFM69_IRQ_PIN (2)
67 #define DEFAULT_RFM69_IRQ_NUM (2)
68 #endif
69 
70 #define DEFAULT_RFM69_CS_PIN (SS)
71 
72 // SPI clock divier for non-transaction implementations
73 #if (MY_RFM69_SPI_SPEED >= F_CPU / 2)
74 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV2
75 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 4)
76 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV4
77 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 8)
78 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV8
79 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 16)
80 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV16
81 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 32)
82 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV32
83 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 64)
84 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV64
85 #elif (MY_RFM69_SPI_SPEED >= F_CPU / 128)
86 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV128
87 #else
88 #define RFM69_CLOCK_DIV SPI_CLOCK_DIV256
89 #endif
90 
91 // powerup delay
92 #define RFM69_POWERUP_DELAY_MS (100u)
93 
94 #define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
95 #define RFM69_MODE_SLEEP 0 // XTAL OFF
96 #define RFM69_MODE_STANDBY 1 // XTAL ON
97 #define RFM69_MODE_SYNTH 2 // PLL ON
98 #define RFM69_MODE_RX 3 // RX MODE
99 #define RFM69_MODE_TX 4 // TX MODE
100 
101 // available frequency bands
102 #define RFM69_315MHZ 31 // non trivial values to avoid misconfiguration
103 #define RFM69_433MHZ 43
104 #define RFM69_868MHZ 86
105 #define RFM69_915MHZ 91
106 
107 #define null 0
108 #define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
109 #define RFM69_BROADCAST_ADDR 255
110 #define RFM69_CSMA_LIMIT_MS 1000
111 #define RFM69_TX_LIMIT_MS 1000
112 
113 #define RFM69_FXOSC (32*1000000ul)
114 #define RFM69_FSTEP (RFM69_FXOSC / 524288ul)
115 
116 // TWS: define CTLbyte bits
117 #define RFM69_CTL_SENDACK 0x80
118 #define RFM69_CTL_REQACK 0x40
119 
121 class RFM69
122 {
123 public:
124  static volatile uint8_t DATA[RFM69_MAX_DATA_LEN];
125  static volatile uint8_t DATALEN;
126  static volatile uint8_t SENDERID;
127  static volatile uint8_t TARGETID;
128  static volatile uint8_t PAYLOADLEN;
129  static volatile uint8_t ACK_REQUESTED;
130  static volatile uint8_t
132  static volatile int16_t RSSI;
133  static volatile uint8_t _mode;
134 
143  // cppcheck-suppress uninitMemberVar
144  RFM69(uint8_t slaveSelectPin=MY_RFM69_CS_PIN, uint8_t interruptPin=MY_RFM69_IRQ_PIN,
145  bool isRFM69HW=false,
146  uint8_t interruptNum=digitalPinToInterrupt(MY_RFM69_IRQ_PIN))
147  {
148  _slaveSelectPin = slaveSelectPin;
149  _interruptPin = interruptPin;
150  _interruptNum = interruptNum;
151  _mode = RFM69_MODE_STANDBY;
152  _promiscuousMode = false;
153  _powerLevel = 31;
154  _isRFM69HW = isRFM69HW;
155  _address = RFM69_BROADCAST_ADDR;
156 #if !defined(SPI_HAS_TRANSACTION)
157 #if defined (SPCR) && defined (SPSR)
158  _SPCR = 0;
159  _SPSR = 0;
160 #endif
161 #if defined (SREG)
162  _SREG = 0;
163 #endif
164 #endif // SPI_HAS_TRANSACTION
165  }
166 
167  bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
168  void setAddress(uint8_t addr);
169  void setNetwork(uint8_t networkID);
170  bool canSend();
171  virtual void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
172  bool requestACK=false);
173  virtual bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
174  uint8_t retries=5, uint8_t retryWaitTime=
175  200);
176  virtual bool receiveDone();
177  bool ACKReceived(uint8_t fromNodeID);
178  bool ACKRequested();
179  virtual void sendACK(const void* buffer = "", uint8_t bufferSize=0);
180  uint32_t getFrequency();
181  void setFrequency(uint32_t freqHz);
182  void encrypt(const char* key);
183  void setCS(uint8_t newSPISlaveSelect);
184  int16_t readRSSI(bool forceTrigger=false);
185  void promiscuous(bool onOff=true);
186  virtual void setHighPower(bool onOFF=
187  true);
188  virtual void setPowerLevel(uint8_t level);
189  void sleep(void);
190  void standBy(void);
191  void powerDown(void);
192  void powerUp(void);
193  void reset(void);
194  bool sanityCheck(void);
195  uint8_t readTemperature(uint8_t calFactor=0);
196  void rcCalibration();
197 
198  // allow hacking registers by making these public
199  uint8_t readReg(uint8_t addr);
200  void writeReg(uint8_t addr, uint8_t val);
201  void readAllRegs();
202 protected:
203  static void isr0();
204  void virtual interruptHandler();
205  virtual void interruptHook(uint8_t CTLbyte);
206  virtual void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false,
207  bool sendACK=false);
208 
209  static RFM69* selfPointer;
210  uint8_t _slaveSelectPin;
211  uint8_t _interruptPin;
212  uint8_t _interruptNum;
213  uint8_t _address;
215  uint8_t _powerLevel;
216  bool _isRFM69HW;
217 #if defined (SPCR) && defined (SPSR)
218  uint8_t _SPCR;
219  uint8_t _SPSR;
220 #endif
221 #if defined (SREG)
222  uint8_t _SREG;
223 #endif
224 
225  virtual void receiveBegin();
226  virtual void setMode(uint8_t mode);
227  virtual void setHighPowerRegs(bool onOff);
228  virtual void select();
229  virtual void unselect();
230 };
231 
232 #endif
RFM69::canSend
bool canSend()
canSend
RFM69::readReg
uint8_t readReg(uint8_t addr)
readReg
RFM69::encrypt
void encrypt(const char *key)
encrypt
RFM69::sleep
void sleep(void)
sleep
RFM69::unselect
virtual void unselect()
unselect
RFM69::reset
void reset(void)
reset
RFM69::rcCalibration
void rcCalibration()
rcCalibration (calibrate the internal RC oscillator for use in wide temperature variations - see data...
RFM69::ACKRequested
bool ACKRequested()
ACKRequested.
RFM69::_interruptNum
uint8_t _interruptNum
_interruptNum
Definition: RFM69_old.h:212
RFM69::readTemperature
uint8_t readTemperature(uint8_t calFactor=0)
readTemperature (get CMOS temperature (8bit))
RFM69::SENDERID
static volatile uint8_t SENDERID
SENDERID.
Definition: RFM69_old.h:126
RFM69::DATALEN
static volatile uint8_t DATALEN
DATALEN.
Definition: RFM69_old.h:125
RFM69::DATA
static volatile uint8_t DATA[RFM69_MAX_DATA_LEN]
recv/xmit buf, including hdr & crc bytes
Definition: RFM69_old.h:124
RFM69::receiveBegin
virtual void receiveBegin()
receiveBegin
RFM69::interruptHandler
virtual void interruptHandler()
interruptHandler
RFM69::setCS
void setCS(uint8_t newSPISlaveSelect)
setCS
RFM69::setFrequency
void setFrequency(uint32_t freqHz)
setFrequency
RFM69::_interruptPin
uint8_t _interruptPin
_interruptPin
Definition: RFM69_old.h:211
RFM69::powerDown
void powerDown(void)
powerDown
RFM69::PAYLOADLEN
static volatile uint8_t PAYLOADLEN
PAYLOADLEN.
Definition: RFM69_old.h:128
RFM69::ACKReceived
bool ACKReceived(uint8_t fromNodeID)
ACKReceived.
RFM69::select
virtual void select()
select
MY_RFM69_CS_PIN
#define MY_RFM69_CS_PIN
RFM69 SPI chip select pin.
Definition: MyConfig.h:812
MY_RFM69_IRQ_PIN
#define MY_RFM69_IRQ_PIN
Define this to override the default RFM69 IRQ pin assignment.
Definition: MyConfig.h:786
RFM69::setMode
virtual void setMode(uint8_t mode)
setMode
RFM69::ACK_RECEIVED
static volatile uint8_t ACK_RECEIVED
Should be polled immediately after sending a packet with ACK requestwith ACK request.
Definition: RFM69_old.h:131
RFM69::initialize
bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1)
initialize
RFM69::setHighPowerRegs
virtual void setHighPowerRegs(bool onOff)
setHighPowerRegs
RFM69::_promiscuousMode
bool _promiscuousMode
_promiscuousMode
Definition: RFM69_old.h:214
RFM69::selfPointer
static RFM69 * selfPointer
selfPointer
Definition: RFM69_old.h:209
RFM69::powerUp
void powerUp(void)
powerUp
RFM69::_mode
static volatile uint8_t _mode
should be protected?
Definition: RFM69_old.h:133
RFM69::_address
uint8_t _address
_address
Definition: RFM69_old.h:213
RFM69::standBy
void standBy(void)
standBy
RFM69::RSSI
static volatile int16_t RSSI
most accurate RSSI during reception (closest to the reception)
Definition: RFM69_old.h:132
RFM69::sendFrame
virtual void sendFrame(uint8_t toAddress, const void *buffer, uint8_t size, bool requestACK=false, bool sendACK=false)
sendFrame
RFM69::sanityCheck
bool sanityCheck(void)
sanityCheck
RFM69::sendACK
virtual void sendACK(const void *buffer="", uint8_t bufferSize=0)
sendACK
RFM69::interruptHook
virtual void interruptHook(uint8_t CTLbyte)
interruptHook
RFM69
Definition: RFM69_old.h:121
RFM69::setNetwork
void setNetwork(uint8_t networkID)
setNetwork
RFM69::_isRFM69HW
bool _isRFM69HW
_isRFM69HW
Definition: RFM69_old.h:216
RFM69::isr0
static void isr0()
isr0
RFM69::setHighPower
virtual void setHighPower(bool onOFF=true)
setHighPower (have to call it after initialize for RFM69HW)
RFM69::getFrequency
uint32_t getFrequency()
getFrequency
RFM69::ACK_REQUESTED
static volatile uint8_t ACK_REQUESTED
ACK_REQUESTED.
Definition: RFM69_old.h:129
RFM69::send
virtual void send(uint8_t toAddress, const void *buffer, uint8_t bufferSize, bool requestACK=false)
send
RFM69::promiscuous
void promiscuous(bool onOff=true)
promiscuous
RFM69::writeReg
void writeReg(uint8_t addr, uint8_t val)
writeReg
RFM69::RFM69
RFM69(uint8_t slaveSelectPin=MY_RFM69_CS_PIN, uint8_t interruptPin=MY_RFM69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=digitalPinToInterrupt(MY_RFM69_IRQ_PIN))
Constructor.
Definition: RFM69_old.h:144
RFM69::_powerLevel
uint8_t _powerLevel
_powerLevel
Definition: RFM69_old.h:215
RFM69::_slaveSelectPin
uint8_t _slaveSelectPin
_slaveSelectPin
Definition: RFM69_old.h:210
RFM69::readRSSI
int16_t readRSSI(bool forceTrigger=false)
readRSSI
RFM69::setPowerLevel
virtual void setPowerLevel(uint8_t level)
setPowerLevel (reduce/increase transmit power level)
RFM69::receiveDone
virtual bool receiveDone()
receiveDone
RFM69::readAllRegs
void readAllRegs()
readAllRegs
RFM69::setAddress
void setAddress(uint8_t addr)
setAddress
RFM69::sendWithRetry
virtual bool sendWithRetry(uint8_t toAddress, const void *buffer, uint8_t bufferSize, uint8_t retries=5, uint8_t retryWaitTime=200)
sendWithRetry (40ms roundtrip req for 61byte packets, adjusted)
RFM69::TARGETID
static volatile uint8_t TARGETID
should match _address
Definition: RFM69_old.h:127